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 <osgViewer/Viewer>
32 #include <osgUtil/IntersectVisitor>
34 #include <simgear/constants.h>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/scene/tgdb/userdata.hxx>
37 #include <simgear/scene/material/matlib.hxx>
38 #include <simgear/scene/util/SGNodeMasks.hxx>
39 #include <simgear/scene/util/SGSceneUserData.hxx>
40 #include <simgear/scene/model/CheckSceneryVisitor.hxx>
42 #include <Main/renderer.hxx>
43 #include <Main/fg_props.hxx>
45 #include "tilemgr.hxx"
46 #include "scenery.hxx"
48 using namespace flightgear;
49 using namespace simgear;
51 class FGGroundPickCallback : public SGPickCallback {
53 virtual bool buttonPressed(int button, const Info& info)
55 // only on left mouse button
59 SGGeod geod = SGGeod::fromCart(info.wgs84);
60 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
62 SGPropertyNode *c = fgGetNode("/sim/input/click", true);
63 c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
64 c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
65 c->setDoubleValue("elevation-m", geod.getElevationM());
66 c->setDoubleValue("elevation-ft", geod.getElevationFt());
67 fgSetBool("/sim/signals/click", 1);
73 // Scenery Management system
74 FGScenery::FGScenery()
76 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
79 FGScenery::~FGScenery() {
83 // Initialize the Scenery Management system
84 void FGScenery::init() {
86 scene_graph = new osg::Group;
87 scene_graph->setName( "Scene" );
90 terrain_branch = new osg::Group;
91 terrain_branch->setName( "Terrain" );
92 scene_graph->addChild( terrain_branch.get() );
93 SGSceneUserData* userData;
94 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
95 userData->setPickCallback(new FGGroundPickCallback);
97 models_branch = new osg::Group;
98 models_branch->setName( "Models" );
99 scene_graph->addChild( models_branch.get() );
101 aircraft_branch = new osg::Group;
102 aircraft_branch->setName( "Aircraft" );
103 scene_graph->addChild( aircraft_branch.get() );
105 // Initials values needed by the draw-time object loader
106 sgUserDataInit( globals->get_props() );
110 void FGScenery::update(double dt) {
114 void FGScenery::bind() {
118 void FGScenery::unbind() {
122 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
123 double& alt, const SGMaterial** material,
124 const osg::Node* butNotFrom)
126 SGGeod geod = SGGeod::fromCart(pos);
127 geod.setElevationM(geod.getElevationM() + max_altoff);
128 return get_elevation_m(geod, alt, material, butNotFrom);
132 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
133 const SGMaterial** material,
134 const osg::Node* butNotFrom)
136 SGVec3d start = SGVec3d::fromGeod(geod);
138 SGGeod geodEnd = geod;
139 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
140 SGVec3d end = SGVec3d::fromGeod(geodEnd);
142 osgUtil::IntersectVisitor intersectVisitor;
143 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
144 osg::ref_ptr<osg::LineSegment> lineSegment;
145 lineSegment = new osg::LineSegment(toOsg(start), toOsg(end));
146 intersectVisitor.addLineSegment(lineSegment.get());
147 get_scene_graph()->accept(intersectVisitor);
149 if (intersectVisitor.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];
156 std::find(hit.getNodePath().begin(), hit.getNodePath().end(),
157 butNotFrom) != hit.getNodePath().end())
160 // We might need the double variant of the intersection point.
161 // Thus we cannot use the float variant delivered by
162 // hit.getWorldIntersectPoint() but we have to redo that with osg::Vec3d.
163 osg::Vec3d point = hit.getLocalIntersectPoint();
165 point = point*(*hit.getMatrix());
166 SGGeod geod = SGGeod::fromCart(toSG(point));
167 double elevation = geod.getElevationM();
168 if (alt < elevation) {
172 *material = SGMaterialLib::findMaterial(hit.getGeode());
181 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
183 const osg::Node* butNotFrom)
185 // We assume that starting positions in the center of the earth are invalid
186 if ( norm1(pos) < 1 )
189 // Make really sure the direction is normalized, is really cheap compared to
190 // computation of ground intersection.
192 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
194 osgUtil::IntersectVisitor intersectVisitor;
195 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
196 osg::ref_ptr<osg::LineSegment> lineSegment;
197 lineSegment = new osg::LineSegment(toOsg(start), toOsg(end));
198 intersectVisitor.addLineSegment(lineSegment.get());
199 get_scene_graph()->accept(intersectVisitor);
201 if (intersectVisitor.hits()) {
202 int nHits = intersectVisitor.getNumHits(lineSegment.get());
203 double dist = SGLimitsd::max();
204 for (int i = 0; i < nHits; ++i) {
205 const osgUtil::Hit& hit
206 = intersectVisitor.getHitList(lineSegment.get())[i];
208 std::find(hit.getNodePath().begin(), hit.getNodePath().end(),
209 butNotFrom) != hit.getNodePath().end())
211 // We might need the double variant of the intersection point.
212 // Thus we cannot use the float variant delivered by
213 // hit.getWorldIntersectPoint() but we have to redo that with osg::Vec3d.
214 osg::Vec3d point = hit.getLocalIntersectPoint();
216 point = point*(*hit.getMatrix());
217 double newdist = length(start - toSG(point));
218 if (newdist < dist) {
220 nearestHit = toSG(point);
229 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
231 if(globals->get_tile_mgr()->schedule_scenery(position, range_m, 0.0))
234 if (!get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0, 0))
236 SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
237 osg::FrameStamp* framestamp
238 = globals->get_renderer()->getViewer()->getFrameStamp();
239 simgear::CheckSceneryVisitor csnv(getPagerSingleton(), toOsg(p), range_m, framestamp);
240 // currently the PagedLODs will not be loaded by the DatabasePager
241 // while the splashscreen is there, so CheckSceneryVisitor force-loads
242 // missing objects in the main thread
243 get_scene_graph()->accept(csnv);
251 SceneryPager* FGScenery::getPagerSingleton()
253 static osg::ref_ptr<SceneryPager> pager = new SceneryPager;