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;
47 using namespace simgear;
49 class FGGroundPickCallback : public SGPickCallback {
51 virtual bool buttonPressed(int button, const Info& info)
53 // only on left mouse button
57 SGGeod geod = SGGeod::fromCart(info.wgs84);
58 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
60 SGPropertyNode *c = fgGetNode("/sim/input/click", true);
61 c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
62 c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
63 c->setDoubleValue("elevation-m", geod.getElevationM());
64 c->setDoubleValue("elevation-ft", geod.getElevationFt());
65 fgSetBool("/sim/signals/click", 1);
71 // Scenery Management system
72 FGScenery::FGScenery()
74 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
77 FGScenery::~FGScenery() {
81 // Initialize the Scenery Management system
82 void FGScenery::init() {
84 scene_graph = new osg::Group;
85 scene_graph->setName( "Scene" );
88 terrain_branch = new osg::Group;
89 terrain_branch->setName( "Terrain" );
90 scene_graph->addChild( terrain_branch.get() );
91 SGSceneUserData* userData;
92 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
93 userData->setPickCallback(new FGGroundPickCallback);
95 models_branch = new osg::Group;
96 models_branch->setName( "Models" );
97 scene_graph->addChild( models_branch.get() );
99 aircraft_branch = new osg::Group;
100 aircraft_branch->setName( "Aircraft" );
101 scene_graph->addChild( aircraft_branch.get() );
103 // Initials values needed by the draw-time object loader
104 sgUserDataInit( globals->get_props() );
108 void FGScenery::update(double dt) {
112 void FGScenery::bind() {
116 void FGScenery::unbind() {
120 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
121 double& alt, const SGMaterial** material,
122 const osg::Node* butNotFrom)
124 SGGeod geod = SGGeod::fromCart(pos);
125 geod.setElevationM(geod.getElevationM() + max_altoff);
126 return get_elevation_m(geod, alt, material, butNotFrom);
130 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
131 const SGMaterial** material,
132 const osg::Node* butNotFrom)
134 SGVec3d start = SGVec3d::fromGeod(geod);
136 SGGeod geodEnd = geod;
137 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
138 SGVec3d end = SGVec3d::fromGeod(geodEnd);
140 osgUtil::IntersectVisitor intersectVisitor;
141 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
142 osg::ref_ptr<osg::LineSegment> lineSegment;
143 lineSegment = new osg::LineSegment(toOsg(start), toOsg(end));
144 intersectVisitor.addLineSegment(lineSegment.get());
145 get_scene_graph()->accept(intersectVisitor);
147 if (intersectVisitor.hits()) {
148 int nHits = intersectVisitor.getNumHits(lineSegment.get());
149 alt = -SGLimitsd::max();
150 for (int i = 0; i < nHits; ++i) {
151 const osgUtil::Hit& hit
152 = intersectVisitor.getHitList(lineSegment.get())[i];
154 std::find(hit.getNodePath().begin(), hit.getNodePath().end(),
155 butNotFrom) != hit.getNodePath().end())
158 // We might need the double variant of the intersection point.
159 // Thus we cannot use the float variant delivered by
160 // hit.getWorldIntersectPoint() but we have to redo that with osg::Vec3d.
161 osg::Vec3d point = hit.getLocalIntersectPoint();
163 point = point*(*hit.getMatrix());
164 SGGeod geod = SGGeod::fromCart(toSG(point));
165 double elevation = geod.getElevationM();
166 if (alt < elevation) {
170 *material = SGMaterialLib::findMaterial(hit.getGeode());
179 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
181 const osg::Node* butNotFrom)
183 // We assume that starting positions in the center of the earth are invalid
184 if ( norm1(pos) < 1 )
187 // Make really sure the direction is normalized, is really cheap compared to
188 // computation of ground intersection.
190 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
192 osgUtil::IntersectVisitor intersectVisitor;
193 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
194 osg::ref_ptr<osg::LineSegment> lineSegment;
195 lineSegment = new osg::LineSegment(toOsg(start), toOsg(end));
196 intersectVisitor.addLineSegment(lineSegment.get());
197 get_scene_graph()->accept(intersectVisitor);
199 if (intersectVisitor.hits()) {
200 int nHits = intersectVisitor.getNumHits(lineSegment.get());
201 double dist = SGLimitsd::max();
202 for (int i = 0; i < nHits; ++i) {
203 const osgUtil::Hit& hit
204 = intersectVisitor.getHitList(lineSegment.get())[i];
206 std::find(hit.getNodePath().begin(), hit.getNodePath().end(),
207 butNotFrom) != hit.getNodePath().end())
209 // We might need the double variant of the intersection point.
210 // Thus we cannot use the float variant delivered by
211 // hit.getWorldIntersectPoint() but we have to redo that with osg::Vec3d.
212 osg::Vec3d point = hit.getLocalIntersectPoint();
214 point = point*(*hit.getMatrix());
215 double newdist = length(start - toSG(point));
216 if (newdist < dist) {
218 nearestHit = toSG(point);
227 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
229 if(globals->get_tile_mgr()->scenery_available(position, range_m))
232 if (!get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0, 0))
234 SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
235 simgear::CheckSceneryVisitor csnv(getPagerSingleton(), toOsg(p), range_m);
236 // currently the PagedLODs will not be loaded by the DatabasePager
237 // while the splashscreen is there, so CheckSceneryVisitor force-loads
238 // missing objects in the main thread
239 get_scene_graph()->accept(csnv);
247 SceneryPager* FGScenery::getPagerSingleton()
249 static osg::ref_ptr<SceneryPager> pager = new SceneryPager;