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_elevation_m(double lat, double lon, double max_alt,
122 double& alt, const SGMaterial** material)
124 return get_elevation_m(SGGeod::fromDegM(lon, lat, max_alt), alt, material);
128 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
129 double& alt, const SGMaterial** material)
131 SGGeod geod = SGGeod::fromCart(pos);
132 geod.setElevationM(geod.getElevationM() + max_altoff);
133 return get_elevation_m(geod, alt, material);
137 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
138 const SGMaterial** material)
140 SGVec3d start = SGVec3d::fromGeod(geod);
142 SGGeod geodEnd = geod;
143 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
144 SGVec3d end = SGVec3d::fromGeod(geodEnd);
146 osgUtil::IntersectVisitor intersectVisitor;
147 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
148 osg::ref_ptr<osg::LineSegment> lineSegment;
149 lineSegment = new osg::LineSegment(start.osg(), end.osg());
150 intersectVisitor.addLineSegment(lineSegment.get());
151 get_scene_graph()->accept(intersectVisitor);
152 bool hits = intersectVisitor.hits();
154 int nHits = intersectVisitor.getNumHits(lineSegment.get());
155 alt = -SGLimitsd::max();
156 for (int i = 0; i < nHits; ++i) {
157 const osgUtil::Hit& hit
158 = intersectVisitor.getHitList(lineSegment.get())[i];
160 point.osg() = hit.getWorldIntersectPoint();
161 SGGeod geod = SGGeod::fromCart(point);
162 double elevation = geod.getElevationM();
163 if (alt < elevation) {
166 const osg::StateSet* stateSet = hit.getDrawable()->getStateSet();
167 *material = globals->get_matlib()->findMaterial(stateSet);
177 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
180 // We assume that starting positions in the center of the earth are invalid
181 if ( norm1(pos) < 1 )
184 // Make really sure the direction is normalized, is really cheap compared to
185 // computation of ground intersection.
187 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
189 osgUtil::IntersectVisitor intersectVisitor;
190 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
191 osg::ref_ptr<osg::LineSegment> lineSegment;
192 lineSegment = new osg::LineSegment(start.osg(), end.osg());
193 intersectVisitor.addLineSegment(lineSegment.get());
194 get_scene_graph()->accept(intersectVisitor);
195 bool hits = intersectVisitor.hits();
197 int nHits = intersectVisitor.getNumHits(lineSegment.get());
198 double dist = SGLimitsd::max();
199 for (int i = 0; i < nHits; ++i) {
200 const osgUtil::Hit& hit
201 = intersectVisitor.getHitList(lineSegment.get())[i];
203 point.osg() = hit.getWorldIntersectPoint();
204 double newdist = length(start - point);
205 if (newdist < dist) {
215 bool FGScenery::scenery_available(double lat, double lon, double range_m)
217 if(globals->get_tile_mgr()->scenery_available(lat, lon, range_m))
220 get_elevation_m(lat, lon, SG_MAX_ELEVATION_M, elev, 0);
221 SGVec3f p = SGVec3f::fromGeod(SGGeod::fromDegM(lon,lat,elev));
222 simgear::CheckSceneryVisitor csnv(getPagerSingleton(), p.osg(), range_m);
223 // currently the PagedLODs will not be loaded by the DatabasePager
224 // while the splashscreen is there, so CheckSceneryVisitor force-loads
225 // missing objects in the main thread
226 get_scene_graph()->accept(csnv);
234 SceneryPager* FGScenery::getPagerSingleton()
236 static osg::ref_ptr<SceneryPager> pager = new SceneryPager;