]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/wxradar.cxx
Added copyright to ground radar files
[flightgear.git] / src / Instrumentation / wxradar.cxx
index ae1aeb845716fab4729296cfa8b2933909313f18..e4a96b3bff6d2dd8ca6fbed4c1763a41d068ab10 100644 (file)
@@ -32,6 +32,8 @@
 #include <osg/Matrixf>
 #include <osg/PrimitiveSet>
 #include <osg/StateSet>
+#include <osg/Version>
+#include <osgDB/ReaderWriter>
 #include <osgDB/WriteFile>
 
 #include <simgear/constants.h>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/math/sg_geodesy.hxx>
 
+#include <sstream>
+#include <iomanip>
+SG_USING_STD(stringstream);
+SG_USING_STD(endl);
+SG_USING_STD(setprecision);
+SG_USING_STD(fixed);
+SG_USING_STD(setw);
+SG_USING_STD(setfill);
+
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
 #include <Cockpit/panel.hxx>
@@ -60,10 +71,8 @@ typedef radar_list_type::iterator radar_list_iterator;
 typedef radar_list_type::const_iterator radar_list_const_iterator;
 
 
-// texture name to use in 2D and 3D instruments
-static const char *ODGAUGE_NAME = "Aircraft/Instruments/Textures/od_wxradar.rgb";
 static const float UNIT = 1.0f / 8.0f;  // 8 symbols in a row/column in the texture
-
+static const char *DEFAULT_FONT = "typewriter.txf";
 
 wxRadarBg::wxRadarBg ( SGPropertyNode *node) :
     _name(node->getStringValue("name", "radar")),
@@ -84,6 +93,9 @@ wxRadarBg::wxRadarBg ( SGPropertyNode *node) :
 
 wxRadarBg::~wxRadarBg ()
 {
+    if (_radar_font_node != 0) {
+        _radar_font_node->removeChangeListener(this);
+    }
 }
 
 
@@ -95,7 +107,11 @@ wxRadarBg::init ()
 
     _Instrument = fgGetNode(branch.c_str(), _num, true );
     _serviceable_node = _Instrument->getNode("serviceable", true);
