]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/groundradar.cxx
Merge branch 'topic/tape' into next
[flightgear.git] / src / Instrumentation / groundradar.cxx
1 //  groundradar.cxx - Background layer for the ATC radar.
2 //
3 //  Copyright (C) 2007 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_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";
50
51 struct SingleFrameCallback : public osg::Camera::DrawCallback
52 {
53     virtual void operator () (const osg::Camera& camera) const
54     {
55         const_cast<osg::Camera&>(camera).setNodeMask(0);
56     }
57 };
58
59 GroundRadar::GroundRadar(SGPropertyNode *node)
60 {
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));
64     updateTexture();
65     _airport_node->addChangeListener(this);
66     _range_node->addChangeListener(this);
67 }
68
69 GroundRadar::~GroundRadar()
70 {
71     _airport_node->removeChangeListener(this);
72     _range_node->removeChangeListener(this);
73 }
74
75 void GroundRadar::valueChanged(SGPropertyNode*)
76 {
77     updateTexture();
78 }
79
80 inline static osg::Vec3 fromPolar(double fi, double r)
81 {
82     return osg::Vec3(sin(fi * SGD_DEGREES_TO_RADIANS) * r, cos(fi * SGD_DEGREES_TO_RADIANS) * r, 0);
83 }
84
85 void GroundRadar::createTexture(const char* texture_name)
86 {
87     setSize(512);
88     allocRT();
89
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));
93
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));
99
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());
104
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());
110
111     // Texture in the 2D panel system
112     FGTextureManager::addTexture(texture_name, getTexture());
113 }
114
115 void GroundRadar::addRunwayVertices(const FGRunwayBase* aRunway, double aTowerLat, double aTowerLon, double aScale, osg::Vec3Array* aVertices)
116 {
117   double az1, az2, dist_m;
118   geo_inverse_wgs_84(aTowerLat, aTowerLon, aRunway->latitude(), aRunway->longitude(), &az1, &az2, &dist_m);
119
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;
126
127   aVertices->push_back(lefttop);
128   aVertices->push_back(leftbottom);
129   aVertices->push_back(rightbottom);
130   aVertices->push_back(righttop);
131 }
132
133 void GroundRadar::updateTexture()
134 {
135     osg::ref_ptr<osg::Vec3Array> rwy_vertices = new osg::Vec3Array;
136     osg::ref_ptr<osg::Vec3Array> taxi_vertices = new osg::Vec3Array;
137
138     const string airport_name = _airport_node->getStringValue();
139
140     const FGAirport* airport = fgFindAirportID(airport_name);
141     if (airport == 0)
142         return;
143
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();
148   
149     const FGAirport* apt = fgFindAirportID(airport_name);
150     assert(apt);
151
152     for (unsigned int i=0; i<apt->numRunways(); ++i)
153     {
154       FGRunway* runway(apt->getRunwayByIndex(i));
155       if (runway->isReciprocal()) continue;
156       
157       addRunwayVertices(runway, tower_lat, tower_lon, scale, rwy_vertices.get());
158     }
159     
160     for (unsigned int i=0; i<apt->numTaxiways(); ++i)
161     {
162       FGTaxiway* txwy(apt->getTaxiwayByIndex(i));
163       addRunwayVertices(txwy, tower_lat, tower_lon, scale, taxi_vertices.get());
164     }
165     
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());
174
175     getCamera()->setNodeMask(0xffffffff);
176 }
177
178 // end of GroundRadar.cxx