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>
|
|
|
|
|
|
2002-04-20 14:07:03 +00:00
|
|
|
|
#include <plib/ssg.h>
|
2002-04-12 12:45:49 +00:00
|
|
|
|
|
2003-05-09 19:39:48 +00:00
|
|
|
|
#include <simgear/scene/model/placement.hxx>
|
2003-05-09 20:40:59 +00:00
|
|
|
|
#include <simgear/scene/model/model.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);
|
|
|
|
|
|
2002-04-12 12:45:49 +00:00
|
|
|
|
|
|
|
|
|
FGModelMgr::FGModelMgr ()
|
2002-04-13 21:36:22 +00:00
|
|
|
|
: _selector(new ssgSelector)
|
2002-04-12 12:45:49 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FGModelMgr::~FGModelMgr ()
|
|
|
|
|
{
|
2002-05-14 05:49:47 +00:00
|
|
|
|
for (unsigned int i = 0; i < _instances.size(); i++) {
|
|
|
|
|
globals->get_scenery()->get_models_branch()
|
2002-04-13 21:36:22 +00:00
|
|
|
|
->removeKid(_instances[i]->model->getSceneGraph());
|
2002-04-12 12:45:49 +00:00
|
|
|
|
delete _instances[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGModelMgr::init ()
|
|
|
|
|
{
|
2002-05-11 00:59:26 +00:00
|
|
|
|
vector<SGPropertyNode_ptr> model_nodes =
|
2002-04-12 12:45:49 +00:00
|
|
|
|
fgGetNode("/models", true)->getChildren("model");
|
2002-05-14 05:49:47 +00:00
|
|
|
|
for (unsigned int i = 0; i < model_nodes.size(); i++) {
|
2002-04-12 12:45:49 +00:00
|
|
|
|
SGPropertyNode * node = model_nodes[i];
|
|
|
|
|
SG_LOG(SG_GENERAL, SG_INFO,
|
|
|
|
|
"Adding model " << node->getStringValue("name", "[unnamed]"));
|
|
|
|
|
Instance * instance = new Instance;
|
2003-05-13 03:18:42 +00:00
|
|
|
|
SGModelPlacement *model = new SGModelPlacement;
|
2002-04-12 12:45:49 +00:00
|
|
|
|
instance->model = model;
|
2003-05-08 20:28:46 +00:00
|
|
|
|
ssgBranch *object
|
2003-05-13 03:18:42 +00:00
|
|
|
|
= sgLoad3DModel( globals->get_fg_root(),
|
2003-05-08 20:28:46 +00:00
|
|
|
|
node->getStringValue("path",
|
|
|
|
|
"Models/Geometry/glider.ac"),
|
|
|
|
|
globals->get_props(),
|
|
|
|
|
globals->get_sim_time_sec() );
|
|
|
|
|
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.
|
|
|
|
|
SGPropertyNode * child = node->getChild("longitude-deg-prop");
|
|
|
|
|
if (child != 0)
|
2002-04-12 15:07:49 +00:00
|
|
|
|
instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
|
2002-04-12 12:45:49 +00:00
|
|
|
|
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"));
|
|
|
|
|
|
2002-04-13 21:36:22 +00:00
|
|
|
|
// Add this model to the global scene graph
|
2002-05-14 05:49:47 +00:00
|
|
|
|
globals->get_scenery()->get_scene_graph()->addKid(model->getSceneGraph());
|
2002-04-12 12:45:49 +00:00
|
|
|
|
|
Mathias:
I have done a patch to eliminate the jitter of 3D-objects near the viewpoint
(for example 3D cockpit objects).
The problem is the roundoff accuracy of the float values used in the
scenegraph together with the transforms of the eyepoint relative to the
scenery center.
The solution will be to move the scenery center near the view point.
This way floats relative accuracy is enough to show a stable picture.
To get that right I have introduced a transform node for the scenegraph which
is responsible for that shift and uses double values as long as possible.
The scenery subsystem now has a list of all those transforms required to place
objects in the world and will tell all those transforms that the scenery
center has changed when the set_scenery_center() of the scenery subsystem is
called.
The problem was not solvable by SGModelPlacement and SGLocation, since not all
objects, especially the scenery, are placed using these classes.
The first approach was to have the scenery center exactly at the eyepoint.
This works well for the cockpit.
But then the ground jitters a bit below the aircraft. With our default views
you can't see that, but that F-18 has a camera view below the left engine
intake with the nose gear and the ground in its field of view, here I could
see that.
Having the scenery center constant will still have this roundoff problems, but
like it is now too, the roundoff error here is exactly the same in each
frame, so you will not notice any jitter.
The real solution is now to keep the scenery center constant as long as it is
in a ball of 30m radius around the view point. If the scenery center is
outside this ball, just put it at the view point.
As a sideeffect of now beeing able to switch the scenery center in the whole
scenegraph with one function call, I was able to remove a one half of a
problem when switching views, where the scenery center was far off for one or
two frames past switching from one view to the next. Also included is a fix
to the other half of this problem, where the view position was not yet copied
into a view when it is switched (at least under glut). This was responsible
for the 'Error: ...' messages of the cloud subsystem when views were
switched.
2005-04-29 14:38:24 +00:00
|
|
|
|
// Register that one at the scenery manager
|
|
|
|
|
globals->get_scenery()->register_placement_transform(model->getTransform());
|
|
|
|
|
|
|
|
|
|
|
2002-04-12 12:45:49 +00:00
|
|
|
|
// Save this instance for updating
|
2002-12-30 15:44:11 +00:00
|
|
|
|
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();
|
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
|
|
|
|
void
|
|
|
|
|
FGModelMgr::draw ()
|
|
|
|
|
{
|
2002-04-13 21:36:22 +00:00
|
|
|
|
// ssgSetNearFar(_nearplane, _farplane);
|
|
|
|
|
// ssgCullAndDraw(_scene);
|
2002-04-12 12:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Implementation of FGModelMgr::Instance
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
FGModelMgr::Instance::Instance ()
|
|
|
|
|
: model(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)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FGModelMgr::Instance::~Instance ()
|
|
|
|
|
{
|
Mathias:
I have done a patch to eliminate the jitter of 3D-objects near the viewpoint
(for example 3D cockpit objects).
The problem is the roundoff accuracy of the float values used in the
scenegraph together with the transforms of the eyepoint relative to the
scenery center.
The solution will be to move the scenery center near the view point.
This way floats relative accuracy is enough to show a stable picture.
To get that right I have introduced a transform node for the scenegraph which
is responsible for that shift and uses double values as long as possible.
The scenery subsystem now has a list of all those transforms required to place
objects in the world and will tell all those transforms that the scenery
center has changed when the set_scenery_center() of the scenery subsystem is
called.
The problem was not solvable by SGModelPlacement and SGLocation, since not all
objects, especially the scenery, are placed using these classes.
The first approach was to have the scenery center exactly at the eyepoint.
This works well for the cockpit.
But then the ground jitters a bit below the aircraft. With our default views
you can't see that, but that F-18 has a camera view below the left engine
intake with the nose gear and the ground in its field of view, here I could
see that.
Having the scenery center constant will still have this roundoff problems, but
like it is now too, the roundoff error here is exactly the same in each
frame, so you will not notice any jitter.
The real solution is now to keep the scenery center constant as long as it is
in a ball of 30m radius around the view point. If the scenery center is
outside this ball, just put it at the view point.
As a sideeffect of now beeing able to switch the scenery center in the whole
scenegraph with one function call, I was able to remove a one half of a
problem when switching views, where the scenery center was far off for one or
two frames past switching from one view to the next. Also included is a fix
to the other half of this problem, where the view position was not yet copied
into a view when it is switched (at least under glut). This was responsible
for the 'Error: ...' messages of the cloud subsystem when views were
switched.
2005-04-29 14:38:24 +00:00
|
|
|
|
// Unregister that one at the scenery manager
|
|
|
|
|
globals->get_scenery()->unregister_placement_transform(model->getTransform());
|
|
|
|
|
|
2002-04-12 12:45:49 +00:00
|
|
|
|
delete model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// end of modelmgr.cxx
|