1 // scenery.cxx -- data structures and routines for managing scenery.
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
31 #include <osgUtil/IntersectVisitor>
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>
41 #include <Main/fg_props.hxx>
43 #include "scenery.hxx"
45 class FGGroundPickCallback : public SGPickCallback {
47 virtual bool buttonPressed(int button, const Info& info)
49 // only on left mouse button
53 SGGeod geod = SGGeod::fromCart(info.wgs84);
54 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
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);
67 // Scenery Management system
68 FGScenery::FGScenery() :
71 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
75 // Initialize the Scenery Management system
76 FGScenery::~FGScenery() {
80 void FGScenery::init() {
82 scene_graph = new osg::Group;
83 scene_graph->setName( "Scene" );
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);
93 models_branch = new osg::Group;
94 models_branch->setName( "Models" );
95 scene_graph->addChild( models_branch.get() );
97 aircraft_branch = new osg::Group;
98 aircraft_branch->setName( "Aircraft" );
99 scene_graph->addChild( aircraft_branch.get() );
102 gnd_lights_root = new osg::Group;
103 gnd_lights_root->setName( "Ground Lighting Root" );
105 vasi_lights_root = new osg::Group;
106 vasi_lights_root->setName( "VASI/PAPI Lighting Root" );
108 rwy_lights_root = new osg::Group;
109 rwy_lights_root->setName( "Runway Lighting Root" );
111 taxi_lights_root = new osg::Group;
112 taxi_lights_root->setName( "Taxi Lighting Root" );
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() );
120 void FGScenery::update(double dt) {
124 void FGScenery::bind() {
128 void FGScenery::unbind() {
131 void FGScenery::set_center( const SGVec3d& p ) {
135 placement_list_type::iterator it = _placement_list.begin();
136 while (it != _placement_list.end()) {
137 (*it)->setSceneryCenter(center);
142 void FGScenery::register_placement_transform(SGPlacementTransform *trans) {
143 _placement_list.push_back(trans);
144 trans->setSceneryCenter(center);
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);
158 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
159 double& alt, const SGMaterial** material,
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);
168 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
169 double& alt, const SGMaterial** material,
172 if ( norm1(pos) < 1 )
175 SGVec3d saved_center = center;
176 bool replaced_center = false;
178 if (30*30 < distSqr(pos, center)) {
180 replaced_center = true;
185 SGVec3d start = pos + max_altoff*normalize(pos) - center;
186 SGVec3d end = - center;
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();
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];
202 point.osg() = hit.getWorldIntersectPoint();
204 SGGeod geod = SGGeod::fromCart(point);
205 double elevation = geod.getElevationM();
206 if (alt < elevation) {
209 *material = globals->get_matlib()->findMaterial(hit.getGeode());
215 set_center( saved_center );
220 static const osgUtil::Hit*
221 getNearestHit(const osgUtil::IntersectVisitor::HitList& hitList,
222 const SGVec3d& start)
224 const osgUtil::Hit* nearestHit = 0;
225 double dist = SGLimitsd::max();
226 osgUtil::IntersectVisitor::HitList::const_iterator hit;
227 for (hit = hitList.begin(); hit != hitList.end(); ++hit) {
228 SGVec3d point(hit->getWorldIntersectPoint());
229 double newdist = length(start - point);
230 if (newdist < dist) {
240 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
241 SGVec3d& nearestHit, bool exact)
243 // We assume that starting positions in the center of the earth are invalid
244 if ( norm1(pos) < 1 )
247 // Well that 'exactness' is somehow problematic, but makes at least sure
248 // that we don't compute that with a cenery center at the other side of
250 SGVec3d saved_center = center;
251 bool replaced_center = false;
253 if (30*30 < distSqr(pos, center)) {
255 replaced_center = true;
259 // Make really sure the direction is normalized, is really cheap compared to
260 // computation of ground intersection.
261 SGVec3d start = pos - center;
262 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
264 osgUtil::IntersectVisitor intersectVisitor;
265 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
266 osg::ref_ptr<osg::LineSegment> lineSegment;
267 lineSegment = new osg::LineSegment(start.osg(), end.osg());
268 intersectVisitor.addLineSegment(lineSegment.get());
269 get_scene_graph()->accept(intersectVisitor);
270 bool hits = intersectVisitor.hits();
272 int nHits = intersectVisitor.getNumHits(lineSegment.get());
273 double dist = SGLimitsd::max();
274 for (int i = 0; i < nHits; ++i) {
275 const osgUtil::Hit& hit
276 = intersectVisitor.getHitList(lineSegment.get())[i];
278 point.osg() = hit.getWorldIntersectPoint();
279 double newdist = length(start - point);
280 if (newdist < dist) {
282 nearestHit = point + center;
288 set_center( saved_center );
294 FGScenery::pick(const SGVec3d& pos, const SGVec3d& dir,
295 std::vector<SGSceneryPick>& pickList)
299 // Make really sure the direction is normalized, is really cheap compared to
300 // computation of ground intersection.
301 SGVec3d start = pos - center;
302 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
304 osgUtil::IntersectVisitor intersectVisitor;
305 // osgUtil::PickVisitor intersectVisitor;
306 // intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
307 osg::ref_ptr<osg::LineSegment> lineSegment;
308 lineSegment = new osg::LineSegment(start.osg(), end.osg());
309 intersectVisitor.addLineSegment(lineSegment.get());
310 get_scene_graph()->accept(intersectVisitor);
311 if (!intersectVisitor.hits())
314 // collect all interaction callbacks on the pick ray.
315 // They get stored in the pickCallbacks list where they are sorted back
316 // to front and croasest to finest wrt the scenery node they are attached to
317 osgUtil::IntersectVisitor::HitList::const_iterator hi;
318 for (hi = intersectVisitor.getHitList(lineSegment.get()).begin();
319 hi != intersectVisitor.getHitList(lineSegment.get()).end();
322 // ok, go back the nodes and ask for intersection callbacks,
323 // execute them in top down order
324 const osg::NodePath& np = hi->getNodePath();
325 osg::NodePath::const_reverse_iterator npi;
326 for (npi = np.rbegin(); npi != np.rend(); ++npi) {
327 SGSceneUserData* ud = SGSceneUserData::getSceneUserData(*npi);
330 SGPickCallback* pickCallback = ud->getPickCallback();
334 SGSceneryPick sceneryPick;
335 sceneryPick.info.wgs84 = center + SGVec3d(hi->getWorldIntersectPoint());
336 sceneryPick.info.local = SGVec3d(hi->getLocalIntersectPoint());
337 sceneryPick.callback = pickCallback;
338 pickList.push_back(sceneryPick);