]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
13cc67ffcadf120ee5bba41040264e703a19969d
[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_elevation_m(double lat, double lon, double max_alt,
122                            double& alt, const SGMaterial** material)
123 {
124   return get_elevation_m(SGGeod::fromDegM(lon, lat, max_alt), alt, material);
125 }
126
127 bool
128 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
129                                 double& alt, const SGMaterial** material)
130 {
131   SGGeod geod = SGGeod::fromCart(pos);
132   geod.setElevationM(geod.getElevationM() + max_altoff);
133   return get_elevation_m(geod, alt, material);
134 }
135
136 bool
137 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
138                            const SGMaterial** material)
139 {
140   SGVec3d start = SGVec3d::fromGeod(geod);
141
142   SGGeod geodEnd = geod;
143   geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
144   SGVec3d end = SGVec3d::fromGeod(geodEnd);
145   
146   osgUtil::IntersectVisitor intersectVisitor;
147   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
148   osg::ref_ptr<osg::LineSegment> lineSegment;
149   lineSegment = new osg::LineSegment(start.osg(), end.osg());
150   intersectVisitor.addLineSegment(lineSegment.get());
151   get_scene_graph()->accept(intersectVisitor);
152   bool hits = intersectVisitor.hits();
153   if (hits) {
154     int nHits = intersectVisitor.getNumHits(lineSegment.get());
155     alt = -SGLimitsd::max();
156     for (int i = 0; i < nHits; ++i) {
157       const osgUtil::Hit& hit
158         = intersectVisitor.getHitList(lineSegment.get())[i];
159       SGVec3d point;
160       point.osg() = hit.getWorldIntersectPoint();
161       SGGeod geod = SGGeod::fromCart(point);
162       double elevation = geod.getElevationM();
163       if (alt < elevation) {
164         alt = elevation;
165         if (material) {
166           const osg::StateSet* stateSet = hit.getDrawable()->getStateSet();
167           *material = SGMaterialLib::findMaterial(stateSet);
168         }
169       }
170     }
171   }
172
173   return hits;
174 }
175
176 bool
177 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
178                                         SGVec3d& nearestHit)
179 {
180   // We assume that starting positions in the center of the earth are invalid
181   if ( norm1(pos) < 1 )
182     return false;
183
184   // Make really sure the direction is normalized, is really cheap compared to
185   // computation of ground intersection.
186   SGVec3d start = pos;
187   SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
188   
189   osgUtil::IntersectVisitor intersectVisitor;
190   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
191   osg::ref_ptr<osg::LineSegment> lineSegment;
192   lineSegment = new osg::LineSegment(start.osg(), end.osg());
193   intersectVisitor.addLineSegment(lineSegment.get());
194   get_scene_graph()->accept(intersectVisitor);
195   bool hits = intersectVisitor.hits();
196   if (hits) {
197     int nHits = intersectVisitor.getNumHits(lineSegment.get());
198     double dist = SGLimitsd::max();
199     for (int i = 0; i < nHits; ++i) {
200       const osgUtil::Hit& hit
201         = intersectVisitor.getHitList(lineSegment.get())[i];
202       SGVec3d point;
203       point.osg() = hit.getWorldIntersectPoint();
204       double newdist = length(start - point);
205       if (newdist < dist) {
206         dist = newdist;
207         nearestHit = point;
208       }
209     }
210   }
211
212   return hits;
213 }
214
215 bool FGScenery::scenery_available(double lat, double lon, double range_m)
216 {
217   if(globals->get_tile_mgr()->scenery_available(lat, lon, range_m))
218   {
219     double elev;
220     get_elevation_m(lat, lon, SG_MAX_ELEVATION_M, elev, 0);
221     SGVec3f p = SGVec3f::fromGeod(SGGeod::fromDegM(lon,lat,elev));
222     simgear::CheckSceneryVisitor csnv(getPagerSingleton(), p.osg(), range_m);
223     // currently the PagedLODs will not be loaded by the DatabasePager
224     // while the splashscreen is there, so CheckSceneryVisitor force-loads
225     // missing objects in the main thread
226     get_scene_graph()->accept(csnv);
227     if(!csnv.isLoaded())
228         return false;
229     return true;
230   }
231   return false;
232 }
233
234 SceneryPager* FGScenery::getPagerSingleton()
235 {
236     static osg::ref_ptr<SceneryPager> pager = new SceneryPager;
237     return pager.get();
238 }