]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
Change FGSteam into a proper subsystem rather than a collection of
[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(path);
58   } catch (const sg_exception &ex) {
59     SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path);
60     SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
61     _aircraft->init("Models/Geometry/glider.ac");
62   }
63   _scene->addKid(_aircraft->getSceneGraph());
64   _selector->addKid(_aircraft->getSceneGraph());
65   globals->get_scenery()->get_aircraft_branch()->addKid(_selector);
66 }
67
68 void 
69 FGAircraftModel::bind ()
70 {
71   // No-op
72 }
73
74 void 
75 FGAircraftModel::unbind ()
76 {
77   // No-op
78 }
79
80 void
81 FGAircraftModel::update (double dt)
82 {
83   int view_number = globals->get_viewmgr()->get_current();
84
85   if (view_number == 0 && !fgGetBool("/sim/view/internal")) {
86     _aircraft->setVisible(false);
87   } else {
88     _aircraft->setVisible(true);
89   }
90
91   _aircraft->setPosition(fgGetDouble("/position/longitude-deg"),
92                          fgGetDouble("/position/latitude-deg"),
93                          fgGetDouble("/position/altitude-ft"));
94   _aircraft->setOrientation(fgGetDouble("/orientation/roll-deg"),
95                             fgGetDouble("/orientation/pitch-deg"),
96                             fgGetDouble("/orientation/heading-deg"));
97   _aircraft->update(dt);
98
99 }
100
101 void
102 FGAircraftModel::draw ()
103 {
104                                 // OK, now adjust the clip planes and draw
105                                 // FIXME: view number shouldn't be 
106                                 // hard-coded.
107   int view_number = globals->get_viewmgr()->get_current();
108   if (_aircraft->getVisible() && view_number == 0) {
109     glClearDepth(1);
110     glClear(GL_DEPTH_BUFFER_BIT);
111     ssgSetNearFar(_nearplane, _farplane);
112     ssgCullAndDraw(_scene);
113     _selector->select(0);
114   } else {
115     _selector->select(1);
116   }
117
118 }
119
120 // end of model.cxx