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>
34 #include <Main/fg_props.hxx>
35 #include <Main/globals.hxx>
36 #include <Main/renderer.hxx>
37 #include <Cockpit/panel.hxx>
38 #include <Airports/simple.hxx>
39 #include <Airports/runways.hxx>
40 #include <simgear/math/sg_geodesy.hxx>
42 #include "groundradar.hxx"
44 static const char* airport_source_node_name = "airport-id-source";
45 static const char* default_airport_node_name = "/sim/tower/airport-id";
46 static const char* texture_node_name = "texture-name";
47 static const char* default_texture_name = "Aircraft/Instruments/Textures/od_groundradar.rgb";
48 static const char* range_source_node_name = "range-source";
49 static const char* default_range_node_name = "/instrumentation/radar/range";
51 struct SingleFrameCallback : public osg::Camera::DrawCallback
53 virtual void operator () (const osg::Camera& camera) const
55 const_cast<osg::Camera&>(camera).setNodeMask(0);
59 GroundRadar::GroundRadar(SGPropertyNode *node)
61 _airport_node = fgGetNode(node->getStringValue(airport_source_node_name, default_airport_node_name), true);
62 _range_node = fgGetNode(node->getStringValue(range_source_node_name, default_range_node_name), true);
63 createTexture(node->getStringValue(texture_node_name, default_texture_name));
65 _airport_node->addChangeListener(this);
66 _range_node->addChangeListener(this);
69 GroundRadar::~GroundRadar()
71 _airport_node->removeChangeListener(this);
72 _range_node->removeChangeListener(this);
75 void GroundRadar::valueChanged(SGPropertyNode*)
80 inline static osg::Vec3 fromPolar(double fi, double r)
82 return osg::Vec3(sin(fi * SGD_DEGREES_TO_RADIANS) * r, cos(fi * SGD_DEGREES_TO_RADIANS) * r, 0);
85 void GroundRadar::createTexture(const char* texture_name)
90 osg::Vec4Array* colors = new osg::Vec4Array;
91 colors->push_back(osg::Vec4(0.0f, 0.5f, 0.0f, 1.0f));
92 colors->push_back(osg::Vec4(0.0f, 0.5f, 0.5f, 1.0f));
94 _geom = new osg::Geometry();
95 _geom->setColorArray(colors);
96 _geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
97 _geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 0));
98 _geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 0));
100 osg::Geode* geode = new osg::Geode();
101 osg::StateSet* stateset = geode->getOrCreateStateSet();
102 stateset->setMode(GL_BLEND, osg::StateAttribute::OFF);
103 geode->addDrawable(_geom.get());
105 osg::Camera* camera = getCamera();
106 camera->setPostDrawCallback(new SingleFrameCallback());
107 camera->addChild(geode);
108 camera->setNodeMask(0);
109 camera->setProjectionMatrixAsOrtho2D(0, getTexture()->getTextureWidth(), 0, getTexture()->getTextureHeight());
111 // Texture in the 2D panel system
112 FGTextureManager::addTexture(texture_name, getTexture());
115 void GroundRadar::addRunwayVertices(const FGRunway* aRunway, double aTowerLat, double aTowerLon, double aScale, osg::Vec3Array* aVertices)
117 double az1, az2, dist_m;
118 geo_inverse_wgs_84(aTowerLat, aTowerLon, aRunway->latitude(), aRunway->longitude(), &az1, &az2, &dist_m);
120 osg::Vec3 center = fromPolar(az1, dist_m * aScale) + osg::Vec3(256, 256, 0);
121 osg::Vec3 leftcenter = fromPolar(aRunway->headingDeg(), aRunway->lengthM() * aScale / 2) + center;
122 osg::Vec3 lefttop = fromPolar(aRunway->headingDeg() - 90, aRunway->widthM() * aScale / 2) + leftcenter;
123 osg::Vec3 leftbottom = leftcenter * 2 - lefttop;
124 osg::Vec3 rightbottom = center * 2 - lefttop;
125 osg::Vec3 righttop = center * 2 - leftbottom;
127 aVertices->push_back(lefttop);
128 aVertices->push_back(leftbottom);
129 aVertices->push_back(rightbottom);
130 aVertices->push_back(righttop);
133 void GroundRadar::updateTexture()
135 osg::ref_ptr<osg::Vec3Array> rwy_vertices = new osg::Vec3Array;
136 osg::ref_ptr<osg::Vec3Array> taxi_vertices = new osg::Vec3Array;
138 const string airport_name = _airport_node->getStringValue();
140 const FGAirport* airport = fgFindAirportID(airport_name);
144 const SGGeod& tower_location = airport->getTowerLocation();
145 const double tower_lat = tower_location.getLatitudeDeg();
146 const double tower_lon = tower_location.getLongitudeDeg();
147 double scale = SG_METER_TO_NM * 200 / _range_node->getDoubleValue();
149 const FGAirport* apt = fgFindAirportID(airport_name);
152 for (unsigned int i=0; i<apt->numRunways(); ++i)
154 FGRunway* runway(apt->getRunwayByIndex(i));
155 if (runway->isReciprocal()) continue;
157 addRunwayVertices(runway, tower_lat, tower_lon, scale, rwy_vertices.get());
160 for (unsigned int i=0; i<apt->numTaxiways(); ++i)
162 FGRunway* runway(apt->getTaxiwayByIndex(i));
163 addRunwayVertices(runway, tower_lat, tower_lon, scale, taxi_vertices.get());
166 osg::Vec3Array* vertices = new osg::Vec3Array(*taxi_vertices.get());
167 vertices->insert(vertices->end(), rwy_vertices->begin(), rwy_vertices->end());
168 _geom->setVertexArray(vertices);
169 osg::DrawArrays* taxi = dynamic_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(0));
170 osg::DrawArrays* rwy = dynamic_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(1));
171 taxi->setCount(taxi_vertices->size());
172 rwy->setFirst(taxi_vertices->size());
173 rwy->setCount(rwy_vertices->size());
175 getCamera()->setNodeMask(0xffffffff);
178 // end of GroundRadar.cxx