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/scene/material/matlib.hxx>
37 #include <simgear/scene/util/SGNodeMasks.hxx>
38 #include <simgear/scene/util/SGSceneUserData.hxx>
39 #include <simgear/scene/model/CheckSceneryVisitor.hxx>
41 #include <Main/fg_props.hxx>
43 #include "tilemgr.hxx"
44 #include "scenery.hxx"
46 using namespace flightgear;
48 class FGGroundPickCallback : public SGPickCallback {
50 virtual bool buttonPressed(int button, const Info& info)
52 // only on left mouse button
56 SGGeod geod = SGGeod::fromCart(info.wgs84);
57 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
59 SGPropertyNode *c = fgGetNode("/sim/input/click", true);
60 c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
61 c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
62 c->setDoubleValue("elevation-m", geod.getElevationM());
63 c->setDoubleValue("elevation-ft", geod.getElevationFt());
64 fgSetBool("/sim/signals/click", 1);
70 // Scenery Management system
71 FGScenery::FGScenery()
73 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
76 FGScenery::~FGScenery() {
80 // Initialize the Scenery Management system
81 void FGScenery::init() {
83 scene_graph = new osg::Group;
84 scene_graph->setName( "Scene" );
87 terrain_branch = new osg::Group;
88 terrain_branch->setName( "Terrain" );
89 scene_graph->addChild( terrain_branch.get() );
90 SGSceneUserData* userData;
91 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
92 userData->setPickCallback(new FGGroundPickCallback);
94 models_branch = new osg::Group;
95 models_branch->setName( "Models" );
96 scene_graph->addChild( models_branch.get() );
98 aircraft_branch = new osg::Group;
99 aircraft_branch->setName( "Aircraft" );
100 scene_graph->addChild( aircraft_branch.get() );
102 // Initials values needed by the draw-time object loader
103 sgUserDataInit( globals->get_props() );
107 void FGScenery::update(double dt) {
111 void FGScenery::bind() {
115 void FGScenery::unbind() {
119 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
120 double& alt, const SGMaterial** material)
122 SGGeod geod = SGGeod::fromCart(pos);
123 geod.setElevationM(geod.getElevationM() + max_altoff);
124 return get_elevation_m(geod, alt, material);
128 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
129 const SGMaterial** material)
131 SGVec3d start = SGVec3d::fromGeod(geod);
133 SGGeod geodEnd = geod;
134 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
135 SGVec3d end = SGVec3d::fromGeod(geodEnd);
137 osgUtil::IntersectVisitor intersectVisitor;
138 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
139 osg::ref_ptr<osg::LineSegment> lineSegment;
140 lineSegment = new osg::LineSegment(start.osg(), end.osg());
141 intersectVisitor.addLineSegment(lineSegment.get());
142 get_scene_graph()->accept(intersectVisitor);
143 bool hits = intersectVisitor.hits();
145 int nHits = intersectVisitor.getNumHits(lineSegment.get());
146 alt = -SGLimitsd::max();
147 for (int i = 0; i < nHits; ++i) {
148 const osgUtil::Hit& hit
149 = intersectVisitor.getHitList(lineSegment.get())[i];
151 point.osg() = hit.getWorldIntersectPoint();
152 SGGeod geod = SGGeod::fromCart(point);
153 double elevation = geod.getElevationM();
154 if (alt < elevation) {
157 const osg::StateSet* stateSet = hit.getDrawable()->getStateSet();
158 *material = SGMaterialLib::findMaterial(stateSet);
168 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
171 // We assume that starting positions in the center of the earth are invalid
172 if ( norm1(pos) < 1 )
175 // Make really sure the direction is normalized, is really cheap compared to
176 // computation of ground intersection.
178 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
180 osgUtil::IntersectVisitor intersectVisitor;
181 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
182 osg::ref_ptr<osg::LineSegment> lineSegment;
183 lineSegment = new osg::LineSegment(start.osg(), end.osg());
184 intersectVisitor.addLineSegment(lineSegment.get());
185 get_scene_graph()->accept(intersectVisitor);
186 bool hits = intersectVisitor.hits();
188 int nHits = intersectVisitor.getNumHits(lineSegment.get());
189 double dist = SGLimitsd::max();
190 for (int i = 0; i < nHits; ++i) {
191 const osgUtil::Hit& hit
192 = intersectVisitor.getHitList(lineSegment.get())[i];
194 point.osg() = hit.getWorldIntersectPoint();
195 double newdist = length(start - point);
196 if (newdist < dist) {
206 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
208 if(globals->get_tile_mgr()->scenery_available(position, range_m))
211 get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0);
212 SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
213 simgear::CheckSceneryVisitor csnv(getPagerSingleton(), p.osg(), range_m);
214 // currently the PagedLODs will not be loaded by the DatabasePager
215 // while the splashscreen is there, so CheckSceneryVisitor force-loads
216 // missing objects in the main thread
217 get_scene_graph()->accept(csnv);
225 SceneryPager* FGScenery::getPagerSingleton()
227 static osg::ref_ptr<SceneryPager> pager = new SceneryPager;