]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
- Added ultra-light traffic is now a separate traffic class that can have its
[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 {
70     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
71 }
72
73
74 // Initialize the Scenery Management system
75 FGScenery::~FGScenery() {
76 }
77
78
79 void FGScenery::init() {
80     // Scene graph root
81     scene_graph = new osg::Group;
82     scene_graph->setName( "Scene" );
83
84     // Terrain branch
85     terrain_branch = new osg::Group;
86     terrain_branch->setName( "Terrain" );
87     scene_graph->addChild( terrain_branch.get() );
88     SGSceneUserData* userData;
89     userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
90     userData->setPickCallback(new FGGroundPickCallback);
91
92     models_branch = new osg::Group;
93     models_branch->setName( "Models" );
94     scene_graph->addChild( models_branch.get() );
95
96     aircraft_branch = new osg::Group;
97     aircraft_branch->setName( "Aircraft" );
98     scene_graph->addChild( aircraft_branch.get() );
99
100     // Initials values needed by the draw-time object loader
101     sgUserDataInit( globals->get_model_lib(), globals->get_fg_root(),
102                     globals->get_props(), globals->get_sim_time_sec() );
103 }
104
105
106 void FGScenery::update(double dt) {
107 }
108
109
110 void FGScenery::bind() {
111 }
112
113
114 void FGScenery::unbind() {
115 }
116
117 bool
118 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
119                            double& alt, const SGMaterial** material)
120 {
121   SGGeod geod = SGGeod::fromDegM(lon, lat, max_alt);
122   SGVec3d pos = SGVec3d::fromGeod(geod);
123   return get_cart_elevation_m(pos, 0, alt, material);
124 }
125
126 bool
127 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
128                                 double& alt, const SGMaterial** material)
129 {
130   if ( norm1(pos) < 1 )
131     return false;
132
133   SGVec3d start = pos + max_altoff*normalize(pos);
134   SGVec3d end(0, 0, 0);
135   
136   osgUtil::IntersectVisitor intersectVisitor;
137   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
138   osg::ref_ptr<osg::LineSegment> lineSegment;
139   lineSegment = new osg::LineSegment(start.osg(), end.osg());
140   intersectVisitor.addLineSegment(lineSegment.get());
141   get_scene_graph()->accept(intersectVisitor);
142   bool hits = intersectVisitor.hits();
143   if (hits) {
144     int nHits = intersectVisitor.getNumHits(lineSegment.get());
145     alt = -SGLimitsd::max();
146     for (int i = 0; i < nHits; ++i) {
147       const osgUtil::Hit& hit
148         = intersectVisitor.getHitList(lineSegment.get())[i];
149       SGVec3d point;
150       point.osg() = hit.getWorldIntersectPoint();
151       SGGeod geod = SGGeod::fromCart(point);
152       double elevation = geod.getElevationM();
153       if (alt < elevation) {
154         alt = elevation;
155         if (material) {
156           const osg::StateSet* stateSet = hit.getDrawable()->getStateSet();
157           *material = globals->get_matlib()->findMaterial(stateSet);
158         }
159       }
160     }
161   }
162
163   return hits;
164 }
165
166 bool
167 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
168                                         SGVec3d& nearestHit)
169 {
170   // We assume that starting positions in the center of the earth are invalid
171   if ( norm1(pos) < 1 )
172     return false;
173
174   // Make really sure the direction is normalized, is really cheap compared to
175   // computation of ground intersection.
176   SGVec3d start = pos;
177   SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
178   
179   osgUtil::IntersectVisitor intersectVisitor;
180   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
181   osg::ref_ptr<osg::LineSegment> lineSegment;
182   lineSegment = new osg::LineSegment(start.osg(), end.osg());
183   intersectVisitor.addLineSegment(lineSegment.get());
184   get_scene_graph()->accept(intersectVisitor);
185   bool hits = intersectVisitor.hits();
186   if (hits) {
187     int nHits = intersectVisitor.getNumHits(lineSegment.get());
188     double dist = SGLimitsd::max();
189     for (int i = 0; i < nHits; ++i) {
190       const osgUtil::Hit& hit
191         = intersectVisitor.getHitList(lineSegment.get())[i];
192       SGVec3d point;
193       point.osg() = hit.getWorldIntersectPoint();
194       double newdist = length(start - point);
195       if (newdist < dist) {
196         dist = newdist;
197         nearestHit = point;
198       }
199     }
200   }
201
202   return hits;
203 }