]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
93f5c2f7ecd254d41c1551d53f9fa285dc401ab8
[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 using namespace flightgear;
46
47 class FGGroundPickCallback : public SGPickCallback {
48 public:
49   virtual bool buttonPressed(int button, const Info& info)
50   {
51     // only on left mouse button
52     if (button != 0)
53       return false;
54
55     SGGeod geod = SGGeod::fromCart(info.wgs84);
56     SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
57
58     SGPropertyNode *c = fgGetNode("/sim/input/click", true);
59     c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
60     c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
61     c->setDoubleValue("elevation-m", geod.getElevationM());
62     c->setDoubleValue("elevation-ft", geod.getElevationFt());
63     fgSetBool("/sim/signals/click", 1);
64
65     return true;
66   }
67 };
68
69 // Scenery Management system
70 FGScenery::FGScenery()
71 {
72     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
73 }
74
75
76 // Initialize the Scenery Management system
77 FGScenery::~FGScenery() {
78 }
79
80
81 void FGScenery::init() {
82     // Scene graph root
83     scene_graph = new osg::Group;
84     scene_graph->setName( "Scene" );
85
86     // Terrain branch
87     terrain_branch = new osg::Group;
88     terrain_branch->setName( "Terrain" );
89     scene_graph->addChild( terrain_branch.get() );
90     SGSceneUserData* userData;
91     userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
92     userData->setPickCallback(new FGGroundPickCallback);
93
94     models_branch = new osg::Group;
95     models_branch->setName( "Models" );
96     scene_graph->addChild( models_branch.get() );
97
98     aircraft_branch = new osg::Group;
99     aircraft_branch->setName( "Aircraft" );
100     scene_graph->addChild( aircraft_branch.get() );
101
102     // Initials values needed by the draw-time object loader
103     sgUserDataInit( globals->get_model_lib(), globals->get_fg_root(),
104                     globals->get_props(), globals->get_sim_time_sec() );
105 }
106
107
108 void FGScenery::update(double dt) {
109 }
110
111
112 void FGScenery::bind() {
113 }
114
115
116 void FGScenery::unbind() {
117 }
118
119 bool
120 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
121                            double& alt, const SGMaterial** material)
122 {
123   return get_elevation_m(SGGeod::fromDegM(lon, lat, max_alt), 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   SGGeod geod = SGGeod::fromCart(pos);
131   geod.setElevationM(geod.getElevationM() + max_altoff);
132   return get_elevation_m(geod, alt, material);
133 }
134
135 bool
136 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
137                            const SGMaterial** material)
138 {
139   SGVec3d start = SGVec3d::fromGeod(geod);
140
141   SGGeod geodEnd = geod;
142   geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
143   SGVec3d end = SGVec3d::fromGeod(geodEnd);
144   
145   osgUtil::IntersectVisitor intersectVisitor;
146   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
147   osg::ref_ptr<osg::LineSegment> lineSegment;
148   lineSegment = new osg::LineSegment(start.osg(), end.osg());
149   intersectVisitor.addLineSegment(lineSegment.get());
150   get_scene_graph()->accept(intersectVisitor);
151   bool hits = intersectVisitor.hits();
152   if (hits) {
153     int nHits = intersectVisitor.getNumHits(lineSegment.get());
154     alt = -SGLimitsd::max();
155     for (int i = 0; i < nHits; ++i) {
156       const osgUtil::Hit& hit
157         = intersectVisitor.getHitList(lineSegment.get())[i];
158       SGVec3d point;
159       point.osg() = hit.getWorldIntersectPoint();
160       SGGeod geod = SGGeod::fromCart(point);
161       double elevation = geod.getElevationM();
162       if (alt < elevation) {
163         alt = elevation;
164         if (material) {
165           const osg::StateSet* stateSet = hit.getDrawable()->getStateSet();
166           *material = globals->get_matlib()->findMaterial(stateSet);
167         }
168       }
169     }
170   }
171
172   return hits;
173 }
174
175 bool
176 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
177                                         SGVec3d& nearestHit)
178 {
179   // We assume that starting positions in the center of the earth are invalid
180   if ( norm1(pos) < 1 )
181     return false;
182
183   // Make really sure the direction is normalized, is really cheap compared to
184   // computation of ground intersection.
185   SGVec3d start = pos;
186   SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
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     double dist = 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       double newdist = length(start - point);
204       if (newdist < dist) {
205         dist = newdist;
206         nearestHit = point;
207       }
208     }
209   }
210
211   return hits;
212 }
213
214 SceneryPager* FGScenery::getPagerSingleton()
215 {
216     static osg::ref_ptr<SceneryPager> pager = new SceneryPager;
217     return pager.get();
218 }