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