-    _resultTexture = FGTextureManager::createTexture( ODGAUGE_NAME );
+
+    // texture name to use in 2D and 3D instruments
+    _texture_path = _Instrument->getStringValue("radar-texture-path",
+            "Aircraft/Instruments/Textures/od_wxradar.rgb");
+    _resultTexture = FGTextureManager::createTexture(_texture_path.c_str());
 
     SGPath tpath(globals->get_fg_root());
     string path = _Instrument->getStringValue("echo-texture-path",
@@ -105,6 +121,7 @@ wxRadarBg::init ()
     // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect
     _wxEcho = SGLoadTexture2D(tpath.c_str(), false, false);
 
+
     _Instrument->setFloatValue("trk", 0.0);
     _Instrument->setFloatValue("tilt", 0.0);
     _Instrument->setStringValue("status","");
@@ -121,7 +138,7 @@ wxRadarBg::init ()
 
     FGInstrumentMgr *imgr = (FGInstrumentMgr *) globals->get_subsystem("instrumentation");
     _odg = (FGODGauge *) imgr->get_subsystem("od_gauge");
-    _odg->setSize(256);
+    _odg->setSize(512);
 
     _ai = (FGAIManager*)globals->get_subsystem("ai_model");
     _ai_enabled_node = fgGetNode("/sim/ai/enabled", true);
@@ -142,18 +159,27 @@ wxRadarBg::init ()
     _radar_mode_control_node = _Instrument->getNode("mode-control", true);
     _radar_coverage_node     = _Instrument->getNode("limit-deg", true);
     _radar_ref_rng_node      = _Instrument->getNode("reference-range-nm", true);
+    _radar_hdg_marker_node   = _Instrument->getNode("heading-marker", true);
 
     SGPropertyNode *n = _Instrument->getNode("display-controls", true);
     _radar_weather_node     = n->getNode("WX", true);
     _radar_position_node    = n->getNode("pos", true);
     _radar_data_node        = n->getNode("data", true);
+    _radar_symbol_node      = n->getNode("symbol", true);
     _radar_centre_node      = n->getNode("centre", true);
+    _radar_rotate_node      = n->getNode("rotate", true);
+    _radar_font_node        = _Instrument->getNode("font", true);
+    _radar_font_node->addChangeListener(this);
+    
+    updateFont();
 
     _radar_centre_node->setBoolValue(false);
     if (_radar_coverage_node->getType() == SGPropertyNode::NONE)
         _radar_coverage_node->setFloatValue(120);
     if (_radar_ref_rng_node->getType() == SGPropertyNode::NONE)
         _radar_ref_rng_node->setDoubleValue(35);
+    if (_radar_hdg_marker_node->getType() == SGPropertyNode::NONE)
+        _radar_hdg_marker_node->setBoolValue(true);
 
     _x_offset = 0;
     _y_offset = 0;
@@ -165,45 +191,44 @@ wxRadarBg::init ()
     _radarGeode = new osg::Geode;
     osg::StateSet* stateSet = _radarGeode->getOrCreateStateSet();
     stateSet->setTextureAttributeAndModes(0, _wxEcho.get());
-    osg::Geometry* geom = new osg::Geometry;
-    geom->setUseDisplayList(false);
+    _geom = new osg::Geometry;
+    _geom->setUseDisplayList(false);
     // Initially allocate space for 128 quads
-    osg::Vec2Array* vertices = new osg::Vec2Array;
-    vertices->setDataVariance(osg::Object::DYNAMIC);
-    vertices->reserve(128 * 4);
-    geom->setVertexArray(vertices);
-    osg::Vec2Array* texCoords = new osg::Vec2Array;
-    texCoords->setDataVariance(osg::Object::DYNAMIC);
-    texCoords->reserve(128 * 4);
-    geom->setTexCoordArray(0, texCoords);
+    _vertices = new osg::Vec2Array;
+    _vertices->setDataVariance(osg::Object::DYNAMIC);
+    _vertices->reserve(128 * 4);
+    _geom->setVertexArray(_vertices);
+    _texCoords = new osg::Vec2Array;
+    _texCoords->setDataVariance(osg::Object::DYNAMIC);
+    _texCoords->reserve(128 * 4);
+    _geom->setTexCoordArray(0, _texCoords);
     osg::Vec3Array* colors = new osg::Vec3Array;
     colors->push_back(osg::Vec3(1.0f, 1.0f, 1.0f)); // color of echos
     colors->push_back(osg::Vec3(1.0f, 0.0f, 0.0f)); // arc mask
     colors->push_back(osg::Vec3(0.0f, 0.0f, 0.0f)); // rest of mask
-    geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
-    geom->setColorArray(colors);
+    _geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
+    _geom->setColorArray(colors);
     osg::PrimitiveSet* pset = new osg::DrawArrays(osg::PrimitiveSet::QUADS);
     pset->setDataVariance(osg::Object::DYNAMIC);
-    geom->addPrimitiveSet(pset);
+    _geom->addPrimitiveSet(pset);
     pset = new osg::DrawArrays(osg::PrimitiveSet::QUADS);
     pset->setDataVariance(osg::Object::DYNAMIC);
-    geom->addPrimitiveSet(pset);
+    _geom->addPrimitiveSet(pset);
     pset = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES);
     pset->setDataVariance(osg::Object::DYNAMIC);
-    geom->addPrimitiveSet(pset);
-    geom->setInitialBound(osg::BoundingBox(osg::Vec3f(-256.0f, -256.0f, 0.0f),
+    _geom->addPrimitiveSet(pset);
+    _geom->setInitialBound(osg::BoundingBox(osg::Vec3f(-256.0f, -256.0f, 0.0f),
             osg::Vec3f(256.0f, 256.0f, 0.0f)));
-    _radarGeode->addDrawable(geom);
+    _radarGeode->addDrawable(_geom);
     _odg->allocRT();
     // Texture in the 2D panel system
-    FGTextureManager::addTexture(ODGAUGE_NAME, _odg->getTexture());
+    FGTextureManager::addTexture(_texture_path.c_str(), _odg->getTexture());
+
+    _textGeode = new osg::Geode;
 
     osg::Camera* camera = _odg->getCamera();
     camera->addChild(_radarGeode.get());
-
-    _geom = static_cast<osg::Geometry*>(_radarGeode->getDrawable(0));
-    _vertices = static_cast<osg::Vec2Array*>(_geom->getVertexArray());
-    _texCoords = static_cast<osg::Vec2Array*>(_geom->getTexCoordArray(0));
+    camera->addChild(_textGeode.get());
 }
 
 
@@ -278,7 +303,8 @@ wxRadarBg::update (double delta_time_sec)
         // we must locate them and replace their handle by hand
         // only do that when the instrument is turned on
         //if ( _last_switchKnob == "off" )
-        //_odg->set_texture( ODGAUGE_NAME, _resultTexture->getHandle());
+        //_odg->set_texture(_texture_path.c_str(), _resultTexture->getHandle());
+
         _last_switchKnob = switchKnob;
     }
 
