]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/groundradar.cxx
Csaba HALASZ & Syd ADAMS: make radar font configurable
[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::updateTexture()
116 {
117     osg::ref_ptr<osg::Vec3Array> rwy_vertices = new osg::Vec3Array;
118     osg::ref_ptr<osg::Vec3Array> taxi_vertices = new osg::Vec3Array;
119
120     const string airport_name = _airport_node->getStringValue();
121     FGRunwayList* runways = globals->get_runways();
122
123     const FGAirport* airport = fgFindAirportID(airport_name);
124     if (airport == 0)
125         return;
126
127     const SGGeod& tower_location = airport->getTowerLocation();
128     const double tower_lat = tower_location.getLatitudeDeg();
129     const double tower_lon = tower_location.getLongitudeDeg();
130     double scale = SG_METER_TO_NM * 200 / _range_node->getDoubleValue();
131
132     for (FGRunway runway = runways->search(airport_name); runway._id == airport_name; runway = runways->next())
133     {
134         double az1, az2, dist_m;
135         geo_inverse_wgs_84(tower_lat, tower_lon, runway._lat, runway._lon, &az1, &az2, &dist_m);
136
137         osg::Vec3 center = fromPolar(az1, dist_m * scale) + osg::Vec3(256, 256, 0);
138         osg::Vec3 leftcenter = fromPolar(runway._heading, runway._length * SG_FEET_TO_METER * scale / 2) + center;
139         osg::Vec3 lefttop = fromPolar(runway._heading - 90, runway._width * SG_FEET_TO_METER * scale / 2) + leftcenter;
140         osg::Vec3 leftbottom = leftcenter * 2 - lefttop;
141         osg::Vec3 rightbottom = center * 2 - lefttop;
142         osg::Vec3 righttop = center * 2 - leftbottom;
143
144         osg::Vec3Array* vertices = runway._rwy_no[0] == 'x' ? taxi_vertices.get() : rwy_vertices.get();
145         vertices->push_back(lefttop);
146         vertices->push_back(leftbottom);
147         vertices->push_back(rightbottom);
148         vertices->push_back(righttop);
149     }
150
151     osg::Vec3Array* vertices = new osg::Vec3Array(*taxi_vertices.get());
152     vertices->insert(vertices->end(), rwy_vertices->begin(), rwy_vertices->end());
153     _geom->setVertexArray(vertices);
154     osg::DrawArrays* taxi = dynamic_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(0));
155     osg::DrawArrays* rwy = dynamic_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(1));
156     taxi->setCount(taxi_vertices->size());
157     rwy->setFirst(taxi_vertices->size());
158     rwy->setCount(rwy_vertices->size());
159
160     getCamera()->setNodeMask(0xffffffff);
161 }
162
163 // end of GroundRadar.cxx