]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
remove unused and empty files
[flightgear.git] / src / Scenery / scenery.cxx
1 // scenery.cxx -- data structures and routines for managing scenery.
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30
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>
36
37 #include <Main/fg_props.hxx>
38
39 #include "hitlist.hxx"
40 #include "scenery.hxx"
41
42
43 // Scenery Management system
44 FGScenery::FGScenery() :
45   center(0, 0, 0)
46 {
47     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
48 }
49
50
51 // Initialize the Scenery Management system
52 FGScenery::~FGScenery() {
53 }
54
55
56 void FGScenery::init() {
57     // Scene graph root
58     scene_graph = new ssgRoot;
59     scene_graph->setName( "Scene" );
60
61     // Terrain branch
62     terrain_branch = new ssgBranch;
63     terrain_branch->setName( "Terrain" );
64     scene_graph->addKid( terrain_branch );
65
66     models_branch = new ssgBranch;
67     models_branch->setName( "Models" );
68     scene_graph->addKid( models_branch );
69
70     aircraft_branch = new ssgBranch;
71     aircraft_branch->setName( "Aircraft" );
72     scene_graph->addKid( aircraft_branch );
73
74     // Lighting
75     gnd_lights_root = new ssgRoot;
76     gnd_lights_root->setName( "Ground Lighting Root" );
77
78     vasi_lights_root = new ssgRoot;
79     vasi_lights_root->setName( "VASI/PAPI Lighting Root" );
80
81     rwy_lights_root = new ssgRoot;
82     rwy_lights_root->setName( "Runway Lighting Root" );
83
84     taxi_lights_root = new ssgRoot;
85     taxi_lights_root->setName( "Taxi Lighting Root" );
86
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() );
90 }
91
92
93 void FGScenery::update(double dt) {
94 }
95
96
97 void FGScenery::bind() {
98 }
99
100
101 void FGScenery::unbind() {
102 }
103
104 void FGScenery::set_center( const SGVec3d& p ) {
105     if (center == p)
106       return;
107     center = p;
108     placement_list_type::iterator it = _placement_list.begin();
109     while (it != _placement_list.end()) {
110         (*it)->setSceneryCenter(center.sg());
111         ++it;
112     }
113 }
114
115 void FGScenery::register_placement_transform(ssgPlacementTransform *trans) {
116     _placement_list.push_back(trans);        
117     trans->setSceneryCenter(center.sg());
118 }
119
120 void FGScenery::unregister_placement_transform(ssgPlacementTransform *trans) {
121     placement_list_type::iterator it = _placement_list.begin();
122     while (it != _placement_list.end()) {
123         if ((*it) == trans) {
124             it = _placement_list.erase(it);        
125         } else
126             ++it;
127     }
128 }
129
130 bool
131 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
132                            double& alt, const SGMaterial** material,
133                            bool exact)
134 {
135   SGGeod geod = SGGeod::fromDegM(lon, lat, max_alt);
136   SGVec3d pos = SGVec3d::fromGeod(geod);
137   return get_cart_elevation_m(pos, 0, alt, material, exact);
138 }
139
140 bool
141 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
142                                 double& alt, const SGMaterial** material,
143                                 bool exact)
144 {
145   if ( norm1(pos) < 1 )
146     return false;
147
148   SGVec3d saved_center = center;
149   bool replaced_center = false;
150   if (exact) {
151     if (30*30 < distSqr(pos, center)) {
152       set_center( pos );
153       replaced_center = true;
154     }
155   }
156
157   // overridden with actual values if a terrain intersection is
158   // found
159   int this_hit;
160   double hit_radius = 0.0;
161   SGVec3d hit_normal(0, 0, 0);
162   
163   SGVec3d sc = center;
164   SGVec3d ncpos = pos;
165
166   FGHitList hit_list;
167   // scenery center has been properly defined so any hit should
168   // be valid (and not just luck)
169   bool hit = fgCurrentElev(ncpos.sg(), max_altoff+length(pos), sc.sg(),
170                            get_scene_graph(), &hit_list, &alt,
171                            &hit_radius, hit_normal.sg(), this_hit);
172
173   if (material) {
174     *material = 0;
175     if (hit) {
176       ssgEntity *entity = hit_list.get_entity( this_hit );
177       if (entity && entity->isAKindOf(ssgTypeLeaf())) {
178         ssgLeaf* leaf = static_cast<ssgLeaf*>(entity);
179         *material = globals->get_matlib()->findMaterial(leaf);
180       }
181     }
182   }
183
184   if (replaced_center)
185     set_center( saved_center );
186   
187   return hit;
188 }
189
190 bool
191 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
192                                         SGVec3d& nearestHit, bool exact)
193 {
194   // We assume that starting positions in the center of the earth are invalid
195   if ( norm1(pos) < 1 )
196     return false;
197
198   // Well that 'exactness' is somehow problematic, but makes at least sure
199   // that we don't compute that with a cenery center at the other side of
200   // the world ...
201   SGVec3d saved_center = center;
202   bool replaced_center = false;
203   if (exact) {
204     if (30*30 < distSqr(pos, center)) {
205       set_center( pos );
206       replaced_center = true;
207     }
208   }
209
210   // Not yet found any hit ...
211   bool result = false;
212
213   // Make really sure the direction is normalized, is really cheap compared to
214   // computation of ground intersection.
215   SGVec3d normalizedDir = normalize(dir);
216   SGVec3d relativePos = pos - center;
217
218   // At the moment only intersection with the terrain?
219   FGHitList hit_list;
220   hit_list.Intersect(globals->get_scenery()->get_terrain_branch(),
221                      relativePos.sg(), normalizedDir.sg());
222
223   double dist = DBL_MAX;
224   int hitcount = hit_list.num_hits();
225   for (int i = 0; i < hitcount; ++i) {
226     // Check for the nearest hit
227     SGVec3d diff = SGVec3d(hit_list.get_point(i)) - relativePos;
228     
229     // We only want hits in front of us ...
230     if (dot(normalizedDir, diff) < 0)
231       continue;
232
233     // find the nearest hit
234     double nDist = dot(diff, diff);
235     if (dist < nDist)
236       continue;
237
238     // Store the hit point
239     dist = nDist;
240     nearestHit = SGVec3d(hit_list.get_point(i)) + center;
241     result = true;
242   }
243
244   if (replaced_center)
245     set_center( saved_center );
246
247   return result;
248 }
249