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