Power::Shaft::Shaft(SimpleXMLTransfer* xml) { for (int n=0; n<xml->getChildCount(); n++) { SimpleXMLTransfer* it = xml->getChildAt(n); Gearing* s = (Gearing*)0; if (it->getName().compare("engine") == 0) s = new Engine_DCM(); else if (it->getName().compare("propeller") == 0) s = new Propeller(); else if (it->getName().compare("simplethrust") == 0) s = new SimpleThrust(); if (s != (Gearing*)0) gear.push_back(s); } }
void Power::Shaft::ReloadParams(SimpleXMLTransfer* xml) { int nChildCnt = 0; double J_ges; fBrake = (xml->attributeAsInt("brake", 1) != 0); J_ges = xml->attributeAsDouble("J", 0); std::cout << " Shaft: J=" << J_ges << " kg m^2\n"; for (int n=0; n<xml->getChildCount(); n++) { SimpleXMLTransfer* it = xml->getChildAt(n); if (it->getName().compare("engine") == 0 || it->getName().compare("propeller") == 0 || it->getName().compare("simplethrust") == 0 ) { gear[nChildCnt]->ReloadParams(it); J_ges += gear[nChildCnt++]->getJ(); } } J_inv = 1/J_ges; }
void CRRCAirplaneLaRCSim::initSound(SimpleXMLTransfer* xml) { SimpleXMLTransfer* cfg = XMLModelFile::getConfig(xml); SimpleXMLTransfer* sndcfg = cfg->getChild("sound", true); int children = sndcfg->getChildCount(); int units = sndcfg->getInt("units", 0); for (int i = 0; i < children; i++) { SimpleXMLTransfer *child = sndcfg->getChildAt(i); std::string name = child->getName(); if (name.compare("sample") == 0) { T_AirplaneSound *sample; // assemble relative path std::string soundfile; soundfile = child->attribute("filename"); // other sound attributes int sound_type = child->getInt("type", SOUND_TYPE_GLIDER); double dPitchFactor = child->getDouble("pitchfactor", 0.002); double dMaxVolume = child->getDouble("maxvolume", 1.0); if (dMaxVolume < 0.0) { dMaxVolume = 0.0; } else if (dMaxVolume > 1.0) { dMaxVolume = 1.0; } //~ if (cfg->indexOfChild("power") < 0) //~ max_thrust = 0; //~ else //~ max_thrust = 1; if (soundfile != "") { // Get full path (considering search paths). soundfile = FileSysTools::getDataPath("sounds/" + soundfile); } // File ok? Use default otherwise. if (!FileSysTools::fileExists(soundfile)) soundfile = FileSysTools::getDataPath("sounds/fan.wav"); std::cout << "soundfile: " << soundfile << "\n"; //~ std::cout << "max_thrust: " << max_thrust << "\n"; std::cout << "soundserver: " << Global::soundserver << "\n"; // Only make noise if a sound file is available if (soundfile != "" && Global::soundserver != (CRRCAudioServer*)0) { std::cout << "Using airplane sound " << soundfile << ", type " << sound_type << ", max vol " << dMaxVolume << std::endl; if (sound_type == SOUND_TYPE_GLIDER) { T_GliderSound *glidersound; float flMinRelV, flMaxRelV, flMaxDist; flMinRelV = (float)child->getDouble("v_min", 1.5); flMaxRelV = (float)child->getDouble("v_max", 4.0); flMaxDist = (float)child->getDouble("dist_max", 300); if (units == 1) { // convert from metric units to ft. flMaxDist *= M_TO_FT; } glidersound = new T_GliderSound(soundfile.c_str(), Global::soundserver->getAudioSpec()); glidersound->setMinRelVelocity(flMinRelV); glidersound->setMaxRelVelocity(flMaxRelV); glidersound->setMaxDistanceFeet(flMaxDist); sample = glidersound; } else { sample = new T_EngineSound(soundfile.c_str(), Global::soundserver->getAudioSpec()); } sample->setType(sound_type); sample->setPitchFactor(dPitchFactor); sample->setMaxVolume(dMaxVolume); sample->setChannel(Global::soundserver->playSample((T_SoundSample*)sample)); sound.push_back(sample); } } } }
/** * Create a CRRCControlSurfaceAnimation object * * Initialize the animation from an * <animation type="ControlSurface"> tag */ CRRCControlSurfaceAnimation::CRRCControlSurfaceAnimation(SimpleXMLTransfer *xml) : CRRCAnimation(new ssgTransform()), fallback_data(0.0f), eventAdapter(this, &CRRCControlSurfaceAnimation::axisValueCallback, Event::Input), aileron(0.0f), elevator(0.0f), rudder(0.0f), throttle(0.0f), spoiler(0.0f), flap(0.0f), retract(0.0f), pitch(0.0f) { bool failed = false; // evaluate <object> tag SimpleXMLTransfer *map = xml->getChild("object", true); symbolic_name = map->getString("name", "no_name_set"); max_angle = (float)(map->getDouble("max_angle", 0.0) * SG_RADIANS_TO_DEGREES); abs_max_angle = (float)fabs((double)max_angle); // find hinges and evaluate all <control> tags int num_controls = 0; int num_hinges = 0; for (int i = 0; i < xml->getChildCount(); i++) { SimpleXMLTransfer *child = xml->getChildAt(i); if (child->getName() == "hinge") { // found a <hinge> child sgVec3 pos; pos[SG_X] = (float)(-1 * child->getDouble("y", 0.0)); pos[SG_Y] = (float)(-1 * child->getDouble("x", 0.0)); pos[SG_Z] = (float)(-1 * child->getDouble("z", 0.0)); if (num_hinges == 0) { sgCopyVec3(hinge_1, pos); } else if (num_hinges == 1) { sgCopyVec3(hinge_2, pos); } num_hinges++; } else if (child->getName() == "control") { // found a <control> child // The "*2" factor for each gain value scales the control input // values from -0.5...+0.5 to -1.0...+1.0. This saves one // float multiplication per mapping in the runtime update() routine. std::string mapping = child->getString("mapping", "NOTHING"); float gain = (float)child->getDouble("gain", 1.0); if (mapping == "ELEVATOR") { datasource.push_back(&elevator); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "AILERON") { datasource.push_back(&aileron); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "THROTTLE") { datasource.push_back(&throttle); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "RUDDER") { datasource.push_back(&rudder); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "FLAP") { datasource.push_back(&flap); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "SPOILER") { datasource.push_back(&spoiler); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "RETRACT") { datasource.push_back(&retract); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "PITCH") { datasource.push_back(&pitch); source_gain.push_back(gain * 2); num_controls++; } else { std::cerr << "ControlSurfaceAnimation: ignoring <control> tag without mapping." << std::endl; } } } if (num_controls < 1) { std::cerr << "ControlSurfaceAnimation: found animation without proper <control> tag. Animation disabled." << std::endl; failed = true; } if (num_hinges < 2) { std::cerr << "ControlSurfaceAnimation: Must specify exactly two hinges!" << std::endl; failed = true; } else { if (num_hinges > 2) { std::cerr << "ControlSurfaceAnimation: Must specify exactly two hinges!" << std::endl; std::cerr << "ControlSurfaceAnimation: Ignoring excessive hinge tag(s)." << std::endl; } sgSubVec3(axis, hinge_2, hinge_1); if (sgLengthVec3(axis) < 0.001) { std::cerr << "ControlSurfaceAnimation: Insufficient spacing between hinges!" << std::endl; failed = true; } } if (failed) { std::cerr << "ControlSurfaceAnimation: Animation setup failed." << std::endl; // set to some non-critical defaults datasource.resize(1); datasource[0] = &fallback_data; source_gain.resize(1); source_gain[0] = 1.0; sgSetVec3(hinge_1, 0.0f, 0.0f, 0.0f); sgSetVec3(hinge_2, 1.0f, 0.0f, 0.0f); sgSubVec3(axis, hinge_2, hinge_1); } sgMakeIdentMat4(move_to_origin); move_to_origin[3][0] = -hinge_1[0]; move_to_origin[3][1] = -hinge_1[1]; move_to_origin[3][2] = -hinge_1[2]; sgMakeTransMat4(move_back, hinge_1); realInit(); }
ModelBasedScenery::ModelBasedScenery(SimpleXMLTransfer *xml, int sky_variant) : Scenery(xml, sky_variant), location(Scenery::MODEL_BASED) { ssgEntity *model = NULL; SimpleXMLTransfer *scene = xml->getChild("scene", true); getHeight_mode = scene->attributeAsInt("getHeight_mode", DEFAULT_HEIGHT_MODE); //std::cout << "----getHeight_mode : " << getHeight_mode <<std::endl; SceneGraph = new ssgRoot(); // Create an "invisible" state. This state actually makes a node // visible in a predefined way. This is used to visualize invisible // objects (e.g. collision boxes). invisible_state = new ssgSimpleState(); invisible_state->disable(GL_COLOR_MATERIAL); invisible_state->disable(GL_TEXTURE_2D); invisible_state->enable(GL_LIGHTING); invisible_state->enable(GL_BLEND); //invisible_state->setShadeModel(GL_SMOOTH); //invisible_state->setShininess(0.0f); invisible_state->setMaterial(GL_EMISSION, 0.0, 0.0, 0.0, 0.0); invisible_state->setMaterial(GL_AMBIENT, 1.0, 0.0, 0.0, 0.5); invisible_state->setMaterial(GL_DIFFUSE, 1.0, 0.0, 0.0, 0.5); invisible_state->setMaterial(GL_SPECULAR, 1.0, 0.0, 0.0, 0.5); // transform everything from SSG coordinates to CRRCsim coordinates initial_trans = new ssgTransform(); SceneGraph->addKid(initial_trans); //10.76 sgMat4 it = { {1, 0.0, 0.0, 0}, {0.0, 0.0, -1, 0}, {0.0, 1, 0.0, 0}, {0.0, 0.0, 0.0, 1.0} }; initial_trans->setTransform(it); // find all "objects" defined in the file int num_children = scene->getChildCount(); for (int cur_child = 0; cur_child < num_children; cur_child++) { SimpleXMLTransfer *kid = scene->getChildAt(cur_child); // only use "object" tags if (kid->getName() == "object") { std::string filename = kid->attribute("filename", "not_specified"); bool is_terrain = (kid->attributeAsInt("terrain", 1) != 0); bool is_visible = (kid->attributeAsInt("visible", 1) != 0); // PLIB automatically loads the texture file, // but it does not know which directory to use. // Where is the object file? std::string of = FileSysTools::getDataPath("objects/" + filename, TRUE); // compile and set relative texture path std::string tp = of.substr(0, of.length()-filename.length()-1-7) + "textures"; ssgTexturePath(tp.c_str()); // load model std::cout << "Loading 3D object \"" << of.c_str() << "\""; if (is_terrain) { std::cout << " (part of terrain)"; } if (!is_visible) { std::cout << " (invisible)"; } std::cout << std::endl; model = ssgLoad(of.c_str()); if (model != NULL) { if (!is_visible) { setToInvisibleState(model); } // The model may contain internal node attributes (e.g. for // integrated collision boxes). Parse these attributes now. evaluateNodeAttributes(model); // now parse the instances and place the model in the SceneGraph for (int cur_instance = 0; cur_instance < kid->getChildCount(); cur_instance++) { SimpleXMLTransfer *instance = kid->getChildAt(cur_instance); if (instance->getName() == "instance") { sgCoord coord; // try north/east/height first, then fallback to x/y/z try { coord.xyz[SG_X] = instance->attributeAsDouble("east"); } catch (XMLException &e) { coord.xyz[SG_X] = instance->attributeAsDouble("y", 0.0); } try { coord.xyz[SG_Y] = instance->attributeAsDouble("north"); } catch (XMLException &e) { coord.xyz[SG_Y] = instance->attributeAsDouble("x", 0.0); } try { coord.xyz[SG_Z] = instance->attributeAsDouble("height"); } catch (XMLException &e) { coord.xyz[SG_Z] = instance->attributeAsDouble("z", 0.0); } coord.hpr[0] = 180 - instance->attributeAsDouble("h", 0.0); coord.hpr[1] = -instance->attributeAsDouble("p", 0.0); coord.hpr[2] = -instance->attributeAsDouble("r", 0.0); std::cout << std::setprecision(1); std::cout << " Placing instance at " << coord.xyz[SG_X] << ";" << coord.xyz[SG_Y] << ";" << coord.xyz[SG_Z]; std::cout << ", orientation " << (180-coord.hpr[0]) << ";" << -coord.hpr[1] << ";" << -coord.hpr[2] << std::endl; std::cout << std::setprecision(6); ssgTransform *trans = new ssgTransform(); trans->setTransform(&coord); // In PLIB::SSG, intersection testing is done by a tree-walking // function. This can be influenced by the tree traversal mask // bits. The HOT and LOS flags are cleared for objects that are // not part of the terrain, so that the height-of-terrain and // line-of-sight algorithms ignore this branch of the tree. if (!is_terrain) { trans->clrTraversalMaskBits(SSGTRAV_HOT | SSGTRAV_LOS); } // Objects are made invisible by clearing the CULL traversal flag. // This means that ssgCullAndDraw will ignore this branch. if (!is_visible) { trans->clrTraversalMaskBits(SSGTRAV_CULL); } initial_trans->addKid(trans); trans->addKid(model); } } } } } // create actual terrain height model if (getHeight_mode == 1) { heightdata = new HD_TabulatedTerrain(SceneGraph); } else if (getHeight_mode == 2) { heightdata = new HD_TilingTerrain(SceneGraph); } else { heightdata = new HD_SsgLOSTerrain(SceneGraph); } //wind SimpleXMLTransfer *wind = xml->getChild("wind", true); std::string wind_filename = wind->attribute("filename",""); #if WINDDATA3D == 1 wind_data = 0;//default : no wind_data std::string wind_position_unit = wind->attribute("unit",""); try { flDefaultWindDirection = wind->attributeAsInt("direction"); ImposeWindDirection = true; } catch (XMLException) { // if not attribut "direction", normal mode } if (wind_position_unit.compare("m")==0) { wind_position_coef = FT_TO_M; } else { wind_position_coef = 1; } std::cout << "wind file name : " << wind_filename.c_str()<< std::endl; if (wind_filename.length() > 0) { wind_filename = FileSysTools::getDataPath(wind_filename); std::cout << "init wind ---------"; int n = init_wind_data((wind_filename.c_str())); std::cout << n << " points processed" << std::endl; } #else if (wind_filename.length() > 0) { new CGUIMsgBox("Insufficient configuration to read windfields."); } #endif }
/** * Create a HardPointRotation * * \param xml <animation> part of the model file that contains the * description of the animation */ HardPointRotation::HardPointRotation(SimpleXMLTransfer *xml, TSimInputs const& in) { bool failed = false; // evaluate <object> tag SimpleXMLTransfer *map = xml->getChild("object", true); symbolic_name = map->getString("name", "no_name_set"); max_angle_rad = (float)map->getDouble("max_angle", 0.0); abs_max_angle_rad = (float)fabs((double)max_angle_rad); // find hinges and evaluate all <control> tags int num_controls = 0; int num_hinges = 0; for (int i = 0; i < xml->getChildCount(); i++) { SimpleXMLTransfer *child = xml->getChildAt(i); if (child->getName() == "hinge") { // found a <hinge> child CRRCMath::Vector3 pos; pos.r[0] = (float)(child->getDouble("x", 0.0)); pos.r[1] = (float)(child->getDouble("y", 0.0)); pos.r[2] = (float)(child->getDouble("z", 0.0)); if (num_hinges < 2) { hinge[num_hinges] = pos; } num_hinges++; } else if (child->getName() == "control") { // found a <control> child // The "*2" factor for each gain value scales the control input // values from -0.5...+0.5 to -1.0...+1.0. This saves one // float multiplication per mapping in the runtime update() routine. // NB: unsigned control (throttle, spoiler, retract) are not scaled, // since control input is already in range 0.0...+1.0 std::string mapping = child->getString("mapping", "NOTHING"); float gain = (float)child->getDouble("gain", 1.0); std::cout << " mapped to " << mapping << " with gain " << gain; std::cout << " and max_angle_rad " << max_angle_rad << std::endl; if (mapping == "ELEVATOR") { datasource.push_back(&in.elevator); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "AILERON") { datasource.push_back(&in.aileron); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "THROTTLE") { datasource.push_back(&in.throttle); source_gain.push_back(gain); num_controls++; } else if (mapping == "RUDDER") { datasource.push_back(&in.rudder); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "FLAP") { datasource.push_back(&in.flap); source_gain.push_back(gain * 2); num_controls++; } else if (mapping == "SPOILER") { datasource.push_back(&in.spoiler); source_gain.push_back(gain); num_controls++; } else if (mapping == "RETRACT") { datasource.push_back(&in.retract); source_gain.push_back(gain); num_controls++; } else if (mapping == "PITCH") { datasource.push_back(&in.pitch); source_gain.push_back(gain * 2); num_controls++; } else { fprintf(stderr, "HardPointRotation: ignoring <control> tag without mapping.\n"); } } } if (num_controls < 1) { fprintf(stderr, "HardPointRotation: found animation without proper <control> tag. Animation disabled.\n"); failed = true; } if (num_hinges < 2) { fprintf(stderr, "HardPointRotation: Must specify exactly two hinges!\n"); failed = true; } else { if (num_hinges > 2) { fprintf(stderr, "HardPointRotation: Must specify exactly two hinges!\n"); fprintf(stderr, "HardPointRotation: Ignoring excessive hinge tag(s).\n"); } axis = hinge[1] - hinge[0]; if (axis.length() < 0.001) { fprintf(stderr, "HardPointRotation: Insufficient spacing between hinges!\n"); failed = true; } } if (failed) { fprintf(stderr, "HardPointRotation: Animation setup failed.\n"); // set to some non-critical defaults datasource.resize(1); datasource[0] = &fallback_data; source_gain.resize(1); source_gain[0] = 1.0; hinge[0] = CRRCMath::Vector3(0.0, 0.0, 0.0); hinge[1] = CRRCMath::Vector3(1.0, 0.0, 0.0); axis = hinge[1] - hinge[0]; } else { std::cerr << "HardPointRotation: set up animated hardpoint "; std::cerr << symbolic_name << std::endl; } move_orig.makeTranslation(hinge[0] * -1); move_back.makeTranslation(hinge[0]); //~ realInit(); }
/** \brief Add animations to a model * * This method reads animation description tags from a model file * and tries to add the corresponding animations to the 3D model. * * \todo Right now there's only one type of animation: movable control * surfaces. Therefore this method receives a pointer to the control * input class. If animations are added that need a different kind of * input for their update() method, we need to decide how to pass all * this stuff to initAnimations(). * * \param model_file XML model description file * \param model scenegraph of the 3D model * \param fInputs pointer to the control input class * \param anim_list list of all created CRRCAnimation objects */ void initAnimations(SimpleXMLTransfer *model_file, ssgEntity* model, TSimInputs *fInput, std::vector<CRRCAnimation*>& anim_list) { SimpleXMLTransfer *animations = model_file->getChild("animations", true); int num_anims = animations->getChildCount(); fprintf(stdout, "initAnimations: found %d children\n", num_anims); for (int i = 0; i < num_anims; i++) { SimpleXMLTransfer *animation = animations->getChildAt(i); ssgEntity *node; if (animation->getName() != "animation") { fprintf(stderr, "initAnimations: invalid child <%s>\n", animation->getName().c_str()); } else { std::string node_name = animation->getString("object.name", "default"); std::string type = animation->getString("type", "default"); node = SSGUtil::findNamedNode(model, node_name.c_str()); if (node != NULL) { CRRCAnimation *anim = NULL; printf("initAnimations: found animation node %s, type %s\n", node_name.c_str(), type.c_str()); if (type == "ControlSurface") { anim = new CRRCControlSurfaceAnimation(animation, fInput); } else { fprintf(stderr, "initAnimations: unknown animation type '%s'\n", type.c_str()); } if (anim != NULL) { if (anim->getBranch() == NULL) { fprintf(stderr, "initAnimations: defunct animation class (animation branch is <NULL>)\n"); exit(0); } else { SSGUtil::spliceBranch(anim->getBranch(), node); anim->init(); anim->setName("Animation"); anim->getBranch()->setUserData(anim); anim->getBranch()->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback); anim_list.push_back(anim); } } } else { fprintf(stderr, "initAnimations: node '%s' not found in 3D model\n", node_name.c_str()); } } } }