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