1
0
Fork 0

Support explicit aircraft interior models.

- allow multiple model files in the -set.xml specification
- allow each model to be tagged with a usage string
- put models tagged 'interior' into a LOD group.

This is experimental, the LOD ranges will be made configurable soon.
This commit is contained in:
James Turner 2013-10-20 20:46:33 +01:00
parent e81df0df1f
commit 38193d7843
8 changed files with 131 additions and 187 deletions

View file

@ -2,14 +2,12 @@ include(FlightGearComponent)
set(SOURCES set(SOURCES
acmodel.cxx acmodel.cxx
model_panel.cxx
modelmgr.cxx modelmgr.cxx
panelnode.cxx panelnode.cxx
) )
set(HEADERS set(HEADERS
acmodel.hxx acmodel.hxx
model_panel.hxx
modelmgr.hxx modelmgr.hxx
panelnode.hxx panelnode.hxx
) )

View file

@ -7,7 +7,8 @@
# include <config.h> # include <config.h>
#endif #endif
#include <string.h> // for strcmp() #include <cstring> // for strcmp()
#include <boost/foreach.hpp>
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <simgear/debug/logstream.hxx> #include <simgear/debug/logstream.hxx>
@ -15,6 +16,7 @@
#include <simgear/misc/sg_path.hxx> #include <simgear/misc/sg_path.hxx>
#include <simgear/scene/model/placement.hxx> #include <simgear/scene/model/placement.hxx>
#include <simgear/scene/util/SGNodeMasks.hxx> #include <simgear/scene/util/SGNodeMasks.hxx>
#include <simgear/scene/model/modellib.hxx>
#include <Main/globals.hxx> #include <Main/globals.hxx>
#include <Main/fg_props.hxx> #include <Main/fg_props.hxx>
@ -24,26 +26,26 @@
#include <Scenery/scenery.hxx> #include <Scenery/scenery.hxx>
#include <Sound/fg_fx.hxx> #include <Sound/fg_fx.hxx>
#include "model_panel.hxx"
#include "acmodel.hxx" #include "acmodel.hxx"
static osg::Node *
fgLoad3DModelPanel(const std::string &path, SGPropertyNode *prop_root)
{
bool loadPanels = true;
osg::Node* node = simgear::SGModelLib::loadModel(path, prop_root, NULL, loadPanels);
if (node)
node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
return node;
}
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Implementation of FGAircraftModel // Implementation of FGAircraftModel
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
FGAircraftModel::FGAircraftModel () FGAircraftModel::FGAircraftModel ()
: _aircraft(0), : _velocity(SGVec3d::zeros()),
_velocity(SGVec3d::zeros()),
_fx(0), _fx(0),
_lon(0),
_lat(0),
_alt(0),
_pitch(0),
_roll(0),
_heading(0),
_speed_n(0), _speed_n(0),
_speed_e(0), _speed_e(0),
_speed_d(0) _speed_d(0)
@ -62,38 +64,66 @@ FGAircraftModel::~FGAircraftModel ()
void void
FGAircraftModel::init () FGAircraftModel::init ()
{ {
osg::Node *model = NULL; if (_aircraft.get()) {
SG_LOG(SG_AIRCRAFT, SG_ALERT, "FGAircraftModel::init: already inited");
return;
}
_aircraft = new SGModelPlacement; SGPropertyNode_ptr sim = fgGetNode("/sim", true);
std::string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac"); BOOST_FOREACH(SGPropertyNode_ptr model, sim->getChildren("model")) {
std::string path = model->getStringValue("path", "Models/Geometry/glider.ac");
std::string usage = model->getStringValue("usage", "external");
SGPath resolvedPath = globals->resolve_aircraft_path(path); SGPath resolvedPath = globals->resolve_aircraft_path(path);
if (resolvedPath.isNull()) if (resolvedPath.isNull()) {
{ SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to find aircraft model: " << path);
SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to load aircraft from " << path << ':'); continue;
} }
else
{ osg::Node* node = NULL;
try { try {
model = fgLoad3DModelPanel( resolvedPath.str(), globals->get_props()); node = fgLoad3DModelPanel( resolvedPath.str(), globals->get_props());
} catch (const sg_exception &ex) { } catch (const sg_exception &ex) {
SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to load aircraft from " << path << ':'); SG_LOG(SG_AIRCRAFT, SG_ALERT, "Failed to load aircraft from " << path << ':');
SG_LOG(SG_AIRCRAFT, SG_ALERT, " " << ex.getFormattedMessage()); SG_LOG(SG_AIRCRAFT, SG_ALERT, " " << ex.getFormattedMessage());
} }
}
if (!model) if (usage == "interior") {
{ // interior model
SG_LOG(SG_AIRCRAFT, SG_ALERT, "(Falling back to glider.ac.)"); if (!_interior.get()) {
model = fgLoad3DModelPanel( "Models/Geometry/glider.ac", _interior.reset(new SGModelPlacement);
globals->get_props()); _interior->init(node);
} else {
_interior->add(node);
}
} else {
// normal / exterior model
if (!_aircraft.get()) {
_aircraft.reset(new SGModelPlacement);
_aircraft->init(node);
} else {
_aircraft->add(node);
}
} // of model usage switch
} // of models iteration
// no models loaded, load the glider instead
if (!_aircraft.get()) {
SG_LOG(SG_AIRCRAFT, SG_ALERT, "(Falling back to glider.ac.)");
osg::Node* model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
globals->get_props());
_aircraft.reset(new SGModelPlacement);
_aircraft->init(model);
} }
_aircraft->init( model );
osg::Node* node = _aircraft->getSceneGraph(); osg::Node* node = _aircraft->getSceneGraph();
// Do not do altitude computations with that model
node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
globals->get_scenery()->get_aircraft_branch()->addChild(node); globals->get_scenery()->get_aircraft_branch()->addChild(node);
if (_interior.get()) {
node = _interior->getSceneGraph();
globals->get_scenery()->get_interior_branch()->addChild(node);
}
} }
void void
@ -109,26 +139,24 @@ FGAircraftModel::reinit()
void void
FGAircraftModel::deinit() FGAircraftModel::deinit()
{ {
if (!_aircraft) { if (!_aircraft.get()) {
return; return;
} }
osg::Node* node = _aircraft->getSceneGraph(); osg::Node* node = _aircraft->getSceneGraph();
globals->get_scenery()->get_aircraft_branch()->removeChild(node); globals->get_scenery()->get_aircraft_branch()->removeChild(node);
delete _aircraft; if (_interior.get()) {
_aircraft = NULL; globals->get_scenery()->get_interior_branch()->removeChild(_interior->getSceneGraph());
}
_aircraft.reset();
_interior.reset();
} }
void void
FGAircraftModel::bind () FGAircraftModel::bind ()
{ {
_lon = fgGetNode("position/longitude-deg", true);
_lat = fgGetNode("position/latitude-deg", true);
_alt = fgGetNode("position/altitude-ft", true);
_pitch = fgGetNode("orientation/pitch-deg", true);
_roll = fgGetNode("orientation/roll-deg", true);
_heading = fgGetNode("orientation/heading-deg", true);
_speed_n = fgGetNode("velocities/speed-north-fps", true); _speed_n = fgGetNode("velocities/speed-north-fps", true);
_speed_e = fgGetNode("velocities/speed-east-fps", true); _speed_e = fgGetNode("velocities/speed-east-fps", true);
_speed_d = fgGetNode("velocities/speed-down-fps", true); _speed_d = fgGetNode("velocities/speed-down-fps", true);
@ -152,21 +180,24 @@ FGAircraftModel::update (double dt)
_aircraft->setVisible(true); _aircraft->setVisible(true);
} }
_aircraft->setPosition(_lon->getDoubleValue(), double heading, pitch, roll;
_lat->getDoubleValue(), globals->get_aircraft_orientation(heading, pitch, roll);
_alt->getDoubleValue()); SGQuatd orient = SGQuatd::fromYawPitchRollDeg(heading, pitch, roll);
_aircraft->setOrientation(_roll->getDoubleValue(),
_pitch->getDoubleValue(), SGGeod pos = globals->get_aircraft_position();
_heading->getDoubleValue());
_aircraft->setPosition(pos);
_aircraft->setOrientation(orient);
_aircraft->update(); _aircraft->update();
// update model's audio sample values if (_interior.get()) {
SGGeod position = _aircraft->getPosition(); _interior->setPosition(pos);
_fx->set_position_geod( position ); _interior->setOrientation(orient);
_interior->update();
}
SGQuatd orient = SGQuatd::fromYawPitchRollDeg(_heading->getDoubleValue(), // update model's audio sample values
_pitch->getDoubleValue(), _fx->set_position_geod( pos );
_roll->getDoubleValue());
_fx->set_orientation( orient ); _fx->set_orientation( orient );
_velocity = SGVec3d( _speed_n->getDoubleValue(), _velocity = SGVec3d( _speed_n->getDoubleValue(),

View file

@ -10,6 +10,7 @@
#include <osg/Group> #include <osg/Group>
#include <osg/Switch> #include <osg/Switch>
#include <memory>
#include <simgear/structure/subsystem_mgr.hxx> // for SGSubsystem #include <simgear/structure/subsystem_mgr.hxx> // for SGSubsystem
@ -29,22 +30,18 @@ public:
virtual void bind (); virtual void bind ();
virtual void unbind (); virtual void unbind ();
virtual void update (double dt); virtual void update (double dt);
virtual SGModelPlacement * get3DModel() { return _aircraft; } virtual SGModelPlacement * get3DModel() { return _aircraft.get(); }
virtual SGVec3d& getVelocity() { return _velocity; } virtual SGVec3d& getVelocity() { return _velocity; }
private: private:
void deinit (); void deinit ();
SGModelPlacement * _aircraft; std::auto_ptr<SGModelPlacement> _aircraft;
std::auto_ptr<SGModelPlacement> _interior;
SGVec3d _velocity; SGVec3d _velocity;
SGSharedPtr<FGFX> _fx; SGSharedPtr<FGFX> _fx;
SGPropertyNode_ptr _lon;
SGPropertyNode_ptr _lat;
SGPropertyNode_ptr _alt;
SGPropertyNode_ptr _pitch;
SGPropertyNode_ptr _roll;
SGPropertyNode_ptr _heading;
SGPropertyNode_ptr _speed_n; SGPropertyNode_ptr _speed_n;
SGPropertyNode_ptr _speed_e; SGPropertyNode_ptr _speed_e;
SGPropertyNode_ptr _speed_d; SGPropertyNode_ptr _speed_d;

View file

@ -1,40 +0,0 @@
// model.cxx - manage a 3D aircraft model.
// Written by David Megginson, started 2002.
//
// This file is in the Public Domain, and comes with no warranty.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/compiler.h>
#include <osg/Geode>
#include <simgear/props/props.hxx>
#include <simgear/scene/model/modellib.hxx>
#include <simgear/scene/util/SGNodeMasks.hxx>
#include "panelnode.hxx"
#include "model_panel.hxx"
using std::vector;
using namespace simgear;
////////////////////////////////////////////////////////////////////////
// Global functions.
////////////////////////////////////////////////////////////////////////
osg::Node *
fgLoad3DModelPanel(const std::string &path, SGPropertyNode *prop_root)
{
bool loadPanels = true;
osg::Node* node = SGModelLib::loadModel(path, prop_root, NULL, loadPanels);
if (node)
node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
return node;
}
// end of model_panel.cxx

View file

@ -1,51 +0,0 @@
// model.hxx - manage a 3D aircraft model.
// Written by David Megginson, started 2002.
//
// This file is in the Public Domain, and comes with no warranty.
#ifndef __MODEL_HXX
#define __MODEL_HXX 1
#ifndef __cplusplus
# error This library requires C++
#endif
#include <vector>
using std::vector;
#include <simgear/props/props.hxx>
// Don't pull in the headers, since we don't need them here.
class SGInterpTable;
class FGCondition;
class FGLocation;
// Has anyone done anything *really* stupid, like making min and max macros?
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
/**
* Load a 3D model with or without XML wrapper. This version supports
* also loading the instrument panel and is wired in with dependencies
* on panelnode.hxx, and thus files in src/Cockpit and also GUI/mouse
* input code to support the 3d clickable hotspots.
*
* If the path ends in ".xml", then it will be used as a property-
* list wrapper to add animations to the model.
*
* Subsystems should not normally invoke this function directly;
* instead, they should use the SGModelLoader declared in loader.hxx.
*/
osg::Node *fgLoad3DModelPanel( const std::string &path, SGPropertyNode *prop_root);
#endif // __MODEL_HXX

View file

@ -86,27 +86,25 @@ FGModelMgr::add_model (SGPropertyNode * node)
instance->node = node; instance->node = node;
model->init( object ); model->init( object );
double lon = node->getDoubleValue("longitude-deg"),
lat = node->getDoubleValue("latitude-deg"),
elevFt = node->getDoubleValue("elevation-ft");
// Set position and orientation either model->setPosition(SGGeod::fromDegFt(lon, lat, elevFt));
// indirectly through property refs // Set position and orientation either
// or directly with static values. // indirectly through property refs
// or directly with static values.
SGPropertyNode * child = node->getChild("longitude-deg-prop"); SGPropertyNode * child = node->getChild("longitude-deg-prop");
if (child != 0) if (child != 0)
instance->lon_deg_node = fgGetNode(child->getStringValue(), true); instance->lon_deg_node = fgGetNode(child->getStringValue(), true);
else
model->setLongitudeDeg(node->getDoubleValue("longitude-deg"));
child = node->getChild("latitude-deg-prop"); child = node->getChild("latitude-deg-prop");
if (child != 0) if (child != 0)
instance->lat_deg_node = fgGetNode(child->getStringValue(), true); instance->lat_deg_node = fgGetNode(child->getStringValue(), true);
else
model->setLatitudeDeg(node->getDoubleValue("latitude-deg"));
child = node->getChild("elevation-ft-prop"); child = node->getChild("elevation-ft-prop");
if (child != 0) if (child != 0)
instance->elev_ft_node = fgGetNode(child->getStringValue(), true); instance->elev_ft_node = fgGetNode(child->getStringValue(), true);
else
model->setElevationFt(node->getDoubleValue("elevation-ft"));
child = node->getChild("roll-deg-prop"); child = node->getChild("roll-deg-prop");
if (child != 0) if (child != 0)
@ -158,17 +156,18 @@ struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
void operator()(FGModelMgr::Instance* instance) const void operator()(FGModelMgr::Instance* instance) const
{ {
SGModelPlacement* model = instance->model; SGModelPlacement* model = instance->model;
double lon, lat, elev, roll, pitch, heading; double roll, pitch, heading;
lon = lat = elev = roll = pitch = heading = 0.0; roll = pitch = heading = 0.0;
SGGeod pos = model->getPosition();
try { try {
// Optionally set position from properties // Optionally set position from properties
if (instance->lon_deg_node != 0) if (instance->lon_deg_node != 0)
lon = testNan(instance->lon_deg_node->getDoubleValue()); pos.setLongitudeDeg(testNan(instance->lon_deg_node->getDoubleValue()));
if (instance->lat_deg_node != 0) if (instance->lat_deg_node != 0)
lat = testNan(instance->lat_deg_node->getDoubleValue()); pos.setLatitudeDeg(testNan(instance->lat_deg_node->getDoubleValue()));
if (instance->elev_ft_node != 0) if (instance->elev_ft_node != 0)
elev = testNan(instance->elev_ft_node->getDoubleValue()); pos.setElevationFt(testNan(instance->elev_ft_node->getDoubleValue()));
// Optionally set orientation from properties // Optionally set orientation from properties
if (instance->roll_deg_node != 0) if (instance->roll_deg_node != 0)
@ -184,14 +183,8 @@ struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
<< " has invalid values"); << " has invalid values");
return; 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);
model->setPosition(pos);
// Optionally set orientation from properties // Optionally set orientation from properties
if (instance->roll_deg_node != 0) if (instance->roll_deg_node != 0)
model->setRollDeg(roll); model->setRollDeg(roll);

View file

@ -33,6 +33,8 @@
#include <osg/MatrixTransform> #include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform> #include <osg/PositionAttitudeTransform>
#include <osg/CameraView> #include <osg/CameraView>
#include <osg/LOD>
#include <osgViewer/Viewer> #include <osgViewer/Viewer>
#include <simgear/constants.h> #include <simgear/constants.h>
@ -252,6 +254,17 @@ void FGScenery::init() {
aircraft_branch->setName( "Aircraft" ); aircraft_branch->setName( "Aircraft" );
scene_graph->addChild( aircraft_branch.get() ); scene_graph->addChild( aircraft_branch.get() );
// choosing to make the interior branch a child of the main
// aircraft group, for the moment. This simplifes places which
// assume all aircraft elements are within this group - principally
// FGODGuage::set_aircraft_texture.
interior_branch = new osg::Group;
interior_branch->setName( "Interior" );
osg::LOD* interiorLOD = new osg::LOD;
interiorLOD->addChild(interior_branch.get(), 0.0, 50.0);
aircraft_branch->addChild( interiorLOD );
// Initials values needed by the draw-time object loader // Initials values needed by the draw-time object loader
sgUserDataInit( globals->get_props() ); sgUserDataInit( globals->get_props() );
} }

View file

@ -50,6 +50,8 @@ class FGScenery : public SGSubsystem {
osg::ref_ptr<osg::Group> terrain_branch; osg::ref_ptr<osg::Group> terrain_branch;
osg::ref_ptr<osg::Group> models_branch; osg::ref_ptr<osg::Group> models_branch;
osg::ref_ptr<osg::Group> aircraft_branch; osg::ref_ptr<osg::Group> aircraft_branch;
osg::ref_ptr<osg::Group> interior_branch;
osg::ref_ptr<flightgear::SceneryPager> _pager; osg::ref_ptr<flightgear::SceneryPager> _pager;
public: public:
@ -106,6 +108,7 @@ public:
osg::Group *get_terrain_branch () const { return terrain_branch.get(); } osg::Group *get_terrain_branch () const { return terrain_branch.get(); }
osg::Group *get_models_branch () const { return models_branch.get(); } osg::Group *get_models_branch () const { return models_branch.get(); }
osg::Group *get_aircraft_branch () const { return aircraft_branch.get(); } osg::Group *get_aircraft_branch () const { return aircraft_branch.get(); }
osg::Group *get_interior_branch () const { return interior_branch.get(); }
/// Returns true if scenery is available for the given lat, lon position /// Returns true if scenery is available for the given lat, lon position
/// within a range of range_m. /// within a range of range_m.