2002-04-12 12:45:49 +00:00
|
|
|
|
// 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
|
|
|
|
|
|
2003-05-09 19:39:48 +00:00
|
|
|
|
#include <simgear/compiler.h>
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include <simgear/scene/model/placement.hxx>
|
2006-03-22 13:46:10 +00:00
|
|
|
|
#include <simgear/scene/model/modellib.hxx>
|
2006-03-20 16:00:26 +00:00
|
|
|
|
#include <simgear/structure/exception.hxx>
|
2003-05-09 19:39:48 +00:00
|
|
|
|
|
2002-04-12 12:45:49 +00:00
|
|
|
|
#include <Main/fg_props.hxx>
|
2002-05-14 05:49:47 +00:00
|
|
|
|
#include <Scenery/scenery.hxx>
|
2002-04-12 12:45:49 +00:00
|
|
|
|
|
2003-05-08 20:28:46 +00:00
|
|
|
|
|
|
|
|
|
#include "modelmgr.hxx"
|
2002-04-20 14:07:03 +00:00
|
|
|
|
|
2003-05-09 19:39:48 +00:00
|
|
|
|
SG_USING_STD(vector);
|
|
|
|
|
|
2006-10-29 19:30:21 +00:00
|
|
|
|
// OSGFIXME
|
|
|
|
|
// extern SGShadowVolume *shadows;
|
2006-03-30 14:34:16 +00:00
|
|
|
|
|
2002-04-12 12:45:49 +00:00
|
|
|
|
|
|
|
|
|
FGModelMgr::FGModelMgr ()
|
2006-03-20 16:38:31 +00:00
|
|
|
|
: _models(fgGetNode("/models", true)),
|
2006-03-20 17:32:56 +00:00
|
|
|
|
_listener(new Listener(this))
|
2002-04-12 12:45:49 +00:00
|
|
|
|
{
|
2006-03-20 16:38:31 +00:00
|
|
|
|
_models->addChangeListener(_listener);
|
2002-04-12 12:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FGModelMgr::~FGModelMgr ()
|
|
|
|
|
{
|
2006-03-20 16:38:31 +00:00
|
|
|
|
_models->removeChangeListener(_listener);
|
|
|
|
|
delete _listener;
|
|
|
|
|
|
2002-05-14 05:49:47 +00:00
|
|
|
|
for (unsigned int i = 0; i < _instances.size(); i++) {
|
2006-03-20 17:45:50 +00:00
|
|
|
|
globals->get_scenery()->get_scene_graph()
|
2006-10-29 19:30:21 +00:00
|
|
|
|
->removeChild(_instances[i]->model->getSceneGraph());
|
2002-04-12 12:45:49 +00:00
|
|
|
|
delete _instances[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGModelMgr::init ()
|
|
|
|
|
{
|
2006-03-20 16:38:31 +00:00
|
|
|
|
vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
|
2006-03-20 12:10:20 +00:00
|
|
|
|
|
2007-12-06 18:32:43 +00:00
|
|
|
|
for (unsigned int i = 0; i < model_nodes.size(); i++)
|
2006-03-20 16:00:26 +00:00
|
|
|
|
add_model(model_nodes[i]);
|
2006-03-20 12:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGModelMgr::add_model (SGPropertyNode * node)
|
|
|
|
|
{
|
|
|
|
|
SG_LOG(SG_GENERAL, SG_INFO,
|
2006-03-20 16:00:26 +00:00
|
|
|
|
"Adding model " << node->getStringValue("name", "[unnamed]"));
|
2006-03-20 12:10:20 +00:00
|
|
|
|
Instance * instance = new Instance;
|
|
|
|
|
SGModelPlacement *model = new SGModelPlacement;
|
|
|
|
|
instance->model = model;
|
2006-03-20 17:32:56 +00:00
|
|
|
|
instance->node = node;
|
2006-03-22 13:46:10 +00:00
|
|
|
|
SGModelLib *model_lib = globals->get_model_lib();
|
2007-12-06 18:32:43 +00:00
|
|
|
|
|
|
|
|
|
const char *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);
|
|
|
|
|
} catch (const sg_throwable& t) {
|
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Error loading " << path << ":\n "
|
|
|
|
|
<< t.getFormattedMessage() << t.getOrigin());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2006-03-22 13:46:10 +00:00
|
|
|
|
|
2006-03-20 12:10:20 +00:00
|
|
|
|
model->init( object );
|
2002-04-12 12:45:49 +00:00
|
|
|
|
|
|
|
|
|
// Set position and orientation either
|
|
|
|
|
// indirectly through property refs
|
|
|
|
|
// or directly with static values.
|
2006-03-20 12:10:20 +00:00
|
|
|
|
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
|
2006-10-29 19:30:21 +00:00
|
|
|
|
globals->get_scenery()->get_scene_graph()->addChild(model->getSceneGraph());
|
2006-03-20 12:10:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save this instance for updating
|
|
|
|
|
add_instance(instance);
|
2002-04-12 12:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGModelMgr::bind ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGModelMgr::unbind ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2002-05-11 16:28:50 +00:00
|
|
|
|
FGModelMgr::update (double dt)
|
2002-04-12 12:45:49 +00:00
|
|
|
|
{
|
2002-05-14 05:49:47 +00:00
|
|
|
|
for (unsigned int i = 0; i < _instances.size(); i++) {
|
2002-04-12 12:45:49 +00:00
|
|
|
|
Instance * instance = _instances[i];
|
2003-05-13 03:18:42 +00:00
|
|
|
|
SGModelPlacement * model = instance->model;
|
2002-04-12 12:45:49 +00:00
|
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
|
2005-09-05 13:25:09 +00:00
|
|
|
|
instance->model->update();
|
2006-04-12 22:21:02 +00:00
|
|
|
|
|
2006-10-29 19:30:21 +00:00
|
|
|
|
// OSGFIXME
|
|
|
|
|
// if (shadows && !instance->shadow) {
|
|
|
|
|
// osg::Node *branch = instance->model->getSceneGraph();
|
|
|
|
|
// shadows->addOccluder(branch, SGShadowVolume::occluderTypeTileObject);
|
|
|
|
|
// instance->shadow = true;
|
|
|
|
|
// }
|
2002-04-12 12:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-30 15:44:11 +00:00
|
|
|
|
void
|
|
|
|
|
FGModelMgr::add_instance (Instance * instance)
|
|
|
|
|
{
|
|
|
|
|
_instances.push_back(instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGModelMgr::remove_instance (Instance * instance)
|
|
|
|
|
{
|
2003-01-01 18:47:50 +00:00
|
|
|
|
vector<Instance *>::iterator it;
|
|
|
|
|
for (it = _instances.begin(); it != _instances.end(); it++) {
|
|
|
|
|
if (*it == instance) {
|
|
|
|
|
_instances.erase(it);
|
|
|
|
|
delete instance;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-12-30 15:44:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-12 12:45:49 +00:00
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Implementation of FGModelMgr::Instance
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
FGModelMgr::Instance::Instance ()
|
|
|
|
|
: model(0),
|
2006-03-20 17:32:56 +00:00
|
|
|
|
node(0),
|
2002-04-12 12:45:49 +00:00
|
|
|
|
lon_deg_node(0),
|
|
|
|
|
lat_deg_node(0),
|
|
|
|
|
elev_ft_node(0),
|
|
|
|
|
roll_deg_node(0),
|
|
|
|
|
pitch_deg_node(0),
|
2006-04-12 22:21:02 +00:00
|
|
|
|
heading_deg_node(0),
|
|
|
|
|
shadow(false)
|
2002-04-12 12:45:49 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2007-12-06 18:32:43 +00:00
|
|
|
|
_mgr->add_model(parent);
|
2006-03-20 16:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-03-20 17:32:56 +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);
|
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);
|
2006-03-20 17:32:56 +00:00
|
|
|
|
|
|
|
|
|
delete instance;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-12 12:45:49 +00:00
|
|
|
|
// end of modelmgr.cxx
|