]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
Modified 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 <osgUtil/IntersectVisitor>
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/scene/tgdb/userdata.hxx>
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/scene/model/placementtrans.hxx>
37 #include <simgear/scene/material/matlib.hxx>
38 #include <simgear/scene/util/SGNodeMasks.hxx>
39
40 #include <Main/fg_props.hxx>
41
42 #include "scenery.hxx"
43
44
45 // Scenery Management system
46 FGScenery::FGScenery() :
47   center(0, 0, 0)
48 {
49     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
50 }
51
52
53 // Initialize the Scenery Management system
54 FGScenery::~FGScenery() {
55 }
56
57
58 void FGScenery::init() {
59     // Scene graph root
60     scene_graph = new osg::Group;
61     scene_graph->setName( "Scene" );
62
63     // Terrain branch
64     terrain_branch = new osg::Group;
65     terrain_branch->setName( "Terrain" );
66     scene_graph->addChild( terrain_branch.get() );
67
68     models_branch = new osg::Group;
69     models_branch->setName( "Models" );
70     scene_graph->addChild( models_branch.get() );
71
72     aircraft_branch = new osg::Group;
73     aircraft_branch->setName( "Aircraft" );
74     scene_graph->addChild( aircraft_branch.get() );
75
76     // Lighting
77     gnd_lights_root = new osg::Group;
78     gnd_lights_root->setName( "Ground Lighting Root" );
79
80     vasi_lights_root = new osg::Group;
81     vasi_lights_root->setName( "VASI/PAPI Lighting Root" );
82
83     rwy_lights_root = new osg::Group;
84     rwy_lights_root->setName( "Runway Lighting Root" );
85
86     taxi_lights_root = new osg::Group;
87     taxi_lights_root->setName( "Taxi Lighting Root" );
88
89     // Initials values needed by the draw-time object loader
90     sgUserDataInit( globals->get_model_lib(), globals->get_fg_root(),
91                     globals->get_props(), globals->get_sim_time_sec() );
92 }
93
94
95 void FGScenery::update(double dt) {
96 }
97
98
99 void FGScenery::bind() {
100 }
101
102
103 void FGScenery::unbind() {
104 }
105
106 void FGScenery::set_center( const SGVec3d& p ) {
107     if (center == p)
108       return;
109     center = p;
110     placement_list_type::iterator it = _placement_list.begin();
111     while (it != _placement_list.end()) {
112         (*it)->setSceneryCenter(center);
113         ++it;
114     }
115 }
116
117 void FGScenery::register_placement_transform(SGPlacementTransform *trans) {
118     _placement_list.push_back(trans);        
119     trans->setSceneryCenter(center);
120 }
121
122 void FGScenery::unregister_placement_transform(SGPlacementTransform *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, const SGMaterial** material,
135                            bool exact)
136 {
137   SGGeod geod = SGGeod::fromDegM(lon, lat, max_alt);
138   SGVec3d pos = SGVec3d::fromGeod(geod);
139   return get_cart_elevation_m(pos, 0, alt, material, exact);
140 }
141
142 bool
143 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
144                                 double& alt, const SGMaterial** material,
145                                 bool exact)
146 {
147   if ( norm1(pos) < 1 )
148     return false;
149
150   SGVec3d saved_center = center;
151   bool replaced_center = false;
152   if (exact) {
153     if (30*30 < distSqr(pos, center)) {
154       set_center( pos );
155       replaced_center = true;
156     }
157   }
158
159
160   SGVec3d start = pos + max_altoff*normalize(pos) - center;
161   SGVec3d end = - center;
162   
163   osgUtil::IntersectVisitor intersectVisitor;
164   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
165   osg::ref_ptr<osg::LineSegment> lineSegment;
166   lineSegment = new osg::LineSegment(start.osg(), end.osg());
167   intersectVisitor.addLineSegment(lineSegment.get());
168   get_scene_graph()->accept(intersectVisitor);
169   bool hits = intersectVisitor.hits();
170   if (hits) {
171     int nHits = intersectVisitor.getNumHits(lineSegment.get());
172     alt = -SGLimitsd::max();
173     for (int i = 0; i < nHits; ++i) {
174       const osgUtil::Hit& hit
175         = intersectVisitor.getHitList(lineSegment.get())[i];
176       SGVec3d point;
177       point.osg() = hit.getWorldIntersectPoint();
178       point += center;
179       SGGeod geod = SGGeod::fromCart(point);
180       double elevation = geod.getElevationM();
181       if (alt < elevation) {
182         alt = elevation;
183         if (material)
184           *material = globals->get_matlib()->findMaterial(hit.getGeode());
185       }
186     }
187   }
188
189   if (replaced_center)
190     set_center( saved_center );
191   
192   return hits;
193 }
194
195 bool
196 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
197                                         SGVec3d& nearestHit, bool exact)
198 {
199   // We assume that starting positions in the center of the earth are invalid
200   if ( norm1(pos) < 1 )
201     return false;
202
203   // Well that 'exactness' is somehow problematic, but makes at least sure
204   // that we don't compute that with a cenery center at the other side of
205   // the world ...
206   SGVec3d saved_center = center;
207   bool replaced_center = false;
208   if (exact) {
209     if (30*30 < distSqr(pos, center)) {
210       set_center( pos );
211       replaced_center = true;
212     }
213   }
214
215   // Make really sure the direction is normalized, is really cheap compared to
216   // computation of ground intersection.
217   SGVec3d start = pos - center;
218   SGVec3d end = start + 1e5*dir;
219   
220   osgUtil::IntersectVisitor intersectVisitor;
221   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
222   osg::ref_ptr<osg::LineSegment> lineSegment;
223   lineSegment = new osg::LineSegment(start.osg(), end.osg());
224   intersectVisitor.addLineSegment(lineSegment.get());
225   get_scene_graph()->accept(intersectVisitor);
226   bool hits = intersectVisitor.hits();
227   if (hits) {
228     int nHits = intersectVisitor.getNumHits(lineSegment.get());
229     double dist = SGLimitsd::max();
230     for (int i = 0; i < nHits; ++i) {
231       const osgUtil::Hit& hit
232         = intersectVisitor.getHitList(lineSegment.get())[i];
233       SGVec3d point;
234       point.osg() = hit.getWorldIntersectPoint();
235       double newdist = length(start - point);
236       if (newdist < dist) {
237         dist = newdist;
238         nearestHit = point + center;
239       }
240     }
241   }
242
243   if (replaced_center)
244     set_center( saved_center );
245
246   return hits;
247 }
248