1 // model.cxx - manage a 3D aircraft model.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
10 #include <string.h> // for strcmp()
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>
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>
29 #include "model_panel.hxx"
31 #include "acmodel.hxx"
34 class fgLoaderOptions : ssgLoaderOptions {
37 virtual void makeTexturePath ( char* path, const char *fname ) const ;
42 void fgLoaderOptions::makeTexturePath ( char *path, const char *fname ) const
44 /* Remove all leading path information. */
45 const char* seps = "\\/" ;
46 const char* fn = & fname [ strlen ( fname ) - 1 ] ;
47 for ( ; fn != fname && strchr(seps,*fn) == NULL ; fn-- )
48 /* Search back for a seperator */ ;
49 if ( strchr(seps,*fn) != NULL )
52 // if we have a livery path and the texture is found there then we use that
53 // path in priority, if the texture was not found or we add no additional
54 // livery path then we use the current model path or model/texture-path
55 if( livery_path.size() ) {
56 make_path( path, livery_path.c_str(), fname );
57 if( ulFileExists( path ) )
60 make_path ( path, texture_dir, fname ) ;
63 static fgLoaderOptions _fgLoaderOptions;
66 ////////////////////////////////////////////////////////////////////////
67 // Implementation of FGAircraftModel
68 ////////////////////////////////////////////////////////////////////////
70 FGAircraftModel::FGAircraftModel ()
72 _selector(new ssgSelector),
79 FGAircraftModel::~FGAircraftModel ()
81 // Unregister that one at the scenery manager
83 globals->get_scenery()->unregister_placement_transform(_aircraft->getTransform());
88 globals->get_scenery()->get_aircraft_branch()->removeKid(_selector);
92 FGAircraftModel::init ()
94 ssgLoaderOptions *currLoaderOptions = ssgGetCurrentOptions();
95 ssgSetCurrentOptions( (ssgLoaderOptions*)&_fgLoaderOptions );
96 _aircraft = new SGModelPlacement;
97 string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
98 string texture_path = fgGetString("/sim/model/texture-path");
99 if( texture_path.size() ) {
101 if ( !ulIsAbsolutePathName( texture_path.c_str() ) ) {
102 temp_path = globals->get_fg_root();
103 temp_path.append( SGPath( path ).dir() );
104 temp_path.append( texture_path );
105 _fgLoaderOptions.livery_path = temp_path.str();
107 _fgLoaderOptions.livery_path = texture_path;
110 ssgBranch *model = fgLoad3DModelPanel( globals->get_fg_root(),
112 globals->get_props(),
113 globals->get_sim_time_sec() );
114 _aircraft->init( model );
115 } catch (const sg_exception &ex) {
116 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path);
117 SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
118 ssgBranch *model = fgLoad3DModelPanel( globals->get_fg_root(),
119 "Models/Geometry/glider.ac",
120 globals->get_props(),
121 globals->get_sim_time_sec() );
122 _aircraft->init( model );
124 _scene->addKid(_aircraft->getSceneGraph());
125 _selector->addKid(_aircraft->getSceneGraph());
126 globals->get_scenery()->get_aircraft_branch()->addKid(_selector);
128 // Register that one at the scenery manager
129 globals->get_scenery()->register_placement_transform(_aircraft->getTransform());
130 ssgSetCurrentOptions( currLoaderOptions );
134 FGAircraftModel::bind ()
140 FGAircraftModel::unbind ()
146 FGAircraftModel::update (double dt)
148 int view_number = globals->get_viewmgr()->get_current();
149 int is_internal = fgGetBool("/sim/current-view/internal");
151 if (view_number == 0 && !is_internal) {
152 _aircraft->setVisible(false);
154 _aircraft->setVisible(true);
157 _aircraft->setPosition(fgGetDouble("/position/longitude-deg"),
158 fgGetDouble("/position/latitude-deg"),
159 fgGetDouble("/position/altitude-ft"));
160 _aircraft->setOrientation(fgGetDouble("/orientation/roll-deg"),
161 fgGetDouble("/orientation/pitch-deg"),
162 fgGetDouble("/orientation/heading-deg"));
167 FGAircraftModel::draw ()
169 // OK, now adjust the clip planes and draw
170 // FIXME: view number shouldn't be
172 bool is_internal = globals->get_current_view()->getInternal();
173 if (_aircraft->getVisible() && is_internal) {
175 glClear(GL_DEPTH_BUFFER_BIT);
176 FGRenderer::setNearFar(_nearplane, _farplane);
177 ssgCullAndDraw(_scene);
178 _selector->select(0);
180 _selector->select(1);
181 // in external view the shadows are drawn before the transparent parts of the ac
182 _scene->setTravCallback( SSG_CALLBACK_POSTTRAV, SGShadowVolume::ACpostTravCB);
183 ssgCullAndDraw(_scene);
184 _scene->setTravCallback( SSG_CALLBACK_POSTTRAV, 0);