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() :
71 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
75 // Initialize the Scenery Management system
76 FGScenery::~FGScenery() {
80 void FGScenery::init() {
82 scene_graph = new osg::Group;
83 scene_graph->setName( "Scene" );
86 terrain_branch = new osg::Group;
87 terrain_branch->setName( "Terrain" );
88 scene_graph->addChild( terrain_branch.get() );
89 SGSceneUserData* userData;
90 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
91 userData->setPickCallback(new FGGroundPickCallback);
93 models_branch = new osg::Group;
94 models_branch->setName( "Models" );
95 scene_graph->addChild( models_branch.get() );
97 aircraft_branch = new osg::Group;
98 aircraft_branch->setName( "Aircraft" );
99 scene_graph->addChild( aircraft_branch.get() );
102 gnd_lights_root = new osg::Group;
103 gnd_lights_root->setName( "Ground Lighting Root" );
105 vasi_lights_root = new osg::Group;
106 vasi_lights_root->setName( "VASI/PAPI Lighting Root" );
108 rwy_lights_root = new osg::Group;
109 rwy_lights_root->setName( "Runway Lighting Root" );
111 taxi_lights_root = new osg::Group;
112 taxi_lights_root->setName( "Taxi Lighting Root" );
114 // Initials values needed by the draw-time object loader
115 sgUserDataInit( globals->get_model_lib(), globals->get_fg_root(),
116 globals->get_props(), globals->get_sim_time_sec() );
120 void FGScenery::update(double dt) {
124 void FGScenery::bind() {
128 void FGScenery::unbind() {
131 void FGScenery::set_center( const SGVec3d& p ) {
135 placement_list_type::iterator it = _placement_list.begin();
136 while (it != _placement_list.end()) {
137 (*it)->setSceneryCenter(center);
142 void FGScenery::register_placement_transform(SGPlacementTransform *trans) {
143 _placement_list.push_back(trans);
144 trans->setSceneryCenter(center);
147 void FGScenery::unregister_placement_transform(SGPlacementTransform *trans) {
148 placement_list_type::iterator it = _placement_list.begin();
149 while (it != _placement_list.end()) {
150 if ((*it) == trans) {
151 it = _placement_list.erase(it);
158 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
159 double& alt, const SGMaterial** material,
162 SGGeod geod = SGGeod::fromDegM(lon, lat, max_alt);
163 SGVec3d pos = SGVec3d::fromGeod(geod);
164 return get_cart_elevation_m(pos, 0, alt, material, exact);
168 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
169 double& alt, const SGMaterial** material,
172 if ( norm1(pos) < 1 )
175 SGVec3d saved_center = center;
176 bool replaced_center = false;
178 if (30*30 < distSqr(pos, center)) {
180 replaced_center = true;
185 SGVec3d start = pos + max_altoff*normalize(pos) - center;
186 SGVec3d end = - center;
188 osgUtil::IntersectVisitor intersectVisitor;
189 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
190 osg::ref_ptr<osg::LineSegment> lineSegment;
191 lineSegment = new osg::LineSegment(start.osg(), end.osg());
192 intersectVisitor.addLineSegment(lineSegment.get());
193 get_scene_graph()->accept(intersectVisitor);
194 bool hits = intersectVisitor.hits();
196 int nHits = intersectVisitor.getNumHits(lineSegment.get());
197 alt = -SGLimitsd::max();
198 for (int i = 0; i < nHits; ++i) {
199 const osgUtil::Hit& hit
200 = intersectVisitor.getHitList(lineSegment.get())[i];
202 point.osg() = hit.getWorldIntersectPoint();
204 SGGeod geod = SGGeod::fromCart(point);
205 double elevation = geod.getElevationM();
206 if (alt < elevation) {
209 *material = globals->get_matlib()->findMaterial(hit.getGeode());
215 set_center( saved_center );
221 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
222 SGVec3d& nearestHit, bool exact)
224 // We assume that starting positions in the center of the earth are invalid
225 if ( norm1(pos) < 1 )
228 // Well that 'exactness' is somehow problematic, but makes at least sure
229 // that we don't compute that with a cenery center at the other side of
231 SGVec3d saved_center = center;
232 bool replaced_center = false;
234 if (30*30 < distSqr(pos, center)) {
236 replaced_center = true;
240 // Make really sure the direction is normalized, is really cheap compared to
241 // computation of ground intersection.
242 SGVec3d start = pos - center;
243 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
245 osgUtil::IntersectVisitor intersectVisitor;
246 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
247 osg::ref_ptr<osg::LineSegment> lineSegment;
248 lineSegment = new osg::LineSegment(start.osg(), end.osg());
249 intersectVisitor.addLineSegment(lineSegment.get());
250 get_scene_graph()->accept(intersectVisitor);
251 bool hits = intersectVisitor.hits();
253 int nHits = intersectVisitor.getNumHits(lineSegment.get());
254 double dist = SGLimitsd::max();
255 for (int i = 0; i < nHits; ++i) {
256 const osgUtil::Hit& hit
257 = intersectVisitor.getHitList(lineSegment.get())[i];
259 point.osg() = hit.getWorldIntersectPoint();
260 double newdist = length(start - point);
261 if (newdist < dist) {
263 nearestHit = point + center;
269 set_center( saved_center );