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, const osgGA::GUIEventAdapter*, const Info& info)
69 // only on left mouse button
73 SGGeod geod = SGGeod::fromCart(info.wgs84);
74 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
76 SGPropertyNode *c = fgGetNode("/sim/input/click", true);
77 c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
78 c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
79 c->setDoubleValue("elevation-m", geod.getElevationM());
80 c->setDoubleValue("elevation-ft", geod.getElevationFt());
81 fgSetBool("/sim/signals/click", 1);
87 class FGSceneryIntersect : public osg::NodeVisitor {
89 FGSceneryIntersect(const SGLineSegmentd& lineSegment,
90 const osg::Node* skipNode) :
91 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
92 _lineSegment(lineSegment),
98 bool getHaveHit() const
100 const SGLineSegmentd& getLineSegment() const
101 { return _lineSegment; }
102 const simgear::BVHMaterial* getMaterial() const
103 { return _material; }
105 virtual void apply(osg::Node& node)
107 if (&node == _skipNode)
109 if (!testBoundingSphere(node.getBound()))
112 addBoundingVolume(node);
115 virtual void apply(osg::Group& group)
117 if (&group == _skipNode)
119 if (!testBoundingSphere(group.getBound()))
123 addBoundingVolume(group);
126 virtual void apply(osg::Transform& transform)
127 { handleTransform(transform); }
128 virtual void apply(osg::Camera& camera)
130 if (camera.getRenderOrder() != osg::Camera::NESTED_RENDER)
132 handleTransform(camera);
134 virtual void apply(osg::CameraView& transform)
135 { handleTransform(transform); }
136 virtual void apply(osg::MatrixTransform& transform)
137 { handleTransform(transform); }
138 virtual void apply(osg::PositionAttitudeTransform& transform)
139 { handleTransform(transform); }
142 void handleTransform(osg::Transform& transform)
144 if (&transform == _skipNode)
146 // Hmm, may be this needs to be refined somehow ...
147 if (transform.getReferenceFrame() != osg::Transform::RELATIVE_RF)
150 if (!testBoundingSphere(transform.getBound()))
153 osg::Matrix inverseMatrix;
154 if (!transform.computeWorldToLocalMatrix(inverseMatrix, this))
157 if (!transform.computeLocalToWorldMatrix(matrix, this))
160 SGLineSegmentd lineSegment = _lineSegment;
161 bool haveHit = _haveHit;
162 const simgear::BVHMaterial* material = _material;
165 _lineSegment = lineSegment.transform(SGMatrixd(inverseMatrix.ptr()));
167 addBoundingVolume(transform);
171 _lineSegment = _lineSegment.transform(SGMatrixd(matrix.ptr()));
173 _lineSegment = lineSegment;
174 _material = material;
179 simgear::BVHNode* getNodeBoundingVolume(osg::Node& node)
181 SGSceneUserData* userData = SGSceneUserData::getSceneUserData(&node);
184 return userData->getBVHNode();
186 void addBoundingVolume(osg::Node& node)
188 simgear::BVHNode* bvNode = getNodeBoundingVolume(node);
192 // Find ground intersection on the bvh nodes
193 simgear::BVHLineSegmentVisitor lineSegmentVisitor(_lineSegment,
195 bvNode->accept(lineSegmentVisitor);
196 if (!lineSegmentVisitor.empty()) {
197 _lineSegment = lineSegmentVisitor.getLineSegment();
198 _material = lineSegmentVisitor.getMaterial();
203 bool testBoundingSphere(const osg::BoundingSphere& bound) const
208 SGSphered sphere(toVec3d(toSG(bound._center)), bound._radius);
209 return intersects(_lineSegment, sphere);
212 SGLineSegmentd _lineSegment;
213 const osg::Node* _skipNode;
215 const simgear::BVHMaterial* _material;
219 // Scenery Management system
220 FGScenery::FGScenery()
222 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
223 // keep reference to pager singleton, so it cannot be destroyed while FGScenery lives
224 _pager = FGScenery::getPagerSingleton();
227 FGScenery::~FGScenery() {
231 // Initialize the Scenery Management system
232 void FGScenery::init() {
234 scene_graph = new osg::Group;
235 scene_graph->setName( "Scene" );
238 terrain_branch = new osg::Group;
239 terrain_branch->setName( "Terrain" );
240 scene_graph->addChild( terrain_branch.get() );
241 SGSceneUserData* userData;
242 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
243 userData->setPickCallback(new FGGroundPickCallback);
245 models_branch = new osg::Group;
246 models_branch->setName( "Models" );
247 scene_graph->addChild( models_branch.get() );
249 aircraft_branch = new osg::Group;
250 aircraft_branch->setName( "Aircraft" );
251 scene_graph->addChild( aircraft_branch.get() );
253 // Initials values needed by the draw-time object loader
254 sgUserDataInit( globals->get_props() );
258 void FGScenery::update(double dt)
261 // nothing here, don't call again
266 void FGScenery::bind() {
270 void FGScenery::unbind() {
274 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
276 const simgear::BVHMaterial** material,
277 const osg::Node* butNotFrom)
279 SGGeod geod = SGGeod::fromCart(pos);
280 geod.setElevationM(geod.getElevationM() + max_altoff);
281 return get_elevation_m(geod, alt, material, butNotFrom);
285 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
286 const simgear::BVHMaterial** material,
287 const osg::Node* butNotFrom)
289 SGVec3d start = SGVec3d::fromGeod(geod);
291 SGGeod geodEnd = geod;
292 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
293 SGVec3d end = SGVec3d::fromGeod(geodEnd);
295 FGSceneryIntersect intersectVisitor(SGLineSegmentd(start, end), butNotFrom);
296 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
297 get_scene_graph()->accept(intersectVisitor);
299 if (!intersectVisitor.getHaveHit())
302 geodEnd = SGGeod::fromCart(intersectVisitor.getLineSegment().getEnd());
303 alt = geodEnd.getElevationM();
305 *material = intersectVisitor.getMaterial();
311 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
313 const osg::Node* butNotFrom)
315 // We assume that starting positions in the center of the earth are invalid
316 if ( norm1(pos) < 1 )
319 // Make really sure the direction is normalized, is really cheap compared to
320 // computation of ground intersection.
322 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
324 FGSceneryIntersect intersectVisitor(SGLineSegmentd(start, end), butNotFrom);
325 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
326 get_scene_graph()->accept(intersectVisitor);
328 if (!intersectVisitor.getHaveHit())
331 nearestHit = intersectVisitor.getLineSegment().getEnd();
335 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
337 if(globals->get_tile_mgr()->schedule_scenery(position, range_m, 0.0))
340 if (!get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0, 0))
342 SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
343 osg::FrameStamp* framestamp
344 = globals->get_renderer()->getViewer()->getFrameStamp();
345 simgear::CheckSceneryVisitor csnv(_pager, toOsg(p), range_m, framestamp);
346 // currently the PagedLODs will not be loaded by the DatabasePager
347 // while the splashscreen is there, so CheckSceneryVisitor force-loads
348 // missing objects in the main thread
349 get_scene_graph()->accept(csnv);
357 SceneryPager* FGScenery::getPagerSingleton()
359 static osg::ref_ptr<SceneryPager> pager = new SceneryPager;