]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/wxradar.cxx
Harald JOHNSEN:
[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 - hjohnsen@evc.net
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     resultTexture->ref();
85     SGPath tpath(globals->get_fg_root());
86     tpath.append("Aircraft/Instruments/Textures/wxecho.rgb");
87     // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect
88     wxEcho = new ssgTexture( tpath.c_str(), false, false, false);
89     wxEcho->ref();
90
91     _Instrument->setFloatValue("trk", 0.0);
92     _Instrument->setFloatValue("tilt", 0.0);
93     _Instrument->setStringValue("status","");
94     // those properties are used by a radar instrument of a MFD
95     // input switch = OFF | TST | STBY | ON
96     // input mode = WX | WXA | MAP
97     // ouput status = STBY | TEST | WX | WXA | MAP | blank
98     // input lightning = true | false
99     // input TRK = +/- n degrees
100     // input TILT = +/- n degree
101     // input autotilt = true | false
102     // input range = n nm (20/40/80)
103     // input display-mode = arc | rose | map | plan
104
105     FGInstrumentMgr *imgr = (FGInstrumentMgr *) globals->get_subsystem("instrumentation");
106     odg = (FGODGauge *) imgr->get_subsystem("od_gauge");
107 }
108
109 void
110 wxRadarBg::update (double delta_time_sec)
111 {
112     if( ! sim_init_done ) {
113         if( ! fgGetBool("sim/sceneryloaded", false) )
114             return;
115         sim_init_done = true;
116     }
117     if ( !odg || ! _serviceable_node->getBoolValue() ) {
118         _Instrument->setStringValue("status","");
119         return;
120     }
121     string switchKnob = _Instrument->getStringValue("switch", "on");
122     string modeButton = _Instrument->getStringValue("mode", "wx");
123     bool drawLightning = _Instrument->getBoolValue("lightning", true);
124     float range_nm = _Instrument->getFloatValue("range", 40.0);
125     float range_m = range_nm * SG_NM_TO_METER;
126
127     if( last_switchKnob != switchKnob ) {
128         // since 3D models don't share textures with the rest of the world
129         // we must locate them and replace their handle by hand
130         // only do that when the instrument is turned on
131         if( last_switchKnob == "off" )
132             odg->set_texture( odgauge_name, resultTexture->getHandle());
133         last_switchKnob = switchKnob;
134     }
135     odg->beginCapture(256);
136     odg->Clear();
137     glMatrixMode(GL_MODELVIEW);
138     glLoadIdentity();
139     glPushMatrix();
140         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
141         glBindTexture(GL_TEXTURE_2D, 0);
142         if( switchKnob == "off" ) {
143             _Instrument->setStringValue("status","");
144         } else if( switchKnob == "stby" ) {
145             _Instrument->setStringValue("status","STBY");
146         } else if( switchKnob == "tst" ) {
147             _Instrument->setStringValue("status","TST");
148             // find something interesting to do...
149         } else {
150             string display_mode = _Instrument->getStringValue("display-mode", "arc");
151             // pretend we have a scan angle bigger then the FOV
152             // TODO:check real fov, enlarge if < nn, and do clipping if > mm
153             const float fovFactor = 1.45f;
154             float view_heading = get_heading() * SG_DEGREES_TO_RADIANS;
155             float range = 200.0f / range_nm;
156             _Instrument->setStringValue("status", modeButton.c_str());
157             if( display_mode == "arc" ) {
158                 glTranslatef(0.0f, -180.0f, 0.0f);
159                 range = 2*180.0f / range_nm;
160             } else if( display_mode == "map" ) {
161 //                float view_heading = get_track() * SG_DEGREES_TO_RADIANS;
162             } else if( display_mode == "plan" ) {
163                 // no sense I presume
164                 float view_heading = 0.0;
165             } else {
166                 // rose
167             }
168             range /= SG_NM_TO_METER;
169             // we will rotate the echo quads, this gives a better rendering
170             const float rot_x = cos ( view_heading );
171             const float rot_y = sin ( view_heading );
172
173             list_of_SGWxRadarEcho *radarEcho = sgEnviro.get_radar_echo();
174             list_of_SGWxRadarEcho::iterator iradarEcho;
175             const float LWClevel[] = { 0.1f, 0.5f, 2.1f };
176             const float symbolSize = 1.0f / 8.0f ;
177             // draw the radar echo, we do that in 3 passes, one for each color level
178             // this is to 'merge' same colors together
179             glBindTexture(GL_TEXTURE_2D, wxEcho->getHandle() );
180             glColor3f(1.0f, 1.0f, 1.0f);
181             glBegin( GL_QUADS );
182
183             for(int level = 0; level <= 2 ; level++ ) {
184                 float col = level * symbolSize;
185                 for(iradarEcho = radarEcho->begin() ; iradarEcho != radarEcho->end() ; iradarEcho++ ) {
186                     int cloudId = (iradarEcho->cloudId) ;
187                     bool upgrade = ((cloudId >> 5) & 1);
188                     float lwc = iradarEcho->LWC + (upgrade ? 1.0f : 0.0f);
189                     // skip ns
190                     if( iradarEcho->LWC >= 0.5 && iradarEcho->LWC <= 0.6)
191                         continue;
192                     if( (! iradarEcho->lightning) && ( lwc >= LWClevel[level]) ) {
193                         float dist = sgSqrt( iradarEcho->dist );
194                         float size = iradarEcho->radius * 2.0;
195                         if( dist - size > range_m )
196                             continue;
197                         dist = dist * range;
198                         size = size * range;
199                         // compute the relative angle from the view direction
200                         float angle = ( view_heading + iradarEcho->heading );
201                         if( angle > SG_PI )
202                             angle -= 2.0*SG_PI;
203                         if( angle < - SG_PI )
204                             angle += 2.0*SG_PI;
205                         // and apply a fov factor to simulate a greater scan angle
206                         angle =  angle * fovFactor + SG_PI / 2.0;
207                         float x = cos( angle ) * dist;
208                         float y = sin( angle ) * dist;
209                         // use different shapes so the display is less boring
210                         float row = symbolSize * (float) (4 + (cloudId & 3) );
211                         float size_x = rot_x * size;
212                         float size_y = rot_y * size;
213                         glTexCoord2f( col, row);
214                         glVertex2f( x - size_x, y - size_y);
215                         glTexCoord2f( col+symbolSize, row);
216                         glVertex2f( x + size_y, y - size_x);
217                         glTexCoord2f( col+symbolSize, row+symbolSize);
218                         glVertex2f( x + size_x, y + size_y);
219                         glTexCoord2f( col, row+symbolSize);
220                         glVertex2f( x - size_y, y + size_x);
221                     }
222                 }
223             }
224             glEnd(); // GL_QUADS
225
226             // draw lightning echos
227             if( drawLightning ) {
228                 float col = 3 * symbolSize;
229                 float row = 4 * symbolSize;
230                 for(iradarEcho = radarEcho->begin() ; iradarEcho != radarEcho->end() ; iradarEcho++ ) {
231                     if( iradarEcho->lightning ) {
232                         float dist = iradarEcho->dist;
233                         dist = dist * range;
234                         float angle = (view_heading - iradarEcho->heading);
235                         if( angle > SG_PI )
236                             angle -= 2.0*SG_PI;
237                         if( angle < - SG_PI )
238                             angle += 2.0*SG_PI;
239                         angle =  angle * fovFactor - SG_PI / 2.0;
240                         float x = cos( angle ) * dist;
241                         float y = sin( angle ) * dist;
242                         glColor3f(1.0f, 1.0f, 1.0f);
243                         float size = symbolSize * 0.5f;
244                         glBegin( GL_QUADS );
245                             glTexCoord2f( col, row);
246                             glVertex2f( x - size, y - size);
247                             glTexCoord2f( col+symbolSize, row);
248                             glVertex2f( x + size, y - size);
249                             glTexCoord2f( col+symbolSize, row+symbolSize);
250                             glVertex2f( x + size, y + size);
251                             glTexCoord2f( col, row+symbolSize);
252                             glVertex2f( x - size, y + size);
253                         glEnd();
254                     }
255                 }
256             }
257             // erase what is out of sight of antenna
258             /*
259                 |\     /|
260                 | \   / |
261                 |  \ /  |
262                 ---------
263                 |       |
264                 |       |
265                 ---------
266             */
267             float yOffset = 180.0f, xOffset = 256.0f;
268             if( display_mode != "arc" ) {
269                 yOffset = 40.0f;
270                 xOffset = 240.0f;
271             }
272              glDisable(GL_BLEND);
273                glColor4f(1.0f, 0.0f, 0.0f, 0.01f);
274             glBegin( GL_QUADS );
275                 glTexCoord2f( 0.5f, 0.25f);
276                 glVertex2f(-xOffset, 0.0 + yOffset);
277                 glTexCoord2f( 1.0f, 0.25f);
278                 glVertex2f(xOffset, 0.0 + yOffset);
279                 glTexCoord2f( 1.0f, 0.5f);
280                 glVertex2f(xOffset, 256.0 + yOffset);
281                 glTexCoord2f( 0.5f, 0.5f);
282                 glVertex2f(-xOffset, 256.0 + yOffset);
283             glEnd();
284
285             glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
286 //            glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
287             glDisable(GL_ALPHA_TEST);
288             glBindTexture(GL_TEXTURE_2D, 0);
289
290             glBegin( GL_TRIANGLES );
291                 glVertex2f(0.0, 0.0);
292                 glVertex2f(-256.0, 0.0);
293                 glVertex2f(-256.0, 256.0);
294
295                 glVertex2f(0.0, 0.0);
296                 glVertex2f(256.0, 0.0);
297                 glVertex2f(256.0, 256.0);
298
299                 glVertex2f(-256, 0.0);
300                 glVertex2f(256.0, 0.0);
301                 glVertex2f(-256.0, -256.0);
302
303                 glVertex2f(256, 0.0);
304                 glVertex2f(256.0, -256.0);
305                 glVertex2f(-256.0, -256.0);
306             glEnd();
307
308             // DEBUG only
309 /*            glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
310             glBegin( GL_LINES );
311                 glVertex2f(0.0, 0.0);
312                 glVertex2f(-256.0, 256.0);
313                 glVertex2f(0.0, 0.0);
314                 glVertex2f(256.0, 256.0);
315             glEnd();*/
316
317             glEnable(GL_BLEND);
318             glEnable(GL_ALPHA_TEST);
319         }
320     glPopMatrix();
321     odg->endCapture( resultTexture->getHandle() );
322 }