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 using std::stringstream;
51 using std::setprecision;
56 #include <Main/fg_props.hxx>
57 #include <Main/globals.hxx>
58 #include <Cockpit/panel.hxx>
59 #include <Cockpit/hud.hxx>
61 #include <Include/general.hxx>
62 #include "instrument_mgr.hxx"
63 #include "od_gauge.hxx"
64 #include "wxradar.hxx"
68 static const float UNIT = 1.0f / 8.0f; // 8 symbols in a row/column in the texture
69 static const char *DEFAULT_FONT = "typewriter.txf";
72 wxRadarBg::wxRadarBg(SGPropertyNode *node) :
73 _name(node->getStringValue("name", "radar")),
74 _num(node->getIntValue("number", 0)),
75 _interval(node->getDoubleValue("update-interval-sec", 1.0)),
77 _sim_init_done(false),
79 _last_switchKnob("off"),
84 branch = "/instrumentation/" + _name;
85 _Instrument = fgGetNode(branch.c_str(), _num, true);
87 const char *tacan_source = node->getStringValue("tacan-source", "/instrumentation/tacan");
88 _Tacan = fgGetNode(tacan_source, true);
90 _font_node = _Instrument->getNode("font", true);
92 #define INITFONT(p, val, type) if (!_font_node->hasValue(p)) _font_node->set##type##Value(p, val)
93 INITFONT("name", DEFAULT_FONT, String);
94 INITFONT("size", 8, Float);
95 INITFONT("line-spacing", 0.25, Float);
96 INITFONT("color/red", 0, Float);
97 INITFONT("color/green", 0.8, Float);
98 INITFONT("color/blue", 0, Float);
99 INITFONT("color/alpha", 1, Float);
102 _font_node->addChangeListener(this, true);
106 wxRadarBg::~wxRadarBg ()
108 _font_node->removeChangeListener(this);
115 _serviceable_node = _Instrument->getNode("serviceable", true);
117 // texture name to use in 2D and 3D instruments
118 _texture_path = _Instrument->getStringValue("radar-texture-path",
119 "Aircraft/Instruments/Textures/od_wxradar.rgb");
120 _resultTexture = FGTextureManager::createTexture(_texture_path.c_str(), false);
122 SGPath tpath(globals->get_fg_root());
123 string path = _Instrument->getStringValue("echo-texture-path",
124 "Aircraft/Instruments/Textures/wxecho.rgb");
127 // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect
128 _wxEcho = SGLoadTexture2D(tpath, false, false);
131 _Instrument->setFloatValue("trk", 0.0);
132 _Instrument->setFloatValue("tilt", 0.0);
133 _Instrument->setStringValue("status", "");
134 // those properties are used by a radar instrument of a MFD
135 // input switch = OFF | TST | STBY | ON
136 // input mode = WX | WXA | MAP
137 // output status = STBY | TEST | WX | WXA | MAP | blank
138 // input lightning = true | false
139 // input TRK = +/- n degrees
140 // input TILT = +/- n degree
141 // input autotilt = true | false
142 // input range = n nm (20/40/80)
143 // input display-mode = arc | rose | map | plan
145 FGInstrumentMgr *imgr = (FGInstrumentMgr *)globals->get_subsystem("instrumentation");
146 _odg = (FGODGauge *)imgr->get_subsystem("od_gauge");
149 _ai_enabled_node = fgGetNode("/sim/ai/enabled", true);
151 _user_lat_node = fgGetNode("/position/latitude-deg", true);
152 _user_lon_node = fgGetNode("/position/longitude-deg", true);
153 _user_alt_node = fgGetNode("/position/altitude-ft", true);
155 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps", true);
156 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps", true);
158 _tacan_serviceable_node = _Tacan->getNode("serviceable", true);
159 _tacan_distance_node = _Tacan->getNode("indicated-distance-nm", true);
160 _tacan_name_node = _Tacan->getNode("name", true);
161 _tacan_bearing_node = _Tacan->getNode("indicated-bearing-true-deg", true);
162 _tacan_in_range_node = _Tacan->getNode("in-range", true);
164 _radar_mode_control_node = _Instrument->getNode("mode-control", true);
165 _radar_coverage_node = _Instrument->getNode("limit-deg", true);
166 _radar_ref_rng_node = _Instrument->getNode("reference-range-nm", true);
167 _radar_hdg_marker_node = _Instrument->getNode("heading-marker", true);
169 SGPropertyNode *n = _Instrument->getNode("display-controls", true);
170 _radar_weather_node = n->getNode("WX", true);
171 _radar_position_node = n->getNode("pos", true);
172 _radar_data_node = n->getNode("data", true);
173 _radar_symbol_node = n->getNode("symbol", true);
174 _radar_centre_node = n->getNode("centre", true);
175 _radar_rotate_node = n->getNode("rotate", true);
177 _radar_centre_node->setBoolValue(false);
178 if (!_radar_coverage_node->hasValue())
179 _radar_coverage_node->setFloatValue(120);
180 if (!_radar_ref_rng_node->hasValue())
181 _radar_ref_rng_node->setDoubleValue(35);
182 if (!_radar_hdg_marker_node->hasValue())
183 _radar_hdg_marker_node->setBoolValue(true);
188 // OSG geometry setup. The polygons for the radar returns will be
189 // stored in a single Geometry. The geometry will have several
190 // primitive sets so we can have different kinds of polys and
191 // choose a different overall color for each set.
192 _radarGeode = new osg::Geode;
193 osg::StateSet *stateSet = _radarGeode->getOrCreateStateSet();
194 stateSet->setTextureAttributeAndModes(0, _wxEcho.get());
195 _geom = new osg::Geometry;
196 _geom->setUseDisplayList(false);
197 // Initially allocate space for 128 quads
198 _vertices = new osg::Vec2Array;
199 _vertices->setDataVariance(osg::Object::DYNAMIC);
200 _vertices->reserve(128 * 4);
201 _geom->setVertexArray(_vertices);
202 _texCoords = new osg::Vec2Array;
203 _texCoords->setDataVariance(osg::Object::DYNAMIC);
204 _texCoords->reserve(128 * 4);
205 _geom->setTexCoordArray(0, _texCoords);
206 osg::Vec3Array *colors = new osg::Vec3Array;
207 colors->push_back(osg::Vec3(1.0f, 1.0f, 1.0f)); // color of echos
208 colors->push_back(osg::Vec3(1.0f, 0.0f, 0.0f)); // arc mask
209 colors->push_back(osg::Vec3(0.0f, 0.0f, 0.0f)); // rest of mask
210 _geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
211 _geom->setColorArray(colors);
212 osg::PrimitiveSet *pset = new osg::DrawArrays(osg::PrimitiveSet::QUADS);
213 pset->setDataVariance(osg::Object::DYNAMIC);
214 _geom->addPrimitiveSet(pset);
215 pset = new osg::DrawArrays(osg::PrimitiveSet::QUADS);
216 pset->setDataVariance(osg::Object::DYNAMIC);
217 _geom->addPrimitiveSet(pset);
218 pset = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES);
219 pset->setDataVariance(osg::Object::DYNAMIC);
220 _geom->addPrimitiveSet(pset);
221 _geom->setInitialBound(osg::BoundingBox(osg::Vec3f(-256.0f, -256.0f, 0.0f),
222 osg::Vec3f(256.0f, 256.0f, 0.0f)));
223 _radarGeode->addDrawable(_geom);
225 // Texture in the 2D panel system
226 FGTextureManager::addTexture(_texture_path.c_str(), _odg->getTexture());
228 _textGeode = new osg::Geode;
230 osg::Camera *camera = _odg->getCamera();
231 camera->addChild(_radarGeode.get());
232 camera->addChild(_textGeode.get());
236 // Local coordinates for each echo
237 const osg::Vec3f echoCoords[4] = {
238 osg::Vec3f(-.7f, -.7f, 0.0f), osg::Vec3f(.7f, -.7f, 0.0f),
239 osg::Vec3f(.7f, .7f, 0.0f), osg::Vec3f(-.7f, .7f, 0.0f)
243 const osg::Vec2f echoTexCoords[4] = {
244 osg::Vec2f(0.0f, 0.0f), osg::Vec2f(UNIT, 0.0f),
245 osg::Vec2f(UNIT, UNIT), osg::Vec2f(0.0f, UNIT)
251 addQuad(osg::Vec2Array *vertices, osg::Vec2Array *texCoords,
252 const osg::Matrixf& transform, const osg::Vec2f& texBase)
254 for (int i = 0; i < 4; i++) {
255 const osg::Vec3f coords = transform.preMult(echoCoords[i]);
256 texCoords->push_back(texBase + echoTexCoords[i]);
257 vertices->push_back(osg::Vec2f(coords.x(), coords.y()));
262 // Rotate by a heading value
264 osg::Matrixf wxRotate(float angle)
266 return osg::Matrixf::rotate(angle, 0.0f, 0.0f, -1.0f);
271 wxRadarBg::update (double delta_time_sec)
273 if (!_sim_init_done) {
274 if (!fgGetBool("sim/sceneryloaded", false))
277 _sim_init_done = true;
279 if (!_odg || !_serviceable_node->getBoolValue()) {
280 _Instrument->setStringValue("status", "");
283 _time += delta_time_sec;
284 if (_time < _interval)
289 string mode = _Instrument->getStringValue("display-mode", "arc");
291 if (_display_mode != MAP) {
295 } else if (mode == "plan") {
296 _display_mode = PLAN;
301 string switchKnob = _Instrument->getStringValue("switch", "on");
302 if (_last_switchKnob != switchKnob) {
303 // since 3D models don't share textures with the rest of the world
304 // we must locate them and replace their handle by hand
305 // only do that when the instrument is turned on
306 //if (_last_switchKnob == "off")
307 //_odg->set_texture(_texture_path.c_str(), _resultTexture->getHandle());
309 _last_switchKnob = switchKnob;
312 if (switchKnob == "off") {
313 _Instrument->setStringValue("status", "");
314 } else if (switchKnob == "stby") {
315 _Instrument->setStringValue("status", "STBY");
316 } else if (switchKnob == "tst") {
317 _Instrument->setStringValue("status", "TST");
318 // find something interesting to do...
320 float r = _Instrument->getFloatValue("range", 40.0);
321 if (r != _range_nm) {
326 _radar_ref_rng = _radar_ref_rng_node->getDoubleValue();
327 _view_heading = get_heading() * SG_DEGREES_TO_RADIANS;
328 _centerTrans.makeTranslate(0.0f, 0.0f, 0.0f);
330 _scale = 200.0 / _range_nm;
333 if (_display_mode == ARC) {
334 _scale = 2*200.0f / _range_nm;
335 _angle_offset = -_view_heading;
336 _centerTrans.makeTranslate(0.0f, -200.0f, 0.0f);
338 } else if (_display_mode == MAP) {
341 bool centre = _radar_centre_node->getBoolValue();
344 _radar_centre_node->setBoolValue(false);
347 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: displacement "
348 // << _x_offset <<", "<<_y_offset
349 // << " user_speed_east_fps * SG_FPS_TO_KT "
350 // << user_speed_east_fps * SG_FPS_TO_KT
351 // << " user_speed_north_fps * SG_FPS_TO_KT "
352 // << user_speed_north_fps * SG_FPS_TO_KT
353 // << " dt " << delta_time_sec);
355 _centerTrans.makeTranslate(_x_offset, _y_offset, 0.0f);
357 } else if (_display_mode == PLAN) {
358 if (_radar_rotate_node->getBoolValue()) {
359 _angle_offset = -_view_heading;
367 _textGeode->removeDrawables(0, _textGeode->getNumDrawables());
373 osg::DrawArrays *quadPSet
374 = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(0));
375 quadPSet->set(osg::PrimitiveSet::QUADS, 0, _vertices->size());
378 // erase what is out of sight of antenna
389 osg::DrawArrays *maskPSet
390 = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(1));
391 osg::DrawArrays *trimaskPSet
392 = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(2));
394 if (_display_mode == ARC) {
395 float xOffset = 256.0f;
396 float yOffset = 200.0f;
398 int firstQuadVert = _vertices->size();
399 _texCoords->push_back(osg::Vec2f(0.5f, 0.25f));
400 _vertices->push_back(osg::Vec2f(-xOffset, 0.0 + yOffset));
401 _texCoords->push_back(osg::Vec2f(1.0f, 0.25f));
402 _vertices->push_back(osg::Vec2f(xOffset, 0.0 + yOffset));
403 _texCoords->push_back(osg::Vec2f(1.0f, 0.5f));
404 _vertices->push_back(osg::Vec2f(xOffset, 256.0 + yOffset));
405 _texCoords->push_back(osg::Vec2f(0.5f, 0.5f));
406 _vertices->push_back(osg::Vec2f(-xOffset, 256.0 + yOffset));
407 maskPSet->set(osg::PrimitiveSet::QUADS, firstQuadVert, 4);
409 // The triangles aren't supposed to be textured, but there's
410 // no need to set up a different Geometry, switch modes,
411 // etc. I happen to know that there's a white pixel in the
412 // texture at 1.0, 0.0 :)
413 float centerY = tan(30 * SG_DEGREES_TO_RADIANS);
414 _vertices->push_back(osg::Vec2f(0.0, 0.0));
415 _vertices->push_back(osg::Vec2f(-256.0, 0.0));
416 _vertices->push_back(osg::Vec2f(-256.0, 256.0 * centerY));
418 _vertices->push_back(osg::Vec2f(0.0, 0.0));
419 _vertices->push_back(osg::Vec2f(256.0, 0.0));
420 _vertices->push_back(osg::Vec2f(256.0, 256.0 * centerY));
422 _vertices->push_back(osg::Vec2f(-256, 0.0));
423 _vertices->push_back(osg::Vec2f(256.0, 0.0));
424 _vertices->push_back(osg::Vec2f(-256.0, -256.0));
426 _vertices->push_back(osg::Vec2f(256, 0.0));
427 _vertices->push_back(osg::Vec2f(256.0, -256.0));
428 _vertices->push_back(osg::Vec2f(-256.0, -256.0));
430 const osg::Vec2f whiteSpot(1.0f, 0.0f);
431 for (int i = 0; i < 3 * 4; i++)
432 _texCoords->push_back(whiteSpot);
434 trimaskPSet->set(osg::PrimitiveSet::TRIANGLES, firstQuadVert + 4, 3 * 4);
437 maskPSet->set(osg::PrimitiveSet::QUADS, 0, 0);
438 trimaskPSet->set(osg::PrimitiveSet::TRIANGLES, 0, 0);
442 trimaskPSet->dirty();
450 update_heading_marker();
452 quadPSet->set(osg::PrimitiveSet::QUADS, 0, _vertices->size());
459 wxRadarBg::update_weather()
461 string modeButton = _Instrument->getStringValue("mode", "wx");
462 _radarEchoBuffer = *sgEnviro.get_radar_echo();
464 // pretend we have a scan angle bigger then the FOV
465 // TODO:check real fov, enlarge if < nn, and do clipping if > mm
466 const float fovFactor = 1.45f;
467 _Instrument->setStringValue("status", modeButton.c_str());
469 list_of_SGWxRadarEcho *radarEcho = &_radarEchoBuffer;
470 list_of_SGWxRadarEcho::iterator iradarEcho, end = radarEcho->end();
471 const float LWClevel[] = { 0.1f, 0.5f, 2.1f };
473 // draw the cloud radar echo
474 bool drawClouds = _radar_weather_node->getBoolValue();
477 // we do that in 3 passes, one for each color level
478 // this is to 'merge' same colors together
479 for (int level = 0; level <= 2; level++) {
480 float col = level * UNIT;
482 for (iradarEcho = radarEcho->begin(); iradarEcho != end; ++iradarEcho) {
483 int cloudId = iradarEcho->cloudId;
484 bool upgrade = (cloudId >> 5) & 1;
485 float lwc = iradarEcho->LWC + (upgrade ? 1.0f : 0.0f);
488 if (iradarEcho->LWC >= 0.5 && iradarEcho->LWC <= 0.6)
491 if (iradarEcho->lightning || lwc < LWClevel[level])
494 float radius = sgSqrt(iradarEcho->dist) * SG_METER_TO_NM * _scale;
495 float size = iradarEcho->radius * 2.0 * SG_METER_TO_NM * _scale;
497 if (radius - size > 180)
500 float angle = (iradarEcho->heading - _angle_offset) //* fovFactor
503 // Rotate echo into position, and rotate echo to have
504 // a constant orientation towards the
505 // airplane. Compass headings increase in clockwise
506 // direction, while graphics rotations follow
507 // right-hand (counter-clockwise) rule.
508 const osg::Vec2f texBase(col, (UNIT * (float) (4 + (cloudId & 3))));
510 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
511 * osg::Matrixf::translate(0.0f, radius, 0.0f)
512 * wxRotate(angle) * _centerTrans);
513 addQuad(_vertices, _texCoords, m, texBase);
515 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing clouds"
516 // << " ID=" << cloudId
519 // << " radius=" << radius
520 // << " view_heading=" << _view_heading * SG_RADIANS_TO_DEGREES
521 // << " heading=" << iradarEcho->heading * SG_RADIANS_TO_DEGREES
522 // << " angle=" << angle * SG_RADIANS_TO_DEGREES);
527 // draw lightning echos
528 bool drawLightning = _Instrument->getBoolValue("lightning", true);
530 const osg::Vec2f texBase(3 * UNIT, 4 * UNIT);
532 for (iradarEcho = radarEcho->begin(); iradarEcho != end; ++iradarEcho) {
533 if (!iradarEcho->lightning)
536 float size = UNIT * 0.5f;
537 float radius = iradarEcho->dist * _scale;
538 float angle = iradarEcho->heading * SG_DEGREES_TO_RADIANS
541 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
543 * osg::Matrixf::translate(0.0f, radius, 0.0f)
544 * wxRotate(angle) * _centerTrans);
545 addQuad(_vertices, _texCoords, m, texBase);
552 wxRadarBg::update_data(const SGPropertyNode *ac, double altitude, double heading,
553 double radius, double bearing, bool selected)
555 osgText::Text *callsign = new osgText::Text;
556 callsign->setFont(_font.get());
557 callsign->setFontResolution(12, 12);
558 callsign->setCharacterSize(_font_size);
559 callsign->setColor(selected ? osg::Vec4(1, 1, 1, 1) : _font_color);
560 osg::Matrixf m(wxRotate(-bearing)
561 * osg::Matrixf::translate(0.0f, radius, 0.0f)
562 * wxRotate(bearing) * _centerTrans);
564 osg::Vec3 pos = m.preMult(osg::Vec3(16, 16, 0));
565 // cast to int's, otherwise text comes out ugly
566 callsign->setPosition(osg::Vec3((int)pos.x(), (int)pos.y(), 0));
567 callsign->setAlignment(osgText::Text::LEFT_BOTTOM_BASE_LINE);
568 callsign->setLineSpacing(_font_spacing);
570 const char *identity = ac->getStringValue("transponder-id");
572 identity = ac->getStringValue("callsign");
575 text << identity << endl
576 << setprecision(0) << fixed
577 << setw(3) << setfill('0') << heading * SG_RADIANS_TO_DEGREES << "\xB0 "
578 << setw(0) << altitude << "ft" << endl
579 << ac->getDoubleValue("velocities/true-airspeed-kt") << "kts";
581 callsign->setText(text.str());
582 _textGeode->addDrawable(callsign);
587 wxRadarBg::update_aircraft()
589 if (!_ai_enabled_node->getBoolValue())
592 bool draw_echoes = _radar_position_node->getBoolValue();
593 bool draw_symbols = _radar_symbol_node->getBoolValue();
594 bool draw_data = _radar_data_node->getBoolValue();
595 if (!draw_echoes && !draw_symbols && !draw_data)
598 double user_lat = _user_lat_node->getDoubleValue();
599 double user_lon = _user_lon_node->getDoubleValue();
600 double user_alt = _user_alt_node->getDoubleValue();
602 float limit = _radar_coverage_node->getFloatValue();
607 limit *= SG_DEGREES_TO_RADIANS;
609 int selected_id = fgGetInt("/instrumentation/radar/selected-id", -1);
611 const SGPropertyNode *selected_ac = 0;
612 const SGPropertyNode *ai = fgGetNode("/ai/models", true);
614 for (int i = ai->nChildren() - 1; i >= -1; i--) {
615 const SGPropertyNode *model;
617 if (i < 0) { // last iteration: selected model
620 model = ai->getChild(i);
621 if (!model->nChildren())
623 if (model->getIntValue("id") == selected_id) {
624 selected_ac = model; // save selected model for last iteration
631 double echo_radius, sigma;
632 const string name = model->getName();
633 if (name == "aircraft" || name == "tanker")
634 echo_radius = 1, sigma = 1;
635 else if (name == "multiplayer" || name == "wingman" || name == "static")
636 echo_radius = 1.5, sigma = 1;
637 else if (name == "ship" || name == "carrier" || name == "storm")
638 echo_radius = 1.5, sigma = 100;
639 else if (name == "thermal")
640 echo_radius = 2, sigma = 100;
641 else if (name == "rocket")
642 echo_radius = 0.1, sigma = 0.1;
643 else if (name == "ballistic")
644 echo_radius = 0.001, sigma = 0.001;
648 double lat = model->getDoubleValue("position/latitude-deg");
649 double lon = model->getDoubleValue("position/longitude-deg");
650 double alt = model->getDoubleValue("position/altitude-ft");
651 double heading = model->getDoubleValue("orientation/true-heading-deg");
653 double range, bearing;
654 calcRangeBearing(user_lat, user_lon, lat, lon, range, bearing);
656 bool isVisible = withinRadarHorizon(user_alt, alt, range);
661 if (!inRadarRange(sigma, range))
664 bearing *= SG_DEGREES_TO_RADIANS;
665 heading *= SG_DEGREES_TO_RADIANS;
667 float radius = range * _scale;
668 float angle = calcRelBearing(bearing, _view_heading);
670 if (angle > limit || angle < -limit)
673 bearing += _angle_offset;
674 heading += _angle_offset;
678 float size = echo_radius * 120 * UNIT;
680 const osg::Vec2f texBase(3 * UNIT, 3 * UNIT);
681 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
682 * osg::Matrixf::translate(0.0f, radius, 0.0f)
683 * wxRotate(bearing) * _centerTrans);
684 addQuad(_vertices, _texCoords, m, texBase);
689 const osg::Vec2f texBase(0, 3 * UNIT);
690 float size = 600 * UNIT;
691 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
692 * wxRotate(heading - bearing)
693 * osg::Matrixf::translate(0.0f, radius, 0.0f)
694 * wxRotate(bearing) * _centerTrans);
695 addQuad(_vertices, _texCoords, m, texBase);
698 if (draw_data || i < 0) // selected one (i == -1) is always drawn
699 update_data(model, alt, heading, radius, bearing, i < 0);
705 wxRadarBg::update_tacan()
708 int mode = _radar_mode_control_node->getIntValue();
709 bool inRange = _tacan_in_range_node->getBoolValue();
711 if (mode != 1 || !inRange)
714 float size = 600 * UNIT;
715 float radius = _tacan_distance_node->getFloatValue() * _scale;
716 float angle = _tacan_bearing_node->getFloatValue() * SG_DEGREES_TO_RADIANS
719 const osg::Vec2f texBase(1 * UNIT, 3 * UNIT);
720 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
722 * osg::Matrixf::translate(0.0f, radius, 0.0f)
723 * wxRotate(angle) * _centerTrans);
724 addQuad(_vertices, _texCoords, m, texBase);
726 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing TACAN"
727 // << " dist=" << radius
728 // << " view_heading=" << _view_heading * SG_RADIANS_TO_DEGREES
729 // << " bearing=" << angle * SG_RADIANS_TO_DEGREES
730 // << " x=" << x << " y="<< y
731 // << " size=" << size);
736 wxRadarBg::update_heading_marker()
738 if (!_radar_hdg_marker_node->getBoolValue())
741 const osg::Vec2f texBase(2 * UNIT, 3 * UNIT);
742 float size = 600 * UNIT;
743 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
744 * wxRotate(_view_heading + _angle_offset));
747 addQuad(_vertices, _texCoords, m, texBase);
749 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing heading marker"
750 // << " x,y " << x <<","<< y
751 // << " dist" << dist
752 // << " view_heading" << _view_heading * SG_RADIANS_TO_DEGREES
753 // << " heading " << iradarEcho->heading * SG_RADIANS_TO_DEGREES
754 // << " angle " << angle * SG_RADIANS_TO_DEGREES);
759 wxRadarBg::center_map()
761 _lat = _user_lat_node->getDoubleValue();
762 _lon = _user_lon_node->getDoubleValue();
763 _x_offset = _y_offset = 0;
768 wxRadarBg::apply_map_offset()
770 double lat = _user_lat_node->getDoubleValue();
771 double lon = _user_lon_node->getDoubleValue();
772 double bearing, distance, az2;
773 geo_inverse_wgs_84(_lat, _lon, lat, lon, &bearing, &az2, &distance);
774 distance *= SG_METER_TO_NM * _scale;
775 bearing *= SG_DEGREES_TO_RADIANS;
776 _x_offset += sin(bearing) * distance;
777 _y_offset += cos(bearing) * distance;
784 wxRadarBg::withinRadarHorizon(double user_alt, double alt, double range_nm)
786 // Radar Horizon = 1.23(ht^1/2 + hr^1/2),
787 //don't allow negative altitudes (an approximation - yes altitudes can be negative)
795 double radarhorizon = 1.23 * (sqrt(alt) + sqrt(user_alt));
796 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: horizon " << radarhorizon);
797 return radarhorizon >= range_nm;
802 wxRadarBg::inRadarRange(double sigma, double range_nm)
804 //The Radar Equation:
806 // MaxRange^4 = (TxPower * AntGain^2 * lambda^2 * sigma)/((constant) * MDS)
808 // Where (constant) = (4*pi)3 and MDS is the Minimum Detectable Signal power.
810 // For a given radar we can assume that the only variable is sigma,
811 // the target radar cross section.
813 // Here, we will use a normalised rcs (sigma) for a standard taget and assume that this
814 // will provide a maximum range of 35nm;
816 // TODO - make the maximum range adjustable at runtime
818 double constant = _radar_ref_rng;
823 double maxrange = constant * pow(sigma, 0.25);
824 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: max range " << maxrange);
825 return maxrange >= range_nm;
830 wxRadarBg::calcRangeBearing(double lat, double lon, double lat2, double lon2,
831 double &range, double &bearing) const
833 // calculate the bearing and range of the second pos from the first
834 double az2, distance;
835 geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
836 range = distance *= SG_METER_TO_NM;
841 wxRadarBg::calcRelBearing(float bearing, float heading)
843 float angle = bearing - heading;
846 angle -= 2.0 * SG_PI;
849 angle += 2.0 * SG_PI;
856 wxRadarBg::updateFont()
858 float red = _font_node->getFloatValue("color/red");
859 float green = _font_node->getFloatValue("color/green");
860 float blue = _font_node->getFloatValue("color/blue");
861 float alpha = _font_node->getFloatValue("color/alpha");
862 _font_color.set(red, green, blue, alpha);
864 _font_size = _font_node->getFloatValue("size");
865 _font_spacing = _font_size * _font_node->getFloatValue("line-spacing");
866 string path = _font_node->getStringValue("name", DEFAULT_FONT);
869 if (path[0] != '/') {
870 tpath = globals->get_fg_root();
871 tpath.append("Fonts");
877 #if (FG_OSG_VERSION >= 21000)
878 osg::ref_ptr<osgDB::ReaderWriter::Options> fontOptions = new osgDB::ReaderWriter::Options("monochrome");
879 osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.c_str(), fontOptions.get());
881 osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.c_str());
886 _font->setMinFilterHint(osg::Texture::NEAREST);
887 _font->setMagFilterHint(osg::Texture::NEAREST);
888 _font->setGlyphImageMargin(0);
889 _font->setGlyphImageMarginRatio(0);
894 wxRadarBg::valueChanged(SGPropertyNode*)