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/sg_inlines.h>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/scene/tgdb/userdata.hxx>
42 #include <simgear/scene/material/matlib.hxx>
43 #include <simgear/scene/material/mat.hxx>
44 #include <simgear/scene/util/SGNodeMasks.hxx>
45 #include <simgear/scene/util/OsgMath.hxx>
46 #include <simgear/scene/util/SGSceneUserData.hxx>
47 #include <simgear/scene/model/CheckSceneryVisitor.hxx>
48 #include <simgear/bvh/BVHNode.hxx>
49 #include <simgear/bvh/BVHLineSegmentVisitor.hxx>
50 #include <simgear/structure/commands.hxx>
52 #include <Viewer/renderer.hxx>
53 #include <Main/fg_props.hxx>
54 #include <GUI/MouseCursor.hxx>
56 #include "tilemgr.hxx"
57 #include "scenery.hxx"
59 using namespace flightgear;
60 using namespace simgear;
62 class FGGroundPickCallback : public SGPickCallback {
64 FGGroundPickCallback() : SGPickCallback(PriorityScenery)
67 virtual bool buttonPressed( int button,
68 const osgGA::GUIEventAdapter&,
71 // only on left mouse button
75 SGGeod geod = SGGeod::fromCart(info.wgs84);
76 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
78 SGPropertyNode *c = fgGetNode("/sim/input/click", true);
79 c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
80 c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
81 c->setDoubleValue("elevation-m", geod.getElevationM());
82 c->setDoubleValue("elevation-ft", geod.getElevationFt());
83 fgSetBool("/sim/signals/click", 1);
89 class FGSceneryIntersect : public osg::NodeVisitor {
91 FGSceneryIntersect(const SGLineSegmentd& lineSegment,
92 const osg::Node* skipNode) :
93 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
94 _lineSegment(lineSegment),
100 bool getHaveHit() const
102 const SGLineSegmentd& getLineSegment() const
103 { return _lineSegment; }
104 const simgear::BVHMaterial* getMaterial() const
105 { return _material; }
107 virtual void apply(osg::Node& node)
109 if (&node == _skipNode)
111 if (!testBoundingSphere(node.getBound()))
114 addBoundingVolume(node);
117 virtual void apply(osg::Group& group)
119 if (&group == _skipNode)
121 if (!testBoundingSphere(group.getBound()))
125 addBoundingVolume(group);
128 virtual void apply(osg::Transform& transform)
129 { handleTransform(transform); }
130 virtual void apply(osg::Camera& camera)
132 if (camera.getRenderOrder() != osg::Camera::NESTED_RENDER)
134 handleTransform(camera);
136 virtual void apply(osg::CameraView& transform)
137 { handleTransform(transform); }
138 virtual void apply(osg::MatrixTransform& transform)
139 { handleTransform(transform); }
140 virtual void apply(osg::PositionAttitudeTransform& transform)
141 { handleTransform(transform); }
144 void handleTransform(osg::Transform& transform)
146 if (&transform == _skipNode)
148 // Hmm, may be this needs to be refined somehow ...
149 if (transform.getReferenceFrame() != osg::Transform::RELATIVE_RF)
152 if (!testBoundingSphere(transform.getBound()))
155 osg::Matrix inverseMatrix;
156 if (!transform.computeWorldToLocalMatrix(inverseMatrix, this))
159 if (!transform.computeLocalToWorldMatrix(matrix, this))
162 SGLineSegmentd lineSegment = _lineSegment;
163 bool haveHit = _haveHit;
164 const simgear::BVHMaterial* material = _material;
167 _lineSegment = lineSegment.transform(SGMatrixd(inverseMatrix.ptr()));
169 addBoundingVolume(transform);
173 _lineSegment = _lineSegment.transform(SGMatrixd(matrix.ptr()));
175 _lineSegment = lineSegment;
176 _material = material;
181 simgear::BVHNode* getNodeBoundingVolume(osg::Node& node)
183 SGSceneUserData* userData = SGSceneUserData::getSceneUserData(&node);
186 return userData->getBVHNode();
188 void addBoundingVolume(osg::Node& node)
190 simgear::BVHNode* bvNode = getNodeBoundingVolume(node);
194 // Find ground intersection on the bvh nodes
195 simgear::BVHLineSegmentVisitor lineSegmentVisitor(_lineSegment,
197 bvNode->accept(lineSegmentVisitor);
198 if (!lineSegmentVisitor.empty()) {
199 _lineSegment = lineSegmentVisitor.getLineSegment();
200 _material = lineSegmentVisitor.getMaterial();
205 bool testBoundingSphere(const osg::BoundingSphere& bound) const
210 SGSphered sphere(toVec3d(toSG(bound._center)), bound._radius);
211 return intersects(_lineSegment, sphere);
214 SGLineSegmentd _lineSegment;
215 const osg::Node* _skipNode;
217 const simgear::BVHMaterial* _material;
221 // Scenery Management system
222 FGScenery::FGScenery()
224 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
225 // keep reference to pager singleton, so it cannot be destroyed while FGScenery lives
226 _pager = FGScenery::getPagerSingleton();
229 FGScenery::~FGScenery() {
233 // Initialize the Scenery Management system
234 void FGScenery::init() {
236 scene_graph = new osg::Group;
237 scene_graph->setName( "FGScenery" );
240 terrain_branch = new osg::Group;
241 terrain_branch->setName( "Terrain" );
242 scene_graph->addChild( terrain_branch.get() );
243 SGSceneUserData* userData;
244 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
245 userData->setPickCallback(new FGGroundPickCallback);
247 models_branch = new osg::Group;
248 models_branch->setName( "Models" );
249 scene_graph->addChild( models_branch.get() );
251 aircraft_branch = new osg::Group;
252 aircraft_branch->setName( "Aircraft" );
253 scene_graph->addChild( aircraft_branch.get() );
255 // Initials values needed by the draw-time object loader
256 sgUserDataInit( globals->get_props() );
260 void FGScenery::update(double dt)
263 // nothing here, don't call again
268 void FGScenery::bind() {
272 void FGScenery::unbind() {
276 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
278 const simgear::BVHMaterial** material,
279 const osg::Node* butNotFrom)
281 SGGeod geod = SGGeod::fromCart(pos);
282 geod.setElevationM(geod.getElevationM() + max_altoff);
283 return get_elevation_m(geod, alt, material, butNotFrom);
287 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
288 const simgear::BVHMaterial** material,
289 const osg::Node* butNotFrom)
291 SGVec3d start = SGVec3d::fromGeod(geod);
293 SGGeod geodEnd = geod;
294 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
295 SGVec3d end = SGVec3d::fromGeod(geodEnd);
297 FGSceneryIntersect intersectVisitor(SGLineSegmentd(start, end), butNotFrom);
298 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
299 get_scene_graph()->accept(intersectVisitor);
301 if (!intersectVisitor.getHaveHit())
304 geodEnd = SGGeod::fromCart(intersectVisitor.getLineSegment().getEnd());
305 alt = geodEnd.getElevationM();
307 *material = intersectVisitor.getMaterial();
313 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
315 const osg::Node* butNotFrom)
317 // We assume that starting positions in the center of the earth are invalid
318 if ( norm1(pos) < 1 )
321 // Make really sure the direction is normalized, is really cheap compared to
322 // computation of ground intersection.
324 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
326 FGSceneryIntersect intersectVisitor(SGLineSegmentd(start, end), butNotFrom);
327 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
328 get_scene_graph()->accept(intersectVisitor);
330 if (!intersectVisitor.getHaveHit())
333 nearestHit = intersectVisitor.getLineSegment().getEnd();
337 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
339 if(globals->get_tile_mgr()->schedule_scenery(position, range_m, 0.0))
342 if (!get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0, 0))
344 SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
345 osg::FrameStamp* framestamp
346 = globals->get_renderer()->getViewer()->getFrameStamp();
347 simgear::CheckSceneryVisitor csnv(_pager, toOsg(p), range_m, framestamp);
348 // currently the PagedLODs will not be loaded by the DatabasePager
349 // while the splashscreen is there, so CheckSceneryVisitor force-loads
350 // missing objects in the main thread
351 get_scene_graph()->accept(csnv);
359 SceneryPager* FGScenery::getPagerSingleton()
361 static osg::ref_ptr<SceneryPager> pager = new SceneryPager;