1 // model.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
10 #include <string.h> // for strcmp()
12 #include <simgear/compiler.h>
13 #include <simgear/debug/logstream.hxx>
14 #include <simgear/structure/exception.hxx>
15 #include <simgear/misc/sg_path.hxx>
16 #include <simgear/scene/model/placement.hxx>
17 #include <simgear/scene/util/SGNodeMasks.hxx>
19 #include <Main/globals.hxx>
20 #include <Main/fg_props.hxx>
21 #include <Main/renderer.hxx>
22 #include <Main/viewmgr.hxx>
23 #include <Main/viewer.hxx>
24 #include <Scenery/scenery.hxx>
26 #include "model_panel.hxx"
28 #include "acmodel.hxx"
31 ////////////////////////////////////////////////////////////////////////
32 // Implementation of FGAircraftModel
33 ////////////////////////////////////////////////////////////////////////
35 FGAircraftModel::FGAircraftModel ()
37 _selector(new osg::Switch),
43 FGAircraftModel::~FGAircraftModel ()
47 globals->get_scenery()->get_aircraft_branch()->removeChild(_selector.get());
51 FGAircraftModel::init ()
54 _aircraft = new SGModelPlacement;
55 string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
56 string texture_path = fgGetString("/sim/model/texture-path");
57 if( texture_path.size() ) {
59 if ( !ulIsAbsolutePathName( texture_path.c_str() ) ) {
60 temp_path = globals->get_fg_root();
61 temp_path.append( SGPath( path ).dir() );
62 temp_path.append( texture_path );
63 liveryPath = temp_path;
65 liveryPath = texture_path;
68 osg::Node *model = fgLoad3DModelPanel( globals->get_fg_root(),
71 globals->get_sim_time_sec(),
73 _aircraft->init( model );
74 } catch (const sg_exception &ex) {
75 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path << ':');
76 SG_LOG(SG_GENERAL, SG_ALERT, " " << ex.getFormattedMessage());
77 SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
78 osg::Node *model = fgLoad3DModelPanel( globals->get_fg_root(),
79 "Models/Geometry/glider.ac",
81 globals->get_sim_time_sec(),
83 _aircraft->init( model );
85 _selector->addChild(_aircraft->getSceneGraph(), true);
86 // Do not do altitude computations with that model
87 _selector->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
88 globals->get_scenery()->get_aircraft_branch()->addChild(_selector.get());
92 FGAircraftModel::bind ()
98 FGAircraftModel::unbind ()
104 FGAircraftModel::update (double dt)
106 int view_number = globals->get_viewmgr()->get_current();
107 int is_internal = fgGetBool("/sim/current-view/internal");
109 if (view_number == 0 && !is_internal) {
110 _aircraft->setVisible(false);
112 _aircraft->setVisible(true);
115 _aircraft->setPosition(fgGetDouble("/position/longitude-deg"),
116 fgGetDouble("/position/latitude-deg"),
117 fgGetDouble("/position/altitude-ft"));
118 _aircraft->setOrientation(fgGetDouble("/orientation/roll-deg"),
119 fgGetDouble("/orientation/pitch-deg"),
120 fgGetDouble("/orientation/heading-deg"));