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/scene/model/model.hxx>
42 #include <simgear/structure/exception.hxx>
43 #include <simgear/misc/sg_path.hxx>
44 #include <simgear/math/sg_geodesy.hxx>
48 using std::stringstream;
50 using std::setprecision;
55 #include <Main/fg_props.hxx>
56 #include <Main/globals.hxx>
57 #include <Cockpit/panel.hxx>
59 #include "instrument_mgr.hxx"
60 #include "od_gauge.hxx"
61 #include "wxradar.hxx"
63 #include <iostream> // for cout, endl
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";
71 wxRadarBg::wxRadarBg(SGPropertyNode *node) :
72 _name(node->getStringValue("name", "radar")),
73 _num(node->getIntValue("number", 0)),
75 _interval(node->getDoubleValue("update-interval-sec", 1.0)),
78 _sim_init_done(false),
89 _antenna_ht(node->getDoubleValue("antenna-ht-ft", 0.0)),
96 branch = "/instrumentation/" + _name;
97 _Instrument = fgGetNode(branch.c_str(), _num, true);
99 const char *tacan_source = node->getStringValue("tacan-source", "/instrumentation/tacan");
100 _Tacan = fgGetNode(tacan_source, true);
102 _font_node = _Instrument->getNode("font", true);
104 #define INITFONT(p, val, type) if (!_font_node->hasValue(p)) _font_node->set##type##Value(p, val)
105 INITFONT("name", DEFAULT_FONT, String);
106 INITFONT("size", 8, Float);
107 INITFONT("line-spacing", 0.25, Float);
108 INITFONT("color/red", 0, Float);
109 INITFONT("color/green", 0.8, Float);
110 INITFONT("color/blue", 0, Float);
111 INITFONT("color/alpha", 1, Float);
114 _font_node->addChangeListener(this, true);
118 wxRadarBg::~wxRadarBg ()
120 _font_node->removeChangeListener(this);
127 _serviceable_node = _Instrument->getNode("serviceable", true);
129 // texture name to use in 2D and 3D instruments
130 _texture_path = _Instrument->getStringValue("radar-texture-path",
131 "Aircraft/Instruments/Textures/od_wxradar.rgb");
132 _resultTexture = FGTextureManager::createTexture(_texture_path.c_str(), false);
134 string path = _Instrument->getStringValue("echo-texture-path",
135 "Aircraft/Instruments/Textures/wxecho.rgb");
136 SGPath tpath = globals->resolve_aircraft_path(path);
138 // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect
139 _wxEcho = SGLoadTexture2D(tpath, NULL, false, false);
142 _Instrument->setFloatValue("trk", 0.0);
143 _Instrument->setFloatValue("tilt", 0.0);
144 _Instrument->setStringValue("status", "");
145 // those properties are used by a radar instrument of a MFD
146 // input switch = OFF | TST | STBY | ON
147 // input mode = WX | WXA | MAP
148 // output status = STBY | TEST | WX | WXA | MAP | blank
149 // input lightning = true | false
150 // input TRK = +/- n degrees
151 // input TILT = +/- n degree
152 // input autotilt = true | false
153 // input range = n nm (20/40/80)
154 // input display-mode = arc | rose | map | plan
156 FGInstrumentMgr *imgr = (FGInstrumentMgr *)globals->get_subsystem("instrumentation");
157 _odg = (FGODGauge *)imgr->get_subsystem("od_gauge");
160 _ai_enabled_node = fgGetNode("/sim/ai/enabled", true);
162 _user_lat_node = fgGetNode("/position/latitude-deg", true);
163 _user_lon_node = fgGetNode("/position/longitude-deg", true);
164 _user_alt_node = fgGetNode("/position/altitude-ft", true);
166 _user_speed_east_fps_node = fgGetNode("/velocities/speed-east-fps", true);
167 _user_speed_north_fps_node = fgGetNode("/velocities/speed-north-fps", true);
169 _tacan_serviceable_node = _Tacan->getNode("serviceable", true);
170 _tacan_distance_node = _Tacan->getNode("indicated-distance-nm", true);
171 _tacan_name_node = _Tacan->getNode("name", true);
172 _tacan_bearing_node = _Tacan->getNode("indicated-bearing-true-deg", true);
173 _tacan_in_range_node = _Tacan->getNode("in-range", true);
175 _radar_mode_control_node = _Instrument->getNode("mode-control", true);
176 _radar_coverage_node = _Instrument->getNode("limit-deg", true);
177 _radar_ref_rng_node = _Instrument->getNode("reference-range-nm", true);
178 _radar_hdg_marker_node = _Instrument->getNode("heading-marker", true);
180 SGPropertyNode *n = _Instrument->getNode("display-controls", true);
181 _radar_weather_node = n->getNode("WX", true);
182 _radar_position_node = n->getNode("pos", true);
183 _radar_data_node = n->getNode("data", true);
184 _radar_symbol_node = n->getNode("symbol", true);
185 _radar_centre_node = n->getNode("centre", true);
186 _radar_rotate_node = n->getNode("rotate", true);
187 _radar_tcas_node = n->getNode("tcas", true);
188 _radar_absalt_node = n->getNode("abs-altitude", true);
190 _radar_centre_node->setBoolValue(false);
191 if (!_radar_coverage_node->hasValue())
192 _radar_coverage_node->setFloatValue(120);
193 if (!_radar_ref_rng_node->hasValue())
194 _radar_ref_rng_node->setDoubleValue(35);
195 if (!_radar_hdg_marker_node->hasValue())
196 _radar_hdg_marker_node->setBoolValue(true);
201 // OSG geometry setup. The polygons for the radar returns will be
202 // stored in a single Geometry. The geometry will have several
203 // primitive sets so we can have different kinds of polys and
204 // choose a different overall color for each set.
205 _radarGeode = new osg::Geode;
206 osg::StateSet *stateSet = _radarGeode->getOrCreateStateSet();
207 stateSet->setTextureAttributeAndModes(0, _wxEcho.get());
208 _geom = new osg::Geometry;
209 _geom->setUseDisplayList(false);
210 // Initially allocate space for 128 quads
211 _vertices = new osg::Vec2Array;
212 _vertices->setDataVariance(osg::Object::DYNAMIC);
213 _vertices->reserve(128 * 4);
214 _geom->setVertexArray(_vertices);
215 _texCoords = new osg::Vec2Array;
216 _texCoords->setDataVariance(osg::Object::DYNAMIC);
217 _texCoords->reserve(128 * 4);
218 _geom->setTexCoordArray(0, _texCoords);
219 osg::Vec3Array *colors = new osg::Vec3Array;
220 colors->push_back(osg::Vec3(1.0f, 1.0f, 1.0f)); // color of echos
221 colors->push_back(osg::Vec3(1.0f, 0.0f, 0.0f)); // arc mask
222 colors->push_back(osg::Vec3(0.0f, 0.0f, 0.0f)); // rest of mask
223 _geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
224 _geom->setColorArray(colors);
225 osg::PrimitiveSet *pset = new osg::DrawArrays(osg::PrimitiveSet::QUADS);
226 pset->setDataVariance(osg::Object::DYNAMIC);
227 _geom->addPrimitiveSet(pset);
228 pset = new osg::DrawArrays(osg::PrimitiveSet::QUADS);
229 pset->setDataVariance(osg::Object::DYNAMIC);
230 _geom->addPrimitiveSet(pset);
231 pset = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES);
232 pset->setDataVariance(osg::Object::DYNAMIC);
233 _geom->addPrimitiveSet(pset);
234 _geom->setInitialBound(osg::BoundingBox(osg::Vec3f(-256.0f, -256.0f, 0.0f),
235 osg::Vec3f(256.0f, 256.0f, 0.0f)));
236 _radarGeode->addDrawable(_geom);
238 // Texture in the 2D panel system
239 FGTextureManager::addTexture(_texture_path.c_str(), _odg->getTexture());
241 _textGeode = new osg::Geode;
243 osg::Camera *camera = _odg->getCamera();
244 camera->addChild(_radarGeode.get());
245 camera->addChild(_textGeode.get());
251 // Local coordinates for each echo
252 const osg::Vec3f echoCoords[4] = {
253 osg::Vec3f(-.7f, -.7f, 0.0f), osg::Vec3f(.7f, -.7f, 0.0f),
254 osg::Vec3f(.7f, .7f, 0.0f), osg::Vec3f(-.7f, .7f, 0.0f)
258 const osg::Vec2f echoTexCoords[4] = {
259 osg::Vec2f(0.0f, 0.0f), osg::Vec2f(UNIT, 0.0f),
260 osg::Vec2f(UNIT, UNIT), osg::Vec2f(0.0f, UNIT)
266 addQuad(osg::Vec2Array *vertices, osg::Vec2Array *texCoords,
267 const osg::Matrixf& transform, const osg::Vec2f& texBase)
269 for (int i = 0; i < 4; i++) {
270 const osg::Vec3f coords = transform.preMult(echoCoords[i]);
271 texCoords->push_back(texBase + echoTexCoords[i]);
272 vertices->push_back(osg::Vec2f(coords.x(), coords.y()));
277 // Rotate by a heading value
279 osg::Matrixf wxRotate(float angle)
281 return osg::Matrixf::rotate(angle, 0.0f, 0.0f, -1.0f);
286 wxRadarBg::update (double delta_time_sec)
288 if (!_sim_init_done) {
289 if (!fgGetBool("sim/sceneryloaded", false))
292 _sim_init_done = true;
294 if (!_odg || !_serviceable_node->getBoolValue()) {
295 _Instrument->setStringValue("status", "");
298 _time += delta_time_sec;
299 if (_time < _interval){
300 // cout << "WXradar update too soon " << _time << endl;
303 // cout << "WXradar updating" << _time<< endl;
307 string mode = _Instrument->getStringValue("display-mode", "arc");
309 if (_display_mode != MAP) {
313 } else if (mode == "plan") {
314 _display_mode = PLAN;}
315 else if (mode == "bscan") {
316 _display_mode = BSCAN;
321 string switchKnob = _Instrument->getStringValue("switch", "on");
322 if (switchKnob == "off") {
323 _Instrument->setStringValue("status", "");
324 } else if (switchKnob == "stby") {
325 _Instrument->setStringValue("status", "STBY");
326 } else if (switchKnob == "tst") {
327 _Instrument->setStringValue("status", "TST");
328 // find something interesting to do...
330 float r = _Instrument->getFloatValue("range", 40.0);
331 if (r != _range_nm) {
336 _radar_ref_rng = _radar_ref_rng_node->getDoubleValue();
337 _view_heading = fgGetDouble("/orientation/heading-deg") * SG_DEGREES_TO_RADIANS;
338 _centerTrans.makeTranslate(0.0f, 0.0f, 0.0f);
340 _scale = 200.0 / _range_nm;
343 if (_display_mode == ARC) {
344 _scale = 2*200.0f / _range_nm;
345 _angle_offset = -_view_heading;
346 _centerTrans.makeTranslate(0.0f, -200.0f, 0.0f);
348 } else if (_display_mode == MAP) {
351 bool centre = _radar_centre_node->getBoolValue();
354 _radar_centre_node->setBoolValue(false);
357 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: displacement "
358 // << _x_offset <<", "<<_y_offset
359 // << " user_speed_east_fps * SG_FPS_TO_KT "
360 // << user_speed_east_fps * SG_FPS_TO_KT
361 // << " user_speed_north_fps * SG_FPS_TO_KT "
362 // << user_speed_north_fps * SG_FPS_TO_KT
363 // << " dt " << delta_time_sec);
365 _centerTrans.makeTranslate(_x_offset, _y_offset, 0.0f);
367 } else if (_display_mode == PLAN) {
368 if (_radar_rotate_node->getBoolValue()) {
369 _angle_offset = -_view_heading;
371 } else if (_display_mode == BSCAN) {
372 _angle_offset = -_view_heading;
379 _textGeode->removeDrawables(0, _textGeode->getNumDrawables());
382 //TODO FIXME Mask below (only used for ARC mode) isn't properly aligned, i.e.
383 // it assumes the a/c position at the center of the display - though it's somewhere at
384 // bottom part for ARC mode.
385 // The mask hadn't worked at all for a while (probably since the OSG port) due to
386 // another bug (which is fixed now). Now, the mask is disabled completely until s.o.
387 // adapted the coordinates below. And the mask is only really useful to limit displayed
388 // weather blobs (not support yet).
389 // Aircraft echos are already limited properly through wxradar's "limit-deg" property.
391 osg::DrawArrays *maskPSet
392 = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(1));
393 osg::DrawArrays *trimaskPSet
394 = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(2));
396 if (_display_mode == ARC) {
397 // erase what is out of sight of antenna
407 float xOffset = 256.0f;
408 float yOffset = 200.0f;
410 int firstQuadVert = _vertices->size();
411 _texCoords->push_back(osg::Vec2f(0.5f, 0.25f));
412 _vertices->push_back(osg::Vec2f(-xOffset, 0.0 + yOffset));
413 _texCoords->push_back(osg::Vec2f(1.0f, 0.25f));
414 _vertices->push_back(osg::Vec2f(xOffset, 0.0 + yOffset));
415 _texCoords->push_back(osg::Vec2f(1.0f, 0.5f));
416 _vertices->push_back(osg::Vec2f(xOffset, 256.0 + yOffset));
417 _texCoords->push_back(osg::Vec2f(0.5f, 0.5f));
418 _vertices->push_back(osg::Vec2f(-xOffset, 256.0 + yOffset));
419 maskPSet->set(osg::PrimitiveSet::QUADS, firstQuadVert, 4);
422 // The triangles aren't supposed to be textured, but there's
423 // no need to set up a different Geometry, switch modes,
424 // etc. I happen to know that there's a white pixel in the
425 // texture at 1.0, 0.0 :)
426 float centerY = tan(30 * SG_DEGREES_TO_RADIANS);
427 _vertices->push_back(osg::Vec2f(0.0, 0.0));
428 _vertices->push_back(osg::Vec2f(-256.0, 0.0));
429 _vertices->push_back(osg::Vec2f(-256.0, 256.0 * centerY));
431 _vertices->push_back(osg::Vec2f(0.0, 0.0));
432 _vertices->push_back(osg::Vec2f(256.0, 0.0));
433 _vertices->push_back(osg::Vec2f(256.0, 256.0 * centerY));
435 _vertices->push_back(osg::Vec2f(-256, 0.0));
436 _vertices->push_back(osg::Vec2f(256.0, 0.0));
437 _vertices->push_back(osg::Vec2f(-256.0, -256.0));
439 _vertices->push_back(osg::Vec2f(256, 0.0));
440 _vertices->push_back(osg::Vec2f(256.0, -256.0));
441 _vertices->push_back(osg::Vec2f(-256.0, -256.0));
443 const osg::Vec2f whiteSpot(1.0f, 0.0f);
444 for (int i = 0; i < 3 * 4; i++)
445 _texCoords->push_back(whiteSpot);
447 trimaskPSet->set(osg::PrimitiveSet::TRIANGLES, firstQuadVert, 3 * 4);
451 maskPSet->set(osg::PrimitiveSet::QUADS, 0, 0);
452 trimaskPSet->set(osg::PrimitiveSet::TRIANGLES, 0, 0);
456 trimaskPSet->dirty();
460 // remember index of next vertex
461 int vIndex = _vertices->size();
465 osg::DrawArrays *quadPSet
466 = static_cast<osg::DrawArrays*>(_geom->getPrimitiveSet(0));
470 update_heading_marker();
472 // draw all new vertices are quads
473 quadPSet->set(osg::PrimitiveSet::QUADS, vIndex, _vertices->size()-vIndex);
480 wxRadarBg::update_weather()
482 string modeButton = _Instrument->getStringValue("mode", "WX");
483 // FIXME: implementation of radar echoes missing
484 // _radarEchoBuffer = *sgEnviro.get_radar_echo();
486 // pretend we have a scan angle bigger then the FOV
487 // TODO:check real fov, enlarge if < nn, and do clipping if > mm
488 // const float fovFactor = 1.45f;
489 _Instrument->setStringValue("status", modeButton.c_str());
491 // FIXME: implementation of radar echoes missing
493 list_of_SGWxRadarEcho *radarEcho = &_radarEchoBuffer;
494 list_of_SGWxRadarEcho::iterator iradarEcho, end = radarEcho->end();
495 const float LWClevel[] = { 0.1f, 0.5f, 2.1f };
497 // draw the cloud radar echo
498 bool drawClouds = _radar_weather_node->getBoolValue();
501 // we do that in 3 passes, one for each color level
502 // this is to 'merge' same colors together
503 for (int level = 0; level <= 2; level++) {
504 float col = level * UNIT;
506 for (iradarEcho = radarEcho->begin(); iradarEcho != end; ++iradarEcho) {
507 int cloudId = iradarEcho->cloudId;
508 bool upgrade = (cloudId >> 5) & 1;
509 float lwc = iradarEcho->LWC + (upgrade ? 1.0f : 0.0f);
512 if (iradarEcho->LWC >= 0.5 && iradarEcho->LWC <= 0.6)
515 if (iradarEcho->lightning || lwc < LWClevel[level])
518 float radius = sqrt(iradarEcho->dist) * SG_METER_TO_NM * _scale;
519 float size = iradarEcho->radius * 2.0 * SG_METER_TO_NM * _scale;
521 if (radius - size > 180)
524 float angle = (iradarEcho->heading - _angle_offset) //* fovFactor
527 // Rotate echo into position, and rotate echo to have
528 // a constant orientation towards the
529 // airplane. Compass headings increase in clockwise
530 // direction, while graphics rotations follow
531 // right-hand (counter-clockwise) rule.
532 const osg::Vec2f texBase(col, (UNIT * (float) (4 + (cloudId & 3))));
534 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
535 * osg::Matrixf::translate(0.0f, radius, 0.0f)
536 * wxRotate(angle) * _centerTrans);
537 addQuad(_vertices, _texCoords, m, texBase);
539 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing clouds"
540 // << " ID=" << cloudId
543 // << " radius=" << radius
544 // << " view_heading=" << _view_heading * SG_RADIANS_TO_DEGREES
545 // << " heading=" << iradarEcho->heading * SG_RADIANS_TO_DEGREES
546 // << " angle=" << angle * SG_RADIANS_TO_DEGREES);
551 // draw lightning echos
552 bool drawLightning = _Instrument->getBoolValue("lightning", true);
554 const osg::Vec2f texBase(3 * UNIT, 4 * UNIT);
556 for (iradarEcho = radarEcho->begin(); iradarEcho != end; ++iradarEcho) {
557 if (!iradarEcho->lightning)
560 float size = UNIT * 0.5f;
561 float radius = iradarEcho->dist * _scale;
562 float angle = iradarEcho->heading * SG_DEGREES_TO_RADIANS
565 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
567 * osg::Matrixf::translate(0.0f, radius, 0.0f)
568 * wxRotate(angle) * _centerTrans);
569 addQuad(_vertices, _texCoords, m, texBase);
577 wxRadarBg::update_data(const SGPropertyNode *ac, double altitude, double heading,
578 double radius, double bearing, bool selected)
580 osgText::Text *callsign = new osgText::Text;
581 callsign->setFont(_font.get());
582 callsign->setFontResolution(12, 12);
583 callsign->setCharacterSize(_font_size);
584 callsign->setColor(selected ? osg::Vec4(1, 1, 1, 1) : _font_color);
585 osg::Matrixf m(wxRotate(-bearing)
586 * osg::Matrixf::translate(0.0f, radius, 0.0f)
587 * wxRotate(bearing) * _centerTrans);
589 osg::Vec3 pos = m.preMult(osg::Vec3(16, 16, 0));
590 // cast to int's, otherwise text comes out ugly
591 callsign->setPosition(osg::Vec3((int)pos.x(), (int)pos.y(), 0));
592 callsign->setAlignment(osgText::Text::LEFT_BOTTOM_BASE_LINE);
593 callsign->setLineSpacing(_font_spacing);
595 const char *identity = ac->getStringValue("transponder-id");
597 identity = ac->getStringValue("callsign");
600 text << identity << endl
601 << setprecision(0) << fixed
602 << setw(3) << setfill('0') << heading * SG_RADIANS_TO_DEGREES << "\xB0 "
603 << setw(0) << altitude << "ft" << endl
604 << ac->getDoubleValue("velocities/true-airspeed-kt") << "kts";
606 callsign->setText(text.str());
607 _textGeode->addDrawable(callsign);
612 wxRadarBg::update_aircraft()
615 double age_factor = 1.0;
623 if (!ground_echoes.empty()){
624 ground_echoes_iterator = ground_echoes.begin();
626 while(ground_echoes_iterator != ground_echoes.end()) {
627 diff = _elapsed_time - (*ground_echoes_iterator)->elapsed_time;
629 if( diff > _persistance) {
630 ground_echoes.erase(ground_echoes_iterator++);
632 // double test_brg = (*ground_echoes_iterator)->bearing;
633 // double bearing = test_brg * SG_DEGREES_TO_RADIANS;
634 // float angle = calcRelBearing(bearing, _view_heading);
635 double bumpinessFactor = (*ground_echoes_iterator)->bumpiness;
636 float heading = fgGetDouble("/orientation/heading-deg");
637 if ( _display_mode == BSCAN ){
638 test_rng = (*ground_echoes_iterator)->elevation * 6;
639 test_brg = (*ground_echoes_iterator)->bearing;
640 angle = calcRelBearingDeg(test_brg, heading) * 6;
641 range = sqrt(test_rng * test_rng + angle * angle);
642 bearing = atan2(angle, test_rng);
643 //cout << "angle " << angle <<" bearing "
644 // << bearing / SG_DEGREES_TO_RADIANS << endl;
645 echo_radius = (0.1 + (1.9 * bumpinessFactor)) * 240 * age_factor;
647 test_rng = (*ground_echoes_iterator)->range;
648 range = test_rng * SG_METER_TO_NM;
649 test_brg = (*ground_echoes_iterator)->bearing;
650 bearing = test_brg * SG_DEGREES_TO_RADIANS;
651 echo_radius = (0.1 + (1.9 * bumpinessFactor)) * 120 * age_factor;
652 bearing += _angle_offset;
655 float radius = range * _scale;
656 //double heading = 90 * SG_DEGREES_TO_RADIANS;
657 //heading += _angle_offset;
662 age_factor = 1 - (0.5 * diff/_persistance);
664 float size = echo_radius * UNIT;
666 const osg::Vec2f texBase(3 * UNIT, 3 * UNIT);
667 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
668 * osg::Matrixf::translate(0.0f, radius, 0.0f)
669 * wxRotate(bearing) * _centerTrans);
670 addQuad(_vertices, _texCoords, m, texBase);
672 ++ground_echoes_iterator;
674 //cout << "test bearing " << test_brg
675 //<< " test_rng " << test_rng * SG_METER_TO_NM
676 //<< " persistance " << _persistance
683 if (!_ai_enabled_node->getBoolValue())
686 bool draw_tcas = _radar_tcas_node->getBoolValue();
687 bool draw_absolute = _radar_absalt_node->getBoolValue();
688 bool draw_echoes = _radar_position_node->getBoolValue();
689 bool draw_symbols = _radar_symbol_node->getBoolValue();
690 bool draw_data = _radar_data_node->getBoolValue();
691 if (!draw_echoes && !draw_symbols && !draw_data)
694 double user_lat = _user_lat_node->getDoubleValue();
695 double user_lon = _user_lon_node->getDoubleValue();
696 double user_alt = _user_alt_node->getDoubleValue();
698 float limit = _radar_coverage_node->getFloatValue();
703 limit *= SG_DEGREES_TO_RADIANS;
705 int selected_id = fgGetInt("/instrumentation/radar/selected-id", -1);
707 const SGPropertyNode *selected_ac = 0;
708 const SGPropertyNode *ai = fgGetNode("/ai/models", true);
710 for (int i = ai->nChildren() - 1; i >= -1; i--) {
711 const SGPropertyNode *model;
713 if (i < 0) { // last iteration: selected model
716 model = ai->getChild(i);
717 if (!model->nChildren())
719 if ((model->getIntValue("id") == selected_id)&&
721 selected_ac = model; // save selected model for last iteration
728 double echo_radius, sigma;
729 const string name = model->getName();
731 //cout << "name "<<name << endl;
732 if (name == "aircraft" || name == "tanker")
733 echo_radius = 1, sigma = 1;
734 else if (name == "multiplayer" || name == "wingman" || name == "static")
735 echo_radius = 1.5, sigma = 1;
736 else if (name == "ship" || name == "carrier" || name == "escort" ||name == "storm")
737 echo_radius = 1.5, sigma = 100;
738 else if (name == "thermal")
739 echo_radius = 2, sigma = 100;
740 else if (name == "rocket")
741 echo_radius = 0.1, sigma = 0.1;
742 else if (name == "ballistic")
743 echo_radius = 0.001, sigma = 0.001;
747 double lat = model->getDoubleValue("position/latitude-deg");
748 double lon = model->getDoubleValue("position/longitude-deg");
749 double alt = model->getDoubleValue("position/altitude-ft");
750 double heading = model->getDoubleValue("orientation/true-heading-deg");
752 double range, bearing;
753 calcRangeBearing(user_lat, user_lon, lat, lon, range, bearing);
754 //cout << _antenna_ht << _interval<< endl;
755 bool isVisible = withinRadarHorizon(user_alt, alt, range);
760 if (!inRadarRange(sigma, range))
763 bearing *= SG_DEGREES_TO_RADIANS;
764 heading *= SG_DEGREES_TO_RADIANS;
766 float radius = range * _scale;
767 float angle = calcRelBearing(bearing, _view_heading);
769 if (angle > limit || angle < -limit)
772 bearing += _angle_offset;
773 heading += _angle_offset;
775 bool is_tcas_contact = false;
778 is_tcas_contact = update_tcas(model,range,user_alt,alt,bearing,radius,draw_absolute);
782 if (draw_echoes && (!is_tcas_contact)) {
783 float size = echo_radius * 120 * UNIT;
785 const osg::Vec2f texBase(3 * UNIT, 3 * UNIT);
786 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
787 * osg::Matrixf::translate(0.0f, radius, 0.0f)
788 * wxRotate(bearing) * _centerTrans);
789 addQuad(_vertices, _texCoords, m, texBase);
793 if (draw_symbols && (!draw_tcas)) {
794 const osg::Vec2f texBase(0, 3 * UNIT);
795 float size = 600 * UNIT;
796 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
797 * wxRotate(heading - bearing)
798 * osg::Matrixf::translate(0.0f, radius, 0.0f)
799 * wxRotate(bearing) * _centerTrans);
800 addQuad(_vertices, _texCoords, m, texBase);
803 if ((draw_data || i < 0)&& // selected one (i == -1) is always drawn
804 ((!draw_tcas)||(is_tcas_contact)||(draw_echoes)))
805 update_data(model, alt, heading, radius, bearing, i < 0);
809 /** Update TCAS display.
810 * Return true when processed as TCAS contact, false otherwise. */
812 wxRadarBg::update_tcas(const SGPropertyNode *model,double range,double user_alt,double alt,
813 double bearing,double radius,bool absMode)
817 // update TCAS symbol
819 threatLevel = model->getIntValue("tcas/threat-level",-1);
820 if (threatLevel == -1)
822 // no TCAS information (i.e. no transponder) => not visible to TCAS
825 int row = 7 - threatLevel;
827 double vspeed = model->getDoubleValue("velocities/vertical-speed-fps");
828 if (vspeed < -3.0) // descending
831 if (vspeed > 3.0) // climbing
833 texBase = osg::Vec2f(col*UNIT,row * UNIT);
834 float size = 200 * UNIT;
835 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
837 * osg::Matrixf::translate(0.0f, radius, 0.0f)
838 * wxRotate(bearing) * _centerTrans);
839 addQuad(_vertices, _texCoords, m, texBase);
844 osgText::Text *altStr = new osgText::Text;
845 altStr->setFont(_font.get());
846 altStr->setFontResolution(12, 12);
847 altStr->setCharacterSize(_font_size);
848 altStr->setColor(_tcas_colors[threatLevel]);
849 osg::Matrixf m(wxRotate(-bearing)
850 * osg::Matrixf::translate(0.0f, radius, 0.0f)
851 * wxRotate(bearing) * _centerTrans);
853 osg::Vec3 pos = m.preMult(osg::Vec3(16, 16, 0));
854 // cast to int's, otherwise text comes out ugly
855 altStr->setLineSpacing(_font_spacing);
858 altStr->setAlignment(osgText::Text::LEFT_CENTER);
859 int altDif = (alt-user_alt+50)/100;
874 altStr->setPosition(osg::Vec3((int)pos.x()-30, (int)pos.y()+dy, 0));
877 // absolute altitude display
878 text << setprecision(0) << fixed
879 << setw(3) << setfill('0') << alt/100 << endl;
881 else // relative altitude display
885 << setprecision(0) << fixed
886 << setw(2) << setfill('0') << altDif << endl;
889 altStr->setText(text.str());
890 _textGeode->addDrawable(altStr);
897 wxRadarBg::update_tacan()
900 int mode = _radar_mode_control_node->getIntValue();
901 bool inRange = _tacan_in_range_node->getBoolValue();
903 if (mode != 1 || !inRange)
906 float size = 600 * UNIT;
907 float radius = _tacan_distance_node->getFloatValue() * _scale;
908 float angle = _tacan_bearing_node->getFloatValue() * SG_DEGREES_TO_RADIANS
911 const osg::Vec2f texBase(1 * UNIT, 3 * UNIT);
912 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
914 * osg::Matrixf::translate(0.0f, radius, 0.0f)
915 * wxRotate(angle) * _centerTrans);
916 addQuad(_vertices, _texCoords, m, texBase);
918 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing TACAN"
919 // << " dist=" << radius
920 // << " view_heading=" << _view_heading * SG_RADIANS_TO_DEGREES
921 // << " bearing=" << angle * SG_RADIANS_TO_DEGREES
922 // << " x=" << x << " y="<< y
923 // << " size=" << size);
928 wxRadarBg::update_heading_marker()
930 if (!_radar_hdg_marker_node->getBoolValue())
933 const osg::Vec2f texBase(2 * UNIT, 3 * UNIT);
934 float size = 600 * UNIT;
935 osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
936 * wxRotate(_view_heading + _angle_offset));
939 addQuad(_vertices, _texCoords, m, texBase);
941 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: drawing heading marker"
942 // << " x,y " << x <<","<< y
943 // << " dist" << dist
944 // << " view_heading" << _view_heading * SG_RADIANS_TO_DEGREES
945 // << " heading " << iradarEcho->heading * SG_RADIANS_TO_DEGREES
946 // << " angle " << angle * SG_RADIANS_TO_DEGREES);
951 wxRadarBg::center_map()
953 _lat = _user_lat_node->getDoubleValue();
954 _lon = _user_lon_node->getDoubleValue();
955 _x_offset = _y_offset = 0;
960 wxRadarBg::apply_map_offset()
962 double lat = _user_lat_node->getDoubleValue();
963 double lon = _user_lon_node->getDoubleValue();
964 double bearing, distance, az2;
965 geo_inverse_wgs_84(_lat, _lon, lat, lon, &bearing, &az2, &distance);
966 distance *= SG_METER_TO_NM * _scale;
967 bearing *= SG_DEGREES_TO_RADIANS;
968 _x_offset += sin(bearing) * distance;
969 _y_offset += cos(bearing) * distance;
976 wxRadarBg::withinRadarHorizon(double user_alt, double alt, double range_nm)
978 // Radar Horizon = 1.23(ht^1/2 + hr^1/2),
979 //don't allow negative altitudes (an approximation - yes altitudes can be negative)
980 // Allow antenna ht to be set, but only on ground
981 _antenna_ht = _Instrument->getDoubleValue("antenna-ht-ft");
984 user_alt = _antenna_ht;
987 alt = 0; // to allow some vertical extent of target
989 double radarhorizon = 1.23 * (sqrt(alt) + sqrt(user_alt));
990 // SG_LOG(SG_GENERAL, SG_ALERT, "Radar: radar horizon " << radarhorizon);
991 return radarhorizon >= range_nm;
996 wxRadarBg::inRadarRange(double sigma, double range_nm)
998 //The Radar Equation:
1000 // MaxRange^4 = (TxPower * AntGain^2 * lambda^2 * sigma)/((constant) * MDS)
1002 // Where (constant) = (4*pi)3 and MDS is the Minimum Detectable Signal power.
1004 // For a given radar we can assume that the only variable is sigma,
1005 // the target radar cross section.
1007 // Here, we will use a normalised rcs (sigma) for a standard taget and assume that this
1008 // will provide a maximum range of 35nm;
1010 // TODO - make the maximum range adjustable at runtime
1012 double constant = _radar_ref_rng;
1017 double maxrange = constant * pow(sigma, 0.25);
1018 //SG_LOG(SG_GENERAL, SG_DEBUG, "Radar: max range " << maxrange);
1019 return maxrange >= range_nm;
1024 wxRadarBg::calcRangeBearing(double lat, double lon, double lat2, double lon2,
1025 double &range, double &bearing) const
1027 // calculate the bearing and range of the second pos from the first
1028 double az2, distance;
1029 geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
1030 range = distance *= SG_METER_TO_NM;
1035 wxRadarBg::calcRelBearing(float bearing, float heading)
1037 float angle = bearing - heading;
1040 angle -= 2.0 * SG_PI;
1043 angle += 2.0 * SG_PI;
1049 wxRadarBg::calcRelBearingDeg(float bearing, float heading)
1051 float angle = bearing - heading;
1054 return angle -= 360;
1057 return angle += 360;
1064 wxRadarBg::updateFont()
1066 float red = _font_node->getFloatValue("color/red");
1067 float green = _font_node->getFloatValue("color/green");
1068 float blue = _font_node->getFloatValue("color/blue");
1069 float alpha = _font_node->getFloatValue("color/alpha");
1070 _font_color.set(red, green, blue, alpha);
1072 _font_size = _font_node->getFloatValue("size");
1073 _font_spacing = _font_size * _font_node->getFloatValue("line-spacing");
1074 string path = _font_node->getStringValue("name", DEFAULT_FONT);
1077 if (path[0] != '/') {
1078 tpath = globals->get_fg_root();
1079 tpath.append("Fonts");
1085 #if (FG_OSG_VERSION >= 21000)
1086 osg::ref_ptr<osgDB::ReaderWriter::Options> fontOptions = new osgDB::ReaderWriter::Options("monochrome");
1087 osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.c_str(), fontOptions.get());
1089 osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.c_str());
1094 _font->setMinFilterHint(osg::Texture::NEAREST);
1095 _font->setMagFilterHint(osg::Texture::NEAREST);
1096 _font->setGlyphImageMargin(0);
1097 _font->setGlyphImageMarginRatio(0);
1100 for (int i=0;i<4;i++)
1102 const float defaultColors[4][3] = {{0,1,1},{0,1,1},{1,0.5,0},{1,0,0}};
1103 SGPropertyNode_ptr color_node = _font_node->getNode("tcas/color",i,true);
1104 float red = color_node->getFloatValue("red",defaultColors[i][0]);
1105 float green = color_node->getFloatValue("green",defaultColors[i][1]);
1106 float blue = color_node->getFloatValue("blue",defaultColors[i][2]);
1107 float alpha = color_node->getFloatValue("alpha",1);
1108 _tcas_colors[i]=osg::Vec4(red, green, blue, alpha);
1113 wxRadarBg::valueChanged(SGPropertyNode*)