]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
26f49e4c932b5226ebe6dfe34e1a320f6eb1419c
[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     // Lighting
101     gnd_lights_root = new osg::Group;
102     gnd_lights_root->setName( "Ground Lighting Root" );
103
104     vasi_lights_root = new osg::Group;
105     vasi_lights_root->setName( "VASI/PAPI Lighting Root" );
106
107     rwy_lights_root = new osg::Group;
108     rwy_lights_root->setName( "Runway Lighting Root" );
109
110     taxi_lights_root = new osg::Group;
111     taxi_lights_root->setName( "Taxi Lighting Root" );
112
113     // Initials values needed by the draw-time object loader
114     sgUserDataInit( globals->get_model_lib(), globals->get_fg_root(),
115                     globals->get_props(), globals->get_sim_time_sec() );
116 }
117
118
119 void FGScenery::update(double dt) {
120 }
121
122
123 void FGScenery::bind() {
124 }
125
126
127 void FGScenery::unbind() {
128 }
129
130 bool
131 FGScenery::get_elevation_m(double lat, double lon, double max_alt,
132                            double& alt, const SGMaterial** material,
133                            bool exact)
134 {
135   SGGeod geod = SGGeod::fromDegM(lon, lat, max_alt);
136   SGVec3d pos = SGVec3d::fromGeod(geod);
137   return get_cart_elevation_m(pos, 0, alt, material, exact);
138 }
139
140 bool
141 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
142                                 double& alt, const SGMaterial** material,
143                                 bool exact)
144 {
145   if ( norm1(pos) < 1 )
146     return false;
147
148   SGVec3d start = pos + max_altoff*normalize(pos);
149   SGVec3d end(0, 0, 0);
150   
151   osgUtil::IntersectVisitor intersectVisitor;
152   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
153   osg::ref_ptr<osg::LineSegment> lineSegment;
154   lineSegment = new osg::LineSegment(start.osg(), end.osg());
155   intersectVisitor.addLineSegment(lineSegment.get());
156   get_scene_graph()->accept(intersectVisitor);
157   bool hits = intersectVisitor.hits();
158   if (hits) {
159     int nHits = intersectVisitor.getNumHits(lineSegment.get());
160     alt = -SGLimitsd::max();
161     for (int i = 0; i < nHits; ++i) {
162       const osgUtil::Hit& hit
163         = intersectVisitor.getHitList(lineSegment.get())[i];
164       SGVec3d point;
165       point.osg() = hit.getWorldIntersectPoint();
166       SGGeod geod = SGGeod::fromCart(point);
167       double elevation = geod.getElevationM();
168       if (alt < elevation) {
169         alt = elevation;
170         if (material)
171           *material = globals->get_matlib()->findMaterial(hit.getGeode());
172       }
173     }
174   }
175
176   return hits;
177 }
178
179 bool
180 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
181                                         SGVec3d& nearestHit, bool exact)
182 {
183   // We assume that starting positions in the center of the earth are invalid
184   if ( norm1(pos) < 1 )
185     return false;
186
187   // Make really sure the direction is normalized, is really cheap compared to
188   // computation of ground intersection.
189   SGVec3d start = pos;
190   SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
191   
192   osgUtil::IntersectVisitor intersectVisitor;
193   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
194   osg::ref_ptr<osg::LineSegment> lineSegment;
195   lineSegment = new osg::LineSegment(start.osg(), end.osg());
196   intersectVisitor.addLineSegment(lineSegment.get());
197   get_scene_graph()->accept(intersectVisitor);
198   bool hits = intersectVisitor.hits();
199   if (hits) {
200     int nHits = intersectVisitor.getNumHits(lineSegment.get());
201     double dist = SGLimitsd::max();
202     for (int i = 0; i < nHits; ++i) {
203       const osgUtil::Hit& hit
204         = intersectVisitor.getHitList(lineSegment.get())[i];
205       SGVec3d point;
206       point.osg() = hit.getWorldIntersectPoint();
207       double newdist = length(start - point);
208       if (newdist < dist) {
209         dist = newdist;
210         nearestHit = point;
211       }
212     }
213   }
214
215   return hits;
216 }