]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
Functions should always return a value
[flightgear.git] / src / Scenery / scenery.cxx
1 // scenery.cxx -- data structures and routines for managing scenery.
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <osgUtil/IntersectVisitor>
32
33 #include <simgear/constants.h>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/scene/tgdb/userdata.hxx>
36 #include <simgear/math/sg_geodesy.hxx>
37 #include <simgear/scene/model/placementtrans.hxx>
38 #include <simgear/scene/material/matlib.hxx>
39 #include <simgear/scene/util/SGNodeMasks.hxx>
40 #include <simgear/scene/util/SGSceneUserData.hxx>
41 #include <simgear/scene/model/CheckSceneryVisitor.hxx>
42
43 #include <Main/fg_props.hxx>
44
45 #include "tilemgr.hxx"
46 #include "scenery.hxx"
47
48 using namespace flightgear;
49
50 class FGGroundPickCallback : public SGPickCallback {
51 public:
52   virtual bool buttonPressed(int button, const Info& info)
53   {
54     // only on left mouse button
55     if (button != 0)
56       return false;
57
58     SGGeod geod = SGGeod::fromCart(info.wgs84);
59     SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
60
61     SGPropertyNode *c = fgGetNode("/sim/input/click", true);
62     c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
63     c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
64     c->setDoubleValue("elevation-m", geod.getElevationM());
65     c->setDoubleValue("elevation-ft", geod.getElevationFt());
66     fgSetBool("/sim/signals/click", 1);
67
68     return true;
69   }
70 };
71
72 // Scenery Management system
73 FGScenery::FGScenery()
74 {
75     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
76 }
77
78 FGScenery::~FGScenery() {
79 }
80
81
82 // Initialize the Scenery Management system
83 void FGScenery::init() {
84     // Scene graph root
85     scene_graph = new osg::Group;
86     scene_graph->setName( "Scene" );
87
88     // Terrain branch
89     terrain_branch = new osg::Group;
90     terrain_branch->setName( "Terrain" );
91     scene_graph->addChild( terrain_branch.get() );
92     SGSceneUserData* userData;
93     userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
94     userData->setPickCallback(new FGGroundPickCallback);
95
96     models_branch = new osg::Group;
97     models_branch->setName( "Models" );
98     scene_graph->addChild( models_branch.get() );
99
100     aircraft_branch = new osg::Group;
101     aircraft_branch->setName( "Aircraft" );
102     scene_graph->addChild( aircraft_branch.get() );
103
104     // Initials values needed by the draw-time object loader
105     sgUserDataInit( globals->get_props() );
106 }
107
108
109 void FGScenery::update(double dt) {
110 }
111
112
113 void FGScenery::bind() {
114 }
115
116
117 void FGScenery::unbind() {
118 }
119
120 bool
121 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
122                                 double& alt, const SGMaterial** material)
123 {
124   SGGeod geod = SGGeod::fromCart(pos);
125   geod.setElevationM(geod.getElevationM() + max_altoff);
126   return get_elevation_m(geod, alt, material);
127 }
128
129 bool
130 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
131                            const SGMaterial** material)
132 {
133   SGVec3d start = SGVec3d::fromGeod(geod);
134
135   SGGeod geodEnd = geod;
136   geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
137   SGVec3d end = SGVec3d::fromGeod(geodEnd);
138   
139   osgUtil::IntersectVisitor intersectVisitor;
140   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
141   osg::ref_ptr<osg::LineSegment> lineSegment;
142   lineSegment = new osg::LineSegment(start.osg(), end.osg());
143   intersectVisitor.addLineSegment(lineSegment.get());
144   get_scene_graph()->accept(intersectVisitor);
145   bool hits = intersectVisitor.hits();
146   if (hits) {
147     int nHits = intersectVisitor.getNumHits(lineSegment.get());
148     alt = -SGLimitsd::max();
149     for (int i = 0; i < nHits; ++i) {
150       const osgUtil::Hit& hit
151         = intersectVisitor.getHitList(lineSegment.get())[i];
152       SGVec3d point;
153       point.osg() = hit.getWorldIntersectPoint();
154       SGGeod geod = SGGeod::fromCart(point);
155       double elevation = geod.getElevationM();
156       if (alt < elevation) {
157         alt = elevation;
158         if (material) {
159           const osg::StateSet* stateSet = hit.getDrawable()->getStateSet();
160           *material = SGMaterialLib::findMaterial(stateSet);
161         }
162       }
163     }
164   }
165
166   return hits;
167 }
168
169 bool
170 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
171                                         SGVec3d& nearestHit)
172 {
173   // We assume that starting positions in the center of the earth are invalid
174   if ( norm1(pos) < 1 )
175     return false;
176
177   // Make really sure the direction is normalized, is really cheap compared to
178   // computation of ground intersection.
179   SGVec3d start = pos;
180   SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
181   
182   osgUtil::IntersectVisitor intersectVisitor;
183   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
184   osg::ref_ptr<osg::LineSegment> lineSegment;
185   lineSegment = new osg::LineSegment(start.osg(), end.osg());
186   intersectVisitor.addLineSegment(lineSegment.get());
187   get_scene_graph()->accept(intersectVisitor);
188   bool hits = intersectVisitor.hits();
189   if (hits) {
190     int nHits = intersectVisitor.getNumHits(lineSegment.get());
191     double dist = SGLimitsd::max();
192     for (int i = 0; i < nHits; ++i) {
193       const osgUtil::Hit& hit
194         = intersectVisitor.getHitList(lineSegment.get())[i];
195       SGVec3d point;
196       point.osg() = hit.getWorldIntersectPoint();
197       double newdist = length(start - point);
198       if (newdist < dist) {
199         dist = newdist;
200         nearestHit = point;
201       }
202     }
203   }
204
205   return hits;
206 }
207
208 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
209 {
210   if(globals->get_tile_mgr()->scenery_available(position, range_m))
211   {
212     double elev;
213     get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0);
214     SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
215     simgear::CheckSceneryVisitor csnv(getPagerSingleton(), p.osg(), range_m);
216     // currently the PagedLODs will not be loaded by the DatabasePager
217     // while the splashscreen is there, so CheckSceneryVisitor force-loads
218     // missing objects in the main thread
219     get_scene_graph()->accept(csnv);
220     if(!csnv.isLoaded())
221         return false;
222     return true;
223   }
224   return false;
225 }
226
227 SceneryPager* FGScenery::getPagerSingleton()
228 {
229     static osg::ref_ptr<SceneryPager> pager = new SceneryPager;
230     return pager.get();
231 }