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/debug/logstream.hxx>
34 #include <simgear/scene/tgdb/userdata.hxx>
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/scene/model/placementtrans.hxx>
37 #include <simgear/scene/material/matlib.hxx>
38 #include <simgear/scene/util/SGNodeMasks.hxx>
39 #include <simgear/scene/util/SGSceneUserData.hxx>
41 #include <Main/fg_props.hxx>
43 #include "scenery.hxx"
45 class FGGroundPickCallback : public SGPickCallback {
47 virtual bool buttonPressed(int button, const Info& info)
49 // only on left mouse button
53 SGGeod geod = SGGeod::fromCart(info.wgs84);
54 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
56 SGPropertyNode *c = fgGetNode("/sim/input/click", true);
57 c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
58 c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
59 c->setDoubleValue("elevation-m", geod.getElevationM());
60 c->setDoubleValue("elevation-ft", geod.getElevationFt());
61 fgSetBool("/sim/signals/click", 1);
67 // Scenery Management system
68 FGScenery::FGScenery()
70 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
74 // Initialize the Scenery Management system
75 FGScenery::~FGScenery() {
79 void FGScenery::init() {
81 scene_graph = new osg::Group;
82 scene_graph->setName( "Scene" );
85 terrain_branch = new osg::Group;
86 terrain_branch->setName( "Terrain" );
87 scene_graph->addChild( terrain_branch.get() );
88 SGSceneUserData* userData;
89 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
90 userData->setPickCallback(new FGGroundPickCallback);
92 models_branch = new osg::Group;
93 models_branch->setName( "Models" );
94 scene_graph->addChild( models_branch.get() );
96 aircraft_branch = new osg::Group;
97 aircraft_branch->setName( "Aircraft" );
98 scene_graph->addChild( aircraft_branch.get() );
100 // Initials values needed by the draw-time object loader
101 sgUserDataInit( globals->get_model_lib(), globals->get_fg_root(),
102 globals->get_props(), globals->get_sim_time_sec() );
106 void FGScenery::update(double dt) {
110 void FGScenery::bind() {
114 void FGScenery::unbind() {
118 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
119 double& alt, const SGMaterial** material)
121 return get_elevation_m(SGGeod::fromDegM(lon, lat, max_alt), alt, material);
125 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
126 double& alt, const SGMaterial** material)
128 SGGeod geod = SGGeod::fromCart(pos);
129 geod.setElevationM(geod.getElevationM() + max_altoff);
130 return get_elevation_m(geod, alt, material);
134 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
135 const SGMaterial** material)
137 SGVec3d start = SGVec3d::fromGeod(geod);
139 SGGeod geodEnd = geod;
140 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
141 SGVec3d end = SGVec3d::fromGeod(geodEnd);
143 osgUtil::IntersectVisitor intersectVisitor;
144 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
145 osg::ref_ptr<osg::LineSegment> lineSegment;
146 lineSegment = new osg::LineSegment(start.osg(), end.osg());
147 intersectVisitor.addLineSegment(lineSegment.get());
148 get_scene_graph()->accept(intersectVisitor);
149 bool hits = intersectVisitor.hits();
151 int nHits = intersectVisitor.getNumHits(lineSegment.get());
152 alt = -SGLimitsd::max();
153 for (int i = 0; i < nHits; ++i) {
154 const osgUtil::Hit& hit
155 = intersectVisitor.getHitList(lineSegment.get())[i];
157 point.osg() = hit.getWorldIntersectPoint();
158 SGGeod geod = SGGeod::fromCart(point);
159 double elevation = geod.getElevationM();
160 if (alt < elevation) {
163 const osg::StateSet* stateSet = hit.getDrawable()->getStateSet();
164 *material = globals->get_matlib()->findMaterial(stateSet);
174 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
177 // We assume that starting positions in the center of the earth are invalid
178 if ( norm1(pos) < 1 )
181 // Make really sure the direction is normalized, is really cheap compared to
182 // computation of ground intersection.
184 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
186 osgUtil::IntersectVisitor intersectVisitor;
187 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
188 osg::ref_ptr<osg::LineSegment> lineSegment;
189 lineSegment = new osg::LineSegment(start.osg(), end.osg());
190 intersectVisitor.addLineSegment(lineSegment.get());
191 get_scene_graph()->accept(intersectVisitor);
192 bool hits = intersectVisitor.hits();
194 int nHits = intersectVisitor.getNumHits(lineSegment.get());
195 double dist = SGLimitsd::max();
196 for (int i = 0; i < nHits; ++i) {
197 const osgUtil::Hit& hit
198 = intersectVisitor.getHitList(lineSegment.get())[i];
200 point.osg() = hit.getWorldIntersectPoint();
201 double newdist = length(start - point);
202 if (newdist < dist) {