]> git.mxchange.org Git - flightgear.git/blob - src/Model/acmodel.cxx
Merge branch 'maint' (early part) into next
[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   delete _aircraft;
46                                 // SSG will delete it
47   globals->get_scenery()->get_aircraft_branch()->removeChild(_selector.get());
48 }
49
50 void 
51 FGAircraftModel::init ()
52 {
53   _aircraft = new SGModelPlacement;
54   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
55   try {
56     osg::Node *model = fgLoad3DModelPanel( path, globals->get_props());
57     _aircraft->init( model );
58   } catch (const sg_exception &ex) {
59     SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path << ':');
60     SG_LOG(SG_GENERAL, SG_ALERT, "  " << ex.getFormattedMessage());
61     SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
62     osg::Node *model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
63                                            globals->get_props());
64     _aircraft->init( model );
65   }
66   _selector->addChild(_aircraft->getSceneGraph(), true);
67   // Do not do altitude computations with that model
68   _selector->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
69   globals->get_scenery()->get_aircraft_branch()->addChild(_selector.get());
70 }
71
72 void
73 FGAircraftModel::bind ()
74 {
75   // No-op
76 }
77
78 void
79 FGAircraftModel::unbind ()
80 {
81   // No-op
82 }
83
84 void
85 FGAircraftModel::update (double dt)
86 {
87   int view_number = globals->get_viewmgr()->get_current();
88   int is_internal = fgGetBool("/sim/current-view/internal");
89
90   if (view_number == 0 && !is_internal) {
91     _aircraft->setVisible(false);
92   } else {
93     _aircraft->setVisible(true);
94   }
95
96   _aircraft->setPosition(fgGetDouble("/position/longitude-deg"),
97                          fgGetDouble("/position/latitude-deg"),
98                          fgGetDouble("/position/altitude-ft"));
99   _aircraft->setOrientation(fgGetDouble("/orientation/roll-deg"),
100                             fgGetDouble("/orientation/pitch-deg"),
101                             fgGetDouble("/orientation/heading-deg"));
102   _aircraft->update();
103 }
104
105
106 // end of model.cxx