]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/groundradar.cxx
#346 related: missing status message for property server
[flightgear.git] / src / Instrumentation / groundradar.cxx
1 //  groundradar.cxx - Background layer for the ATC radar.
2 //
3 //  Copyright (C) 2007 Csaba Halasz.
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License as
7 //  published by the Free Software Foundation; either version 2 of the
8 //  License, or (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful, but
11 //  WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //  General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #endif
22
23 #include <cassert>
24
25 #include <osg/Node>
26 #include <osg/Geode>
27 #include <osg/Geometry>
28 #include <osg/Camera>
29 #include <osg/Texture2D>
30 #include <osgViewer/Viewer>
31
32 #include <osgText/Text>
33 #include <osgDB/Registry>
34 #include <osgDB/ReaderWriter>
35 #include <osgUtil/Tessellator>
36
37 #include <Main/fg_props.hxx>
38 #include <Main/globals.hxx>
39 #include <Main/renderer.hxx>
40 #include <Cockpit/panel.hxx>
41 #include <Airports/simple.hxx>
42 #include <Airports/runways.hxx>
43 #include <Airports/pavement.hxx>
44 #include <simgear/math/sg_geodesy.hxx>
45 #include <simgear/math/beziercurve.hxx>
46
47 #include "groundradar.hxx"
48
49 static const char* airport_source_node_name = "airport-id-source";
50 static const char* default_airport_node_name = "/sim/airport/closest-airport-id";
51 static const char* texture_node_name = "texture-name";
52 static const char* default_texture_name = "Aircraft/Instruments/Textures/od_groundradar.rgb";
53 static const char* range_source_node_name = "range-source";
54 static const char* default_range_node_name = "/instrumentation/radar/range";
55
56 struct SingleFrameCallback : public osg::Camera::DrawCallback
57 {
58     virtual void operator () (const osg::Camera& camera) const
59     {
60         const_cast<osg::Camera&>(camera).setNodeMask(0);
61     }
62 };
63
64 GroundRadar::GroundRadar(SGPropertyNode *node)
65 {
66     _airport_node = fgGetNode(node->getStringValue(airport_source_node_name, default_airport_node_name), true);
67     _range_node = fgGetNode(node->getStringValue(range_source_node_name, default_range_node_name), true);
68     createTexture(node->getStringValue(texture_node_name, default_texture_name));
69     updateTexture();
70     _airport_node->addChangeListener(this);
71     _range_node->addChangeListener(this);
72 }
73
74 GroundRadar::~GroundRadar()
75 {
76     _airport_node->removeChangeListener(this);
77     _range_node->removeChangeListener(this);
78 }
79
80 void GroundRadar::valueChanged(SGPropertyNode*)
81 {
82     updateTexture();
83 }
84
85 inline static osg::Vec3 fromPolar(double fi, double r)
86 {
87     return osg::Vec3(sin(fi * SGD_DEGREES_TO_RADIANS) * r, cos(fi * SGD_DEGREES_TO_RADIANS) * r, 0);
88 }
89
90 void GroundRadar::createTexture(const char* texture_name)
91 {
92     setSize(TextureHalfSize + TextureHalfSize);
93     allocRT();
94
95     _geode = new osg::Geode();
96     osg::StateSet* stateset = _geode->getOrCreateStateSet();
97     stateset->setMode(GL_BLEND, osg::StateAttribute::OFF);
98
99     osg::Vec4Array* taxi_color = new osg::Vec4Array;
100     taxi_color->push_back(osg::Vec4(0.0f, 0.5f, 0.0f, 1.0f));
101     osg::Vec4Array* rwy_color = new osg::Vec4Array;
102     rwy_color->push_back(osg::Vec4(0.0f, 0.5f, 0.5f, 1.0f));
103
104     osg::Geometry *taxi_geom = new osg::Geometry();
105     taxi_geom->setColorArray(taxi_color);
106     taxi_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
107     taxi_geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 0)); // Taxiways
108     _geode->addDrawable(taxi_geom);
109
110     osg::Geometry *pvt_geom = new osg::Geometry();
111     pvt_geom->setColorArray(taxi_color);
112     pvt_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
113     // no primitive set for the moment. It needs tessellation
114     _geode->addDrawable(pvt_geom);
115
116     osg::Geometry *rwy_geom = new osg::Geometry();
117     rwy_geom->setColorArray(rwy_color);
118     rwy_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
119     rwy_geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 0)); // Runways
120     _geode->addDrawable(rwy_geom);
121
122     osg::Camera* camera = getCamera();
123     camera->setPostDrawCallback(new SingleFrameCallback());
124     camera->addChild(_geode.get());
125     camera->setNodeMask(0);
126     camera->setProjectionMatrixAsOrtho2D(0, getTexture()->getTextureWidth(), 0, getTexture()->getTextureHeight());
127
128     // Texture in the 2D panel system
129     FGTextureManager::addTexture(texture_name, getTexture());
130 }
131
132 void GroundRadar::addRunwayVertices(const FGRunwayBase* aRunway, double aTowerLat, double aTowerLon, double aScale, osg::Vec3Array* aVertices)
133 {
134   double az1, az2, dist_m;
135   geo_inverse_wgs_84(aTowerLat, aTowerLon, aRunway->latitude(), aRunway->longitude(), &az1, &az2, &dist_m);
136
137   osg::Vec3 center = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
138   osg::Vec3 leftcenter = fromPolar(aRunway->headingDeg(), aRunway->lengthM() * aScale / 2) + center;
139   osg::Vec3 lefttop = fromPolar(aRunway->headingDeg() - 90, aRunway->widthM() * aScale / 2) + leftcenter;
140   osg::Vec3 leftbottom = leftcenter * 2 - lefttop;
141   osg::Vec3 rightbottom = center * 2 - lefttop;
142   osg::Vec3 righttop = center * 2 - leftbottom;
143
144   aVertices->push_back(lefttop);
145   aVertices->push_back(leftbottom);
146   aVertices->push_back(rightbottom);
147   aVertices->push_back(righttop);
148 }
149
150 osg::Geometry *GroundRadar::addPavementGeometry(const FGPavement* aPavement, double aTowerLat, double aTowerLon, double aScale)
151 {
152
153   osg::ref_ptr<osgUtil::Tessellator> tess = new osgUtil::Tessellator;
154   osg::ref_ptr<osg::Geometry> polygon = new osg::Geometry;
155   osg::ref_ptr<osg::Vec3Array> pts = new osg::Vec3Array;
156
157   double az1, az2, dist_m;
158   const FGPavement::NodeList &nodeLst = aPavement->getNodeList();
159   FGPavement::NodeList::const_iterator it = nodeLst.begin(),
160                                         loopBegin = it;
161   while ( it != nodeLst.end() )
162   {
163     bool close = (*it)->mClose;
164     geo_inverse_wgs_84(aTowerLat, aTowerLon, (*it)->mPos.getLatitudeDeg(), (*it)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
165     osg::Vec3 p1 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
166     const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>( it->ptr() );
167     if ( bn != 0 )
168     {
169       geo_inverse_wgs_84(aTowerLat, aTowerLon, bn->mControl.getLatitudeDeg(), bn->mControl.getLongitudeDeg(), &az1, &az2, &dist_m);
170       osg::Vec3 p2 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0),
171                 p3;
172       ++it;
173       if ( it == nodeLst.end() || close )
174       {
175         geo_inverse_wgs_84(aTowerLat, aTowerLon, (*loopBegin)->mPos.getLatitudeDeg(), (*loopBegin)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
176       }
177       else
178       {
179         geo_inverse_wgs_84(aTowerLat, aTowerLon, (*it)->mPos.getLatitudeDeg(), (*it)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
180       }
181       p3 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
182       simgear::BezierCurve<osg::Vec3> bCurv( p1, p2, p3 );
183       simgear::BezierCurve<osg::Vec3>::PointList &ptList = bCurv.pointList();
184       for ( simgear::BezierCurve<osg::Vec3>::PointList::iterator ii = ptList.begin(); ii != ptList.end(); ++ii )
185       {
186         pts->push_back( *ii );
187       }
188       pts->pop_back(); // Last point belongs to next segment
189     }
190     else
191     {
192       pts->push_back( p1 );
193       ++it;
194     }
195
196     if ( close ) // One loop for the moment
197       break;
198   }
199   geo_inverse_wgs_84(aTowerLat, aTowerLon, (*loopBegin)->mPos.getLatitudeDeg(), (*loopBegin)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
200   osg::Vec3 p1 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
201   pts->push_back( p1 );
202   polygon->setVertexArray( pts.get() );
203
204   polygon->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::POLYGON, 0, pts->size() ) );
205
206   tess->setTessellationType( osgUtil::Tessellator::TESS_TYPE_GEOMETRY );
207   tess->setBoundaryOnly( false );
208   tess->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD );
209   tess->retessellatePolygons( *polygon );
210   return polygon.release();
211 }
212
213 void GroundRadar::updateTexture()
214 {
215     osg::ref_ptr<osg::Vec3Array> rwy_vertices = new osg::Vec3Array;
216     osg::ref_ptr<osg::Vec3Array> taxi_vertices = new osg::Vec3Array;
217     osg::ref_ptr<osg::Vec3Array> pvt_vertices = new osg::Vec3Array;
218
219     const string airport_name = _airport_node->getStringValue();
220
221     const FGAirport* airport = fgFindAirportID(airport_name);
222     if (airport == 0)
223         return;
224
225     const SGGeod& tower_location = airport->getTowerLocation();
226     const double tower_lat = tower_location.getLatitudeDeg();
227     const double tower_lon = tower_location.getLongitudeDeg();
228     double scale = SG_METER_TO_NM * 200 / _range_node->getDoubleValue();
229   
230     const FGAirport* apt = fgFindAirportID(airport_name);
231     assert(apt);
232
233     for (unsigned int i=0; i<apt->numTaxiways(); ++i)
234     {
235       FGTaxiway* txwy(apt->getTaxiwayByIndex(i));
236       addRunwayVertices(txwy, tower_lat, tower_lon, scale, taxi_vertices.get());
237     }
238     osg::Geometry *taxi_geom = dynamic_cast<osg::Geometry *>(_geode->getDrawable(0));
239     taxi_geom->setVertexArray(taxi_vertices.get());
240     osg::DrawArrays* taxi = dynamic_cast<osg::DrawArrays*>(taxi_geom->getPrimitiveSet(0));
241     taxi->setCount(taxi_vertices->size());
242
243     osg::Geometry *pvt_geom = dynamic_cast<osg::Geometry *>(_geode->getDrawable(1));
244     osg::Geometry::PrimitiveSetList &pvt_prim_list = pvt_geom->getPrimitiveSetList();
245     pvt_prim_list.clear();
246     for (unsigned int i=0; i<apt->numPavements(); ++i)
247     {
248       FGPavement* pvt(apt->getPavementByIndex(i));
249       osg::ref_ptr<osg::Geometry> geom = addPavementGeometry(pvt, tower_lat, tower_lon, scale);
250       osg::Geometry::PrimitiveSetList &prim_list = geom->getPrimitiveSetList();
251       osg::Vec3Array *vertices = dynamic_cast<osg::Vec3Array *>(geom->getVertexArray());
252       size_t before = pvt_vertices->size(),
253             count = vertices->size();
254       for (size_t i = 0; i < count; ++i )
255       {
256         pvt_vertices->push_back( (*vertices)[i] );
257       }
258       for (osg::Geometry::PrimitiveSetList::iterator ii = prim_list.begin(); ii != prim_list.end(); ++ii )
259       {
260         osg::DrawArrays *da;
261         osg::DrawElementsUByte *de1;
262         osg::DrawElementsUShort *de2;
263         osg::DrawElementsUInt *de3;
264         if ((da = dynamic_cast<osg::DrawArrays *>(ii->get())) != 0)
265         {
266           osg::DrawArrays *ps = new osg::DrawArrays(*da);
267           ps->setFirst(da->getFirst() + before);
268           pvt_prim_list.push_back(ps);
269         }
270         else if ((de1 = dynamic_cast<osg::DrawElementsUByte *>(ii->get())) != 0)
271         {
272           if (before + count <= 255)
273           {
274             osg::DrawElementsUByte *ps = new osg::DrawElementsUByte(*de1);
275             for (size_t j = 0; j < ps->size(); ++j)
276             {
277               (*ps)[j] += before;
278             }
279             pvt_prim_list.push_back(ps);
280           }
281           else if (before + count <= 65535)
282           {
283             osg::DrawElementsUShort *ps = new osg::DrawElementsUShort(de1->getMode(), de1->begin(), de1->end());
284             for (size_t j = 0; j < ps->size(); ++j)
285             {
286               (*ps)[j] += before;
287             }
288             pvt_prim_list.push_back(ps);
289           }
290           else
291           {
292             osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(de1->getMode(), de1->begin(), de1->end());
293             for (size_t j = 0; j < ps->size(); ++j)
294             {
295               (*ps)[j] += before;
296             }
297             pvt_prim_list.push_back(ps);
298           }
299         }
300         else if ((de2 = dynamic_cast<osg::DrawElementsUShort *>(ii->get())) != 0)
301         {
302           if (before + count <= 65535)
303           {
304             osg::DrawElementsUShort *ps = new osg::DrawElementsUShort(*de2);
305             for (size_t j = 0; j < ps->size(); ++j)
306             {
307               (*ps)[j] += before;
308             }
309             pvt_prim_list.push_back(ps);
310           }
311           else
312           {
313             osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(de2->getMode(), de2->begin(), de2->end());
314             for (size_t j = 0; j < ps->size(); ++j)
315             {
316               (*ps)[j] += before;
317             }
318             pvt_prim_list.push_back(ps);
319           }
320         }
321         else if ((de3 = dynamic_cast<osg::DrawElementsUInt *>(ii->get())) != 0)
322         {
323           osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(*de3);
324           for (size_t j = 0; j < ps->size(); ++j)
325           {
326             (*ps)[j] += before;
327           }
328           pvt_prim_list.push_back(ps);
329         }
330       }
331     }
332     pvt_geom->setVertexArray(pvt_vertices.get());
333
334     for (unsigned int i=0; i<apt->numRunways(); ++i)
335     {
336       FGRunway* runway(apt->getRunwayByIndex(i));
337       if (runway->isReciprocal()) continue;
338       
339       addRunwayVertices(runway, tower_lat, tower_lon, scale, rwy_vertices.get());
340     }
341     osg::Geometry *rwy_geom = dynamic_cast<osg::Geometry *>(_geode->getDrawable(2));
342     rwy_geom->setVertexArray(rwy_vertices.get());
343     osg::DrawArrays* rwy = dynamic_cast<osg::DrawArrays*>(rwy_geom->getPrimitiveSet(0));
344     rwy->setCount(rwy_vertices->size());
345
346     getCamera()->setNodeMask(0xffffffff);
347 }
348
349 // end of GroundRadar.cxx