X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2Fmodelmgr.cxx;h=46ed240f4c8d2f17c7e3d17a4e099bc8d484ad0c;hb=e600cd3d000723b7b7fd4557ad72cfe971daf423;hp=0ec617b0b4b9486976681f82292b7346613c50d8;hpb=8380fb4463fee4260499960fd68fd076830c38a6;p=flightgear.git diff --git a/src/Model/modelmgr.cxx b/src/Model/modelmgr.cxx index 0ec617b0b..46ed240f4 100644 --- a/src/Model/modelmgr.cxx +++ b/src/Model/modelmgr.cxx @@ -3,13 +3,20 @@ // // This file is in the Public Domain, and comes with no warranty. +#ifdef _MSC_VER +# pragma warning( disable: 4355 ) +#endif + #ifdef HAVE_CONFIG_H # include #endif #include +#include +#include #include +#include #include #include @@ -21,12 +28,13 @@ #include "modelmgr.hxx" -SG_USING_STD(vector); +using std::vector; + +using namespace simgear; // OSGFIXME // extern SGShadowVolume *shadows; - FGModelMgr::FGModelMgr () : _models(fgGetNode("/models", true)), _listener(new Listener(this)) @@ -58,51 +66,46 @@ FGModelMgr::init () void FGModelMgr::add_model (SGPropertyNode * node) { - SG_LOG(SG_GENERAL, SG_INFO, + SG_LOG(SG_AIRCRAFT, SG_INFO, "Adding model " << node->getStringValue("name", "[unnamed]")); - Instance * instance = new Instance; - SGModelPlacement *model = new SGModelPlacement; - instance->model = model; - instance->node = node; - SGModelLib *model_lib = globals->get_model_lib(); - const char *path = node->getStringValue("path", "Models/Geometry/glider.ac"); + const char *model_path = node->getStringValue("path", "Models/Geometry/glider.ac"); osg::Node *object; try { - object = model_lib->load_model( - globals->get_fg_root(), - path, - globals->get_props(), - globals->get_sim_time_sec(), /*cache_object=*/false); + std::string fullPath = simgear::SGModelLib::findDataFile(model_path); + object = SGModelLib::loadDeferredModel(fullPath, globals->get_props()); } catch (const sg_throwable& t) { - SG_LOG(SG_GENERAL, SG_ALERT, "Error loading " << path << ":\n " + SG_LOG(SG_AIRCRAFT, SG_ALERT, "Error loading " << model_path << ":\n " << t.getFormattedMessage() << t.getOrigin()); return; } - model->init( object ); + Instance * instance = new Instance; + SGModelPlacement *model = new SGModelPlacement; + instance->model = model; + instance->node = node; - // Set position and orientation either - // indirectly through property refs - // or directly with static values. + model->init( object ); + double lon = node->getDoubleValue("longitude-deg"), + lat = node->getDoubleValue("latitude-deg"), + elevFt = node->getDoubleValue("elevation-ft"); + + model->setPosition(SGGeod::fromDegFt(lon, lat, elevFt)); +// Set position and orientation either +// indirectly through property refs +// or directly with static values. SGPropertyNode * child = node->getChild("longitude-deg-prop"); if (child != 0) instance->lon_deg_node = fgGetNode(child->getStringValue(), true); - else - model->setLongitudeDeg(node->getDoubleValue("longitude-deg")); child = node->getChild("latitude-deg-prop"); if (child != 0) instance->lat_deg_node = fgGetNode(child->getStringValue(), true); - else - model->setLatitudeDeg(node->getDoubleValue("latitude-deg")); child = node->getChild("elevation-ft-prop"); if (child != 0) instance->elev_ft_node = fgGetNode(child->getStringValue(), true); - else - model->setElevationFt(node->getDoubleValue("elevation-ft")); child = node->getChild("roll-deg-prop"); if (child != 0) @@ -140,38 +143,66 @@ FGModelMgr::unbind () { } +namespace +{ +double testNan(double val) throw (sg_range_exception) +{ + if (SGMisc::isNaN(val)) + throw sg_range_exception("value is nan"); + return val; +} + +struct UpdateFunctor : public std::unary_function +{ + void operator()(FGModelMgr::Instance* instance) const + { + SGModelPlacement* model = instance->model; + double roll, pitch, heading; + roll = pitch = heading = 0.0; + SGGeod pos = model->getPosition(); + + try { + // Optionally set position from properties + if (instance->lon_deg_node != 0) + pos.setLongitudeDeg(testNan(instance->lon_deg_node->getDoubleValue())); + if (instance->lat_deg_node != 0) + pos.setLatitudeDeg(testNan(instance->lat_deg_node->getDoubleValue())); + if (instance->elev_ft_node != 0) + pos.setElevationFt(testNan(instance->elev_ft_node->getDoubleValue())); + + // Optionally set orientation from properties + if (instance->roll_deg_node != 0) + roll = testNan(instance->roll_deg_node->getDoubleValue()); + if (instance->pitch_deg_node != 0) + pitch = testNan(instance->pitch_deg_node->getDoubleValue()); + if (instance->heading_deg_node != 0) + heading = testNan(instance->heading_deg_node->getDoubleValue()); + } catch (const sg_range_exception&) { + const char *path = instance->node->getStringValue("path", + "unknown"); + SG_LOG(SG_AIRCRAFT, SG_INFO, "Instance of model " << path + << " has invalid values"); + return; + } + + model->setPosition(pos); + // Optionally set orientation from properties + if (instance->roll_deg_node != 0) + model->setRollDeg(roll); + if (instance->pitch_deg_node != 0) + model->setPitchDeg(pitch); + if (instance->heading_deg_node != 0) + model->setHeadingDeg(heading); + + instance->model->update(); + } +}; +} + void FGModelMgr::update (double dt) { - for (unsigned int i = 0; i < _instances.size(); i++) { - Instance * instance = _instances[i]; - SGModelPlacement * model = instance->model; - - // Optionally set position from properties - if (instance->lon_deg_node != 0) - model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue()); - if (instance->lat_deg_node != 0) - model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue()); - if (instance->elev_ft_node != 0) - model->setElevationFt(instance->elev_ft_node->getDoubleValue()); - - // Optionally set orientation from properties - if (instance->roll_deg_node != 0) - model->setRollDeg(instance->roll_deg_node->getDoubleValue()); - if (instance->pitch_deg_node != 0) - model->setPitchDeg(instance->pitch_deg_node->getDoubleValue()); - if (instance->heading_deg_node != 0) - model->setHeadingDeg(instance->heading_deg_node->getDoubleValue()); - - instance->model->update(); - - // OSGFIXME -// if (shadows && !instance->shadow) { -// osg::Node *branch = instance->model->getSceneGraph(); -// shadows->addOccluder(branch, SGShadowVolume::occluderTypeTileObject); -// instance->shadow = true; -// } - } + std::for_each(_instances.begin(), _instances.end(), UpdateFunctor()); } void