]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
e7d33aba8dc109ca2f8ee6cdb94b795da9a3a749
[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   return get_elevation_m(SGGeod::fromDegM(lon, lat, max_alt), alt, material);
122 }
123
124 bool
125 FGScenery::get_elevation_m(const SGGeod& geod,
126                            double& alt, const SGMaterial** material)
127 {
128   SGVec3d pos = SGVec3d::fromGeod(geod);
129   return get_cart_elevation_m(pos, 0, alt, material);
130 }
131
132 bool
133 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
134                                 double& alt, const SGMaterial** material)
135 {
136   if ( norm1(pos) < 1 )
137     return false;
138
139   SGVec3d start = pos + max_altoff*normalize(pos);
140   SGVec3d end(0, 0, 0);
141   
142   osgUtil::IntersectVisitor intersectVisitor;
143   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
144   osg::ref_ptr<osg::LineSegment> lineSegment;
145   lineSegment = new osg::LineSegment(start.osg(), end.osg());
146   intersectVisitor.addLineSegment(lineSegment.get());
147   get_scene_graph()->accept(intersectVisitor);
148   bool hits = intersectVisitor.hits();
149   if (hits) {
150     int nHits = intersectVisitor.getNumHits(lineSegment.get());
151     alt = -SGLimitsd::max();
152     for (int i = 0; i < nHits; ++i) {
153       const osgUtil::Hit& hit
154         = intersectVisitor.getHitList(lineSegment.get())[i];
155       SGVec3d point;
156       point.osg() = hit.getWorldIntersectPoint();
157       SGGeod geod = SGGeod::fromCart(point);
158       double elevation = geod.getElevationM();
159       if (alt < elevation) {
160         alt = elevation;
161         if (material) {
162           const osg::StateSet* stateSet = hit.getDrawable()->getStateSet();
163           *material = globals->get_matlib()->findMaterial(stateSet);
164         }
165       }
166     }
167   }
168
169   return hits;
170 }
171
172 bool
173 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
174                                         SGVec3d& nearestHit)
175 {
176   // We assume that starting positions in the center of the earth are invalid
177   if ( norm1(pos) < 1 )
178     return false;
179
180   // Make really sure the direction is normalized, is really cheap compared to
181   // computation of ground intersection.
182   SGVec3d start = pos;
183   SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
184   
185   osgUtil::IntersectVisitor intersectVisitor;
186   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
187   osg::ref_ptr<osg::LineSegment> lineSegment;
188   lineSegment = new osg::LineSegment(start.osg(), end.osg());
189   intersectVisitor.addLineSegment(lineSegment.get());
190   get_scene_graph()->accept(intersectVisitor);
191   bool hits = intersectVisitor.hits();
192   if (hits) {
193     int nHits = intersectVisitor.getNumHits(lineSegment.get());
194     double dist = SGLimitsd::max();
195     for (int i = 0; i < nHits; ++i) {
196       const osgUtil::Hit& hit
197         = intersectVisitor.getHitList(lineSegment.get())[i];
198       SGVec3d point;
199       point.osg() = hit.getWorldIntersectPoint();
200       double newdist = length(start - point);
201       if (newdist < dist) {
202         dist = newdist;
203         nearestHit = point;
204       }
205     }
206   }
207
208   return hits;
209 }