@@ -328,13 +354,16 @@ wxRadarBg::update (double delta_time_sec)
             _centerTrans.makeTranslate(_x_offset, _y_offset, 0.0f);
 
         } else if ( _display_mode == PLAN ) {
-            // no sense I presume
+            if (_radar_rotate_node->getBoolValue()) {
+                _angle_offset = -_view_heading;
+            }
         } else {
             // rose
         }
 
         _vertices->clear();
         _texCoords->clear();
+        _textGeode->removeDrawables(0, _textGeode->getNumDrawables());
 
 
         update_weather();
@@ -375,12 +404,12 @@ wxRadarBg::update (double delta_time_sec)
             _texCoords->push_back(osg::Vec2f(0.5f, 0.5f));
             _vertices->push_back(osg::Vec2f(-xOffset, 256.0 + yOffset));
             maskPSet->set(osg::PrimitiveSet::QUADS, firstQuadVert, 4);
+
             // The triangles aren't supposed to be textured, but there's
             // no need to set up a different Geometry, switch modes,
             // etc. I happen to know that there's a white pixel in the
             // texture at 1.0, 0.0 :)
             float centerY = tan(30 * SG_DEGREES_TO_RADIANS);
-            const osg::Vec2f whiteSpot(1.0f, 0.0f);
             _vertices->push_back(osg::Vec2f(0.0, 0.0));
             _vertices->push_back(osg::Vec2f(-256.0, 0.0));
             _vertices->push_back(osg::Vec2f(-256.0, 256.0 * centerY));
@@ -397,6 +426,7 @@ wxRadarBg::update (double delta_time_sec)
             _vertices->push_back(osg::Vec2f(256.0, -256.0));
             _vertices->push_back(osg::Vec2f(-256.0, -256.0));
 
+            const osg::Vec2f whiteSpot(1.0f, 0.0f);
             for (int i = 0; i < 3 * 4; i++)
                 _texCoords->push_back(whiteSpot);
 
@@ -517,6 +547,34 @@ wxRadarBg::update_weather()
     }
 }
 
+void
+wxRadarBg::update_data(FGAIBase* ac, double radius, double bearing, bool selected)
+{
+    osgText::Text* callsign = new osgText::Text;
+    callsign->setFont(_font.get());
+    callsign->setFontResolution(12, 12);
+    callsign->setCharacterSize(12);
+    callsign->setColor(selected ? osg::Vec4(1, 1, 1, 1) : osg::Vec4(0, 1, 0, 1));
+    osg::Matrixf m(wxRotate(-bearing)
+            * osg::Matrixf::translate(0.0f, radius, 0.0f)
+            * wxRotate(bearing) * _centerTrans);
+
+    osg::Vec3 pos = m.preMult(osg::Vec3(16, 16, 0));
+    // cast to int's, otherwise text comes out ugly
+    callsign->setPosition(osg::Vec3((int)pos.x(), (int)pos.y(), 0));
+    callsign->setAlignment(osgText::Text::LEFT_BOTTOM_BASE_LINE);
+    callsign->setLineSpacing(0.25);
+                        
+    stringstream text;
+    text << ac->_getCallsign() << endl 
+        << setprecision(0) << fixed
+        << setw(3) << setfill('0') << ac->_getHeading() << "\xB0 "
+        << setw(0) << ac->_getAltitude() << "ft" << endl
+        << ac->_getSpeed() << "kts";
+
+    callsign->setText(text.str());
+    _textGeode->addDrawable(callsign);
+}
 
 void
 wxRadarBg::update_aircraft()
@@ -525,8 +583,9 @@ wxRadarBg::update_aircraft()
         return;
 
     bool draw_echoes = _radar_position_node->getBoolValue();
-    bool draw_symbols = _radar_data_node->getBoolValue();
-    if (!draw_echoes && !draw_symbols)
+    bool draw_symbols = _radar_symbol_node->getBoolValue();
+    bool draw_data = _radar_data_node->getBoolValue();
+    if (!draw_echoes && !draw_symbols && !draw_data)
         return;
 
     radar_list_type radar_list = _ai->get_ai_list();
@@ -541,8 +600,19 @@ wxRadarBg::update_aircraft()
     double user_lon = _user_lon_node->getDoubleValue();
     double user_alt = _user_alt_node->getDoubleValue();
 
