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