1
0
Fork 0
flightgear/src/Model/modelmgr.cxx

294 lines
7.8 KiB
C++
Raw Normal View History

// modelmgr.cxx - manage a collection of 3D models.
// Written by David Megginson, started 2002.
//
// This file is in the Public Domain, and comes with no warranty.
2003-06-03 13:51:21 +00:00
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/compiler.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <osg/Math>
#include <simgear/scene/model/placement.hxx>
#include <simgear/scene/model/modellib.hxx>
#include <simgear/structure/exception.hxx>
#include <Main/fg_props.hxx>
#include <Scenery/scenery.hxx>
#include "modelmgr.hxx"
using std::vector;
using namespace simgear;
Modified Files: configure.ac src/AIModel/AIAircraft.cxx src/AIModel/AIBase.cxx src/AIModel/AIBase.hxx src/AIModel/AICarrier.cxx src/AIModel/AICarrier.hxx src/AIModel/AIManager.cxx src/AIModel/AIManager.hxx src/ATC/AIEntity.cxx src/ATC/AIEntity.hxx src/ATC/AIMgr.cxx src/ATC/AIMgr.hxx src/ATC/ATCdisplay.cxx src/ATC/ATCdisplay.hxx src/Cockpit/cockpit.cxx src/Cockpit/cockpit.hxx src/Cockpit/hud.cxx src/Cockpit/hud.hxx src/Cockpit/hud_rwy.cxx src/Cockpit/panel.cxx src/Cockpit/panel.hxx src/Cockpit/built_in/FGMagRibbon.cxx src/Cockpit/built_in/FGMagRibbon.hxx src/FDM/flight.cxx src/FDM/groundcache.cxx src/FDM/groundcache.hxx src/GUI/gui_funcs.cxx src/Input/input.cxx src/Instrumentation/od_gauge.cxx src/Instrumentation/od_gauge.hxx src/Instrumentation/render_area_2d.cxx src/Instrumentation/render_area_2d.hxx src/Instrumentation/wxradar.cxx src/Instrumentation/wxradar.hxx src/Instrumentation/HUD/HUD.cxx src/Instrumentation/HUD/HUD.hxx src/Instrumentation/HUD/HUD_runway.cxx src/Main/Makefile.am src/Main/main.cxx src/Main/renderer.cxx src/Main/renderer.hxx src/Main/viewmgr.cxx src/Model/acmodel.cxx src/Model/acmodel.hxx src/Model/model_panel.cxx src/Model/model_panel.hxx src/Model/modelmgr.cxx src/Model/modelmgr.hxx src/Model/panelnode.cxx src/Model/panelnode.hxx src/Navaids/awynet.cxx src/Scenery/Makefile.am src/Scenery/hitlist.cxx src/Scenery/hitlist.hxx src/Scenery/newcache.cxx src/Scenery/scenery.cxx src/Scenery/scenery.hxx src/Scenery/tileentry.cxx src/Scenery/tileentry.hxx src/Scenery/tilemgr.cxx src/Scripting/NasalSys.cxx src/Scripting/NasalSys.hxx src/Time/light.cxx Big BLOB on the way to OSG.
2006-10-29 19:30:21 +00:00
// OSGFIXME
// extern SGShadowVolume *shadows;
FGModelMgr::FGModelMgr ()
2006-03-20 16:38:31 +00:00
: _models(fgGetNode("/models", true)),
_listener(new Listener(this))
{
2006-03-20 16:38:31 +00:00
_models->addChangeListener(_listener);
}
FGModelMgr::~FGModelMgr ()
{
2006-03-20 16:38:31 +00:00
_models->removeChangeListener(_listener);
delete _listener;
for (unsigned int i = 0; i < _instances.size(); i++) {
globals->get_scenery()->get_scene_graph()
Modified Files: configure.ac src/AIModel/AIAircraft.cxx src/AIModel/AIBase.cxx src/AIModel/AIBase.hxx src/AIModel/AICarrier.cxx src/AIModel/AICarrier.hxx src/AIModel/AIManager.cxx src/AIModel/AIManager.hxx src/ATC/AIEntity.cxx src/ATC/AIEntity.hxx src/ATC/AIMgr.cxx src/ATC/AIMgr.hxx src/ATC/ATCdisplay.cxx src/ATC/ATCdisplay.hxx src/Cockpit/cockpit.cxx src/Cockpit/cockpit.hxx src/Cockpit/hud.cxx src/Cockpit/hud.hxx src/Cockpit/hud_rwy.cxx src/Cockpit/panel.cxx src/Cockpit/panel.hxx src/Cockpit/built_in/FGMagRibbon.cxx src/Cockpit/built_in/FGMagRibbon.hxx src/FDM/flight.cxx src/FDM/groundcache.cxx src/FDM/groundcache.hxx src/GUI/gui_funcs.cxx src/Input/input.cxx src/Instrumentation/od_gauge.cxx src/Instrumentation/od_gauge.hxx src/Instrumentation/render_area_2d.cxx src/Instrumentation/render_area_2d.hxx src/Instrumentation/wxradar.cxx src/Instrumentation/wxradar.hxx src/Instrumentation/HUD/HUD.cxx src/Instrumentation/HUD/HUD.hxx src/Instrumentation/HUD/HUD_runway.cxx src/Main/Makefile.am src/Main/main.cxx src/Main/renderer.cxx src/Main/renderer.hxx src/Main/viewmgr.cxx src/Model/acmodel.cxx src/Model/acmodel.hxx src/Model/model_panel.cxx src/Model/model_panel.hxx src/Model/modelmgr.cxx src/Model/modelmgr.hxx src/Model/panelnode.cxx src/Model/panelnode.hxx src/Navaids/awynet.cxx src/Scenery/Makefile.am src/Scenery/hitlist.cxx src/Scenery/hitlist.hxx src/Scenery/newcache.cxx src/Scenery/scenery.cxx src/Scenery/scenery.hxx src/Scenery/tileentry.cxx src/Scenery/tileentry.hxx src/Scenery/tilemgr.cxx src/Scripting/NasalSys.cxx src/Scripting/NasalSys.hxx src/Time/light.cxx Big BLOB on the way to OSG.
2006-10-29 19:30:21 +00:00
->removeChild(_instances[i]->model->getSceneGraph());
delete _instances[i];
}
}
void
FGModelMgr::init ()
{
2006-03-20 16:38:31 +00:00
vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
for (unsigned int i = 0; i < model_nodes.size(); i++)
add_model(model_nodes[i]);
}
void
FGModelMgr::add_model (SGPropertyNode * node)
{
SG_LOG(SG_GENERAL, SG_INFO,
"Adding model " << node->getStringValue("name", "[unnamed]"));
Instance * instance = new Instance;
SGModelPlacement *model = new SGModelPlacement;
instance->model = model;
instance->node = node;
const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
osg::Node *object;
try {
object = SGModelLib::loadPagedModel(path, globals->get_props());
} catch (const sg_throwable& t) {
SG_LOG(SG_GENERAL, SG_ALERT, "Error loading " << path << ":\n "
<< t.getFormattedMessage() << t.getOrigin());
return;
}
model->init( object );
// 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)
instance->roll_deg_node = fgGetNode(child->getStringValue(), true);
else
model->setRollDeg(node->getDoubleValue("roll-deg"));
child = node->getChild("pitch-deg-prop");
if (child != 0)
instance->pitch_deg_node = fgGetNode(child->getStringValue(), true);
else
model->setPitchDeg(node->getDoubleValue("pitch-deg"));
child = node->getChild("heading-deg-prop");
if (child != 0)
instance->heading_deg_node = fgGetNode(child->getStringValue(), true);
else
model->setHeadingDeg(node->getDoubleValue("heading-deg"));
// Add this model to the global scene graph
Modified Files: configure.ac src/AIModel/AIAircraft.cxx src/AIModel/AIBase.cxx src/AIModel/AIBase.hxx src/AIModel/AICarrier.cxx src/AIModel/AICarrier.hxx src/AIModel/AIManager.cxx src/AIModel/AIManager.hxx src/ATC/AIEntity.cxx src/ATC/AIEntity.hxx src/ATC/AIMgr.cxx src/ATC/AIMgr.hxx src/ATC/ATCdisplay.cxx src/ATC/ATCdisplay.hxx src/Cockpit/cockpit.cxx src/Cockpit/cockpit.hxx src/Cockpit/hud.cxx src/Cockpit/hud.hxx src/Cockpit/hud_rwy.cxx src/Cockpit/panel.cxx src/Cockpit/panel.hxx src/Cockpit/built_in/FGMagRibbon.cxx src/Cockpit/built_in/FGMagRibbon.hxx src/FDM/flight.cxx src/FDM/groundcache.cxx src/FDM/groundcache.hxx src/GUI/gui_funcs.cxx src/Input/input.cxx src/Instrumentation/od_gauge.cxx src/Instrumentation/od_gauge.hxx src/Instrumentation/render_area_2d.cxx src/Instrumentation/render_area_2d.hxx src/Instrumentation/wxradar.cxx src/Instrumentation/wxradar.hxx src/Instrumentation/HUD/HUD.cxx src/Instrumentation/HUD/HUD.hxx src/Instrumentation/HUD/HUD_runway.cxx src/Main/Makefile.am src/Main/main.cxx src/Main/renderer.cxx src/Main/renderer.hxx src/Main/viewmgr.cxx src/Model/acmodel.cxx src/Model/acmodel.hxx src/Model/model_panel.cxx src/Model/model_panel.hxx src/Model/modelmgr.cxx src/Model/modelmgr.hxx src/Model/panelnode.cxx src/Model/panelnode.hxx src/Navaids/awynet.cxx src/Scenery/Makefile.am src/Scenery/hitlist.cxx src/Scenery/hitlist.hxx src/Scenery/newcache.cxx src/Scenery/scenery.cxx src/Scenery/scenery.hxx src/Scenery/tileentry.cxx src/Scenery/tileentry.hxx src/Scenery/tilemgr.cxx src/Scripting/NasalSys.cxx src/Scripting/NasalSys.hxx src/Time/light.cxx Big BLOB on the way to OSG.
2006-10-29 19:30:21 +00:00
globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
// Save this instance for updating
add_instance(instance);
}
void
FGModelMgr::bind ()
{
}
void
FGModelMgr::unbind ()
{
}
namespace
{
double testNan(double val) throw (sg_range_exception)
{
if (osg::isNaN(val))
throw sg_range_exception("value is nan");
return val;
}
struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
{
void operator()(FGModelMgr::Instance* instance) const
{
SGModelPlacement* model = instance->model;
double lon, lat, elev, roll, pitch, heading;
// Optionally set position from properties
if (instance->lon_deg_node != 0)
lon =instance->lon_deg_node->getDoubleValue();
if (instance->lat_deg_node != 0)
lat = instance->lat_deg_node->getDoubleValue();
if (instance->elev_ft_node != 0)
elev = instance->elev_ft_node->getDoubleValue();
// Optionally set orientation from properties
if (instance->roll_deg_node != 0)
roll = instance->roll_deg_node->getDoubleValue();
if (instance->pitch_deg_node != 0)
pitch = instance->pitch_deg_node->getDoubleValue();
if (instance->heading_deg_node != 0)
heading = instance->heading_deg_node->getDoubleValue();
if ( osg::isNaN(lon) || osg::isNaN(lat) || osg::isNaN(elev) ||
osg::isNaN(roll) || osg::isNaN(pitch) || osg::isNaN(heading) ) {
const char *path = instance->node->getStringValue("path",
"unknown");
SG_LOG(SG_GENERAL, SG_INFO, "Instance of model " << path
<< " has invalid values");
return;
}
// Optionally set position from properties
if (instance->lon_deg_node != 0)
model->setLongitudeDeg(lon);
if (instance->lat_deg_node != 0)
model->setLatitudeDeg(lat);
if (instance->elev_ft_node != 0)
model->setElevationFt(elev);
// 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)
{
std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
}
void
FGModelMgr::add_instance (Instance * instance)
{
_instances.push_back(instance);
}
void
FGModelMgr::remove_instance (Instance * instance)
{
vector<Instance *>::iterator it;
for (it = _instances.begin(); it != _instances.end(); it++) {
if (*it == instance) {
_instances.erase(it);
delete instance;
return;
}
}
}
////////////////////////////////////////////////////////////////////////
// Implementation of FGModelMgr::Instance
////////////////////////////////////////////////////////////////////////
FGModelMgr::Instance::Instance ()
: model(0),
node(0),
lon_deg_node(0),
lat_deg_node(0),
elev_ft_node(0),
roll_deg_node(0),
pitch_deg_node(0),
heading_deg_node(0),
shadow(false)
{
}
FGModelMgr::Instance::~Instance ()
{
delete model;
}
2006-03-20 16:38:31 +00:00
////////////////////////////////////////////////////////////////////////
// Implementation of FGModelMgr::Listener
////////////////////////////////////////////////////////////////////////
void
FGModelMgr::Listener::childAdded(SGPropertyNode * parent, SGPropertyNode * child)
{
2006-03-21 17:35:30 +00:00
if (strcmp(parent->getName(), "model") || strcmp(child->getName(), "load"))
2006-03-20 16:38:31 +00:00
return;
_mgr->add_model(parent);
2006-03-20 16:38:31 +00:00
}
void
FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * child)
{
if (strcmp(parent->getName(), "models") || strcmp(child->getName(), "model"))
return;
// search instance by node and remove it from scenegraph
vector<Instance *>::iterator it = _mgr->_instances.begin();
vector<Instance *>::iterator end = _mgr->_instances.end();
for (; it != end; ++it) {
Instance *instance = *it;
if (instance->node != child)
continue;
_mgr->_instances.erase(it);
Modified Files: configure.ac src/AIModel/AIAircraft.cxx src/AIModel/AIBase.cxx src/AIModel/AIBase.hxx src/AIModel/AICarrier.cxx src/AIModel/AICarrier.hxx src/AIModel/AIManager.cxx src/AIModel/AIManager.hxx src/ATC/AIEntity.cxx src/ATC/AIEntity.hxx src/ATC/AIMgr.cxx src/ATC/AIMgr.hxx src/ATC/ATCdisplay.cxx src/ATC/ATCdisplay.hxx src/Cockpit/cockpit.cxx src/Cockpit/cockpit.hxx src/Cockpit/hud.cxx src/Cockpit/hud.hxx src/Cockpit/hud_rwy.cxx src/Cockpit/panel.cxx src/Cockpit/panel.hxx src/Cockpit/built_in/FGMagRibbon.cxx src/Cockpit/built_in/FGMagRibbon.hxx src/FDM/flight.cxx src/FDM/groundcache.cxx src/FDM/groundcache.hxx src/GUI/gui_funcs.cxx src/Input/input.cxx src/Instrumentation/od_gauge.cxx src/Instrumentation/od_gauge.hxx src/Instrumentation/render_area_2d.cxx src/Instrumentation/render_area_2d.hxx src/Instrumentation/wxradar.cxx src/Instrumentation/wxradar.hxx src/Instrumentation/HUD/HUD.cxx src/Instrumentation/HUD/HUD.hxx src/Instrumentation/HUD/HUD_runway.cxx src/Main/Makefile.am src/Main/main.cxx src/Main/renderer.cxx src/Main/renderer.hxx src/Main/viewmgr.cxx src/Model/acmodel.cxx src/Model/acmodel.hxx src/Model/model_panel.cxx src/Model/model_panel.hxx src/Model/modelmgr.cxx src/Model/modelmgr.hxx src/Model/panelnode.cxx src/Model/panelnode.hxx src/Navaids/awynet.cxx src/Scenery/Makefile.am src/Scenery/hitlist.cxx src/Scenery/hitlist.hxx src/Scenery/newcache.cxx src/Scenery/scenery.cxx src/Scenery/scenery.hxx src/Scenery/tileentry.cxx src/Scenery/tileentry.hxx src/Scenery/tilemgr.cxx src/Scripting/NasalSys.cxx src/Scripting/NasalSys.hxx src/Time/light.cxx Big BLOB on the way to OSG.
2006-10-29 19:30:21 +00:00
osg::Node *branch = instance->model->getSceneGraph();
// OSGFIXME
// if (shadows && instance->shadow)
// shadows->deleteOccluder(branch);
globals->get_scenery()->get_scene_graph()->removeChild(branch);
delete instance;
break;
}
}
// end of modelmgr.cxx