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.
31 #include <osgUtil/IntersectVisitor>
33 #include <simgear/constants.h>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/scene/tgdb/userdata.hxx>
36 #include <simgear/math/sg_geodesy.hxx>
37 #include <simgear/scene/model/placementtrans.hxx>
38 #include <simgear/scene/material/matlib.hxx>
39 #include <simgear/scene/util/SGNodeMasks.hxx>
40 #include <simgear/scene/util/SGSceneUserData.hxx>
41 #include <simgear/scene/model/CheckSceneryVisitor.hxx>
43 #include <Main/fg_props.hxx>
45 #include "tilemgr.hxx"
46 #include "scenery.hxx"
48 using namespace flightgear;
50 class FGGroundPickCallback : public SGPickCallback {
52 virtual bool buttonPressed(int button, const Info& info)
54 // only on left mouse button
58 SGGeod geod = SGGeod::fromCart(info.wgs84);
59 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
61 SGPropertyNode *c = fgGetNode("/sim/input/click", true);
62 c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
63 c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
64 c->setDoubleValue("elevation-m", geod.getElevationM());
65 c->setDoubleValue("elevation-ft", geod.getElevationFt());
66 fgSetBool("/sim/signals/click", 1);
72 // Scenery Management system
73 FGScenery::FGScenery()
75 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
78 FGScenery::~FGScenery() {
82 // Initialize the Scenery Management system
83 void FGScenery::init() {
85 scene_graph = new osg::Group;
86 scene_graph->setName( "Scene" );
89 terrain_branch = new osg::Group;
90 terrain_branch->setName( "Terrain" );
91 scene_graph->addChild( terrain_branch.get() );
92 SGSceneUserData* userData;
93 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
94 userData->setPickCallback(new FGGroundPickCallback);
96 models_branch = new osg::Group;
97 models_branch->setName( "Models" );
98 scene_graph->addChild( models_branch.get() );
100 aircraft_branch = new osg::Group;
101 aircraft_branch->setName( "Aircraft" );
102 scene_graph->addChild( aircraft_branch.get() );
104 // Initials values needed by the draw-time object loader
105 sgUserDataInit( globals->get_props() );
109 void FGScenery::update(double dt) {
113 void FGScenery::bind() {
117 void FGScenery::unbind() {
121 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
122 double& alt, const SGMaterial** material)
124 SGGeod geod = SGGeod::fromCart(pos);
125 geod.setElevationM(geod.getElevationM() + max_altoff);
126 return get_elevation_m(geod, alt, material);
130 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
131 const SGMaterial** material)
133 SGVec3d start = SGVec3d::fromGeod(geod);
135 SGGeod geodEnd = geod;
136 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
137 SGVec3d end = SGVec3d::fromGeod(geodEnd);
139 osgUtil::IntersectVisitor intersectVisitor;
140 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
141 osg::ref_ptr<osg::LineSegment> lineSegment;
142 lineSegment = new osg::LineSegment(start.osg(), end.osg());
143 intersectVisitor.addLineSegment(lineSegment.get());
144 get_scene_graph()->accept(intersectVisitor);
145 bool hits = intersectVisitor.hits();
147 int nHits = intersectVisitor.getNumHits(lineSegment.get());
148 alt = -SGLimitsd::max();
149 for (int i = 0; i < nHits; ++i) {
150 const osgUtil::Hit& hit
151 = intersectVisitor.getHitList(lineSegment.get())[i];
153 point.osg() = hit.getWorldIntersectPoint();
154 SGGeod geod = SGGeod::fromCart(point);
155 double elevation = geod.getElevationM();
156 if (alt < elevation) {
159 const osg::StateSet* stateSet = hit.getDrawable()->getStateSet();
160 *material = SGMaterialLib::findMaterial(stateSet);
170 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
173 // We assume that starting positions in the center of the earth are invalid
174 if ( norm1(pos) < 1 )
177 // Make really sure the direction is normalized, is really cheap compared to
178 // computation of ground intersection.
180 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
182 osgUtil::IntersectVisitor intersectVisitor;
183 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
184 osg::ref_ptr<osg::LineSegment> lineSegment;
185 lineSegment = new osg::LineSegment(start.osg(), end.osg());
186 intersectVisitor.addLineSegment(lineSegment.get());
187 get_scene_graph()->accept(intersectVisitor);
188 bool hits = intersectVisitor.hits();
190 int nHits = intersectVisitor.getNumHits(lineSegment.get());
191 double dist = SGLimitsd::max();
192 for (int i = 0; i < nHits; ++i) {
193 const osgUtil::Hit& hit
194 = intersectVisitor.getHitList(lineSegment.get())[i];
196 point.osg() = hit.getWorldIntersectPoint();
197 double newdist = length(start - point);
198 if (newdist < dist) {
208 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
210 if(globals->get_tile_mgr()->scenery_available(position, range_m))
213 get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0);
214 SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
215 simgear::CheckSceneryVisitor csnv(getPagerSingleton(), p.osg(), range_m);
216 // currently the PagedLODs will not be loaded by the DatabasePager
217 // while the splashscreen is there, so CheckSceneryVisitor force-loads
218 // missing objects in the main thread
219 get_scene_graph()->accept(csnv);
227 SceneryPager* FGScenery::getPagerSingleton()
229 static osg::ref_ptr<SceneryPager> pager = new SceneryPager;