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