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., 675 Mass Ave, Cambridge, MA 02139, 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>
36 #include <Main/fg_props.hxx>
38 #include "hitlist.hxx"
39 #include "scenery.hxx"
42 // Scenery Management system
43 FGScenery::FGScenery() {
44 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
46 center = Point3D(0.0);
50 // Initialize the Scenery Management system
51 FGScenery::~FGScenery() {
55 void FGScenery::init() {
57 scene_graph = new ssgRoot;
58 scene_graph->setName( "Scene" );
61 terrain_branch = new ssgBranch;
62 terrain_branch->setName( "Terrain" );
63 scene_graph->addKid( terrain_branch );
65 models_branch = new ssgBranch;
66 models_branch->setName( "Models" );
67 scene_graph->addKid( models_branch );
69 aircraft_branch = new ssgBranch;
70 aircraft_branch->setName( "Aircraft" );
71 scene_graph->addKid( aircraft_branch );
74 gnd_lights_root = new ssgRoot;
75 gnd_lights_root->setName( "Ground Lighting Root" );
77 vasi_lights_root = new ssgRoot;
78 vasi_lights_root->setName( "VASI/PAPI Lighting Root" );
80 rwy_lights_root = new ssgRoot;
81 rwy_lights_root->setName( "Runway Lighting Root" );
83 taxi_lights_root = new ssgRoot;
84 taxi_lights_root->setName( "Taxi Lighting Root" );
86 // Initials values needed by the draw-time object loader
87 sgUserDataInit( globals->get_model_lib(), globals->get_fg_root(),
88 globals->get_props(), globals->get_sim_time_sec() );
92 void FGScenery::update(double dt) {
96 void FGScenery::bind() {
100 void FGScenery::unbind() {
103 void FGScenery::set_center( Point3D p ) {
106 sgdSetVec3(c, p.x(), p.y(), p.z());
107 placement_list_type::iterator it = _placement_list.begin();
108 while (it != _placement_list.end()) {
109 (*it)->setSceneryCenter(c);
114 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) {
127 it = _placement_list.erase(it);
134 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
135 double& alt, bool exact)
137 // std::cout << __PRETTY_FUNCTION__ << " "
143 sgGeodToCart(lat*SG_DEGREES_TO_RADIANS, lon*SG_DEGREES_TO_RADIANS,
145 return get_cart_elevation_m(pos, 0, alt, exact);
149 FGScenery::get_cart_elevation_m(const sgdVec3 pos, double max_altoff,
150 double& alt, bool exact)
152 Point3D saved_center = center;
153 bool replaced_center = false;
155 Point3D ppos(pos[0], pos[1], pos[2]);
156 if (30.0*30.0 < ppos.distance3Dsquared(center)) {
158 replaced_center = false;
162 // overridden with actual values if a terrain intersection is
164 double hit_radius = 0.0;
165 sgdVec3 hit_normal = { 0.0, 0.0, 0.0 };
168 if ( fabs(pos[0]) > 1.0 || fabs(pos[1]) > 1.0 || fabs(pos[2]) > 1.0 ) {
170 sgdSetVec3(sc, center[0], center[1], center[2]);
173 sgdCopyVec3(ncpos, pos);
177 // scenery center has been properly defined so any hit should
178 // be valid (and not just luck)
179 hit = fgCurrentElev(ncpos, max_altoff+sgdLengthVec3(pos),
180 sc, (ssgTransform*)get_scene_graph(),
181 &hit_list, &alt, &hit_radius, hit_normal);
185 set_center( saved_center );