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 <osgUtil/IntersectVisitor>
33 #include <simgear/constants.h>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/scene/tgdb/userdata.hxx>
36 #include <simgear/scene/material/Effect.hxx>
37 #include <simgear/scene/material/EffectGeode.hxx>
38 #include <simgear/scene/material/Technique.hxx>
39 #include <simgear/scene/material/matlib.hxx>
40 #include <simgear/scene/util/SGNodeMasks.hxx>
41 #include <simgear/scene/util/SGSceneUserData.hxx>
42 #include <simgear/scene/model/CheckSceneryVisitor.hxx>
44 #include <Main/fg_props.hxx>
46 #include "tilemgr.hxx"
47 #include "scenery.hxx"
49 using namespace flightgear;
50 using namespace simgear;
52 class FGGroundPickCallback : public SGPickCallback {
54 virtual bool buttonPressed(int button, const Info& info)
56 // only on left mouse button
60 SGGeod geod = SGGeod::fromCart(info.wgs84);
61 SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
63 SGPropertyNode *c = fgGetNode("/sim/input/click", true);
64 c->setDoubleValue("longitude-deg", geod.getLongitudeDeg());
65 c->setDoubleValue("latitude-deg", geod.getLatitudeDeg());
66 c->setDoubleValue("elevation-m", geod.getElevationM());
67 c->setDoubleValue("elevation-ft", geod.getElevationFt());
68 fgSetBool("/sim/signals/click", 1);
74 // Scenery Management system
75 FGScenery::FGScenery()
77 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
80 FGScenery::~FGScenery() {
84 // Initialize the Scenery Management system
85 void FGScenery::init() {
87 scene_graph = new osg::Group;
88 scene_graph->setName( "Scene" );
91 terrain_branch = new osg::Group;
92 terrain_branch->setName( "Terrain" );
93 scene_graph->addChild( terrain_branch.get() );
94 SGSceneUserData* userData;
95 userData = SGSceneUserData::getOrCreateSceneUserData(terrain_branch.get());
96 userData->setPickCallback(new FGGroundPickCallback);
98 models_branch = new osg::Group;
99 models_branch->setName( "Models" );
100 scene_graph->addChild( models_branch.get() );
102 aircraft_branch = new osg::Group;
103 aircraft_branch->setName( "Aircraft" );
104 scene_graph->addChild( aircraft_branch.get() );
106 // Initials values needed by the draw-time object loader
107 sgUserDataInit( globals->get_props() );
111 void FGScenery::update(double dt) {
115 void FGScenery::bind() {
119 void FGScenery::unbind() {
123 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
124 double& alt, const SGMaterial** material)
126 SGGeod geod = SGGeod::fromCart(pos);
127 geod.setElevationM(geod.getElevationM() + max_altoff);
128 return get_elevation_m(geod, alt, material);
132 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
133 const SGMaterial** material)
135 SGVec3d start = SGVec3d::fromGeod(geod);
137 SGGeod geodEnd = geod;
138 geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
139 SGVec3d end = SGVec3d::fromGeod(geodEnd);
141 osgUtil::IntersectVisitor intersectVisitor;
142 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
143 osg::ref_ptr<osg::LineSegment> lineSegment;
144 lineSegment = new osg::LineSegment(start.osg(), end.osg());
145 intersectVisitor.addLineSegment(lineSegment.get());
146 get_scene_graph()->accept(intersectVisitor);
147 bool hits = intersectVisitor.hits();
149 int nHits = intersectVisitor.getNumHits(lineSegment.get());
150 alt = -SGLimitsd::max();
151 for (int i = 0; i < nHits; ++i) {
152 const osgUtil::Hit& hit
153 = intersectVisitor.getHitList(lineSegment.get())[i];
155 point.osg() = hit.getWorldIntersectPoint();
156 SGGeod geod = SGGeod::fromCart(point);
157 double elevation = geod.getElevationM();
158 if (alt < elevation) {
162 const EffectGeode* eg
163 = dynamic_cast<const EffectGeode*>(hit.getGeode());
165 *material = SGMaterialLib::findMaterial(eg->getEffect());
175 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
178 // We assume that starting positions in the center of the earth are invalid
179 if ( norm1(pos) < 1 )
182 // Make really sure the direction is normalized, is really cheap compared to
183 // computation of ground intersection.
185 SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
187 osgUtil::IntersectVisitor intersectVisitor;
188 intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
189 osg::ref_ptr<osg::LineSegment> lineSegment;
190 lineSegment = new osg::LineSegment(start.osg(), end.osg());
191 intersectVisitor.addLineSegment(lineSegment.get());
192 get_scene_graph()->accept(intersectVisitor);
193 bool hits = intersectVisitor.hits();
195 int nHits = intersectVisitor.getNumHits(lineSegment.get());
196 double dist = SGLimitsd::max();
197 for (int i = 0; i < nHits; ++i) {
198 const osgUtil::Hit& hit
199 = intersectVisitor.getHitList(lineSegment.get())[i];
201 point.osg() = hit.getWorldIntersectPoint();
202 double newdist = length(start - point);
203 if (newdist < dist) {
213 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
215 if(globals->get_tile_mgr()->scenery_available(position, range_m))
218 get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0);
219 SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
220 simgear::CheckSceneryVisitor csnv(getPagerSingleton(), p.osg(), range_m);
221 // currently the PagedLODs will not be loaded by the DatabasePager
222 // while the splashscreen is there, so CheckSceneryVisitor force-loads
223 // missing objects in the main thread
224 get_scene_graph()->accept(csnv);
232 SceneryPager* FGScenery::getPagerSingleton()
234 static osg::ref_ptr<SceneryPager> pager = new SceneryPager;
238 // Effect initialization stuff
240 class PropertyExpression : public SGExpression<bool>
243 PropertyExpression(SGPropertyNode* pnode) : _pnode(pnode) {}
245 void eval(bool& value, const expression::Binding*) const
247 value = _pnode->getValue<bool>();
250 SGPropertyNode_ptr _pnode;
253 class EffectPropertyListener : public SGPropertyChangeListener
256 EffectPropertyListener(Technique* tniq) : _tniq(tniq) {}
258 void valueChanged(SGPropertyNode* node)
260 _tniq->refreshValidity();
263 osg::ref_ptr<Technique> _tniq;
266 Expression* propertyExpressionParser(const SGPropertyNode* exp,
267 expression::Parser* parser)
269 SGPropertyNode_ptr pnode = fgGetNode(exp->getStringValue(), true);
270 PropertyExpression* pexp = new PropertyExpression(pnode);
271 TechniquePredParser* predParser
272 = dynamic_cast<TechniquePredParser*>(parser);
274 pnode->addChangeListener(new EffectPropertyListener(predParser
279 expression::ExpParserRegistrar propertyRegistrar("property",
280 propertyExpressionParser);