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 <simgear/debug/logstream.hxx>
32 #include <simgear/scene/tgdb/userdata.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/scene/model/placementtrans.hxx>
35 #include <simgear/scene/material/matlib.hxx>
37 #include <Main/fg_props.hxx>
39 #include "hitlist.hxx"
40 #include "scenery.hxx"
43 // Scenery Management system
44 FGScenery::FGScenery() {
45 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
47 center = Point3D(0.0);
51 // Initialize the Scenery Management system
52 FGScenery::~FGScenery() {
56 void FGScenery::init() {
58 scene_graph = new ssgRoot;
59 scene_graph->setName( "Scene" );
62 terrain_branch = new ssgBranch;
63 terrain_branch->setName( "Terrain" );
64 scene_graph->addKid( terrain_branch );
66 models_branch = new ssgBranch;
67 models_branch->setName( "Models" );
68 scene_graph->addKid( models_branch );
70 aircraft_branch = new ssgBranch;
71 aircraft_branch->setName( "Aircraft" );
72 scene_graph->addKid( aircraft_branch );
75 gnd_lights_root = new ssgRoot;
76 gnd_lights_root->setName( "Ground Lighting Root" );
78 vasi_lights_root = new ssgRoot;
79 vasi_lights_root->setName( "VASI/PAPI Lighting Root" );
81 rwy_lights_root = new ssgRoot;
82 rwy_lights_root->setName( "Runway Lighting Root" );
84 taxi_lights_root = new ssgRoot;
85 taxi_lights_root->setName( "Taxi Lighting Root" );
87 // Initials values needed by the draw-time object loader
88 sgUserDataInit( globals->get_model_lib(), globals->get_fg_root(),
89 globals->get_props(), globals->get_sim_time_sec() );
93 void FGScenery::update(double dt) {
97 void FGScenery::bind() {
101 void FGScenery::unbind() {
104 void FGScenery::set_center( const Point3D& p ) {
107 sgdSetVec3(c, p.x(), p.y(), p.z());
108 placement_list_type::iterator it = _placement_list.begin();
109 while (it != _placement_list.end()) {
110 (*it)->setSceneryCenter(c);
115 void FGScenery::register_placement_transform(ssgPlacementTransform *trans) {
116 _placement_list.push_back(trans);
118 sgdSetVec3(c, center.x(), center.y(), center.z());
119 trans->setSceneryCenter(c);
122 void FGScenery::unregister_placement_transform(ssgPlacementTransform *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,
138 sgGeodToCart(lat*SG_DEGREES_TO_RADIANS, lon*SG_DEGREES_TO_RADIANS,
140 return get_cart_elevation_m(pos, 0, alt, material, exact);
144 FGScenery::get_cart_elevation_m(const sgdVec3& pos, double max_altoff,
145 double& alt, const SGMaterial** material,
148 Point3D saved_center = center;
149 bool replaced_center = false;
151 Point3D ppos(pos[0], pos[1], pos[2]);
152 if (30.0*30.0 < ppos.distance3Dsquared(center)) {
154 replaced_center = true;
158 // overridden with actual values if a terrain intersection is
161 double hit_radius = 0.0;
162 sgdVec3 hit_normal = { 0.0, 0.0, 0.0 };
165 if ( fabs(pos[0]) > 1.0 || fabs(pos[1]) > 1.0 || fabs(pos[2]) > 1.0 ) {
167 sgdSetVec3(sc, center[0], center[1], center[2]);
170 sgdCopyVec3(ncpos, pos);
174 // scenery center has been properly defined so any hit should
175 // be valid (and not just luck)
176 hit = fgCurrentElev(ncpos, max_altoff+sgdLengthVec3(pos),
177 sc, get_scene_graph(),
178 &hit_list, &alt, &hit_radius, hit_normal, this_hit);
183 ssgEntity *entity = hit_list.get_entity( this_hit );
184 if (entity && entity->isAKindOf(ssgTypeLeaf())) {
185 ssgLeaf* leaf = static_cast<ssgLeaf*>(entity);
186 *material = globals->get_matlib()->findMaterial(leaf);
193 set_center( saved_center );
199 FGScenery::get_cart_ground_intersection(const sgdVec3& pos,
201 sgdVec3& nearestHit, bool exact)
203 // We assume that starting positions in the center of the earth are invalid
204 if ( fabs(pos[0]) < 1.0 && fabs(pos[1]) < 1.0 && fabs(pos[2]) < 1.0 )
207 // Well that 'exactness' is somehow problematic, but makes at least sure
208 // that we don't compute that with a cenery center at the other side of
210 Point3D saved_center = center;
211 bool replaced_center = false;
213 Point3D ppos(pos[0], pos[1], pos[2]);
214 if (30.0*30.0 < ppos.distance3Dsquared(center)) {
216 replaced_center = true;
220 // Not yet found any hit ...
223 // Make really sure the direction is normalized, is really cheap compared to
224 // computation of ground intersection.
225 sgdVec3 normalizedDir;
226 sgdCopyVec3(normalizedDir, dir);
227 sgdNormaliseVec3(normalizedDir);
229 sgdVec3 sceneryCenter;
230 sgdSetVec3(sceneryCenter, center[0], center[1], center[2]);
232 sgdSubVec3(relativePos, pos, sceneryCenter);
234 // At the moment only intersection with the terrain?
236 hit_list.Intersect(globals->get_scenery()->get_terrain_branch(),
237 relativePos, normalizedDir);
239 double dist = DBL_MAX;
240 int hitcount = hit_list.num_hits();
241 for (int i = 0; i < hitcount; ++i) {
242 // Check for the nearest hit
244 sgdSubVec3(diff, hit_list.get_point(i), relativePos);
246 // We only want hits in front of us ...
247 if (sgdScalarProductVec3(normalizedDir, diff) < 0)
250 // find the nearest hit
251 double nDist = sgdScalarProductVec3(diff, diff);
255 // Store the hit point
257 sgdAddVec3(nearestHit, hit_list.get_point(i), sceneryCenter);
262 set_center( saved_center );