]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
Make use of the ground material types
[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     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
46
47     center = Point3D(0.0);
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 Point3D& p ) {
105     center = p;
106     sgdVec3 c;
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);
111         ++it;
112     }
113 }
114
115 void FGScenery::register_placement_transform(ssgPlacementTransform *trans) {
116     _placement_list.push_back(trans);        
117     sgdVec3 c;
118     sgdSetVec3(c, center.x(), center.y(), center.z());
119     trans->setSceneryCenter(c);
120 }
121
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);        
127         } else
128             ++it;
129     }
130 }
131
132 bool
133 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
134                            double& alt, bool exact)
135 {
136 //   std::cout << __PRETTY_FUNCTION__ << " "
137 //             << lat << " "
138 //             << lon << " "
139 //             << max_alt
140 //             << std::endl;
141   sgdVec3 pos;
142   sgGeodToCart(lat*SG_DEGREES_TO_RADIANS, lon*SG_DEGREES_TO_RADIANS,
143                max_alt, pos);
144   return get_cart_elevation_m(pos, 0, alt, exact);
145 }
146
147 bool
148 FGScenery::get_material_m(double lat, double lon, double max_alt,
149                           double& alt, string & material, bool exact)
150 {
151   sgdVec3 pos;
152   sgGeodToCart(lat*SG_DEGREES_TO_RADIANS, lon*SG_DEGREES_TO_RADIANS,
153                max_alt, pos);
154
155   return get_cart_material_m(pos, 0, alt, material, exact);
156 }
157
158 bool
159 FGScenery::get_cart_elevation_m(const sgdVec3& pos, double max_altoff,
160                                 double& alt, bool exact)
161 {
162   Point3D saved_center = center;
163   bool replaced_center = false;
164   if (exact) {
165     Point3D ppos(pos[0], pos[1], pos[2]);
166     if (30.0*30.0 < ppos.distance3Dsquared(center)) {
167       set_center( ppos );
168       replaced_center = true;
169     }
170   }
171
172   // overridden with actual values if a terrain intersection is
173   // found
174   int this_hit;
175   double hit_radius = 0.0;
176   sgdVec3 hit_normal = { 0.0, 0.0, 0.0 };
177   
178   bool hit = false;
179   if ( fabs(pos[0]) > 1.0 || fabs(pos[1]) > 1.0 || fabs(pos[2]) > 1.0 ) {
180     sgdVec3 sc;
181     sgdSetVec3(sc, center[0], center[1], center[2]);
182     
183     sgdVec3 ncpos;
184     sgdCopyVec3(ncpos, pos);
185     
186     FGHitList hit_list;
187     
188     // scenery center has been properly defined so any hit should
189     // be valid (and not just luck)
190     hit = fgCurrentElev(ncpos, max_altoff+sgdLengthVec3(pos),
191                         sc, (ssgTransform*)get_scene_graph(),
192                         &hit_list, &alt, &hit_radius, hit_normal, this_hit);
193   }
194
195   if (replaced_center)
196     set_center( saved_center );
197   
198   return hit;
199 }
200
201 bool
202 FGScenery::get_cart_material_m(const sgdVec3& pos, double max_altoff,
203                                double& alt, string& material, bool exact)
204 {
205   Point3D saved_center = center;
206   bool replaced_center = false;
207   if (exact) {
208     Point3D ppos(pos[0], pos[1], pos[2]);
209     if (30.0*30.0 < ppos.distance3Dsquared(center)) {
210       set_center( ppos );
211       replaced_center = true;
212     }
213   }
214
215   material = "";
216
217   // overridden with actual values if a terrain intersection is
218   // found
219   int this_hit;
220   double hit_radius = 0.0;
221   sgdVec3 hit_normal = { 0.0, 0.0, 0.0 };
222   
223   bool hit = false;
224   if ( fabs(pos[0]) > 1.0 || fabs(pos[1]) > 1.0 || fabs(pos[2]) > 1.0 ) {
225     sgdVec3 sc;
226     sgdSetVec3(sc, center[0], center[1], center[2]);
227     
228     sgdVec3 ncpos;
229     sgdCopyVec3(ncpos, pos);
230     
231     FGHitList hit_list;
232     
233     // scenery center has been properly defined so any hit should
234     // be valid (and not just luck)
235     hit = fgCurrentElev(ncpos, max_altoff+sgdLengthVec3(pos),
236                         sc, (ssgTransform*)get_scene_graph(),
237                         &hit_list, &alt, &hit_radius, hit_normal,
238                         this_hit );
239
240     if( hit )
241     {
242         ssgEntity *entity = hit_list.get_entity( this_hit );
243
244         if( entity != NULL && entity->isAKindOf(ssgTypeLeaf()) )
245         {
246             ssgLeaf *leaf = (ssgLeaf*) hit_list.get_entity( this_hit );
247             ssgState *st = leaf->getState();
248
249             if( st != NULL && st->isAKindOf(ssgTypeSimpleState()) )
250             {
251                 ssgSimpleState *ss = (ssgSimpleState *) st;
252
253                 if( !globals->get_matlib()->find( ss, material ) )
254                 {
255                     material = "not-in-matlib";
256                 }
257             }
258         }
259     }
260     else
261     {
262         material = "no-hit";
263     }
264   }
265
266   if (replaced_center)
267     set_center( saved_center );
268   
269   return hit;
270 }
271
272
273 bool
274 FGScenery::get_cart_ground_intersection(const sgdVec3& pos,
275                                         const sgdVec3& dir,
276                                         sgdVec3& nearestHit, bool exact)
277 {
278   // We assume that starting positions in the center of the earth are invalid
279   if ( fabs(pos[0]) < 1.0 && fabs(pos[1]) < 1.0 && fabs(pos[2]) < 1.0 )
280     return false;
281
282   // Well that 'exactness' is somehow problematic, but makes at least sure
283   // that we don't compute that with a cenery center at the other side of
284   // the world ...
285   Point3D saved_center = center;
286   bool replaced_center = false;
287   if (exact) {
288     Point3D ppos(pos[0], pos[1], pos[2]);
289     if (30.0*30.0 < ppos.distance3Dsquared(center)) {
290       set_center( ppos );
291       replaced_center = true;
292     }
293   }
294
295   // Not yet found any hit ...
296   bool result = false;
297
298   // Make really sure the direction is normalized, is really cheap compared to
299   // computation of ground intersection.
300   sgdVec3 normalizedDir;
301   sgdCopyVec3(normalizedDir, dir);
302   sgdNormaliseVec3(normalizedDir);
303
304   sgdVec3 sceneryCenter;
305   sgdSetVec3(sceneryCenter, center[0], center[1], center[2]);
306   sgdVec3 relativePos;
307   sgdSubVec3(relativePos, pos, sceneryCenter);
308
309   // At the moment only intersection with the terrain?
310   FGHitList hit_list;
311   hit_list.Intersect(globals->get_scenery()->get_terrain_branch(),
312                      relativePos, normalizedDir);
313
314   double dist = DBL_MAX;
315   int hitcount = hit_list.num_hits();
316   for (int i = 0; i < hitcount; ++i) {
317     // Check for the nearest hit
318     sgdVec3 diff;
319     sgdSubVec3(diff, hit_list.get_point(i), relativePos);
320     
321     // We only want hits in front of us ...
322     if (sgdScalarProductVec3(normalizedDir, diff) < 0)
323       continue;
324
325     // find the nearest hit
326     double nDist = sgdScalarProductVec3(diff, diff);
327     if (dist < nDist)
328       continue;
329
330     // Store the hit point
331     dist = nDist;
332     sgdAddVec3(nearestHit, hit_list.get_point(i), sceneryCenter);
333     result = true;
334   }
335
336   if (replaced_center)
337     set_center( saved_center );
338
339   return result;
340 }
341