]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/wxradar.cxx
Vivian MEAZZA:
[flightgear.git] / src / Instrumentation / wxradar.cxx
1 // Wx Radar background texture
2 //
3 // Written by Harald JOHNSEN, started May 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN
6 //
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.
11 //
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.
16 //
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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 //
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <plib/sg.h>
28 #include <Main/fg_props.hxx>
29 #include <Main/globals.hxx>
30 #include <Cockpit/panel.hxx>
31 #include <Cockpit/hud.hxx>
32
33 #include <simgear/constants.h>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/environment/visual_enviro.hxx>
36 #include "instrument_mgr.hxx"
37 #include "od_gauge.hxx"
38 #include "wxradar.hxx"
39
40 // texture name to use in 2D and 3D instruments
41 static const char *odgauge_name = "Aircraft/Instruments/Textures/od_wxradar.rgb";
42
43 wxRadarBg::wxRadarBg ( SGPropertyNode *node) :
44     name("wxRadar"),
45     num(0),
46     resultTexture( 0 ),
47     wxEcho( 0 ),
48     last_switchKnob( "off" ),
49     sim_init_done ( false ),
50     odg( 0 )
51 {
52     int i;
53     for ( i = 0; i < node->nChildren(); ++i ) {
54         SGPropertyNode *child = node->getChild(i);
55         string cname = child->getName();
56         string cval = child->getStringValue();
57         if ( cname == "name" ) {
58             name = cval;
59         } else if ( cname == "number" ) {
60             num = child->getIntValue();
61         } else {
62             SG_LOG( SG_INSTR, SG_WARN, "Error in wxRadar config logic" );
63             if ( name.length() ) {
64                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
65             }
66         }
67     }
68 }
69
70 wxRadarBg::wxRadarBg ()
71 {
72 }
73
74 wxRadarBg::~wxRadarBg ()
75 {
76 }
77
78 void
79 wxRadarBg::init ()
80 {
81     string branch;
82     branch = "/instrumentation/" + name;
83
84     _Instrument = fgGetNode(branch.c_str(), num, true );
85     _serviceable_node = _Instrument->getChild("serviceable", 0, true);
86     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);
91
92     _Instrument->setFloatValue("trk", 0.0);
93     _Instrument->setFloatValue("tilt", 0.0);
94     _Instrument->setStringValue("status","");
95     // those properties are used by a radar instrument of a MFD
96     // input switch = OFF | TST | STBY | ON
97     // input mode = WX | WXA | MAP
98     // ouput status = STBY | TEST | WX | WXA | MAP | blank
99     // input lightning = true | false
100     // input TRK = +/- n degrees
101     // input TILT = +/- n degree
102     // input autotilt = true | false
103     // input range = n nm (20/40/80)
104     // input display-mode = arc | rose | map | plan
105
106     FGInstrumentMgr *imgr = (FGInstrumentMgr *) globals->get_subsystem("instrumentation");
107     odg = (FGODGauge *) imgr->get_subsystem("od_gauge");
108 }
109
110 void
111 wxRadarBg::update (double delta_time_sec)
112 {
113     if( ! sim_init_done ) {
114         if( ! fgGetBool("sim/sceneryloaded", false) )
115             return;
116         sim_init_done = true;
117     }
118     if ( !odg || ! _serviceable_node->getBoolValue() ) {
119         _Instrument->setStringValue("status","");
120         return;
121     }
122     string switchKnob = _Instrument->getStringValue("switch", "on");
123     string modeButton = _Instrument->getStringValue("mode", "wx");
124     bool drawLightning = _Instrument->getBoolValue("lightning", true);
125     float range_nm = _Instrument->getFloatValue("range", 40.0);
126     float range_m = range_nm * SG_NM_TO_METER;
127
128     if( last_switchKnob != switchKnob ) {
129         // since 3D models don't share textures with the rest of the world
130         // we must locate them and replace their handle by hand
131         // only do that when the instrument is turned on
132         if( last_switchKnob == "off" )
133             odg->set_texture( odgauge_name, resultTexture->getHandle());
134         last_switchKnob = switchKnob;
135     }
136     FGViewer *current__view = globals->get_current_view();
137     if( current__view->getInternal() && 
138         (current__view->getHeadingOffset_deg() <= 15.0 || current__view->getHeadingOffset_deg() >= 345.0) &&
139         (current__view->getPitchOffset_deg() <= 15.0 || current__view->getPitchOffset_deg() >= 350.0) ) {
140
141         // we don't update the radar echo if the pilot looks around
142         // this is a copy
143         radarEchoBuffer = *sgEnviro.get_radar_echo();
144     }
145     odg->beginCapture(256);
146     odg->Clear();
147
148     glMatrixMode(GL_MODELVIEW);
149     glLoadIdentity();
150     glPushMatrix();
151         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
152         glBindTexture(GL_TEXTURE_2D, 0);
153         if( switchKnob == "off" ) {
154             _Instrument->setStringValue("status","");
155         } else if( switchKnob == "stby" ) {
156             _Instrument->setStringValue("status","STBY");
157         } else if( switchKnob == "tst" ) {
158             _Instrument->setStringValue("status","TST");
159             // find something interesting to do...
160         } else {
161             string display_mode = _Instrument->getStringValue("display-mode", "arc");
162
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                                 view_heading = 0;
177             } else {
178                 // rose
179             }
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 );
184
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);
193             glBegin( GL_QUADS );
194
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);
201                     // skip ns
202                     if( iradarEcho->LWC >= 0.5 && iradarEcho->LWC <= 0.6)
203                         continue;
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 )
208                             continue;
209                         dist = dist * range;
210                         size = size * range;
211                         // compute the relative angle from the view direction
212                         float angle = ( view_heading + iradarEcho->heading );
213                         if( angle > SG_PI )
214                             angle -= 2.0*SG_PI;
215                         if( angle < - SG_PI )
216                             angle += 2.0*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);
233                     }
234                 }
235             }
236             glEnd(); // GL_QUADS
237
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;
245                         dist = dist * range;
246                         float angle = (view_heading - iradarEcho->heading);
247                         if( angle > SG_PI )
248                             angle -= 2.0*SG_PI;
249                         if( angle < - SG_PI )
250                             angle += 2.0*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;
256                         glBegin( GL_QUADS );
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);
265                         glEnd();
266                     }
267                 }
268             }
269             // erase what is out of sight of antenna
270             /*
271                 |\     /|
272                 | \   / |
273                 |  \ /  |
274                 ---------
275                 |       |
276                 |       |
277                 ---------
278             */
279             float yOffset = 180.0f, xOffset = 256.0f;
280                         
281             if( display_mode != "arc" ) {
282                 yOffset = 40.0f;
283                 xOffset = 240.0f;
284             }
285                         
286             if ( display_mode != "plan" ) {     
287              glDisable(GL_BLEND);
288                glColor4f(1.0f, 0.0f, 0.0f, 0.01f);
289             glBegin( GL_QUADS );
290                 glTexCoord2f( 0.5f, 0.25f);
291                 glVertex2f(-xOffset, 0.0 + yOffset);
292                 glTexCoord2f( 1.0f, 0.25f);
293                 glVertex2f(xOffset, 0.0 + yOffset);
294                 glTexCoord2f( 1.0f, 0.5f);
295                 glVertex2f(xOffset, 256.0 + yOffset);
296                 glTexCoord2f( 0.5f, 0.5f);
297                 glVertex2f(-xOffset, 256.0 + yOffset);
298             glEnd();
299
300             glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
301 //            glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
302             glDisable(GL_ALPHA_TEST);
303             glBindTexture(GL_TEXTURE_2D, 0);
304
305             glBegin( GL_TRIANGLES );
306                 glVertex2f(0.0, 0.0);
307                 glVertex2f(-256.0, 0.0);
308                 glVertex2f(-256.0, 256.0 * tan(30*SG_DEGREES_TO_RADIANS));                       
309
310                 glVertex2f(0.0, 0.0);
311                 glVertex2f(256.0, 0.0);
312                 glVertex2f(256.0, 256.0 * tan(30*SG_DEGREES_TO_RADIANS));
313
314                 glVertex2f(-256, 0.0);
315                 glVertex2f(256.0, 0.0);
316                 glVertex2f(-256.0, -256.0);
317
318                 glVertex2f(256, 0.0);
319                 glVertex2f(256.0, -256.0);
320                 glVertex2f(-256.0, -256.0);
321             glEnd();
322                         }
323
324             // DEBUG only
325 /*            glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
326             glBegin( GL_LINES );
327                 glVertex2f(0.0, 0.0);
328                 glVertex2f(-256.0, 256.0);
329                 glVertex2f(0.0, 0.0);
330                 glVertex2f(256.0, 256.0);
331             glEnd();*/
332
333             glEnable(GL_BLEND);
334             glEnable(GL_ALPHA_TEST);
335         }
336     glPopMatrix();
337     odg->endCapture( resultTexture->getHandle() );
338 }