1 // groundradar.cxx - Background layer for the ATC radar.
3 // Copyright (C) 2007 Csaba Halasz.
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.
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.
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.
27 #include <osg/Geometry>
29 #include <osg/Texture2D>
30 #include <osgViewer/Viewer>
32 #include <osgText/Text>
33 #include <osgDB/Registry>
34 #include <osgDB/ReaderWriter>
35 #include <osgUtil/Tessellator>
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>
47 #include "groundradar.hxx"
49 static const char* airport_source_node_name = "airport-id-source";
50 static const char* default_airport_node_name = "/sim/tower/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";
56 struct SingleFrameCallback : public osg::Camera::DrawCallback
58 virtual void operator () (const osg::Camera& camera) const
60 const_cast<osg::Camera&>(camera).setNodeMask(0);
64 GroundRadar::GroundRadar(SGPropertyNode *node)
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));
70 _airport_node->addChangeListener(this);
71 _range_node->addChangeListener(this);
74 GroundRadar::~GroundRadar()
76 _airport_node->removeChangeListener(this);
77 _range_node->removeChangeListener(this);
80 void GroundRadar::valueChanged(SGPropertyNode*)
85 inline static osg::Vec3 fromPolar(double fi, double r)
87 return osg::Vec3(sin(fi * SGD_DEGREES_TO_RADIANS) * r, cos(fi * SGD_DEGREES_TO_RADIANS) * r, 0);
90 void GroundRadar::createTexture(const char* texture_name)
92 setSize(TextureHalfSize + TextureHalfSize);
95 _geode = new osg::Geode();
96 osg::StateSet* stateset = _geode->getOrCreateStateSet();
97 stateset->setMode(GL_BLEND, osg::StateAttribute::OFF);
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));
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);
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);
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);
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());
128 // Texture in the 2D panel system
129 FGTextureManager::addTexture(texture_name, getTexture());
132 void GroundRadar::addRunwayVertices(const FGRunwayBase* aRunway, double aTowerLat, double aTowerLon, double aScale, osg::Vec3Array* aVertices)
134 double az1, az2, dist_m;
135 geo_inverse_wgs_84(aTowerLat, aTowerLon, aRunway->latitude(), aRunway->longitude(), &az1, &az2, &dist_m);
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;
144 aVertices->push_back(lefttop);
145 aVertices->push_back(leftbottom);
146 aVertices->push_back(rightbottom);
147 aVertices->push_back(righttop);
150 osg::Geometry *GroundRadar::addPavementGeometry(const FGPavement* aPavement, double aTowerLat, double aTowerLon, double aScale)
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;
157 double az1, az2, dist_m;
158 const FGPavement::NodeList &nodeLst = aPavement->getNodeList();
159 FGPavement::NodeList::const_iterator it = nodeLst.begin(),
161 while ( it != nodeLst.end() )
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() );
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),
173 if ( it == nodeLst.end() || close )
175 geo_inverse_wgs_84(aTowerLat, aTowerLon, (*loopBegin)->mPos.getLatitudeDeg(), (*loopBegin)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
179 geo_inverse_wgs_84(aTowerLat, aTowerLon, (*it)->mPos.getLatitudeDeg(), (*it)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
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 )
186 pts->push_back( *ii );
188 pts->pop_back(); // Last point belongs to next segment
192 pts->push_back( p1 );
196 if ( close ) // One loop for the moment
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() );
204 polygon->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::POLYGON, 0, pts->size() ) );
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();
213 void GroundRadar::updateTexture()
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;
219 const string airport_name = _airport_node->getStringValue();
221 const FGAirport* airport = fgFindAirportID(airport_name);
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();
230 const FGAirport* apt = fgFindAirportID(airport_name);
233 for (unsigned int i=0; i<apt->numTaxiways(); ++i)
235 FGTaxiway* txwy(apt->getTaxiwayByIndex(i));
236 addRunwayVertices(txwy, tower_lat, tower_lon, scale, taxi_vertices.get());
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());
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)
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 )
256 pvt_vertices->push_back( (*vertices)[i] );
258 for (osg::Geometry::PrimitiveSetList::iterator ii = prim_list.begin(); ii != prim_list.end(); ++ii )
261 osg::DrawElementsUByte *de1;
262 osg::DrawElementsUShort *de2;
263 osg::DrawElementsUInt *de3;
264 if ((da = dynamic_cast<osg::DrawArrays *>(ii->get())) != 0)
266 osg::DrawArrays *ps = new osg::DrawArrays(*da);
267 ps->setFirst(da->getFirst() + before);
268 pvt_prim_list.push_back(ps);
270 else if ((de1 = dynamic_cast<osg::DrawElementsUByte *>(ii->get())) != 0)
272 if (before + count <= 255)
274 osg::DrawElementsUByte *ps = new osg::DrawElementsUByte(*de1);
275 for (size_t j = 0; j < ps->size(); ++j)
279 pvt_prim_list.push_back(ps);
281 else if (before + count <= 65535)
283 osg::DrawElementsUShort *ps = new osg::DrawElementsUShort(de1->getMode(), de1->begin(), de1->end());
284 for (size_t j = 0; j < ps->size(); ++j)
288 pvt_prim_list.push_back(ps);
292 osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(de1->getMode(), de1->begin(), de1->end());
293 for (size_t j = 0; j < ps->size(); ++j)
297 pvt_prim_list.push_back(ps);
300 else if ((de2 = dynamic_cast<osg::DrawElementsUShort *>(ii->get())) != 0)
302 if (before + count <= 65535)
304 osg::DrawElementsUShort *ps = new osg::DrawElementsUShort(*de2);
305 for (size_t j = 0; j < ps->size(); ++j)
309 pvt_prim_list.push_back(ps);
313 osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(de2->getMode(), de2->begin(), de2->end());
314 for (size_t j = 0; j < ps->size(); ++j)
318 pvt_prim_list.push_back(ps);
321 else if ((de3 = dynamic_cast<osg::DrawElementsUInt *>(ii->get())) != 0)
323 osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(*de3);
324 for (size_t j = 0; j < ps->size(); ++j)
328 pvt_prim_list.push_back(ps);
332 pvt_geom->setVertexArray(pvt_vertices.get());
334 for (unsigned int i=0; i<apt->numRunways(); ++i)
336 FGRunway* runway(apt->getRunwayByIndex(i));
337 if (runway->isReciprocal()) continue;
339 addRunwayVertices(runway, tower_lat, tower_lon, scale, rwy_vertices.get());
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());
346 getCamera()->setNodeMask(0xffffffff);
349 // end of GroundRadar.cxx