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/SGSceneUserData.hxx>
44 #include <simgear/scene/model/CheckSceneryVisitor.hxx>
45 #include <simgear/scene/bvh/BVHNode.hxx>
46 #include <simgear/scene/bvh/BVHLineSegmentVisitor.hxx>
48 #include <Viewer/renderer.hxx>
49 #include <Main/fg_props.hxx>
51 #include "tilemgr.hxx"
52 #include "scenery.hxx"
54 using namespace flightgear;
55 using namespace simgear;
57 class FGGroundPickCallback : public SGPickCallback {
59 virtual bool buttonPressed(int button, const Info& info)
61 // only on left mouse button
65 SGGeod geod = SGGeod::fromCart(info.wgs84);
66 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
68 SGPropertyNode *c = fgGetNode("/sim/input/click", true);
69 c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
70 c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
71 c->setDoubleValue("elevation-m", geod.getElevationM());
72 c->setDoubleValue("elevation-ft", geod.getElevationFt());
73 fgSetBool("/sim/signals/click", 1);
79 class FGSceneryIntersect : public osg::NodeVisitor {
81 FGSceneryIntersect(const SGLineSegmentd& lineSegment,
82 const osg::Node* skipNode) :
83 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
84 _lineSegment(lineSegment),
90 bool getHaveHit() const
92 const SGLineSegmentd& getLineSegment() const
93 { return _lineSegment; }
94 const SGMaterial* getMaterial() const
97 virtual void apply(osg::Node& node)
99 if (&node == _skipNode)
101 if (!testBoundingSphere(node.getBound()))
104 addBoundingVolume(node);
107 virtual void apply(osg::Group& group)
109 if (&group == _skipNode)
111 if (!testBoundingSphere(group.getBound()))
115 addBoundingVolume(group);
118 virtual void apply(osg::Transform& transform)
119 { handleTransform(transform); }
120 virtual void apply(osg::Camera& camera)
122 if (camera.getRenderOrder() != osg::Camera::NESTED_RENDER)
124 handleTransform(camera);
126 virtual void apply(osg::CameraView& transform)
127 { handleTransform(transform); }
128 virtual void apply(osg::MatrixTransform& transform)
129 { handleTransform(transform); }
130 virtual void apply(osg::PositionAttitudeTransform& transform)
131 { handleTransform(transform); }
134 void handleTransform(osg::Transform& transform)
136 if (&transform == _skipNode)
138 // Hmm, may be this needs to be refined somehow ...
139 if (transform.getReferenceFrame() != osg::Transform::RELATIVE_RF)
142 if (!testBoundingSphere(transform.getBound()))
145 osg::Matrix inverseMatrix;
146 if (!transform.computeWorldToLocalMatrix(inverseMatrix, this))
149 if (!transform.computeLocalToWorldMatrix(matrix, this))
152 SGLineSegmentd lineSegment = _lineSegment;
153 bool haveHit = _haveHit;
154 const SGMaterial* material = _material;
157 _lineSegment = lineSegment.transform(SGMatrixd(inverseMatrix.ptr()));
159 addBoundingVolume(transform);
163 _lineSegment = _lineSegment.transform(SGMatrixd(matrix.ptr()));
165 _lineSegment = lineSegment;
166 _material = material;
171 simgear::BVHNode* getNodeBoundingVolume(osg::Node& node)
173 SGSceneUserData* userData = SGSceneUserData::getSceneUserData(&node);
176 return userData->getBVHNode();
178 void addBoundingVolume(osg::Node& node)
180 simgear::BVHNode* bvNode = getNodeBoundingVolume(node);
184 // Find ground intersection on the bvh nodes
185 simgear::BVHLineSegmentVisitor lineSegmentVisitor(_lineSegment,
187 bvNode->accept(lineSegmentVisitor);
188 if (!lineSegmentVisitor.empty()) {
189 _lineSegment = lineSegmentVisitor.getLineSegment();
190 _material = lineSegmentVisitor.getMaterial();
195 bool testBoundingSphere(const osg::BoundingSphere& bound) const
200 SGSphered sphere(toVec3d(toSG(bound._center)), bound._radius);
201 return intersects(_lineSegment, sphere);
204 SGLineSegmentd _lineSegment;
205 const osg::Node* _skipNode;
207 const SGMaterial* _material;
211 // Scenery Management system
212 FGScenery::FGScenery()
214 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
215 // keep reference to pager singleton, so it cannot be destroyed while FGScenery lives
216 _pager = FGScenery::getPagerSingleton();
219 FGScenery::~FGScenery() {
223 // Initialize the Scenery Management system
224 void FGScenery::init() {
226 scene_graph = new osg::Group;
227 scene_graph->setName( "Scene" );
230 terrain_branch = new osg::Group;
231 terrain_branch->setName( "Terrain" );
232 scene_graph->addChild( terrain_branch.get() );
233 SGSceneUserData* userData;
234 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
235 userData->setPickCallback(new FGGroundPickCallback);
237 models_branch = new osg::Group;
238 models_branch->setName( "Models" );
239 scene_graph->addChild( models_branch.get() );
241 aircraft_branch = new osg::Group;
242 aircraft_branch->setName( "Aircraft" );
243 scene_graph->addChild( aircraft_branch.get() );
245 // Initials values needed by the draw-time object loader
246 sgUserDataInit( globals->get_props() );
250 void FGScenery::update(double dt) {
254 void FGScenery::bind() {
258 void FGScenery::unbind() {
262 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
263 double& alt, const SGMaterial** material,
264 const osg::Node* butNotFrom)
266 SGGeod geod = SGGeod::fromCart(pos);
267 geod.setElevationM(geod.getElevationM() + max_altoff);
268 return get_elevation_m(geod, alt, material, butNotFrom);
272 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
273 const SGMaterial** material,
274 const osg::Node* butNotFrom)
276 SGVec3d start = SGVec3d::fromGeod(geod);
278 SGGeod geodEnd = geod;
279 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
280 SGVec3d end = SGVec3d::fromGeod(geodEnd);
282 FGSceneryIntersect intersectVisitor(SGLineSegmentd(start, end), butNotFrom);
283 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
284 get_scene_graph()->accept(intersectVisitor);
286 if (!intersectVisitor.getHaveHit())
289 geodEnd = SGGeod::fromCart(intersectVisitor.getLineSegment().getEnd());
290 alt = geodEnd.getElevationM();
292 *material = intersectVisitor.getMaterial();
298 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
300 const osg::Node* butNotFrom)
302 // We assume that starting positions in the center of the earth are invalid
303 if ( norm1(pos) < 1 )
306 // Make really sure the direction is normalized, is really cheap compared to
307 // computation of ground intersection.
309 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
311 FGSceneryIntersect intersectVisitor(SGLineSegmentd(start, end), butNotFrom);
312 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
313 get_scene_graph()->accept(intersectVisitor);
315 if (!intersectVisitor.getHaveHit())
318 nearestHit = intersectVisitor.getLineSegment().getEnd();
322 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
324 if(globals->get_tile_mgr()->schedule_scenery(position, range_m, 0.0))
327 if (!get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0, 0))
329 SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
330 osg::FrameStamp* framestamp
331 = globals->get_renderer()->getViewer()->getFrameStamp();
332 simgear::CheckSceneryVisitor csnv(_pager, toOsg(p), range_m, framestamp);
333 // currently the PagedLODs will not be loaded by the DatabasePager
334 // while the splashscreen is there, so CheckSceneryVisitor force-loads
335 // missing objects in the main thread
336 get_scene_graph()->accept(csnv);
344 SceneryPager* FGScenery::getPagerSingleton()
346 static osg::ref_ptr<SceneryPager> pager = new SceneryPager;