+    float limit = _radar_coverage_node->getFloatValue();
+    if (limit > 180)
+        limit = 180;
+    else if (limit < 0)
+        limit = 0;
+    limit *= SG_DEGREES_TO_RADIANS;
+
     radar_list_iterator it = radar_list.begin();
     radar_list_iterator end = radar_list.end();
+    FGAIBase *selected_ac = 0;
+    double selected_radius = 0;
+    double selected_bearing = 0;
+    int selected_id = fgGetInt("/instrumentation/radar/selected-id", -1);
 
     for (; it != end; ++it) {
         FGAIBase *ac = *it;
@@ -578,12 +648,6 @@ wxRadarBg::update_aircraft()
         float radius = range * _scale;
         float angle = calcRelBearing(bearing, _view_heading);
 
-        float limit = _radar_coverage_node->getFloatValue();
-        if (limit > 180)
-            limit = 180;
-        else if (limit < 0)
-            limit = 0;
-        limit *= SG_DEGREES_TO_RADIANS;
         if (angle > limit || angle < -limit)
             continue;
 
@@ -622,6 +686,19 @@ wxRadarBg::update_aircraft()
             //        << " bearing=" << angle * SG_RADIANS_TO_DEGREES
             //        << " radius=" << radius);
         }
+        
+        if (draw_data) {
+            if (ac->getID() == selected_id) {
+                selected_ac = ac;
+                selected_radius = radius;
+                selected_bearing = bearing;
+            } else {
+                update_data(ac, radius, bearing, false);
+            }
+        }
+    }
+    if (selected_ac) {
+        update_data(selected_ac, selected_radius, selected_bearing, true);
     }
 }
 
@@ -660,27 +737,14 @@ wxRadarBg::update_tacan()
 void
 wxRadarBg::update_heading_marker()
 {
-/*
-    float angle = _view_heading + _angle_offset;
-
-    float x = sin(angle);
-    float y = cos(angle);
-    float s_rot_x = sin(angle + SGD_PI_4);
-    float s_rot_y = cos(angle + SGD_PI_4);
-
-    float size = UNIT * 500;
-*/
+    if (!_radar_hdg_marker_node->getBoolValue())
+        return;
 
     const osg::Vec2f texBase(2 * UNIT, 3 * UNIT);
     float size = 600 * UNIT;
     osg::Matrixf m(osg::Matrixf::scale(size, size, 1.0f)
             * wxRotate(_view_heading + _angle_offset));
 
-    if (_display_mode == MAP) {
-        //cout << "Map Mode " << range << endl;
-//        m *= osg::Matrixf::translate(_scale, _scale, 0.0f);
-    }
-
     m *= _centerTrans;
     addQuad(_vertices, _texCoords, m, texBase);
 
@@ -705,8 +769,6 @@ wxRadarBg::center_map()
 void
 wxRadarBg::apply_map_offset()
 {
-    if (_display_mode != MAP)
-        return;
     double lat = _user_lat_node->getDoubleValue();
     double lon = _user_lon_node->getDoubleValue();
     double bearing, distance, az2;
@@ -792,3 +854,38 @@ wxRadarBg::calcRelBearing(float bearing, float heading)
     return angle;
 }
 
+void
+wxRadarBg::updateFont()
+{
+    SGPath tpath;
+    string path = _radar_font_node->getStringValue();
+    if (path.length() == 0) {
+        path = DEFAULT_FONT;
+    }
+    if (path[0] != '/') {
+        tpath = globals->get_fg_root();
+        tpath.append("Fonts");
+        tpath.append(path);
+    } else {
+        tpath = path;
+    }
+#if OSG_VERSION_MAJOR > 2 || (OSG_VERSION_MAJOR == 2 && OSG_VERSION_MINOR > 0)
+    osg::ref_ptr<osgDB::ReaderWriter::Options> fontOptions = new osgDB::ReaderWriter::Options("monochrome");
+    osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.c_str(), fontOptions.get());    
+#else    
+    osg::ref_ptr<osgText::Font> font = osgText::readFontFile(tpath.c_str());
+#endif
+    if (font != 0) {
+        _font = font;
+        _font->setMinFilterHint(osg::Texture::NEAREST);
+        _font->setMagFilterHint(osg::Texture::NEAREST);
+        _font->setGlyphImageMargin(0);
+        _font->setGlyphImageMarginRatio(0);
+    }
+}
+
+void
+wxRadarBg::valueChanged(SGPropertyNode*)
+{
+    updateFont();
+}