]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
Don't restore initial screen geometry because there is nothing in fg_os* to resize...
[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
36 #include <Main/fg_props.hxx>
37
38 #include "hitlist.hxx"
39 #include "scenery.hxx"
40
41
42 // Scenery Management system
43 FGScenery::FGScenery() {
44     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
45
46     center = Point3D(0.0);
47 }
48
49
50 // Initialize the Scenery Management system
51 FGScenery::~FGScenery() {
52 }
53
54
55 void FGScenery::init() {
56     // Scene graph root
57     scene_graph = new ssgRoot;
58     scene_graph->setName( "Scene" );
59
60     // Terrain branch
61     terrain_branch = new ssgBranch;
62     terrain_branch->setName( "Terrain" );
63     scene_graph->addKid( terrain_branch );
64
65     models_branch = new ssgBranch;
66     models_branch->setName( "Models" );
67     scene_graph->addKid( models_branch );
68
69     aircraft_branch = new ssgBranch;
70     aircraft_branch->setName( "Aircraft" );
71     scene_graph->addKid( aircraft_branch );
72
73     // Lighting
74     gnd_lights_root = new ssgRoot;
75     gnd_lights_root->setName( "Ground Lighting Root" );
76
77     vasi_lights_root = new ssgRoot;
78     vasi_lights_root->setName( "VASI/PAPI Lighting Root" );
79
80     rwy_lights_root = new ssgRoot;
81     rwy_lights_root->setName( "Runway Lighting Root" );
82
83     taxi_lights_root = new ssgRoot;
84     taxi_lights_root->setName( "Taxi Lighting Root" );
85
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() );
89 }
90
91
92 void FGScenery::update(double dt) {
93 }
94
95
96 void FGScenery::bind() {
97 }
98
99
100 void FGScenery::unbind() {
101 }
102
103 void FGScenery::set_center( const Point3D& p ) {
104     center = p;
105     sgdVec3 c;
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);
110         ++it;
111     }
112 }
113
114 void FGScenery::register_placement_transform(ssgPlacementTransform *trans) {
115     _placement_list.push_back(trans);        
116     sgdVec3 c;
117     sgdSetVec3(c, center.x(), center.y(), center.z());
118     trans->setSceneryCenter(c);
119 }
120
121 void FGScenery::unregister_placement_transform(ssgPlacementTransform *trans) {
122     placement_list_type::iterator it = _placement_list.begin();
123     while (it != _placement_list.end()) {
124         if ((*it) == trans) {
125             it = _placement_list.erase(it);        
126         } else
127             ++it;
128     }
129 }
130
131 bool
132 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
133                            double& alt, bool exact)
134 {
135 //   std::cout << __PRETTY_FUNCTION__ << " "
136 //             << lat << " "
137 //             << lon << " "
138 //             << max_alt
139 //             << std::endl;
140   sgdVec3 pos;
141   sgGeodToCart(lat*SG_DEGREES_TO_RADIANS, lon*SG_DEGREES_TO_RADIANS,
142                max_alt, pos);
143   return get_cart_elevation_m(pos, 0, alt, exact);
144 }
145
146 bool
147 FGScenery::get_cart_elevation_m(const sgdVec3& pos, double max_altoff,
148                                 double& alt, bool exact)
149 {
150   Point3D saved_center = center;
151   bool replaced_center = false;
152   if (exact) {
153     Point3D ppos(pos[0], pos[1], pos[2]);
154     if (30.0*30.0 < ppos.distance3Dsquared(center)) {
155       set_center( ppos );
156       replaced_center = true;
157     }
158   }
159
160   // overridden with actual values if a terrain intersection is
161   // found
162   double hit_radius = 0.0;
163   sgdVec3 hit_normal = { 0.0, 0.0, 0.0 };
164   
165   bool hit = false;
166   if ( fabs(pos[0]) > 1.0 || fabs(pos[1]) > 1.0 || fabs(pos[2]) > 1.0 ) {
167     sgdVec3 sc;
168     sgdSetVec3(sc, center[0], center[1], center[2]);
169     
170     sgdVec3 ncpos;
171     sgdCopyVec3(ncpos, pos);
172     
173     FGHitList hit_list;
174     
175     // scenery center has been properly defined so any hit should
176     // be valid (and not just luck)
177     hit = fgCurrentElev(ncpos, max_altoff+sgdLengthVec3(pos),
178                         sc, (ssgTransform*)get_scene_graph(),
179                         &hit_list, &alt, &hit_radius, hit_normal);
180   }
181
182   if (replaced_center)
183     set_center( saved_center );
184   
185   return hit;
186 }
187
188
189 bool
190 FGScenery::get_cart_ground_intersection(const sgdVec3& pos,
191                                         const sgdVec3& dir,
192                                         sgdVec3& nearestHit, bool exact)
193 {
194   // We assume that starting positions in the center of the earth are invalid
195   if ( fabs(pos[0]) < 1.0 && fabs(pos[1]) < 1.0 && fabs(pos[2]) < 1.0 )
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   Point3D saved_center = center;
202   bool replaced_center = false;
203   if (exact) {
204     Point3D ppos(pos[0], pos[1], pos[2]);
205     if (30.0*30.0 < ppos.distance3Dsquared(center)) {
206       set_center( ppos );
207       replaced_center = true;
208     }
209   }
210
211   // Not yet found any hit ...
212   bool result = false;
213
214   // Make really sure the direction is normalized, is really cheap compared to
215   // computation of ground intersection.
216   sgdVec3 normalizedDir;
217   sgdCopyVec3(normalizedDir, dir);
218   sgdNormaliseVec3(normalizedDir);
219
220   sgdVec3 sceneryCenter;
221   sgdSetVec3(sceneryCenter, center[0], center[1], center[2]);
222   sgdVec3 relativePos;
223   sgdSubVec3(relativePos, pos, sceneryCenter);
224
225   // At the moment only intersection with the terrain?
226   FGHitList hit_list;
227   hit_list.Intersect(globals->get_scenery()->get_terrain_branch(),
228                      relativePos, normalizedDir);
229
230   double dist = DBL_MAX;
231   int hitcount = hit_list.num_hits();
232   for (int i = 0; i < hitcount; ++i) {
233     // Check for the nearest hit
234     sgdVec3 diff;
235     sgdSubVec3(diff, hit_list.get_point(i), relativePos);
236     
237     // We only want hits in front of us ...
238     if (sgdScalarProductVec3(normalizedDir, diff) < 0)
239       continue;
240
241     // find the nearest hit
242     double nDist = sgdScalarProductVec3(diff, diff);
243     if (dist < nDist)
244       continue;
245
246     // Store the hit point
247     dist = nDist;
248     sgdAddVec3(nearestHit, hit_list.get_point(i), sceneryCenter);
249     result = true;
250   }
251
252   if (replaced_center)
253     set_center( saved_center );
254
255   return result;
256 }
257