]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
04319fbe8da64988b98dd6d26c01176ece804479
[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/scene/material/matlib.hxx>
37 #include <simgear/scene/util/SGNodeMasks.hxx>
38 #include <simgear/scene/util/SGSceneUserData.hxx>
39 #include <simgear/scene/model/CheckSceneryVisitor.hxx>
40
41 #include <Main/fg_props.hxx>
42
43 #include "tilemgr.hxx"
44 #include "scenery.hxx"
45
46 using namespace flightgear;
47
48 class FGGroundPickCallback : public SGPickCallback {
49 public:
50   virtual bool buttonPressed(int button, const Info& info)
51   {
52     // only on left mouse button
53     if (button != 0)
54       return false;
55
56     SGGeod geod = SGGeod::fromCart(info.wgs84);
57     SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
58
59     SGPropertyNode *c = fgGetNode("/sim/input/click", true);
60     c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
61     c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
62     c->setDoubleValue("elevation-m", geod.getElevationM());
63     c->setDoubleValue("elevation-ft", geod.getElevationFt());
64     fgSetBool("/sim/signals/click", 1);
65
66     return true;
67   }
68 };
69
70 // Scenery Management system
71 FGScenery::FGScenery()
72 {
73     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
74 }
75
76 FGScenery::~FGScenery() {
77 }
78
79
80 // Initialize the Scenery Management system
81 void FGScenery::init() {
82     // Scene graph root
83     scene_graph = new osg::Group;
84     scene_graph->setName( "Scene" );
85
86     // Terrain branch
87     terrain_branch = new osg::Group;
88     terrain_branch->setName( "Terrain" );
89     scene_graph->addChild( terrain_branch.get() );
90     SGSceneUserData* userData;
91     userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
92     userData->setPickCallback(new FGGroundPickCallback);
93
94     models_branch = new osg::Group;
95     models_branch->setName( "Models" );
96     scene_graph->addChild( models_branch.get() );
97
98     aircraft_branch = new osg::Group;
99     aircraft_branch->setName( "Aircraft" );
100     scene_graph->addChild( aircraft_branch.get() );
101
102     // Initials values needed by the draw-time object loader
103     sgUserDataInit( globals->get_props() );
104 }
105
106
107 void FGScenery::update(double dt) {
108 }
109
110
111 void FGScenery::bind() {
112 }
113
114
115 void FGScenery::unbind() {
116 }
117
118 bool
119 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
120                                 double& alt, const SGMaterial** material)
121 {
122   SGGeod geod = SGGeod::fromCart(pos);
123   geod.setElevationM(geod.getElevationM() + max_altoff);
124   return get_elevation_m(geod, alt, material);
125 }
126
127 bool
128 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
129                            const SGMaterial** material)
130 {
131   SGVec3d start = SGVec3d::fromGeod(geod);
132
133   SGGeod geodEnd = geod;
134   geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
135   SGVec3d end = SGVec3d::fromGeod(geodEnd);
136   
137   osgUtil::IntersectVisitor intersectVisitor;
138   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
139   osg::ref_ptr<osg::LineSegment> lineSegment;
140   lineSegment = new osg::LineSegment(start.osg(), end.osg());
141   intersectVisitor.addLineSegment(lineSegment.get());
142   get_scene_graph()->accept(intersectVisitor);
143   bool hits = intersectVisitor.hits();
144   if (hits) {
145     int nHits = intersectVisitor.getNumHits(lineSegment.get());
146     alt = -SGLimitsd::max();
147     for (int i = 0; i < nHits; ++i) {
148       const osgUtil::Hit& hit
149         = intersectVisitor.getHitList(lineSegment.get())[i];
150       SGVec3d point;
151       point.osg() = hit.getWorldIntersectPoint();
152       SGGeod geod = SGGeod::fromCart(point);
153       double elevation = geod.getElevationM();
154       if (alt < elevation) {
155         alt = elevation;
156         if (material) {
157           const osg::StateSet* stateSet = hit.getDrawable()->getStateSet();
158           *material = SGMaterialLib::findMaterial(stateSet);
159         }
160       }
161     }
162   }
163
164   return hits;
165 }
166
167 bool
168 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
169                                         SGVec3d& nearestHit)
170 {
171   // We assume that starting positions in the center of the earth are invalid
172   if ( norm1(pos) < 1 )
173     return false;
174
175   // Make really sure the direction is normalized, is really cheap compared to
176   // computation of ground intersection.
177   SGVec3d start = pos;
178   SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
179   
180   osgUtil::IntersectVisitor intersectVisitor;
181   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
182   osg::ref_ptr<osg::LineSegment> lineSegment;
183   lineSegment = new osg::LineSegment(start.osg(), end.osg());
184   intersectVisitor.addLineSegment(lineSegment.get());
185   get_scene_graph()->accept(intersectVisitor);
186   bool hits = intersectVisitor.hits();
187   if (hits) {
188     int nHits = intersectVisitor.getNumHits(lineSegment.get());
189     double dist = SGLimitsd::max();
190     for (int i = 0; i < nHits; ++i) {
191       const osgUtil::Hit& hit
192         = intersectVisitor.getHitList(lineSegment.get())[i];
193       SGVec3d point;
194       point.osg() = hit.getWorldIntersectPoint();
195       double newdist = length(start - point);
196       if (newdist < dist) {
197         dist = newdist;
198         nearestHit = point;
199       }
200     }
201   }
202
203   return hits;
204 }
205
206 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
207 {
208   if(globals->get_tile_mgr()->scenery_available(position, range_m))
209   {
210     double elev;
211     get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0);
212     SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
213     simgear::CheckSceneryVisitor csnv(getPagerSingleton(), p.osg(), range_m);
214     // currently the PagedLODs will not be loaded by the DatabasePager
215     // while the splashscreen is there, so CheckSceneryVisitor force-loads
216     // missing objects in the main thread
217     get_scene_graph()->accept(csnv);
218     if(!csnv.isLoaded())
219         return false;
220     return true;
221   }
222   return false;
223 }
224
225 SceneryPager* FGScenery::getPagerSingleton()
226 {
227     static osg::ref_ptr<SceneryPager> pager = new SceneryPager;
228     return pager.get();
229 }