]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/groundradar.cxx
Ground radar and tower control panel for ATC.
[flightgear.git] / src / Instrumentation / groundradar.cxx
1 //  groundradar.cxx - Background layer for the ATC radar.
2 //
3 //  Written by 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 <osg/Node>
24 #include <osg/Geode>
25 #include <osg/Geometry>
26 #include <osg/Camera>
27 #include <osg/Texture2D>
28 #include <osgViewer/Viewer>
29
30 #include <osgText/Text>
31 #include <osgDB/Registry>
32 #include <osgDB/ReaderWriter>
33
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>
41
42 #include "groundradar.hxx"
43
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";
47
48 struct SingleFrameCallback : public osg::Camera::DrawCallback
49 {
50     virtual void operator () (const osg::Camera& camera) const
51     {
52         const_cast<osg::Camera&>(camera).setNodeMask(0);
53     }
54 };
55
56 GroundRadar::GroundRadar(SGPropertyNode *node)
57 {
58     _airport_node = fgGetNode(airport_node_name, true);
59     _radar_node = fgGetNode(radar_node_name, true);
60     createTexture();
61     updateTexture();
62     _airport_node->addChangeListener(this);
63     _radar_node->addChangeListener(this);
64 }
65
66 GroundRadar::~GroundRadar()
67 {
68     _airport_node->removeChangeListener(this);
69     _radar_node->removeChangeListener(this);
70 }
71
72 void GroundRadar::valueChanged(SGPropertyNode*)
73 {
74     updateTexture();    
75 }
76
77 inline static osg::Vec3 fromPolar(double fi, double r)
78 {
79     return osg::Vec3(sin(fi * SGD_DEGREES_TO_RADIANS) * r, cos(fi * SGD_DEGREES_TO_RADIANS) * r, 0);
80 }
81
82 void GroundRadar::createTexture()
83 {        
84     setSize(512);
85     allocRT();
86     
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));
90         
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));
96
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());
101
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());
107
108     // Texture in the 2D panel system
109     FGTextureManager::addTexture(texture_name, getTexture());
110 }
111
112 void GroundRadar::updateTexture()
113 {
114     osg::ref_ptr<osg::Vec3Array> rwy_vertices = new osg::Vec3Array;
115     osg::ref_ptr<osg::Vec3Array> taxi_vertices = new osg::Vec3Array;
116
117     const string airport_name = _airport_node->getStringValue();
118     FGRunwayList* runways = globals->get_runways();
119
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();
125
126     for(FGRunway runway = runways->search(airport_name); runway._id == airport_name; runway = runways->next())
127     {
128         double az1, az2, dist_m;
129         geo_inverse_wgs_84(tower_lat, tower_lon, runway._lat, runway._lon, &az1, &az2, &dist_m);
130             
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;
137
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);
143     }
144         
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());
153     
154     getCamera()->setNodeMask(0xffffffff);
155 }
156
157 // end of GroundRadar.cxx