]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/scenery.cxx
Fix the temperature computation.
[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/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>
43
44 #include <Main/fg_props.hxx>
45
46 #include "tilemgr.hxx"
47 #include "scenery.hxx"
48
49 using namespace flightgear;
50 using namespace simgear;
51
52 class FGGroundPickCallback : public SGPickCallback {
53 public:
54   virtual bool buttonPressed(int button, const Info& info)
55   {
56     // only on left mouse button
57     if (button != 0)
58       return false;
59
60     SGGeod geod = SGGeod::fromCart(info.wgs84);
61     SG_LOG( SG_TERRAIN, SG_INFO, "Got ground pick at " << geod );
62
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);
69
70     return true;
71   }
72 };
73
74 // Scenery Management system
75 FGScenery::FGScenery()
76 {
77     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing scenery subsystem" );
78 }
79
80 FGScenery::~FGScenery() {
81 }
82
83
84 // Initialize the Scenery Management system
85 void FGScenery::init() {
86     // Scene graph root
87     scene_graph = new osg::Group;
88     scene_graph->setName( "Scene" );
89
90     // Terrain branch
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);
97
98     models_branch = new osg::Group;
99     models_branch->setName( "Models" );
100     scene_graph->addChild( models_branch.get() );
101
102     aircraft_branch = new osg::Group;
103     aircraft_branch->setName( "Aircraft" );
104     scene_graph->addChild( aircraft_branch.get() );
105
106     // Initials values needed by the draw-time object loader
107     sgUserDataInit( globals->get_props() );
108 }
109
110
111 void FGScenery::update(double dt) {
112 }
113
114
115 void FGScenery::bind() {
116 }
117
118
119 void FGScenery::unbind() {
120 }
121
122 bool
123 FGScenery::get_cart_elevation_m(const SGVec3d& pos, double max_altoff,
124                                 double& alt, const SGMaterial** material,
125                                 const osg::Node* butNotFrom)
126 {
127   SGGeod geod = SGGeod::fromCart(pos);
128   geod.setElevationM(geod.getElevationM() + max_altoff);
129   return get_elevation_m(geod, alt, material, butNotFrom);
130 }
131
132 bool
133 FGScenery::get_elevation_m(const SGGeod& geod, double& alt,
134                            const SGMaterial** material,
135                            const osg::Node* butNotFrom)
136 {
137   SGVec3d start = SGVec3d::fromGeod(geod);
138
139   SGGeod geodEnd = geod;
140   geodEnd.setElevationM(SGMiscd::min(geod.getElevationM() - 10, -10000));
141   SGVec3d end = SGVec3d::fromGeod(geodEnd);
142   
143   osgUtil::IntersectVisitor intersectVisitor;
144   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
145   osg::ref_ptr<osg::LineSegment> lineSegment;
146   lineSegment = new osg::LineSegment(toOsg(start), toOsg(end));
147   intersectVisitor.addLineSegment(lineSegment.get());
148   get_scene_graph()->accept(intersectVisitor);
149   bool hits = false;
150   if (intersectVisitor.hits()) {
151     int nHits = intersectVisitor.getNumHits(lineSegment.get());
152     alt = -SGLimitsd::max();
153     for (int i = 0; i < nHits; ++i) {
154       const osgUtil::Hit& hit
155         = intersectVisitor.getHitList(lineSegment.get())[i];
156       if (butNotFrom &&
157           std::find(hit.getNodePath().begin(), hit.getNodePath().end(),
158                     butNotFrom) != hit.getNodePath().end())
159           continue;
160
161       // We might need the double variant of the intersection point.
162       // Thus we cannot use the float variant delivered by
163       // hit.getWorldIntersectPoint() but we have to redo that with osg::Vec3d.
164       osg::Vec3d point = hit.getLocalIntersectPoint();
165       if (hit.getMatrix())
166         point = point*(*hit.getMatrix());
167       SGGeod geod = SGGeod::fromCart(toSG(point));
168       double elevation = geod.getElevationM();
169       if (alt < elevation) {
170         alt = elevation;
171         hits = true;
172         if (material)
173           *material = SGMaterialLib::findMaterial(hit.getGeode());
174       }
175     }
176   }
177
178   return hits;
179 }
180
181 bool
182 FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
183                                         SGVec3d& nearestHit,
184                                         const osg::Node* butNotFrom)
185 {
186   // We assume that starting positions in the center of the earth are invalid
187   if ( norm1(pos) < 1 )
188     return false;
189
190   // Make really sure the direction is normalized, is really cheap compared to
191   // computation of ground intersection.
192   SGVec3d start = pos;
193   SGVec3d end = start + 1e5*normalize(dir); // FIXME visibility ???
194   
195   osgUtil::IntersectVisitor intersectVisitor;
196   intersectVisitor.setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
197   osg::ref_ptr<osg::LineSegment> lineSegment;
198   lineSegment = new osg::LineSegment(toOsg(start), toOsg(end));
199   intersectVisitor.addLineSegment(lineSegment.get());
200   get_scene_graph()->accept(intersectVisitor);
201   bool hits = false;
202   if (intersectVisitor.hits()) {
203     int nHits = intersectVisitor.getNumHits(lineSegment.get());
204     double dist = SGLimitsd::max();
205     for (int i = 0; i < nHits; ++i) {
206       const osgUtil::Hit& hit
207         = intersectVisitor.getHitList(lineSegment.get())[i];
208       if (butNotFrom &&
209           std::find(hit.getNodePath().begin(), hit.getNodePath().end(),
210                     butNotFrom) != hit.getNodePath().end())
211           continue;
212       // We might need the double variant of the intersection point.
213       // Thus we cannot use the float variant delivered by
214       // hit.getWorldIntersectPoint() but we have to redo that with osg::Vec3d.
215       osg::Vec3d point = hit.getLocalIntersectPoint();
216       if (hit.getMatrix())
217         point = point*(*hit.getMatrix());
218       double newdist = length(start - toSG(point));
219       if (newdist < dist) {
220         dist = newdist;
221         nearestHit = toSG(point);
222         hits = true;
223       }
224     }
225   }
226
227   return hits;
228 }
229
230 bool FGScenery::scenery_available(const SGGeod& position, double range_m)
231 {
232   if(globals->get_tile_mgr()->scenery_available(position, range_m))
233   {
234     double elev;
235     if (!get_elevation_m(SGGeod::fromGeodM(position, SG_MAX_ELEVATION_M), elev, 0, 0))
236       return false;
237     SGVec3f p = SGVec3f::fromGeod(SGGeod::fromGeodM(position, elev));
238     simgear::CheckSceneryVisitor csnv(getPagerSingleton(), toOsg(p), range_m);
239     // currently the PagedLODs will not be loaded by the DatabasePager
240     // while the splashscreen is there, so CheckSceneryVisitor force-loads
241     // missing objects in the main thread
242     get_scene_graph()->accept(csnv);
243     if(!csnv.isLoaded())
244         return false;
245     return true;
246   }
247   return false;
248 }
249
250 SceneryPager* FGScenery::getPagerSingleton()
251 {
252     static osg::ref_ptr<SceneryPager> pager = new SceneryPager;
253     return pager.get();
254 }
255
256 // Effect initialization stuff
257
258 class PropertyExpression : public SGExpression<bool>
259 {
260 public:
261     PropertyExpression(SGPropertyNode* pnode) : _pnode(pnode) {}
262     
263     void eval(bool& value, const expression::Binding*) const
264     {
265         value = _pnode->getValue<bool>();
266     }
267 protected:
268     SGPropertyNode_ptr _pnode;
269 };
270
271 class EffectPropertyListener : public SGPropertyChangeListener
272 {
273 public:
274     EffectPropertyListener(Technique* tniq) : _tniq(tniq) {}
275     
276     void valueChanged(SGPropertyNode* node)
277     {
278         _tniq->refreshValidity();
279     }
280 protected:
281     osg::ref_ptr<Technique> _tniq;
282 };
283
284 Expression* propertyExpressionParser(const SGPropertyNode* exp,
285                                      expression::Parser* parser)
286 {
287     SGPropertyNode_ptr pnode = fgGetNode(exp->getStringValue(), true);
288     PropertyExpression* pexp = new PropertyExpression(pnode);
289     TechniquePredParser* predParser
290         = dynamic_cast<TechniquePredParser*>(parser);
291     if (predParser)
292         pnode->addChangeListener(new EffectPropertyListener(predParser
293                                                             ->getTechnique()));
294     return pexp;
295 }
296
297 expression::ExpParserRegistrar propertyRegistrar("property",
298                                                  propertyExpressionParser);
299