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_node_name = "/sim/tower/airport-id";
45 static const char* texture_name = "Aircraft/Instruments/Textures/od_groundradar.rgb";
46 static const char* radar_node_name = "/instrumentation/radar/range";
48 struct SingleFrameCallback : public osg::Camera::DrawCallback
50 virtual void operator () (const osg::Camera& camera) const
52 const_cast<osg::Camera&>(camera).setNodeMask(0);
56 GroundRadar::GroundRadar(SGPropertyNode *node)
58 _airport_node = fgGetNode(airport_node_name, true);
59 _radar_node = fgGetNode(radar_node_name, true);
62 _airport_node->addChangeListener(this);
63 _radar_node->addChangeListener(this);
66 GroundRadar::~GroundRadar()
68 _airport_node->removeChangeListener(this);
69 _radar_node->removeChangeListener(this);
72 void GroundRadar::valueChanged(SGPropertyNode*)
77 inline static osg::Vec3 fromPolar(double fi, double r)
79 return osg::Vec3(sin(fi * SGD_DEGREES_TO_RADIANS) * r, cos(fi * SGD_DEGREES_TO_RADIANS) * r, 0);
82 void GroundRadar::createTexture()
87 osg::Vec4Array* colors = new osg::Vec4Array;
88 colors->push_back(osg::Vec4(0.0f,0.5f,0.0f,1.0f));
89 colors->push_back(osg::Vec4(0.0f,0.5f,0.5f,1.0f));
91 _geom = new osg::Geometry();
92 _geom->setColorArray(colors);
93 _geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
94 _geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 0));
95 _geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 0));
97 osg::Geode* geode = new osg::Geode();
98 osg::StateSet* stateset = geode->getOrCreateStateSet();
99 stateset->setMode(GL_BLEND, osg::StateAttribute::OFF);
100 geode->addDrawable(_geom.get());
102 osg::Camera* camera = getCamera();
103 camera->setPostDrawCallback(new SingleFrameCallback());
104 camera->addChild(geode);
105 camera->setNodeMask(0);
106 camera->setProjectionMatrixAsOrtho2D(0, getTexture()->getTextureWidth(), 0, getTexture()->getTextureHeight());
108 // Texture in the 2D panel system
109 FGTextureManager::addTexture(texture_name, getTexture());
112 void GroundRadar::updateTexture()
114 osg::ref_ptr<osg::Vec3Array> rwy_vertices = new osg::Vec3Array;
115 osg::ref_ptr<osg::Vec3Array> taxi_vertices = new osg::Vec3Array;
117 const string airport_name = _airport_node->getStringValue();
118 FGRunwayList* runways = globals->get_runways();
120 const FGAirport* airport = fgFindAirportID(airport_name);
121 const SGGeod& tower_location = airport->getTowerLocation();
122 const double tower_lat = tower_location.getLatitudeDeg();
123 const double tower_lon = tower_location.getLongitudeDeg();
124 double scale = SG_METER_TO_NM * 200 / _radar_node->getDoubleValue();
126 for(FGRunway runway = runways->search(airport_name); runway._id == airport_name; runway = runways->next())
128 double az1, az2, dist_m;
129 geo_inverse_wgs_84(tower_lat, tower_lon, runway._lat, runway._lon, &az1, &az2, &dist_m);
131 osg::Vec3 center = fromPolar(az1, dist_m * scale) + osg::Vec3(256, 256, 0);
132 osg::Vec3 leftcenter = fromPolar(runway._heading, runway._length * SG_FEET_TO_METER * scale / 2) + center;
133 osg::Vec3 lefttop = fromPolar(runway._heading - 90, runway._width * SG_FEET_TO_METER * scale / 2) + leftcenter;
134 osg::Vec3 leftbottom = leftcenter * 2 - lefttop;
135 osg::Vec3 rightbottom = center * 2 - lefttop;
136 osg::Vec3 righttop = center * 2 - leftbottom;
138 osg::Vec3Array* vertices = runway._rwy_no[0] == 'x' ? taxi_vertices.get() : rwy_vertices.get();
139 vertices->push_back(lefttop);
140 vertices->push_back(leftbottom);
141 vertices->push_back(rightbottom);
142 vertices->push_back(righttop);
145 osg::Vec3Array* vertices = new osg::Vec3Array(*taxi_vertices.get());
146 vertices->insert(vertices->end(), rwy_vertices->begin(), rwy_vertices->end());
147 _geom->setVertexArray(vertices);
148 osg::DrawArrays* taxi = dynamic_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(0));
149 osg::DrawArrays* rwy = dynamic_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(1));
150 taxi->setCount(taxi_vertices->size());
151 rwy->setFirst(taxi_vertices->size());
152 rwy->setCount(rwy_vertices->size());
154 getCamera()->setNodeMask(0xffffffff);
157 // end of GroundRadar.cxx