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()
15 #include <simgear/compiler.h>
16 #include <simgear/debug/logstream.hxx>
17 #include <simgear/structure/exception.hxx>
18 #include <simgear/misc/sg_path.hxx>
19 #include <simgear/scene/model/placement.hxx>
21 #include <Main/globals.hxx>
22 #include <Main/fg_props.hxx>
23 #include <Main/renderer.hxx>
24 #include <Main/viewmgr.hxx>
25 #include <Main/viewer.hxx>
26 #include <Scenery/scenery.hxx>
28 #include "model_panel.hxx"
30 #include "acmodel.hxx"
34 ////////////////////////////////////////////////////////////////////////
35 // Implementation of FGAircraftModel
36 ////////////////////////////////////////////////////////////////////////
38 FGAircraftModel::FGAircraftModel ()
40 _selector(new ssgSelector),
47 FGAircraftModel::~FGAircraftModel ()
49 // Unregister that one at the scenery manager
51 globals->get_scenery()->unregister_placement_transform(_aircraft->getTransform());
56 globals->get_scenery()->get_aircraft_branch()->removeKid(_selector);
60 FGAircraftModel::init ()
62 _aircraft = new SGModelPlacement;
63 string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
65 ssgBranch *model = fgLoad3DModelPanel( globals->get_fg_root(),
68 globals->get_sim_time_sec() );
69 _aircraft->init( model );
70 } catch (const sg_exception &ex) {
71 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path);
72 SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
73 ssgBranch *model = fgLoad3DModelPanel( globals->get_fg_root(),
74 "Models/Geometry/glider.ac",
76 globals->get_sim_time_sec() );
77 _aircraft->init( model );
79 _scene->addKid(_aircraft->getSceneGraph());
80 _selector->addKid(_aircraft->getSceneGraph());
81 globals->get_scenery()->get_aircraft_branch()->addKid(_selector);
83 // Register that one at the scenery manager
84 globals->get_scenery()->register_placement_transform(_aircraft->getTransform());
88 FGAircraftModel::bind ()
94 FGAircraftModel::unbind ()
100 FGAircraftModel::update (double dt)
102 int view_number = globals->get_viewmgr()->get_current();
103 int is_internal = fgGetBool("/sim/current-view/internal");
105 if (view_number == 0 && !is_internal) {
106 _aircraft->setVisible(false);
108 _aircraft->setVisible(true);
111 _aircraft->setPosition(fgGetDouble("/position/longitude-deg"),
112 fgGetDouble("/position/latitude-deg"),
113 fgGetDouble("/position/altitude-ft"));
114 _aircraft->setOrientation(fgGetDouble("/orientation/roll-deg"),
115 fgGetDouble("/orientation/pitch-deg"),
116 fgGetDouble("/orientation/heading-deg"));
117 _aircraft->update( globals->get_scenery()->get_center() );
122 FGAircraftModel::draw ()
124 // OK, now adjust the clip planes and draw
125 // FIXME: view number shouldn't be
127 bool is_internal = globals->get_current_view()->getInternal();
128 if (_aircraft->getVisible() && is_internal) {
130 glClear(GL_DEPTH_BUFFER_BIT);
131 FGRenderer::setNearFar(_nearplane, _farplane);
132 ssgCullAndDraw(_scene);
133 _selector->select(0);
135 _selector->select(1);