]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
remove obsolete preset dialogs
[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 <simgear/compiler.h>
13 #include <simgear/debug/logstream.hxx>
14 #include <simgear/structure/exception.hxx>
15 #include <simgear/misc/sg_path.hxx>
16 #include <simgear/scene/model/placement.hxx>
17 #include <simgear/scene/util/SGNodeMasks.hxx>
18
19 #include <Main/globals.hxx>
20 #include <Main/fg_props.hxx>
21 #include <Main/renderer.hxx>
22 #include <Main/viewmgr.hxx>
23 #include <Main/viewer.hxx>
24 #include <Scenery/scenery.hxx>
25
26 #include "model_panel.hxx"
27
28 #include "acmodel.hxx"
29
30 \f
31 ////////////////////////////////////////////////////////////////////////
32 // Implementation of FGAircraftModel
33 ////////////////////////////////////////////////////////////////////////
34
35 FGAircraftModel::FGAircraftModel ()
36   : _aircraft(0),
37     _selector(new osg::Switch),
38     _scene(new osg::Group),
39     _nearplane(0.10f),
40     _farplane(1000.0f)
41 {
42 }
43
44 FGAircraftModel::~FGAircraftModel ()
45 {
46   // Unregister that one at the scenery manager
47   if (_aircraft)
48     globals->get_scenery()->unregister_placement_transform(_aircraft->getTransform());
49
50   delete _aircraft;
51                                 // SSG will delete it
52   globals->get_scenery()->get_aircraft_branch()->removeChild(_selector.get());
53 }
54
55 void 
56 FGAircraftModel::init ()
57 {
58   SGPath liveryPath;
59   _aircraft = new SGModelPlacement;
60   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
61   string texture_path = fgGetString("/sim/model/texture-path");
62   if( texture_path.size() ) {
63       SGPath temp_path;
64       if ( !ulIsAbsolutePathName( texture_path.c_str() ) ) {
65           temp_path = globals->get_fg_root();
66           temp_path.append( SGPath( path ).dir() );
67           temp_path.append( texture_path );
68           liveryPath = temp_path;
69       } else
70           liveryPath = texture_path;
71   }
72   try {
73     osg::Node *model = fgLoad3DModelPanel( globals->get_fg_root(),
74                                            path,
75                                            globals->get_props(),
76                                            globals->get_sim_time_sec(),
77                                            liveryPath);
78     _aircraft->init( model );
79   } catch (const sg_exception &ex) {
80     SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path);
81     SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
82     osg::Node *model = fgLoad3DModelPanel( globals->get_fg_root(),
83                                            "Models/Geometry/glider.ac",
84                                            globals->get_props(),
85                                            globals->get_sim_time_sec(),
86                                            liveryPath);
87     _aircraft->init( model );
88   }
89   _scene->addChild(_aircraft->getSceneGraph());
90   _selector->addChild(_aircraft->getSceneGraph());
91   // Do not do altitude computations with that model
92   _selector->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
93   globals->get_scenery()->get_aircraft_branch()->addChild(_selector.get());
94
95   // Register that one at the scenery manager
96   globals->get_scenery()->register_placement_transform(_aircraft->getTransform());
97 }
98
99 void
100 FGAircraftModel::bind ()
101 {
102   // No-op
103 }
104
105 void
106 FGAircraftModel::unbind ()
107 {
108   // No-op
109 }
110
111 void
112 FGAircraftModel::update (double dt)
113 {
114   int view_number = globals->get_viewmgr()->get_current();
115   int is_internal = fgGetBool("/sim/current-view/internal");
116
117   if (view_number == 0 && !is_internal) {
118     _aircraft->setVisible(false);
119   } else {
120     _aircraft->setVisible(true);
121   }
122
123   _aircraft->setPosition(fgGetDouble("/position/longitude-deg"),
124                          fgGetDouble("/position/latitude-deg"),
125                          fgGetDouble("/position/altitude-ft"));
126   _aircraft->setOrientation(fgGetDouble("/orientation/roll-deg"),
127                             fgGetDouble("/orientation/pitch-deg"),
128                             fgGetDouble("/orientation/heading-deg"));
129   _aircraft->update();
130 }
131
132
133 // end of model.cxx