]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/wxradar.cxx
629f9abe11d563eb47bc901b2019b7771f7cf3ee
[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, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20 //
21 //
22
23 #include <plib/sg.h>
24 #include <plib/ssg.h>
25 #include <Main/fg_props.hxx>
26 #include <Main/globals.hxx>
27 #include <Cockpit/panel.hxx>
28 #include <Cockpit/hud.hxx>
29
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"
36
37 // texture name to use in 2D and 3D instruments
38 static const char *odgauge_name = "Aircraft/Instruments/Textures/od_wxradar.rgb";
39
40 wxRadarBg::wxRadarBg ( SGPropertyNode *node) :
41     name("wxRadar"),
42     num(0),
43     resultTexture( 0 ),
44     wxEcho( 0 ),
45     last_switchKnob( "off" ),
46     sim_init_done ( false ),
47     odg( 0 )
48 {
49     int i;
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" ) {
55             name = cval;
56         } else if ( cname == "number" ) {
57             num = child->getIntValue();
58         } else {
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 );
62             }
63         }
64     }
65 }
66
67 wxRadarBg::wxRadarBg ()
68 {
69 }
70
71 wxRadarBg::~wxRadarBg ()
72 {
73 }
74
75 void
76 wxRadarBg::init ()
77 {
78     string branch;
79     branch = "/instrumentation/" + name;
80
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);
88
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
102
103     FGInstrumentMgr *imgr = (FGInstrumentMgr *) globals->get_subsystem("instrumentation");
104     odg = (FGODGauge *) imgr->get_subsystem("od_gauge");
105 }
106
107 void
108 wxRadarBg::update (double delta_time_sec)
109 {
110     if( ! sim_init_done ) {
111         if( ! fgGetBool("sim/sceneryloaded", false) )
112             return;
113         sim_init_done = true;
114     }
115     if ( !odg || ! _serviceable_node->getBoolValue() ) {
116         _Instrument->setStringValue("status","");
117         return;
118     }
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;
124
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;
132     }
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) ) {
137
138         // we don't update the radar echo if the pilot looks around
139         // this is a copy
140         radarEchoBuffer = *sgEnviro.get_radar_echo();
141     }
142     odg->beginCapture(256);
143     odg->Clear();
144
145     glMatrixMode(GL_MODELVIEW);
146     glLoadIdentity();
147     glPushMatrix();
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...
157         } else {
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;
173             } else {
174                 // rose
175             }
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 );
180
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);
189             glBegin( GL_QUADS );
190
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);
197                     // skip ns
198                     if( iradarEcho->LWC >= 0.5 && iradarEcho->LWC <= 0.6)
199                         continue;
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 )
204                             continue;
205                         dist = dist * range;
206                         size = size * range;
207                         // compute the relative angle from the view direction
208                         float angle = ( view_heading + iradarEcho->heading );
209                         if( angle > SG_PI )
210                             angle -= 2.0*SG_PI;
211                         if( angle < - SG_PI )
212                             angle += 2.0*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);
229                     }
230                 }
231             }
232             glEnd(); // GL_QUADS
233
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;
241                         dist = dist * range;
242                         float angle = (view_heading - iradarEcho->heading);
243                         if( angle > SG_PI )
244                             angle -= 2.0*SG_PI;
245                         if( angle < - SG_PI )
246                             angle += 2.0*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;
252                         glBegin( GL_QUADS );
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);
261                         glEnd();
262                     }
263                 }
264             }
265             // erase what is out of sight of antenna
266             /*
267                 |\     /|
268                 | \   / |
269                 |  \ /  |
270                 ---------
271                 |       |
272                 |       |
273                 ---------
274             */
275             float yOffset = 180.0f, xOffset = 256.0f;
276             if( display_mode != "arc" ) {
277                 yOffset = 40.0f;
278                 xOffset = 240.0f;
279             }
280              glDisable(GL_BLEND);
281                glColor4f(1.0f, 0.0f, 0.0f, 0.01f);
282             glBegin( GL_QUADS );
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);
291             glEnd();
292
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);
297
298             glBegin( GL_TRIANGLES );
299                 glVertex2f(0.0, 0.0);
300                 glVertex2f(-256.0, 0.0);
301                 glVertex2f(-256.0, 256.0);
302
303                 glVertex2f(0.0, 0.0);
304                 glVertex2f(256.0, 0.0);
305                 glVertex2f(256.0, 256.0);
306
307                 glVertex2f(-256, 0.0);
308                 glVertex2f(256.0, 0.0);
309                 glVertex2f(-256.0, -256.0);
310
311                 glVertex2f(256, 0.0);
312                 glVertex2f(256.0, -256.0);
313                 glVertex2f(-256.0, -256.0);
314             glEnd();
315
316             // DEBUG only
317 /*            glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
318             glBegin( GL_LINES );
319                 glVertex2f(0.0, 0.0);
320                 glVertex2f(-256.0, 256.0);
321                 glVertex2f(0.0, 0.0);
322                 glVertex2f(256.0, 256.0);
323             glEnd();*/
324
325             glEnable(GL_BLEND);
326             glEnable(GL_ALPHA_TEST);
327         }
328     glPopMatrix();
329     odg->endCapture( resultTexture->getHandle() );
330 }