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