]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
Modified Files:
[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     _nearplane(0.10f),
39     _farplane(1000.0f)
40 {
41 }
42
43 FGAircraftModel::~FGAircraftModel ()
44 {
45   // Unregister that one at the scenery manager
46   if (_aircraft)
47     globals->get_scenery()->unregister_placement_transform(_aircraft->getTransform());
48
49   delete _aircraft;
50                                 // SSG will delete it
51   globals->get_scenery()->get_aircraft_branch()->removeChild(_selector.get());
52 }
53
54 void 
55 FGAircraftModel::init ()
56 {
57   SGPath liveryPath;
58   _aircraft = new SGModelPlacement;
59   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
60   string texture_path = fgGetString("/sim/model/texture-path");
61   if( texture_path.size() ) {
62       SGPath temp_path;
63       if ( !ulIsAbsolutePathName( texture_path.c_str() ) ) {
64           temp_path = globals->get_fg_root();
65           temp_path.append( SGPath( path ).dir() );
66           temp_path.append( texture_path );
67           liveryPath = temp_path;
68       } else
69           liveryPath = texture_path;
70   }
71   try {
72     osg::Node *model = fgLoad3DModelPanel( globals->get_fg_root(),
73                                            path,
74                                            globals->get_props(),
75                                            globals->get_sim_time_sec(),
76                                            liveryPath);
77     _aircraft->init( model );
78   } catch (const sg_exception &ex) {
79     SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path);
80     SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
81     osg::Node *model = fgLoad3DModelPanel( globals->get_fg_root(),
82                                            "Models/Geometry/glider.ac",
83                                            globals->get_props(),
84                                            globals->get_sim_time_sec(),
85                                            liveryPath);
86     _aircraft->init( model );
87   }
88   _selector->addChild(_aircraft->getSceneGraph());
89   // Do not do altitude computations with that model
90   _selector->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
91   globals->get_scenery()->get_aircraft_branch()->addChild(_selector.get());
92
93   // Register that one at the scenery manager
94   globals->get_scenery()->register_placement_transform(_aircraft->getTransform());
95 }
96
97 void
98 FGAircraftModel::bind ()
99 {
100   // No-op
101 }
102
103 void
104 FGAircraftModel::unbind ()
105 {
106   // No-op
107 }
108
109 void
110 FGAircraftModel::update (double dt)
111 {
112   int view_number = globals->get_viewmgr()->get_current();
113   int is_internal = fgGetBool("/sim/current-view/internal");
114
115   if (view_number == 0 && !is_internal) {
116     _aircraft->setVisible(false);
117   } else {
118     _aircraft->setVisible(true);
119   }
120
121   _aircraft->setPosition(fgGetDouble("/position/longitude-deg"),
122                          fgGetDouble("/position/latitude-deg"),
123                          fgGetDouble("/position/altitude-ft"));
124   _aircraft->setOrientation(fgGetDouble("/orientation/roll-deg"),
125                             fgGetDouble("/orientation/pitch-deg"),
126                             fgGetDouble("/orientation/heading-deg"));
127   _aircraft->update();
128 }
129
130
131 // end of model.cxx