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>
40 #include <Main/fg_props.hxx>
42 #include "scenery.hxx"
45 // Scenery Management system
46 FGScenery::FGScenery() :
49 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
53 // Initialize the Scenery Management system
54 FGScenery::~FGScenery() {
58 void FGScenery::init() {
60 scene_graph = new osg::Group;
61 scene_graph->setName( "Scene" );
64 terrain_branch = new osg::Group;
65 terrain_branch->setName( "Terrain" );
66 scene_graph->addChild( terrain_branch.get() );
68 models_branch = new osg::Group;
69 models_branch->setName( "Models" );
70 scene_graph->addChild( models_branch.get() );
72 aircraft_branch = new osg::Group;
73 aircraft_branch->setName( "Aircraft" );
74 scene_graph->addChild( aircraft_branch.get() );
77 gnd_lights_root = new osg::Group;
78 gnd_lights_root->setName( "Ground Lighting Root" );
80 vasi_lights_root = new osg::Group;
81 vasi_lights_root->setName( "VASI/PAPI Lighting Root" );
83 rwy_lights_root = new osg::Group;
84 rwy_lights_root->setName( "Runway Lighting Root" );
86 taxi_lights_root = new osg::Group;
87 taxi_lights_root->setName( "Taxi Lighting Root" );
89 // Initials values needed by the draw-time object loader
90 sgUserDataInit( globals->get_model_lib(), globals->get_fg_root(),
91 globals->get_props(), globals->get_sim_time_sec() );
95 void FGScenery::update(double dt) {
99 void FGScenery::bind() {
103 void FGScenery::unbind() {
106 void FGScenery::set_center( const SGVec3d& p ) {
110 placement_list_type::iterator it = _placement_list.begin();
111 while (it != _placement_list.end()) {
112 (*it)->setSceneryCenter(center);
117 void FGScenery::register_placement_transform(SGPlacementTransform *trans) {
118 _placement_list.push_back(trans);
119 trans->setSceneryCenter(center);
122 void FGScenery::unregister_placement_transform(SGPlacementTransform *trans) {
123 placement_list_type::iterator it = _placement_list.begin();
124 while (it != _placement_list.end()) {
125 if ((*it) == trans) {
126 it = _placement_list.erase(it);
133 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
134 double& alt, const SGMaterial** material,
137 SGGeod geod = SGGeod::fromDegM(lon, lat, max_alt);
138 SGVec3d pos = SGVec3d::fromGeod(geod);
139 return get_cart_elevation_m(pos, 0, alt, material, exact);
143 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
144 double& alt, const SGMaterial** material,
147 if ( norm1(pos) < 1 )
150 SGVec3d saved_center = center;
151 bool replaced_center = false;
153 if (30*30 < distSqr(pos, center)) {
155 replaced_center = true;
160 SGVec3d start = pos + max_altoff*normalize(pos) - center;
161 SGVec3d end = - center;
163 osgUtil::IntersectVisitor intersectVisitor;
164 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
165 osg::ref_ptr<osg::LineSegment> lineSegment;
166 lineSegment = new osg::LineSegment(start.osg(), end.osg());
167 intersectVisitor.addLineSegment(lineSegment.get());
168 get_scene_graph()->accept(intersectVisitor);
169 bool hits = intersectVisitor.hits();
171 int nHits = intersectVisitor.getNumHits(lineSegment.get());
172 alt = -SGLimitsd::max();
173 for (int i = 0; i < nHits; ++i) {
174 const osgUtil::Hit& hit
175 = intersectVisitor.getHitList(lineSegment.get())[i];
177 point.osg() = hit.getWorldIntersectPoint();
179 SGGeod geod = SGGeod::fromCart(point);
180 double elevation = geod.getElevationM();
181 if (alt < elevation) {
184 *material = globals->get_matlib()->findMaterial(hit.getGeode());
190 set_center( saved_center );
196 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
197 SGVec3d& nearestHit, bool exact)
199 // We assume that starting positions in the center of the earth are invalid
200 if ( norm1(pos) < 1 )
203 // Well that 'exactness' is somehow problematic, but makes at least sure
204 // that we don't compute that with a cenery center at the other side of
206 SGVec3d saved_center = center;
207 bool replaced_center = false;
209 if (30*30 < distSqr(pos, center)) {
211 replaced_center = true;
215 // Make really sure the direction is normalized, is really cheap compared to
216 // computation of ground intersection.
217 SGVec3d start = pos - center;
218 SGVec3d end = start + 1e5*dir;
220 osgUtil::IntersectVisitor intersectVisitor;
221 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
222 osg::ref_ptr<osg::LineSegment> lineSegment;
223 lineSegment = new osg::LineSegment(start.osg(), end.osg());
224 intersectVisitor.addLineSegment(lineSegment.get());
225 get_scene_graph()->accept(intersectVisitor);
226 bool hits = intersectVisitor.hits();
228 int nHits = intersectVisitor.getNumHits(lineSegment.get());
229 double dist = SGLimitsd::max();
230 for (int i = 0; i < nHits; ++i) {
231 const osgUtil::Hit& hit
232 = intersectVisitor.getHitList(lineSegment.get())[i];
234 point.osg() = hit.getWorldIntersectPoint();
235 double newdist = length(start - point);
236 if (newdist < dist) {
238 nearestHit = point + center;
244 set_center( saved_center );