1 // Wx Radar background texture
3 // Written by Harald JOHNSEN, started May 2005.
4 // With major amendments by Vivian MEAZZA May 2007
5 // Ported to OSG by Tim Moore Jun 2007
8 // Copyright (C) 2005 Harald JOHNSEN
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <osg/Geometry>
32 #include <osg/Matrixf>
33 #include <osg/PrimitiveSet>
34 #include <osg/StateSet>
35 #include <osg/Version>
36 #include <osgDB/ReaderWriter>
37 #include <osgDB/WriteFile>
39 #include <simgear/constants.h>
40 #include <simgear/misc/sg_path.hxx>
41 #include <simgear/environment/visual_enviro.hxx>
42 #include <simgear/scene/model/model.hxx>
43 #include <simgear/structure/exception.hxx>
44 #include <simgear/misc/sg_path.hxx>
45 #include <simgear/math/sg_geodesy.hxx>
49 SG_USING_STD(stringstream);
51 SG_USING_STD(setprecision);
54 SG_USING_STD(setfill);
56 #include <Main/fg_props.hxx>
57 #include <Main/globals.hxx>
58 #include <Cockpit/panel.hxx>
59 #include <Cockpit/hud.hxx>
60 #include <AIModel/AIBase.hxx>
61 #include <AIModel/AIManager.hxx>
62 #include <AIModel/AIBallistic.hxx>
64 #include <Include/general.hxx>
65 #include "instrument_mgr.hxx"
66 #include "od_gauge.hxx"
67 #include "wxradar.hxx"
70 typedef list <osg::ref_ptr<FGAIBase> > radar_list_type;
71 typedef radar_list_type::iterator radar_list_iterator;
72 typedef radar_list_type::const_iterator radar_list_const_iterator;
75 static const float UNIT = 1.0f / 8.0f; // 8 symbols in a row/column in the texture
76 static const char *DEFAULT_FONT = "typewriter.txf";
79 wxRadarBg::wxRadarBg(SGPropertyNode *node) :
80 _name(node->getStringValue("name", "radar")),
81 _num(node->getIntValue("number", 0)),
82 _interval(node->getDoubleValue("update-interval-sec", 1.0)),
84 _sim_init_done(false),
86 _last_switchKnob("off"),
91 branch = "/instrumentation/" + _name;
92 _Instrument = fgGetNode(branch.c_str(), _num, true);
94 const char *tacan_source = node->getStringValue("tacan-source", "/instrumentation/tacan");
95 _Tacan = fgGetNode(tacan_source, true);
97 _font_node = _Instrument->getNode("font", true);
99 #define INITFONT(p, val, type) if (!_font_node->hasValue(p)) _font_node->set##type##Value(p, val)
100 INITFONT("name", DEFAULT_FONT, String);
101 INITFONT("size", 8, Float);
102 INITFONT("line-spacing", 0.25, Float);
103 INITFONT("color/red", 0, Float);
104 INITFONT("color/green", 0.8, Float);
105 INITFONT("color/blue", 0, Float);
106 INITFONT("color/alpha", 1, Float);
109 _font_node->addChangeListener(this, true);
113 wxRadarBg::~wxRadarBg ()
115 _font_node->removeChangeListener(this);
122 _serviceable_node = _Instrument->getNode("serviceable", true);
124 // texture name to use in 2D and 3D instruments
125 _texture_path = _Instrument->getStringValue("radar-texture-path",
126 "Aircraft/Instruments/Textures/od_wxradar.rgb");
127 _resultTexture = FGTextureManager::createTexture(_texture_path.c_str(), false);
129 SGPath tpath(globals->get_fg_root());
130 string path = _Instrument->getStringValue("echo-texture-path",
131 "Aircraft/Instruments/Textures/wxecho.rgb");
134 // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect
135 _wxEcho = SGLoadTexture2D(tpath, false, false);
138 _Instrument->setFloatValue("trk", 0.0);
139 _Instrument->setFloatValue("tilt", 0.0);
140 _Instrument->setStringValue("status", "");
141 // those properties are used by a radar instrument of a MFD
142 // input switch = OFF | TST | STBY | ON
143 // input mode = WX | WXA | MAP
144 // output status = STBY | TEST | WX | WXA | MAP | blank
145 // input lightning = true | false
146 // input TRK = +/- n degrees
147 // input TILT = +/- n degree
148 // input autotilt = true | false
149 // input range = n nm (20/40/80)
150 // input display-mode = arc | rose | map | plan
152 FGInstrumentMgr *imgr = (FGInstrumentMgr *)globals->get_subsystem("instrumentation");
153 _odg = (FGODGauge *)imgr->get_subsystem("od_gauge");
156 _ai = (FGAIManager*)globals->get_subsystem("ai_model");
157 _ai_enabled_node = fgGetNode("/sim/ai/enabled", true);
159 _user_lat_node = fgGetNode("/position/latitude-deg", true);
160 _user_lon_node = fgGetNode("/position/longitude-deg", true);
161 _user_alt_node = fgGetNode("/position/altitude-ft", true);
163 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps", true);
164 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps", true);
166 _tacan_serviceable_node = _Tacan->getNode("serviceable", true);
167 _tacan_distance_node = _Tacan->getNode("indicated-distance-nm", true);
168 _tacan_name_node = _Tacan->getNode("name", true);
169 _tacan_bearing_node = _Tacan->getNode("indicated-bearing-true-deg", true);
170 _tacan_in_range_node = _Tacan->getNode("in-range", true);
172 _radar_mode_control_node = _Instrument->getNode("mode-control", true);
173 _radar_coverage_node = _Instrument->getNode("limit-deg", true);
174 _radar_ref_rng_node = _Instrument->getNode("reference-range-nm", true);
175 _radar_hdg_marker_node = _Instrument->getNode("heading-marker", true);
177 SGPropertyNode *n = _Instrument->getNode("display-controls", true);
178 _radar_weather_node = n->getNode("WX", true);
179 _radar_position_node = n->getNode("pos", true);
180 _radar_data_node = n->getNode("data", true);
181 _radar_symbol_node = n->getNode("symbol", true);
182 _radar_centre_node = n->getNode("centre", true);
183 _radar_rotate_node = n->getNode("rotate", true);
185 _radar_centre_node->setBoolValue(false);
186 if (!_radar_coverage_node->hasValue())
187 _radar_coverage_node->setFloatValue(120);
188 if (!_radar_ref_rng_node->hasValue())
189 _radar_ref_rng_node->setDoubleValue(35);
190 if (!_radar_hdg_marker_node->hasValue())
191 _radar_hdg_marker_node->setBoolValue(true);
196 // OSG geometry setup. The polygons for the radar returns will be
197 // stored in a single Geometry. The geometry will have several
198 // primitive sets so we can have different kinds of polys and
199 // choose a different overall color for each set.
200 _radarGeode = new osg::Geode;
201 osg::StateSet *stateSet = _radarGeode->getOrCreateStateSet();
202 stateSet->setTextureAttributeAndModes(0, _wxEcho.get());
203 _geom = new osg::Geometry;
204 _geom->setUseDisplayList(false);
205 // Initially allocate space for 128 quads
206 _vertices = new osg::Vec2Array;
207 _vertices->setDataVariance(osg::Object::DYNAMIC);
208 _vertices->reserve(128 * 4);
209 _geom->setVertexArray(_vertices);
210 _texCoords = new osg::Vec2Array;
211 _texCoords->setDataVariance(osg::Object::DYNAMIC);
212 _texCoords->reserve(128 * 4);
213 _geom->setTexCoordArray(0, _texCoords);
214 osg::Vec3Array *colors = new osg::Vec3Array;
215 colors->push_back(osg::Vec3(1.0f, 1.0f, 1.0f)); // color of echos
216 colors->push_back(osg::Vec3(1.0f, 0.0f, 0.0f)); // arc mask
217 colors->push_back(osg::Vec3(0.0f, 0.0f, 0.0f)); // rest of mask
218 _geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
219 _geom->setColorArray(colors);
220 osg::PrimitiveSet *pset = new osg::DrawArrays(osg::PrimitiveSet::QUADS);
221 pset->setDataVariance(osg::Object::DYNAMIC);
222 _geom->addPrimitiveSet(pset);
223 pset = new osg::DrawArrays(osg::PrimitiveSet::QUADS);
224 pset->setDataVariance(osg::Object::DYNAMIC);
225 _geom->addPrimitiveSet(pset);
226 pset = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES);
227 pset->setDataVariance(osg::Object::DYNAMIC);
228 _geom->addPrimitiveSet(pset);
229 _geom->setInitialBound(osg::BoundingBox(osg::Vec3f(-256.0f, -256.0f, 0.0f),
230 osg::Vec3f(256.0f, 256.0f, 0.0f)));
231 _radarGeode->addDrawable(_geom);
233 // Texture in the 2D panel system
234 FGTextureManager::addTexture(_texture_path.c_str(), _odg->getTexture());
236 _textGeode = new osg::Geode;
238 osg::Camera *camera = _odg->getCamera();
239 camera->addChild(_radarGeode.get());
240 camera->addChild(_textGeode.get());
244 // Local coordinates for each echo
245 const osg::Vec3f echoCoords[4] = {
246 osg::Vec3f(-.7f, -.7f, 0.0f), osg::Vec3f(.7f, -.7f, 0.0f),
247 osg::Vec3f(.7f, .7f, 0.0f), osg::Vec3f(-.7f, .7f, 0.0f)
251 const osg::Vec2f echoTexCoords[4] = {
252 osg::Vec2f(0.0f, 0.0f), osg::Vec2f(UNIT, 0.0f),
253 osg::Vec2f(UNIT, UNIT), osg::Vec2f(0.0f, UNIT)
259 addQuad(osg::Vec2Array *vertices, osg::Vec2Array *texCoords,
260 const osg::Matrixf& transform, const osg::Vec2f& texBase)
262 for (int i = 0; i < 4; i++) {
263 const osg::Vec3f coords = transform.preMult(echoCoords[i]);
264 texCoords->push_back(texBase + echoTexCoords[i]);
265 vertices->push_back(osg::Vec2f(coords.x(), coords.y()));
270 // Rotate by a heading value
272 osg::Matrixf wxRotate(float angle)
274 return osg::Matrixf::rotate(angle, 0.0f, 0.0f, -1.0f);
279 wxRadarBg::update (double delta_time_sec)
281 if (!_sim_init_done) {
282 if (!fgGetBool("sim/sceneryloaded", false))
285 _sim_init_done = true;
287 if (!_odg || !_serviceable_node->getBoolValue()) {
288 _Instrument->setStringValue("status", "");
291 _time += delta_time_sec;
292 if (_time < _interval)
297 string mode = _Instrument->getStringValue("display-mode", "arc");
299 if (_display_mode != MAP) {
303 } else if (mode == "plan") {
304 _display_mode = PLAN;
309 string switchKnob = _Instrument->getStringValue("switch", "on");
310 if (_last_switchKnob != switchKnob) {
311 // since 3D models don't share textures with the rest of the world
312 // we must locate them and replace their handle by hand
313 // only do that when the instrument is turned on
314 //if (_last_switchKnob == "off")
315 //_odg->set_texture(_texture_path.c_str(), _resultTexture->getHandle());
317 _last_switchKnob = switchKnob;
320 if (switchKnob == "off") {
321 _Instrument->setStringValue("status", "");
322 } else if (switchKnob == "stby") {
323 _Instrument->setStringValue("status", "STBY");
324 } else if (switchKnob == "tst") {
325 _Instrument->setStringValue("status", "TST");
326 // find something interesting to do...
328 float r = _Instrument->getFloatValue("range", 40.0);
329 if (r != _range_nm) {
334 _radar_ref_rng = _radar_ref_rng_node->getDoubleValue();
335 _view_heading = get_heading() * SG_DEGREES_TO_RADIANS;
336 _centerTrans.makeTranslate(0.0f, 0.0f, 0.0f);
338 _scale = 200.0 / _range_nm;
341 if (_display_mode == ARC) {
342 _scale = 2*200.0f / _range_nm;
343 _angle_offset = -_view_heading;
344 _centerTrans.makeTranslate(0.0f, -200.0f, 0.0f);
346 } else if (_display_mode == MAP) {
349 bool centre = _radar_centre_node->getBoolValue();
352 _radar_centre_node->setBoolValue(false);
355 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: displacement "
356 // << _x_offset <<", "<<_y_offset
357 // << " user_speed_east_fps * SG_FPS_TO_KT "
358 // << user_speed_east_fps * SG_FPS_TO_KT
359 // << " user_speed_north_fps * SG_FPS_TO_KT "
360 // << user_speed_north_fps * SG_FPS_TO_KT
361 // << " dt " << delta_time_sec);
363 _centerTrans.makeTranslate(_x_offset, _y_offset, 0.0f);
365 } else if (_display_mode == PLAN) {
366 if (_radar_rotate_node->getBoolValue()) {
367 _angle_offset = -_view_heading;
375 _textGeode->removeDrawables(0, _textGeode->getNumDrawables());
381 osg::DrawArrays *quadPSet
382 = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(0));
383 quadPSet->set(osg::PrimitiveSet::QUADS, 0, _vertices->size());
386 // erase what is out of sight of antenna
397 osg::DrawArrays *maskPSet
398 = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(1));
399 osg::DrawArrays *trimaskPSet
400 = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(2));
402 if (_display_mode == ARC) {
403 float xOffset = 256.0f;
404 float yOffset = 200.0f;
406 int firstQuadVert = _vertices->size();
407 _texCoords->push_back(osg::Vec2f(0.5f, 0.25f));
408 _vertices->push_back(osg::Vec2f(-xOffset, 0.0 + yOffset));
409 _texCoords->push_back(osg::Vec2f(1.0f, 0.25f));
410 _vertices->push_back(osg::Vec2f(xOffset, 0.0 + yOffset));
411 _texCoords->push_back(osg::Vec2f(1.0f, 0.5f));
412 _vertices->push_back(osg::Vec2f(xOffset, 256.0 + yOffset));
413 _texCoords->push_back(osg::Vec2f(0.5f, 0.5f));
414 _vertices->push_back(osg::Vec2f(-xOffset, 256.0 + yOffset));
415 maskPSet->set(osg::PrimitiveSet::QUADS, firstQuadVert, 4);
417 // The triangles aren't supposed to be textured, but there's
418 // no need to set up a different Geometry, switch modes,
419 // etc. I happen to know that there's a white pixel in the
420 // texture at 1.0, 0.0 :)
421 float centerY = tan(30 * SG_DEGREES_TO_RADIANS);
422 _vertices->push_back(osg::Vec2f(0.0, 0.0));
423 _vertices->push_back(osg::Vec2f(-256.0, 0.0));
424 _vertices->push_back(osg::Vec2f(-256.0, 256.0 * centerY));
426 _vertices->push_back(osg::Vec2f(0.0, 0.0));
427 _vertices->push_back(osg::Vec2f(256.0, 0.0));
428 _vertices->push_back(osg::Vec2f(256.0, 256.0 * centerY));
430 _vertices->push_back(osg::Vec2f(-256, 0.0));
431 _vertices->push_back(osg::Vec2f(256.0, 0.0));
432 _vertices->push_back(osg::Vec2f(-256.0, -256.0));
434 _vertices->push_back(osg::Vec2f(256, 0.0));
435 _vertices->push_back(osg::Vec2f(256.0, -256.0));
436 _vertices->push_back(osg::Vec2f(-256.0, -256.0));
438 const osg::Vec2f whiteSpot(1.0f, 0.0f);
439 for (int i = 0; i < 3 * 4; i++)
440 _texCoords->push_back(whiteSpot);
442 trimaskPSet->set(osg::PrimitiveSet::TRIANGLES, firstQuadVert + 4, 3 * 4);
445 maskPSet->set(osg::PrimitiveSet::QUADS, 0, 0);
446 trimaskPSet->set(osg::PrimitiveSet::TRIANGLES, 0, 0);
450 trimaskPSet->dirty();
458 update_heading_marker();
460 quadPSet->set(osg::PrimitiveSet::QUADS, 0, _vertices->size());
467 wxRadarBg::update_weather()
469 string modeButton = _Instrument->getStringValue("mode", "wx");
470 _radarEchoBuffer = *sgEnviro.get_radar_echo();
472 // pretend we have a scan angle bigger then the FOV
473 // TODO:check real fov, enlarge if < nn, and do clipping if > mm
474 const float fovFactor = 1.45f;
475 _Instrument->setStringValue("status", modeButton.c_str());
477 list_of_SGWxRadarEcho *radarEcho = &_radarEchoBuffer;
478 list_of_SGWxRadarEcho::iterator iradarEcho, end = radarEcho->end();
479 const float LWClevel[] = { 0.1f, 0.5f, 2.1f };
481 // draw the cloud radar echo
482 bool drawClouds = _radar_weather_node->getBoolValue();
485 // we do that in 3 passes, one for each color level
486 // this is to 'merge' same colors together
487 for (int level = 0; level <= 2; level++) {
488 float col = level * UNIT;
490 for (iradarEcho = radarEcho->begin(); iradarEcho != end; ++iradarEcho) {
491 int cloudId = iradarEcho->cloudId;
492 bool upgrade = (cloudId >> 5) & 1;
493 float lwc = iradarEcho->LWC + (upgrade ? 1.0f : 0.0f);
496 if (iradarEcho->LWC >= 0.5 && iradarEcho->LWC <= 0.6)
499 if (iradarEcho->lightning || lwc < LWClevel[level])
502 float radius = sgSqrt(iradarEcho->dist) * SG_METER_TO_NM * _scale;
503 float size = iradarEcho->radius * 2.0 * SG_METER_TO_NM * _scale;
505 if (radius - size > 180)
508 float angle = (iradarEcho->heading - _angle_offset) //* fovFactor
511 // Rotate echo into position, and rotate echo to have
512 // a constant orientation towards the
513 // airplane. Compass headings increase in clockwise
514 // direction, while graphics rotations follow
515 // right-hand (counter-clockwise) rule.
516 const osg::Vec2f texBase(col, (UNIT * (float) (4 + (cloudId & 3))));
518 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
519 * osg::Matrixf::translate(0.0f, radius, 0.0f)
520 * wxRotate(angle) * _centerTrans);
521 addQuad(_vertices, _texCoords, m, texBase);
523 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing clouds"
524 // << " ID=" << cloudId
527 // << " radius=" << radius
528 // << " view_heading=" << _view_heading * SG_RADIANS_TO_DEGREES
529 // << " heading=" << iradarEcho->heading * SG_RADIANS_TO_DEGREES
530 // << " angle=" << angle * SG_RADIANS_TO_DEGREES);
535 // draw lightning echos
536 bool drawLightning = _Instrument->getBoolValue("lightning", true);
538 const osg::Vec2f texBase(3 * UNIT, 4 * UNIT);
540 for (iradarEcho = radarEcho->begin(); iradarEcho != end; ++iradarEcho) {
541 if (!iradarEcho->lightning)
544 float size = UNIT * 0.5f;
545 float radius = iradarEcho->dist * _scale;
546 float angle = iradarEcho->heading * SG_DEGREES_TO_RADIANS
549 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
551 * osg::Matrixf::translate(0.0f, radius, 0.0f)
552 * wxRotate(angle) * _centerTrans);
553 addQuad(_vertices, _texCoords, m, texBase);
560 wxRadarBg::update_data(FGAIBase *ac, double radius, double bearing, bool selected)
562 osgText::Text *callsign = new osgText::Text;
563 callsign->setFont(_font.get());
564 callsign->setFontResolution(12, 12);
565 callsign->setCharacterSize(_font_size);
566 callsign->setColor(selected ? osg::Vec4(1, 1, 1, 1) : _font_color);
567 osg::Matrixf m(wxRotate(-bearing)
568 * osg::Matrixf::translate(0.0f, radius, 0.0f)
569 * wxRotate(bearing) * _centerTrans);
571 osg::Vec3 pos = m.preMult(osg::Vec3(16, 16, 0));
572 // cast to int's, otherwise text comes out ugly
573 callsign->setPosition(osg::Vec3((int)pos.x(), (int)pos.y(), 0));
574 callsign->setAlignment(osgText::Text::LEFT_BOTTOM_BASE_LINE);
575 callsign->setLineSpacing(_font_spacing);
578 text << ac->_getCallsign() << endl
579 << setprecision(0) << fixed
580 << setw(3) << setfill('0') << ac->_getHeading() << "\xB0 "
581 << setw(0) << ac->_getAltitude() << "ft" << endl
582 << ac->_getSpeed() << "kts";
584 callsign->setText(text.str());
585 _textGeode->addDrawable(callsign);
590 wxRadarBg::update_aircraft()
592 if (!_ai_enabled_node->getBoolValue())
595 bool draw_echoes = _radar_position_node->getBoolValue();
596 bool draw_symbols = _radar_symbol_node->getBoolValue();
597 bool draw_data = _radar_data_node->getBoolValue();
598 if (!draw_echoes && !draw_symbols && !draw_data)
601 radar_list_type radar_list = _ai->get_ai_list();
602 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: AI submodel list size" << radar_list.size());
603 if (radar_list.empty())
606 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: Loading AI submodels ");
607 const double echo_radii[] = {0, 1, 1.5, 1.5, 0.001, 0.1, 1.5, 2, 1.5, 1.5, 1.5};
609 double user_lat = _user_lat_node->getDoubleValue();
610 double user_lon = _user_lon_node->getDoubleValue();
611 double user_alt = _user_alt_node->getDoubleValue();
613 float limit = _radar_coverage_node->getFloatValue();
618 limit *= SG_DEGREES_TO_RADIANS;
620 radar_list_iterator it = radar_list.begin();
621 radar_list_iterator end = radar_list.end();
622 FGAIBase *selected_ac = 0;
623 double selected_radius = 0;
624 double selected_bearing = 0;
625 int selected_id = fgGetInt("/instrumentation/radar/selected-id", -1);
627 for (; it != end; ++it) {
628 FGAIBase *ac = (*it).get();
629 int type = ac->getType();
630 double lat = ac->_getLatitude();
631 double lon = ac->_getLongitude();
632 double alt = ac->_getAltitude();
633 double heading = ac->_getHeading();
635 double range, bearing;
636 calcRangeBearing(user_lat, user_lon, lat, lon, range, bearing);
638 //SG_LOG(SG_GENERAL, SG_DEBUG,
639 /* "Radar: ID=" << ac->getID() << "(" << radar_list.size() << ")"
641 << " view_heading=" << _view_heading * SG_RADIANS_TO_DEGREES
643 << " heading=" << heading
644 << " range=" << range
645 << " bearing=" << bearing);*/
647 bool isVisible = withinRadarHorizon(user_alt, alt, range);
648 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: visible " << isVisible);
652 if (!inRadarRange(type, range))
655 bearing *= SG_DEGREES_TO_RADIANS;
656 heading *= SG_DEGREES_TO_RADIANS;
658 float radius = range * _scale;
659 float angle = calcRelBearing(bearing, _view_heading);
661 if (angle > limit || angle < -limit)
664 bearing += _angle_offset;
665 heading += _angle_offset;
669 float echo_radius = echo_radii[type] * 120;
670 float size = echo_radius * UNIT;
672 const osg::Vec2f texBase(3 * UNIT, 3 * UNIT);
673 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
674 * osg::Matrixf::translate(0.0f, radius, 0.0f)
675 * wxRotate(bearing) * _centerTrans);
676 addQuad(_vertices, _texCoords, m, texBase);
678 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing AI"
679 //<< " ID=" << ac->getID()
680 //<< " type=" << type
681 // << " radius=" << radius
682 // << " angle=" << angle * SG_RADIANS_TO_DEGREES);
687 const osg::Vec2f texBase(0, 3 * UNIT);
688 float size = 600 * UNIT;
689 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
690 * wxRotate(heading - bearing)
691 * osg::Matrixf::translate(0.0f, radius, 0.0f)
692 * wxRotate(bearing) * _centerTrans);
693 addQuad(_vertices, _texCoords, m, texBase);
695 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing data"
696 // << " x=" << x <<" y="<< y
697 // << " bearing=" << angle * SG_RADIANS_TO_DEGREES
698 // << " radius=" << radius);
702 if (ac->getID() == selected_id) {
704 selected_radius = radius;
705 selected_bearing = bearing;
707 update_data(ac, radius, bearing, false);
712 update_data(selected_ac, selected_radius, selected_bearing, true);
718 wxRadarBg::update_tacan()
721 int mode = _radar_mode_control_node->getIntValue();
722 bool inRange = _tacan_in_range_node->getBoolValue();
724 if (mode != 1 || !inRange)
727 float size = 600 * UNIT;
728 float radius = _tacan_distance_node->getFloatValue() * _scale;
729 float angle = _tacan_bearing_node->getFloatValue() * SG_DEGREES_TO_RADIANS
732 const osg::Vec2f texBase(1 * UNIT, 3 * UNIT);
733 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
735 * osg::Matrixf::translate(0.0f, radius, 0.0f)
736 * wxRotate(angle) * _centerTrans);
737 addQuad(_vertices, _texCoords, m, texBase);
739 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing TACAN"
740 // << " dist=" << radius
741 // << " view_heading=" << _view_heading * SG_RADIANS_TO_DEGREES
742 // << " bearing=" << angle * SG_RADIANS_TO_DEGREES
743 // << " x=" << x << " y="<< y
744 // << " size=" << size);
749 wxRadarBg::update_heading_marker()
751 if (!_radar_hdg_marker_node->getBoolValue())
754 const osg::Vec2f texBase(2 * UNIT, 3 * UNIT);
755 float size = 600 * UNIT;
756 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
757 * wxRotate(_view_heading + _angle_offset));
760 addQuad(_vertices, _texCoords, m, texBase);
762 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing heading marker"
763 // << " x,y " << x <<","<< y
764 // << " dist" << dist
765 // << " view_heading" << _view_heading * SG_RADIANS_TO_DEGREES
766 // << " heading " << iradarEcho->heading * SG_RADIANS_TO_DEGREES
767 // << " angle " << angle * SG_RADIANS_TO_DEGREES);
772 wxRadarBg::center_map()
774 _lat = _user_lat_node->getDoubleValue();
775 _lon = _user_lon_node->getDoubleValue();
776 _x_offset = _y_offset = 0;
781 wxRadarBg::apply_map_offset()
783 double lat = _user_lat_node->getDoubleValue();
784 double lon = _user_lon_node->getDoubleValue();
785 double bearing, distance, az2;
786 geo_inverse_wgs_84(_lat, _lon, lat, lon, &bearing, &az2, &distance);
787 distance *= SG_METER_TO_NM * _scale;
788 bearing *= SG_DEGREES_TO_RADIANS;
789 _x_offset += sin(bearing) * distance;
790 _y_offset += cos(bearing) * distance;
797 wxRadarBg::withinRadarHorizon(double user_alt, double alt, double range_nm)
799 // Radar Horizon = 1.23(ht^1/2 + hr^1/2),
800 //don't allow negative altitudes (an approximation - yes altitudes can be negative)
808 double radarhorizon = 1.23 * (sqrt(alt) + sqrt(user_alt));
809 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: horizon " << radarhorizon);
810 return radarhorizon >= range_nm;
815 wxRadarBg::inRadarRange(int type, double range_nm)
817 //The Radar Equation:
819 // MaxRange^4 = (TxPower * AntGain^2 * lambda^2 * sigma)/((constant) * MDS)
821 // Where (constant) = (4*pi)3 and MDS is the Minimum Detectable Signal power.
823 // For a given radar we can assume that the only variable is sigma,
824 // the target radar cross section.
826 // Here, we will use a normalised rcs (sigma) for a standard taget and assume that this
827 // will provide a maximum range of 35nm;
829 // TODO - make the maximum range adjustable at runtime
831 const double sigma[] = {0, 1, 100, 100, 0.001, 0.1, 100, 100, 1, 1, 1};
832 double constant = _radar_ref_rng;
837 double maxrange = constant * pow(sigma[type], 0.25);
838 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: max range " << maxrange);
839 return maxrange >= range_nm;
844 wxRadarBg::calcRangeBearing(double lat, double lon, double lat2, double lon2,
845 double &range, double &bearing) const
847 // calculate the bearing and range of the second pos from the first
848 double az2, distance;
849 geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
850 range = distance *= SG_METER_TO_NM;
855 wxRadarBg::calcRelBearing(float bearing, float heading)
857 float angle = bearing - heading;
860 angle -= 2.0 * SG_PI;
863 angle += 2.0 * SG_PI;
870 wxRadarBg::updateFont()
872 float red = _font_node->getFloatValue("color/red");
873 float green = _font_node->getFloatValue("color/green");
874 float blue = _font_node->getFloatValue("color/blue");
875 float alpha = _font_node->getFloatValue("color/alpha");
876 _font_color.set(red, green, blue, alpha);
878 _font_size = _font_node->getFloatValue("size");
879 _font_spacing = _font_size * _font_node->getFloatValue("line-spacing");
880 string path = _font_node->getStringValue("name", DEFAULT_FONT);
883 if (path[0] != '/') {
884 tpath = globals->get_fg_root();
885 tpath.append("Fonts");
891 #if (FG_OSG_VERSION >= 21000)
892 osg::ref_ptr<osgDB::ReaderWriter::Options> fontOptions = new osgDB::ReaderWriter::Options("monochrome");
893 osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.c_str(), fontOptions.get());
895 osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.c_str());
900 _font->setMinFilterHint(osg::Texture::NEAREST);
901 _font->setMagFilterHint(osg::Texture::NEAREST);
902 _font->setGlyphImageMargin(0);
903 _font->setGlyphImageMarginRatio(0);
908 wxRadarBg::valueChanged(SGPropertyNode*)