]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
The view frustum is defined in plib apps using calls to ssgSetFOV() and
[flightgear.git] / src / Model / acmodel.cxx
1 // model.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #ifdef HAVE_CONFIG_H
7 #  include <config.h>
8 #endif
9
10 #include <string.h>             // for strcmp()
11
12 #include <plib/sg.h>
13 #include <plib/ssg.h>
14
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>
20
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>
27
28 #include "model_panel.hxx"
29
30 #include "acmodel.hxx"
31
32
33 \f
34 ////////////////////////////////////////////////////////////////////////
35 // Implementation of FGAircraftModel
36 ////////////////////////////////////////////////////////////////////////
37
38 FGAircraftModel::FGAircraftModel ()
39   : _aircraft(0),
40     _selector(new ssgSelector),
41     _scene(new ssgRoot),
42     _nearplane(0.01f),
43     _farplane(1000.0f)
44 {
45 }
46
47 FGAircraftModel::~FGAircraftModel ()
48 {
49   delete _aircraft;
50   delete _scene;
51                                 // SSG will delete it
52   globals->get_scenery()->get_aircraft_branch()->removeKid(_selector);
53 }
54
55 void 
56 FGAircraftModel::init ()
57 {
58   _aircraft = new SGModelPlacement;
59   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
60   try {
61     ssgBranch *model = fgLoad3DModelPanel( globals->get_fg_root(),
62                                            path,
63                                            globals->get_props(),
64                                            globals->get_sim_time_sec() );
65     _aircraft->init( model );
66   } catch (const sg_exception &ex) {
67     SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path);
68     SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
69     ssgBranch *model = fgLoad3DModelPanel( globals->get_fg_root(),
70                                            "Models/Geometry/glider.ac",
71                                            globals->get_props(),
72                                            globals->get_sim_time_sec() );
73     _aircraft->init( model );
74   }
75   _scene->addKid(_aircraft->getSceneGraph());
76   _selector->addKid(_aircraft->getSceneGraph());
77   globals->get_scenery()->get_aircraft_branch()->addKid(_selector);
78 }
79
80 void 
81 FGAircraftModel::bind ()
82 {
83   // No-op
84 }
85
86 void 
87 FGAircraftModel::unbind ()
88 {
89   // No-op
90 }
91
92 void
93 FGAircraftModel::update (double dt)
94 {
95   int view_number = globals->get_viewmgr()->get_current();
96   int is_internal = fgGetBool("/sim/current-view/internal");
97
98   if (view_number == 0 && !is_internal) {
99     _aircraft->setVisible(false);
100   } else {
101     _aircraft->setVisible(true);
102   }
103
104   _aircraft->setPosition(fgGetDouble("/position/longitude-deg"),
105                          fgGetDouble("/position/latitude-deg"),
106                          fgGetDouble("/position/altitude-ft"));
107   _aircraft->setOrientation(fgGetDouble("/orientation/roll-deg"),
108                             fgGetDouble("/orientation/pitch-deg"),
109                             fgGetDouble("/orientation/heading-deg"));
110   _aircraft->update( globals->get_scenery()->get_center() );
111
112 }
113
114 void
115 FGAircraftModel::draw ()
116 {
117                                 // OK, now adjust the clip planes and draw
118                                 // FIXME: view number shouldn't be 
119                                 // hard-coded.
120   bool is_internal = globals->get_current_view()->getInternal();
121   if (_aircraft->getVisible() && is_internal) {
122     glClearDepth(1);
123     glClear(GL_DEPTH_BUFFER_BIT);
124     FGRenderer::setNearFar(_nearplane, _farplane);
125     ssgCullAndDraw(_scene);
126     _selector->select(0);
127   } else {
128     _selector->select(1);
129   }
130
131 }
132
133 // end of model.cxx