1 // Wx Radar background texture
3 // Written by Harald JOHNSEN, started May 2005.
5 // Copyright (C) 2005 Harald JOHNSEN
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
25 #include <Main/fg_props.hxx>
26 #include <Main/globals.hxx>
27 #include <Cockpit/panel.hxx>
28 #include <Cockpit/hud.hxx>
30 #include <simgear/constants.h>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/environment/visual_enviro.hxx>
33 #include "instrument_mgr.hxx"
34 #include "od_gauge.hxx"
35 #include "wxradar.hxx"
37 // texture name to use in 2D and 3D instruments
38 static const char *odgauge_name = "Aircraft/Instruments/Textures/od_wxradar.rgb";
40 wxRadarBg::wxRadarBg ( SGPropertyNode *node) :
45 last_switchKnob( "off" ),
46 sim_init_done ( false ),
50 for ( i = 0; i < node->nChildren(); ++i ) {
51 SGPropertyNode *child = node->getChild(i);
52 string cname = child->getName();
53 string cval = child->getStringValue();
54 if ( cname == "name" ) {
56 } else if ( cname == "number" ) {
57 num = child->getIntValue();
59 SG_LOG( SG_INSTR, SG_WARN, "Error in wxRadar config logic" );
60 if ( name.length() ) {
61 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
67 wxRadarBg::wxRadarBg ()
71 wxRadarBg::~wxRadarBg ()
79 branch = "/instrumentation/" + name;
81 _Instrument = fgGetNode(branch.c_str(), num, true );
82 _serviceable_node = _Instrument->getChild("serviceable", 0, true);
83 resultTexture = FGTextureManager::createTexture( odgauge_name );
84 SGPath tpath(globals->get_fg_root());
85 tpath.append("Aircraft/Instruments/Textures/wxecho.rgb");
86 // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect
87 wxEcho = new ssgTexture( tpath.c_str(), false, false, false);
89 _Instrument->setFloatValue("trk", 0.0);
90 _Instrument->setFloatValue("tilt", 0.0);
91 _Instrument->setStringValue("status","");
92 // those properties are used by a radar instrument of a MFD
93 // input switch = OFF | TST | STBY | ON
94 // input mode = WX | WXA | MAP
95 // ouput status = STBY | TEST | WX | WXA | MAP | blank
96 // input lightning = true | false
97 // input TRK = +/- n degrees
98 // input TILT = +/- n degree
99 // input autotilt = true | false
100 // input range = n nm (20/40/80)
101 // input display-mode = arc | rose | map | plan
103 FGInstrumentMgr *imgr = (FGInstrumentMgr *) globals->get_subsystem("instrumentation");
104 odg = (FGODGauge *) imgr->get_subsystem("od_gauge");
108 wxRadarBg::update (double delta_time_sec)
110 if( ! sim_init_done ) {
111 if( ! fgGetBool("sim/sceneryloaded", false) )
113 sim_init_done = true;
115 if ( !odg || ! _serviceable_node->getBoolValue() ) {
116 _Instrument->setStringValue("status","");
119 string switchKnob = _Instrument->getStringValue("switch", "on");
120 string modeButton = _Instrument->getStringValue("mode", "wx");
121 bool drawLightning = _Instrument->getBoolValue("lightning", true);
122 float range_nm = _Instrument->getFloatValue("range", 40.0);
123 float range_m = range_nm * SG_NM_TO_METER;
125 if( last_switchKnob != switchKnob ) {
126 // since 3D models don't share textures with the rest of the world
127 // we must locate them and replace their handle by hand
128 // only do that when the instrument is turned on
129 if( last_switchKnob == "off" )
130 odg->set_texture( odgauge_name, resultTexture->getHandle());
131 last_switchKnob = switchKnob;
133 FGViewer *current__view = globals->get_current_view();
134 if( current__view->getInternal() &&
135 (current__view->getHeadingOffset_deg() <= 15.0 || current__view->getHeadingOffset_deg() >= 345.0) &&
136 (current__view->getPitchOffset_deg() <= 15.0 || current__view->getPitchOffset_deg() >= 350.0) ) {
138 // we don't update the radar echo if the pilot looks around
140 radarEchoBuffer = *sgEnviro.get_radar_echo();
142 odg->beginCapture(256);
145 glMatrixMode(GL_MODELVIEW);
148 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
149 glBindTexture(GL_TEXTURE_2D, 0);
150 if( switchKnob == "off" ) {
151 _Instrument->setStringValue("status","");
152 } else if( switchKnob == "stby" ) {
153 _Instrument->setStringValue("status","STBY");
154 } else if( switchKnob == "tst" ) {
155 _Instrument->setStringValue("status","TST");
156 // find something interesting to do...
158 string display_mode = _Instrument->getStringValue("display-mode", "arc");
159 // pretend we have a scan angle bigger then the FOV
160 // TODO:check real fov, enlarge if < nn, and do clipping if > mm
161 const float fovFactor = 1.45f;
162 float view_heading = get_heading() * SG_DEGREES_TO_RADIANS;
163 float range = 200.0f / range_nm;
164 _Instrument->setStringValue("status", modeButton.c_str());
165 if( display_mode == "arc" ) {
166 glTranslatef(0.0f, -180.0f, 0.0f);
167 range = 2*180.0f / range_nm;
168 } else if( display_mode == "map" ) {
169 // float view_heading = get_track() * SG_DEGREES_TO_RADIANS;
170 } else if( display_mode == "plan" ) {
171 // no sense I presume
172 float view_heading = 0.0;
176 range /= SG_NM_TO_METER;
177 // we will rotate the echo quads, this gives a better rendering
178 const float rot_x = cos ( view_heading );
179 const float rot_y = sin ( view_heading );
181 list_of_SGWxRadarEcho *radarEcho = &radarEchoBuffer;
182 list_of_SGWxRadarEcho::iterator iradarEcho;
183 const float LWClevel[] = { 0.1f, 0.5f, 2.1f };
184 const float symbolSize = 1.0f / 8.0f ;
185 // draw the radar echo, we do that in 3 passes, one for each color level
186 // this is to 'merge' same colors together
187 glBindTexture(GL_TEXTURE_2D, wxEcho->getHandle() );
188 glColor3f(1.0f, 1.0f, 1.0f);
191 for(int level = 0; level <= 2 ; level++ ) {
192 float col = level * symbolSize;
193 for(iradarEcho = radarEcho->begin() ; iradarEcho != radarEcho->end() ; iradarEcho++ ) {
194 int cloudId = (iradarEcho->cloudId) ;
195 bool upgrade = ((cloudId >> 5) & 1);
196 float lwc = iradarEcho->LWC + (upgrade ? 1.0f : 0.0f);
198 if( iradarEcho->LWC >= 0.5 && iradarEcho->LWC <= 0.6)
200 if( (! iradarEcho->lightning) && ( lwc >= LWClevel[level]) ) {
201 float dist = sgSqrt( iradarEcho->dist );
202 float size = iradarEcho->radius * 2.0;
203 if( dist - size > range_m )
207 // compute the relative angle from the view direction
208 float angle = ( view_heading + iradarEcho->heading );
211 if( angle < - SG_PI )
213 // and apply a fov factor to simulate a greater scan angle
214 angle = angle * fovFactor + SG_PI / 2.0;
215 float x = cos( angle ) * dist;
216 float y = sin( angle ) * dist;
217 // use different shapes so the display is less boring
218 float row = symbolSize * (float) (4 + (cloudId & 3) );
219 float size_x = rot_x * size;
220 float size_y = rot_y * size;
221 glTexCoord2f( col, row);
222 glVertex2f( x - size_x, y - size_y);
223 glTexCoord2f( col+symbolSize, row);
224 glVertex2f( x + size_y, y - size_x);
225 glTexCoord2f( col+symbolSize, row+symbolSize);
226 glVertex2f( x + size_x, y + size_y);
227 glTexCoord2f( col, row+symbolSize);
228 glVertex2f( x - size_y, y + size_x);
234 // draw lightning echos
235 if( drawLightning ) {
236 float col = 3 * symbolSize;
237 float row = 4 * symbolSize;
238 for(iradarEcho = radarEcho->begin() ; iradarEcho != radarEcho->end() ; iradarEcho++ ) {
239 if( iradarEcho->lightning ) {
240 float dist = iradarEcho->dist;
242 float angle = (view_heading - iradarEcho->heading);
245 if( angle < - SG_PI )
247 angle = angle * fovFactor - SG_PI / 2.0;
248 float x = cos( angle ) * dist;
249 float y = sin( angle ) * dist;
250 glColor3f(1.0f, 1.0f, 1.0f);
251 float size = symbolSize * 0.5f;
253 glTexCoord2f( col, row);
254 glVertex2f( x - size, y - size);
255 glTexCoord2f( col+symbolSize, row);
256 glVertex2f( x + size, y - size);
257 glTexCoord2f( col+symbolSize, row+symbolSize);
258 glVertex2f( x + size, y + size);
259 glTexCoord2f( col, row+symbolSize);
260 glVertex2f( x - size, y + size);
265 // erase what is out of sight of antenna
275 float yOffset = 180.0f, xOffset = 256.0f;
276 if( display_mode != "arc" ) {
281 glColor4f(1.0f, 0.0f, 0.0f, 0.01f);
283 glTexCoord2f( 0.5f, 0.25f);
284 glVertex2f(-xOffset, 0.0 + yOffset);
285 glTexCoord2f( 1.0f, 0.25f);
286 glVertex2f(xOffset, 0.0 + yOffset);
287 glTexCoord2f( 1.0f, 0.5f);
288 glVertex2f(xOffset, 256.0 + yOffset);
289 glTexCoord2f( 0.5f, 0.5f);
290 glVertex2f(-xOffset, 256.0 + yOffset);
293 glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
294 // glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
295 glDisable(GL_ALPHA_TEST);
296 glBindTexture(GL_TEXTURE_2D, 0);
298 glBegin( GL_TRIANGLES );
299 glVertex2f(0.0, 0.0);
300 glVertex2f(-256.0, 0.0);
301 glVertex2f(-256.0, 256.0);
303 glVertex2f(0.0, 0.0);
304 glVertex2f(256.0, 0.0);
305 glVertex2f(256.0, 256.0);
307 glVertex2f(-256, 0.0);
308 glVertex2f(256.0, 0.0);
309 glVertex2f(-256.0, -256.0);
311 glVertex2f(256, 0.0);
312 glVertex2f(256.0, -256.0);
313 glVertex2f(-256.0, -256.0);
317 /* glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
319 glVertex2f(0.0, 0.0);
320 glVertex2f(-256.0, 256.0);
321 glVertex2f(0.0, 0.0);
322 glVertex2f(256.0, 256.0);
326 glEnable(GL_ALPHA_TEST);
329 odg->endCapture( resultTexture->getHandle() );