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.
25 #include <osg/Geometry>
27 #include <osg/Texture2D>
28 #include <osgViewer/Viewer>
30 #include <osgText/Text>
31 #include <osgDB/Registry>
32 #include <osgDB/ReaderWriter>
33 #include <osgUtil/Tessellator>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
37 #include <Main/renderer.hxx>
38 #include <Cockpit/panel.hxx>
39 #include <Airports/simple.hxx>
40 #include <Airports/runways.hxx>
41 #include <Airports/pavement.hxx>
42 #include <simgear/math/sg_geodesy.hxx>
43 #include <simgear/math/beziercurve.hxx>
45 #include "groundradar.hxx"
47 static const char* airport_source_node_name = "airport-id-source";
48 static const char* default_airport_node_name = "/sim/tower/airport-id";
49 static const char* texture_node_name = "texture-name";
50 static const char* default_texture_name = "Aircraft/Instruments/Textures/od_groundradar.rgb";
51 static const char* range_source_node_name = "range-source";
52 static const char* default_range_node_name = "/instrumentation/radar/range";
54 struct SingleFrameCallback : public osg::Camera::DrawCallback
56 virtual void operator () (const osg::Camera& camera) const
58 const_cast<osg::Camera&>(camera).setNodeMask(0);
62 GroundRadar::GroundRadar(SGPropertyNode *node)
64 _airport_node = fgGetNode(node->getStringValue(airport_source_node_name, default_airport_node_name), true);
65 _range_node = fgGetNode(node->getStringValue(range_source_node_name, default_range_node_name), true);
66 createTexture(node->getStringValue(texture_node_name, default_texture_name));
68 _airport_node->addChangeListener(this);
69 _range_node->addChangeListener(this);
72 GroundRadar::~GroundRadar()
74 _airport_node->removeChangeListener(this);
75 _range_node->removeChangeListener(this);
78 void GroundRadar::valueChanged(SGPropertyNode*)
83 inline static osg::Vec3 fromPolar(double fi, double r)
85 return osg::Vec3(sin(fi * SGD_DEGREES_TO_RADIANS) * r, cos(fi * SGD_DEGREES_TO_RADIANS) * r, 0);
88 void GroundRadar::createTexture(const char* texture_name)
90 setSize(TextureHalfSize + TextureHalfSize);
93 _geode = new osg::Geode();
94 osg::StateSet* stateset = _geode->getOrCreateStateSet();
95 stateset->setMode(GL_BLEND, osg::StateAttribute::OFF);
97 osg::Vec4Array* taxi_color = new osg::Vec4Array;
98 taxi_color->push_back(osg::Vec4(0.0f, 0.5f, 0.0f, 1.0f));
99 osg::Vec4Array* rwy_color = new osg::Vec4Array;
100 rwy_color->push_back(osg::Vec4(0.0f, 0.5f, 0.5f, 1.0f));
102 osg::Geometry *taxi_geom = new osg::Geometry();
103 taxi_geom->setColorArray(taxi_color);
104 taxi_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
105 taxi_geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 0)); // Taxiways
106 _geode->addDrawable(taxi_geom);
108 osg::Geometry *pvt_geom = new osg::Geometry();
109 pvt_geom->setColorArray(taxi_color);
110 pvt_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
111 // no primitive set for the moment. It needs tessellation
112 _geode->addDrawable(pvt_geom);
114 osg::Geometry *rwy_geom = new osg::Geometry();
115 rwy_geom->setColorArray(rwy_color);
116 rwy_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
117 rwy_geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 0)); // Runways
118 _geode->addDrawable(rwy_geom);
120 osg::Camera* camera = getCamera();
121 camera->setPostDrawCallback(new SingleFrameCallback());
122 camera->addChild(_geode.get());
123 camera->setNodeMask(0);
124 camera->setProjectionMatrixAsOrtho2D(0, getTexture()->getTextureWidth(), 0, getTexture()->getTextureHeight());
126 // Texture in the 2D panel system
127 FGTextureManager::addTexture(texture_name, getTexture());
130 void GroundRadar::addRunwayVertices(const FGRunwayBase* aRunway, double aTowerLat, double aTowerLon, double aScale, osg::Vec3Array* aVertices)
132 double az1, az2, dist_m;
133 geo_inverse_wgs_84(aTowerLat, aTowerLon, aRunway->latitude(), aRunway->longitude(), &az1, &az2, &dist_m);
135 osg::Vec3 center = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
136 osg::Vec3 leftcenter = fromPolar(aRunway->headingDeg(), aRunway->lengthM() * aScale / 2) + center;
137 osg::Vec3 lefttop = fromPolar(aRunway->headingDeg() - 90, aRunway->widthM() * aScale / 2) + leftcenter;
138 osg::Vec3 leftbottom = leftcenter * 2 - lefttop;
139 osg::Vec3 rightbottom = center * 2 - lefttop;
140 osg::Vec3 righttop = center * 2 - leftbottom;
142 aVertices->push_back(lefttop);
143 aVertices->push_back(leftbottom);
144 aVertices->push_back(rightbottom);
145 aVertices->push_back(righttop);
148 osg::Geometry *GroundRadar::addPavementGeometry(const FGPavement* aPavement, double aTowerLat, double aTowerLon, double aScale)
151 osg::ref_ptr<osgUtil::Tessellator> tess = new osgUtil::Tessellator;
152 osg::ref_ptr<osg::Geometry> polygon = new osg::Geometry;
153 osg::ref_ptr<osg::Vec3Array> pts = new osg::Vec3Array;
155 double az1, az2, dist_m;
156 const FGPavement::NodeList &nodeLst = aPavement->getNodeList();
157 FGPavement::NodeList::const_iterator it = nodeLst.begin(),
159 while ( it != nodeLst.end() )
161 bool close = (*it)->mClose;
162 geo_inverse_wgs_84(aTowerLat, aTowerLon, (*it)->mPos.getLatitudeDeg(), (*it)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
163 osg::Vec3 p1 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
164 const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>( it->ptr() );
167 geo_inverse_wgs_84(aTowerLat, aTowerLon, bn->mControl.getLatitudeDeg(), bn->mControl.getLongitudeDeg(), &az1, &az2, &dist_m);
168 osg::Vec3 p2 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0),
171 if ( it == nodeLst.end() || close )
173 geo_inverse_wgs_84(aTowerLat, aTowerLon, (*loopBegin)->mPos.getLatitudeDeg(), (*loopBegin)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
177 geo_inverse_wgs_84(aTowerLat, aTowerLon, (*it)->mPos.getLatitudeDeg(), (*it)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
179 p3 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
180 simgear::BezierCurve<osg::Vec3> bCurv( p1, p2, p3 );
181 simgear::BezierCurve<osg::Vec3>::PointList &ptList = bCurv.pointList();
182 for ( simgear::BezierCurve<osg::Vec3>::PointList::iterator ii = ptList.begin(); ii != ptList.end(); ++ii )
184 pts->push_back( *ii );
186 pts->pop_back(); // Last point belongs to next segment
190 pts->push_back( p1 );
194 if ( close ) // One loop for the moment
197 geo_inverse_wgs_84(aTowerLat, aTowerLon, (*loopBegin)->mPos.getLatitudeDeg(), (*loopBegin)->mPos.getLongitudeDeg(), &az1, &az2, &dist_m);
198 osg::Vec3 p1 = fromPolar(az1, dist_m * aScale) + osg::Vec3(TextureHalfSize, TextureHalfSize, 0);
199 pts->push_back( p1 );
200 polygon->setVertexArray( pts );
202 polygon->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::POLYGON, 0, pts->size() ) );
204 tess->setTessellationType( osgUtil::Tessellator::TESS_TYPE_GEOMETRY );
205 tess->setBoundaryOnly( false );
206 tess->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD );
207 tess->retessellatePolygons( *polygon );
208 return polygon.release();
211 void GroundRadar::updateTexture()
213 osg::ref_ptr<osg::Vec3Array> rwy_vertices = new osg::Vec3Array;
214 osg::ref_ptr<osg::Vec3Array> taxi_vertices = new osg::Vec3Array;
215 osg::ref_ptr<osg::Vec3Array> pvt_vertices = new osg::Vec3Array;
217 const string airport_name = _airport_node->getStringValue();
219 const FGAirport* airport = fgFindAirportID(airport_name);
223 const SGGeod& tower_location = airport->getTowerLocation();
224 const double tower_lat = tower_location.getLatitudeDeg();
225 const double tower_lon = tower_location.getLongitudeDeg();
226 double scale = SG_METER_TO_NM * 200 / _range_node->getDoubleValue();
228 const FGAirport* apt = fgFindAirportID(airport_name);
231 for (unsigned int i=0; i<apt->numTaxiways(); ++i)
233 FGTaxiway* txwy(apt->getTaxiwayByIndex(i));
234 addRunwayVertices(txwy, tower_lat, tower_lon, scale, taxi_vertices.get());
236 osg::Geometry *taxi_geom = dynamic_cast<osg::Geometry *>(_geode->getDrawable(0));
237 taxi_geom->setVertexArray(taxi_vertices.get());
238 osg::DrawArrays* taxi = dynamic_cast<osg::DrawArrays*>(taxi_geom->getPrimitiveSet(0));
239 taxi->setCount(taxi_vertices->size());
241 osg::Geometry *pvt_geom = dynamic_cast<osg::Geometry *>(_geode->getDrawable(1));
242 osg::Geometry::PrimitiveSetList &pvt_prim_list = pvt_geom->getPrimitiveSetList();
243 pvt_prim_list.clear();
244 for (unsigned int i=0; i<apt->numPavements(); ++i)
246 FGPavement* pvt(apt->getPavementByIndex(i));
247 osg::ref_ptr<osg::Geometry> geom = addPavementGeometry(pvt, tower_lat, tower_lon, scale);
248 osg::Geometry::PrimitiveSetList &prim_list = geom->getPrimitiveSetList();
249 osg::Vec3Array *vertices = dynamic_cast<osg::Vec3Array *>(geom->getVertexArray());
250 size_t before = pvt_vertices->size(),
251 count = vertices->size();
252 for (size_t i = 0; i < count; ++i )
254 pvt_vertices->push_back( (*vertices)[i] );
256 for (osg::Geometry::PrimitiveSetList::iterator ii = prim_list.begin(); ii != prim_list.end(); ++ii )
259 osg::DrawElementsUByte *de1;
260 osg::DrawElementsUShort *de2;
261 osg::DrawElementsUInt *de3;
262 if ((da = dynamic_cast<osg::DrawArrays *>(ii->get())) != 0)
264 osg::DrawArrays *ps = new osg::DrawArrays(*da);
265 ps->setFirst(da->getFirst() + before);
266 pvt_prim_list.push_back(ps);
268 else if ((de1 = dynamic_cast<osg::DrawElementsUByte *>(ii->get())) != 0)
270 if (before + count <= 255)
272 osg::DrawElementsUByte *ps = new osg::DrawElementsUByte(*de1);
273 for (size_t j = 0; j < ps->size(); ++j)
277 pvt_prim_list.push_back(ps);
279 else if (before + count <= 65535)
281 osg::DrawElementsUShort *ps = new osg::DrawElementsUShort(de1->getMode(), de1->begin(), de1->end());
282 for (size_t j = 0; j < ps->size(); ++j)
286 pvt_prim_list.push_back(ps);
290 osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(de1->getMode(), de1->begin(), de1->end());
291 for (size_t j = 0; j < ps->size(); ++j)
295 pvt_prim_list.push_back(ps);
298 else if ((de2 = dynamic_cast<osg::DrawElementsUShort *>(ii->get())) != 0)
300 if (before + count <= 65535)
302 osg::DrawElementsUShort *ps = new osg::DrawElementsUShort(*de2);
303 for (size_t j = 0; j < ps->size(); ++j)
307 pvt_prim_list.push_back(ps);
311 osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(de2->getMode(), de2->begin(), de2->end());
312 for (size_t j = 0; j < ps->size(); ++j)
316 pvt_prim_list.push_back(ps);
319 else if ((de3 = dynamic_cast<osg::DrawElementsUInt *>(ii->get())) != 0)
321 osg::DrawElementsUInt *ps = new osg::DrawElementsUInt(*de3);
322 for (size_t j = 0; j < ps->size(); ++j)
326 pvt_prim_list.push_back(ps);
330 pvt_geom->setVertexArray(pvt_vertices.get());
332 for (unsigned int i=0; i<apt->numRunways(); ++i)
334 FGRunway* runway(apt->getRunwayByIndex(i));
335 if (runway->isReciprocal()) continue;
337 addRunwayVertices(runway, tower_lat, tower_lon, scale, rwy_vertices.get());
339 osg::Geometry *rwy_geom = dynamic_cast<osg::Geometry *>(_geode->getDrawable(2));
340 rwy_geom->setVertexArray(rwy_vertices.get());
341 osg::DrawArrays* rwy = dynamic_cast<osg::DrawArrays*>(rwy_geom->getPrimitiveSet(0));
342 rwy->setCount(rwy_vertices->size());
344 getCamera()->setNodeMask(0xffffffff);
347 // end of GroundRadar.cxx