1 // scenery.cxx -- data structures and routines for managing scenery.
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
32 #include <osg/Transform>
33 #include <osg/MatrixTransform>
34 #include <osg/PositionAttitudeTransform>
35 #include <osg/CameraView>
36 #include <osgViewer/Viewer>
38 #include <simgear/constants.h>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/scene/tgdb/userdata.hxx>
41 #include <simgear/scene/material/matlib.hxx>
42 #include <simgear/scene/util/SGNodeMasks.hxx>
43 #include <simgear/scene/util/OsgMath.hxx>
44 #include <simgear/scene/util/SGSceneUserData.hxx>
45 #include <simgear/scene/model/CheckSceneryVisitor.hxx>
46 #include <simgear/scene/bvh/BVHNode.hxx>
47 #include <simgear/scene/bvh/BVHLineSegmentVisitor.hxx>
49 #include <Viewer/renderer.hxx>
50 #include <Main/fg_props.hxx>
52 #include "tilemgr.hxx"
53 #include "scenery.hxx"
55 using namespace flightgear;
56 using namespace simgear;
58 class FGGroundPickCallback : public SGPickCallback {
60 virtual bool buttonPressed(int button, const Info& info)
62 // only on left mouse button
66 SGGeod geod = SGGeod::fromCart(info.wgs84);
67 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
69 SGPropertyNode *c = fgGetNode("/sim/input/click", true);
70 c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
71 c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
72 c->setDoubleValue("elevation-m", geod.getElevationM());
73 c->setDoubleValue("elevation-ft", geod.getElevationFt());
74 fgSetBool("/sim/signals/click", 1);
80 class FGSceneryIntersect : public osg::NodeVisitor {
82 FGSceneryIntersect(const SGLineSegmentd& lineSegment,
83 const osg::Node* skipNode) :
84 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
85 _lineSegment(lineSegment),
91 bool getHaveHit() const
93 const SGLineSegmentd& getLineSegment() const
94 { return _lineSegment; }
95 const SGMaterial* getMaterial() const
98 virtual void apply(osg::Node& node)
100 if (&node == _skipNode)
102 if (!testBoundingSphere(node.getBound()))
105 addBoundingVolume(node);
108 virtual void apply(osg::Group& group)
110 if (&group == _skipNode)
112 if (!testBoundingSphere(group.getBound()))
116 addBoundingVolume(group);
119 virtual void apply(osg::Transform& transform)
120 { handleTransform(transform); }
121 virtual void apply(osg::Camera& camera)
123 if (camera.getRenderOrder() != osg::Camera::NESTED_RENDER)
125 handleTransform(camera);
127 virtual void apply(osg::CameraView& transform)
128 { handleTransform(transform); }
129 virtual void apply(osg::MatrixTransform& transform)
130 { handleTransform(transform); }
131 virtual void apply(osg::PositionAttitudeTransform& transform)
132 { handleTransform(transform); }
135 void handleTransform(osg::Transform& transform)
137 if (&transform == _skipNode)
139 // Hmm, may be this needs to be refined somehow ...
140 if (transform.getReferenceFrame() != osg::Transform::RELATIVE_RF)
143 if (!testBoundingSphere(transform.getBound()))
146 osg::Matrix inverseMatrix;
147 if (!transform.computeWorldToLocalMatrix(inverseMatrix, this))
150 if (!transform.computeLocalToWorldMatrix(matrix, this))
153 SGLineSegmentd lineSegment = _lineSegment;
154 bool haveHit = _haveHit;
155 const SGMaterial* material = _material;
158 _lineSegment = lineSegment.transform(SGMatrixd(inverseMatrix.ptr()));
160 addBoundingVolume(transform);
164 _lineSegment = _lineSegment.transform(SGMatrixd(matrix.ptr()));
166 _lineSegment = lineSegment;
167 _material = material;
172 simgear::BVHNode* getNodeBoundingVolume(osg::Node& node)
174 SGSceneUserData* userData = SGSceneUserData::getSceneUserData(&node);
177 return userData->getBVHNode();
179 void addBoundingVolume(osg::Node& node)
181 simgear::BVHNode* bvNode = getNodeBoundingVolume(node);
185 // Find ground intersection on the bvh nodes
186 simgear::BVHLineSegmentVisitor lineSegmentVisitor(_lineSegment,
188 bvNode->accept(lineSegmentVisitor);
189 if (!lineSegmentVisitor.empty()) {
190 _lineSegment = lineSegmentVisitor.getLineSegment();
191 _material = lineSegmentVisitor.getMaterial();
196 bool testBoundingSphere(const osg::BoundingSphere& bound) const
201 SGSphered sphere(toVec3d(toSG(bound._center)), bound._radius);
202 return intersects(_lineSegment, sphere);
205 SGLineSegmentd _lineSegment;
206 const osg::Node* _skipNode;
208 const SGMaterial* _material;
212 // Scenery Management system
213 FGScenery::FGScenery()
215 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
216 // keep reference to pager singleton, so it cannot be destroyed while FGScenery lives
217 _pager = FGScenery::getPagerSingleton();
220 FGScenery::~FGScenery() {
224 // Initialize the Scenery Management system
225 void FGScenery::init() {
227 scene_graph = new osg::Group;
228 scene_graph->setName( "Scene" );
231 terrain_branch = new osg::Group;
232 terrain_branch->setName( "Terrain" );
233 scene_graph->addChild( terrain_branch.get() );
234 SGSceneUserData* userData;
235 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
236 userData->setPickCallback(new FGGroundPickCallback);
238 models_branch = new osg::Group;
239 models_branch->setName( "Models" );
240 scene_graph->addChild( models_branch.get() );
242 aircraft_branch = new osg::Group;
243 aircraft_branch->setName( "Aircraft" );
244 scene_graph->addChild( aircraft_branch.get() );
246 // Initials values needed by the draw-time object loader
247 sgUserDataInit( globals->get_props() );
251 void FGScenery::update(double dt) {
255 void FGScenery::bind() {
259 void FGScenery::unbind() {
263 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
264 double& alt, const SGMaterial** material,
265 const osg::Node* butNotFrom)
267 SGGeod geod = SGGeod::fromCart(pos);
268 geod.setElevationM(geod.getElevationM() + max_altoff);
269 return get_elevation_m(geod, alt, material, butNotFrom);
273 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
274 const SGMaterial** material,
275 const osg::Node* butNotFrom)
277 SGVec3d start = SGVec3d::fromGeod(geod);
279 SGGeod geodEnd = geod;
280 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
281 SGVec3d end = SGVec3d::fromGeod(geodEnd);
283 FGSceneryIntersect intersectVisitor(SGLineSegmentd(start, end), butNotFrom);
284 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
285 get_scene_graph()->accept(intersectVisitor);
287 if (!intersectVisitor.getHaveHit())
290 geodEnd = SGGeod::fromCart(intersectVisitor.getLineSegment().getEnd());
291 alt = geodEnd.getElevationM();
293 *material = intersectVisitor.getMaterial();
299 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
301 const osg::Node* butNotFrom)
303 // We assume that starting positions in the center of the earth are invalid
304 if ( norm1(pos) < 1 )
307 // Make really sure the direction is normalized, is really cheap compared to
308 // computation of ground intersection.
310 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
312 FGSceneryIntersect intersectVisitor(SGLineSegmentd(start, end), butNotFrom);
313 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
314 get_scene_graph()->accept(intersectVisitor);
316 if (!intersectVisitor.getHaveHit())
319 nearestHit = intersectVisitor.getLineSegment().getEnd();
323 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
325 if(globals->get_tile_mgr()->schedule_scenery(position, range_m, 0.0))
328 if (!get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0, 0))
330 SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
331 osg::FrameStamp* framestamp
332 = globals->get_renderer()->getViewer()->getFrameStamp();
333 simgear::CheckSceneryVisitor csnv(_pager, toOsg(p), range_m, framestamp);
334 // currently the PagedLODs will not be loaded by the DatabasePager
335 // while the splashscreen is there, so CheckSceneryVisitor force-loads
336 // missing objects in the main thread
337 get_scene_graph()->accept(csnv);
345 SceneryPager* FGScenery::getPagerSingleton()
347 static osg::ref_ptr<SceneryPager> pager = new SceneryPager;