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 ()
73 ssgDeRefDelete(resultTexture);
74 ssgDeRefDelete(wxEcho);
81 branch = "/instrumentation/" + name;
83 _Instrument = fgGetNode(branch.c_str(), num, true );
84 _serviceable_node = _Instrument->getChild("serviceable", 0, true);
85 resultTexture = FGTextureManager::createTexture( odgauge_name );
87 SGPath tpath(globals->get_fg_root());
88 tpath.append("Aircraft/Instruments/Textures/wxecho.rgb");
89 // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect
90 wxEcho = new ssgTexture( tpath.c_str(), false, false, false);
93 _Instrument->setFloatValue("trk", 0.0);
94 _Instrument->setFloatValue("tilt", 0.0);
95 _Instrument->setStringValue("status","");
96 // those properties are used by a radar instrument of a MFD
97 // input switch = OFF | TST | STBY | ON
98 // input mode = WX | WXA | MAP
99 // ouput status = STBY | TEST | WX | WXA | MAP | blank
100 // input lightning = true | false
101 // input TRK = +/- n degrees
102 // input TILT = +/- n degree
103 // input autotilt = true | false
104 // input range = n nm (20/40/80)
105 // input display-mode = arc | rose | map | plan
107 FGInstrumentMgr *imgr = (FGInstrumentMgr *) globals->get_subsystem("instrumentation");
108 odg = (FGODGauge *) imgr->get_subsystem("od_gauge");
112 wxRadarBg::update (double delta_time_sec)
114 if( ! sim_init_done ) {
115 if( ! fgGetBool("sim/sceneryloaded", false) )
117 sim_init_done = true;
119 if ( !odg || ! _serviceable_node->getBoolValue() ) {
120 _Instrument->setStringValue("status","");
123 string switchKnob = _Instrument->getStringValue("switch", "on");
124 string modeButton = _Instrument->getStringValue("mode", "wx");
125 bool drawLightning = _Instrument->getBoolValue("lightning", true);
126 float range_nm = _Instrument->getFloatValue("range", 40.0);
127 float range_m = range_nm * SG_NM_TO_METER;
129 if( last_switchKnob != switchKnob ) {
130 // since 3D models don't share textures with the rest of the world
131 // we must locate them and replace their handle by hand
132 // only do that when the instrument is turned on
133 if( last_switchKnob == "off" )
134 odg->set_texture( odgauge_name, resultTexture->getHandle());
135 last_switchKnob = switchKnob;
137 FGViewer *current__view = globals->get_current_view();
138 if( current__view->getInternal() &&
139 (current__view->getHeadingOffset_deg() <= 15.0 || current__view->getHeadingOffset_deg() >= 345.0) &&
140 (current__view->getPitchOffset_deg() <= 15.0 || current__view->getPitchOffset_deg() >= 350.0) ) {
142 // we don't update the radar echo if the pilot looks around
144 radarEchoBuffer = *sgEnviro.get_radar_echo();
146 odg->beginCapture(256);
149 glMatrixMode(GL_MODELVIEW);
152 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
153 glBindTexture(GL_TEXTURE_2D, 0);
154 if( switchKnob == "off" ) {
155 _Instrument->setStringValue("status","");
156 } else if( switchKnob == "stby" ) {
157 _Instrument->setStringValue("status","STBY");
158 } else if( switchKnob == "tst" ) {
159 _Instrument->setStringValue("status","TST");
160 // find something interesting to do...
162 string display_mode = _Instrument->getStringValue("display-mode", "arc");
163 // pretend we have a scan angle bigger then the FOV
164 // TODO:check real fov, enlarge if < nn, and do clipping if > mm
165 const float fovFactor = 1.45f;
166 float view_heading = get_heading() * SG_DEGREES_TO_RADIANS;
167 float range = 200.0f / range_nm;
168 _Instrument->setStringValue("status", modeButton.c_str());
169 if( display_mode == "arc" ) {
170 glTranslatef(0.0f, -180.0f, 0.0f);
171 range = 2*180.0f / range_nm;
172 } else if( display_mode == "map" ) {
173 // float view_heading = get_track() * SG_DEGREES_TO_RADIANS;
174 } else if( display_mode == "plan" ) {
175 // no sense I presume
176 float view_heading = 0.0;
180 range /= SG_NM_TO_METER;
181 // we will rotate the echo quads, this gives a better rendering
182 const float rot_x = cos ( view_heading );
183 const float rot_y = sin ( view_heading );
185 list_of_SGWxRadarEcho *radarEcho = &radarEchoBuffer;
186 list_of_SGWxRadarEcho::iterator iradarEcho;
187 const float LWClevel[] = { 0.1f, 0.5f, 2.1f };
188 const float symbolSize = 1.0f / 8.0f ;
189 // draw the radar echo, we do that in 3 passes, one for each color level
190 // this is to 'merge' same colors together
191 glBindTexture(GL_TEXTURE_2D, wxEcho->getHandle() );
192 glColor3f(1.0f, 1.0f, 1.0f);
195 for(int level = 0; level <= 2 ; level++ ) {
196 float col = level * symbolSize;
197 for(iradarEcho = radarEcho->begin() ; iradarEcho != radarEcho->end() ; iradarEcho++ ) {
198 int cloudId = (iradarEcho->cloudId) ;
199 bool upgrade = ((cloudId >> 5) & 1);
200 float lwc = iradarEcho->LWC + (upgrade ? 1.0f : 0.0f);
202 if( iradarEcho->LWC >= 0.5 && iradarEcho->LWC <= 0.6)
204 if( (! iradarEcho->lightning) && ( lwc >= LWClevel[level]) ) {
205 float dist = sgSqrt( iradarEcho->dist );
206 float size = iradarEcho->radius * 2.0;
207 if( dist - size > range_m )
211 // compute the relative angle from the view direction
212 float angle = ( view_heading + iradarEcho->heading );
215 if( angle < - SG_PI )
217 // and apply a fov factor to simulate a greater scan angle
218 angle = angle * fovFactor + SG_PI / 2.0;
219 float x = cos( angle ) * dist;
220 float y = sin( angle ) * dist;
221 // use different shapes so the display is less boring
222 float row = symbolSize * (float) (4 + (cloudId & 3) );
223 float size_x = rot_x * size;
224 float size_y = rot_y * size;
225 glTexCoord2f( col, row);
226 glVertex2f( x - size_x, y - size_y);
227 glTexCoord2f( col+symbolSize, row);
228 glVertex2f( x + size_y, y - size_x);
229 glTexCoord2f( col+symbolSize, row+symbolSize);
230 glVertex2f( x + size_x, y + size_y);
231 glTexCoord2f( col, row+symbolSize);
232 glVertex2f( x - size_y, y + size_x);
238 // draw lightning echos
239 if( drawLightning ) {
240 float col = 3 * symbolSize;
241 float row = 4 * symbolSize;
242 for(iradarEcho = radarEcho->begin() ; iradarEcho != radarEcho->end() ; iradarEcho++ ) {
243 if( iradarEcho->lightning ) {
244 float dist = iradarEcho->dist;
246 float angle = (view_heading - iradarEcho->heading);
249 if( angle < - SG_PI )
251 angle = angle * fovFactor - SG_PI / 2.0;
252 float x = cos( angle ) * dist;
253 float y = sin( angle ) * dist;
254 glColor3f(1.0f, 1.0f, 1.0f);
255 float size = symbolSize * 0.5f;
257 glTexCoord2f( col, row);
258 glVertex2f( x - size, y - size);
259 glTexCoord2f( col+symbolSize, row);
260 glVertex2f( x + size, y - size);
261 glTexCoord2f( col+symbolSize, row+symbolSize);
262 glVertex2f( x + size, y + size);
263 glTexCoord2f( col, row+symbolSize);
264 glVertex2f( x - size, y + size);
269 // erase what is out of sight of antenna
279 float yOffset = 180.0f, xOffset = 256.0f;
280 if( display_mode != "arc" ) {
285 glColor4f(1.0f, 0.0f, 0.0f, 0.01f);
287 glTexCoord2f( 0.5f, 0.25f);
288 glVertex2f(-xOffset, 0.0 + yOffset);
289 glTexCoord2f( 1.0f, 0.25f);
290 glVertex2f(xOffset, 0.0 + yOffset);
291 glTexCoord2f( 1.0f, 0.5f);
292 glVertex2f(xOffset, 256.0 + yOffset);
293 glTexCoord2f( 0.5f, 0.5f);
294 glVertex2f(-xOffset, 256.0 + yOffset);
297 glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
298 // glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
299 glDisable(GL_ALPHA_TEST);
300 glBindTexture(GL_TEXTURE_2D, 0);
302 glBegin( GL_TRIANGLES );
303 glVertex2f(0.0, 0.0);
304 glVertex2f(-256.0, 0.0);
305 glVertex2f(-256.0, 256.0);
307 glVertex2f(0.0, 0.0);
308 glVertex2f(256.0, 0.0);
309 glVertex2f(256.0, 256.0);
311 glVertex2f(-256, 0.0);
312 glVertex2f(256.0, 0.0);
313 glVertex2f(-256.0, -256.0);
315 glVertex2f(256, 0.0);
316 glVertex2f(256.0, -256.0);
317 glVertex2f(-256.0, -256.0);
321 /* glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
323 glVertex2f(0.0, 0.0);
324 glVertex2f(-256.0, 256.0);
325 glVertex2f(0.0, 0.0);
326 glVertex2f(256.0, 256.0);
330 glEnable(GL_ALPHA_TEST);
333 odg->endCapture( resultTexture->getHandle() );