]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
This is step "1" of probably "many" in the process of separating out the
[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/misc/exception.hxx>
18 #include <simgear/misc/sg_path.hxx>
19
20 #include <Main/globals.hxx>
21 #include <Main/fg_props.hxx>
22 #include <Main/viewmgr.hxx>
23 #include <Scenery/scenery.hxx>
24
25 #include "acmodel.hxx"
26 #include "model.hxx"
27
28
29 \f
30 ////////////////////////////////////////////////////////////////////////
31 // Implementation of FGAircraftModel
32 ////////////////////////////////////////////////////////////////////////
33
34 FGAircraftModel::FGAircraftModel ()
35   : _aircraft(0),
36     _selector(new ssgSelector),
37     _scene(new ssgRoot),
38     _nearplane(0.01f),
39     _farplane(100.0f)
40 {
41 }
42
43 FGAircraftModel::~FGAircraftModel ()
44 {
45   delete _aircraft;
46   delete _scene;
47                                 // SSG will delete it
48   globals->get_scenery()->get_aircraft_branch()->removeKid(_selector);
49 }
50
51 void 
52 FGAircraftModel::init ()
53 {
54   _aircraft = new FGModelPlacement;
55   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
56   try {
57     _aircraft->init( globals->get_fg_root(),
58                      path,
59                      globals->get_props(),
60                      globals->get_sim_time_sec() );
61   } catch (const sg_exception &ex) {
62     SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path);
63     SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
64     _aircraft->init( globals->get_fg_root(),
65                      "Models/Geometry/glider.ac",
66                      globals->get_props(),
67                      globals->get_sim_time_sec() );
68   }
69   _scene->addKid(_aircraft->getSceneGraph());
70   _selector->addKid(_aircraft->getSceneGraph());
71   globals->get_scenery()->get_aircraft_branch()->addKid(_selector);
72 }
73
74 void 
75 FGAircraftModel::bind ()
76 {
77   // No-op
78 }
79
80 void 
81 FGAircraftModel::unbind ()
82 {
83   // No-op
84 }
85
86 void
87 FGAircraftModel::update (double dt)
88 {
89   int view_number = globals->get_viewmgr()->get_current();
90
91   if (view_number == 0 && !fgGetBool("/sim/view/internal")) {
92     _aircraft->setVisible(false);
93   } else {
94     _aircraft->setVisible(true);
95   }
96
97   _aircraft->setPosition(fgGetDouble("/position/longitude-deg"),
98                          fgGetDouble("/position/latitude-deg"),
99                          fgGetDouble("/position/altitude-ft"));
100   _aircraft->setOrientation(fgGetDouble("/orientation/roll-deg"),
101                             fgGetDouble("/orientation/pitch-deg"),
102                             fgGetDouble("/orientation/heading-deg"));
103   _aircraft->update( globals->get_scenery()->get_center() );
104
105 }
106
107 void
108 FGAircraftModel::draw ()
109 {
110                                 // OK, now adjust the clip planes and draw
111                                 // FIXME: view number shouldn't be 
112                                 // hard-coded.
113   int view_number = globals->get_viewmgr()->get_current();
114   if (_aircraft->getVisible() && view_number == 0) {
115     glClearDepth(1);
116     glClear(GL_DEPTH_BUFFER_BIT);
117     ssgSetNearFar(_nearplane, _farplane);
118     ssgCullAndDraw(_scene);
119     _selector->select(0);
120   } else {
121     _selector->select(1);
122   }
123
124 }
125
126 // end of model.cxx