]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/wxradar.cxx
Add missing include files needed by the new math code under windows
[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 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <plib/sg.h>
28 #include <plib/ssg.h>
29 #include <Main/fg_props.hxx>
30 #include <Main/globals.hxx>
31 #include <Cockpit/panel.hxx>
32 #include <Cockpit/hud.hxx>
33
34 #include <simgear/constants.h>
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/environment/visual_enviro.hxx>
37 #include "instrument_mgr.hxx"
38 #include "od_gauge.hxx"
39 #include "wxradar.hxx"
40
41 // texture name to use in 2D and 3D instruments
42 static const char *odgauge_name = "Aircraft/Instruments/Textures/od_wxradar.rgb";
43
44 wxRadarBg::wxRadarBg ( SGPropertyNode *node) :
45     name("wxRadar"),
46     num(0),
47     resultTexture( 0 ),
48     wxEcho( 0 ),
49     last_switchKnob( "off" ),
50     sim_init_done ( false ),
51     odg( 0 )
52 {
53     int i;
54     for ( i = 0; i < node->nChildren(); ++i ) {
55         SGPropertyNode *child = node->getChild(i);
56         string cname = child->getName();
57         string cval = child->getStringValue();
58         if ( cname == "name" ) {
59             name = cval;
60         } else if ( cname == "number" ) {
61             num = child->getIntValue();
62         } else {
63             SG_LOG( SG_INSTR, SG_WARN, "Error in wxRadar config logic" );
64             if ( name.length() ) {
65                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
66             }
67         }
68     }
69 }
70
71 wxRadarBg::wxRadarBg ()
72 {
73 }
74
75 wxRadarBg::~wxRadarBg ()
76 {
77 }
78
79 void
80 wxRadarBg::init ()
81 {
82     string branch;
83     branch = "/instrumentation/" + name;
84
85     _Instrument = fgGetNode(branch.c_str(), num, true );
86     _serviceable_node = _Instrument->getChild("serviceable", 0, true);
87     resultTexture = FGTextureManager::createTexture( odgauge_name );
88     SGPath tpath(globals->get_fg_root());
89     tpath.append("Aircraft/Instruments/Textures/wxecho.rgb");
90     // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect
91     wxEcho = new ssgTexture( tpath.c_str(), false, false, false);
92
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
106
107     FGInstrumentMgr *imgr = (FGInstrumentMgr *) globals->get_subsystem("instrumentation");
108     odg = (FGODGauge *) imgr->get_subsystem("od_gauge");
109 }
110
111 void
112 wxRadarBg::update (double delta_time_sec)
113 {
114     if( ! sim_init_done ) {
115         if( ! fgGetBool("sim/sceneryloaded", false) )
116             return;
117         sim_init_done = true;
118     }
119     if ( !odg || ! _serviceable_node->getBoolValue() ) {
120         _Instrument->setStringValue("status","");
121         return;
122     }
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;
128
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;
136     }
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) ) {
141
142         // we don't update the radar echo if the pilot looks around
143         // this is a copy
144         radarEchoBuffer = *sgEnviro.get_radar_echo();
145     }
146     odg->beginCapture(256);
147     odg->Clear();
148
149     glMatrixMode(GL_MODELVIEW);
150     glLoadIdentity();
151     glPushMatrix();
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...
161         } else {
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;
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             if( display_mode != "arc" ) {
281                 yOffset = 40.0f;
282                 xOffset = 240.0f;
283             }
284              glDisable(GL_BLEND);
285                glColor4f(1.0f, 0.0f, 0.0f, 0.01f);
286             glBegin( GL_QUADS );
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);
295             glEnd();
296
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);
301
302             glBegin( GL_TRIANGLES );
303                 glVertex2f(0.0, 0.0);
304                 glVertex2f(-256.0, 0.0);
305                 glVertex2f(-256.0, 256.0);
306
307                 glVertex2f(0.0, 0.0);
308                 glVertex2f(256.0, 0.0);
309                 glVertex2f(256.0, 256.0);
310
311                 glVertex2f(-256, 0.0);
312                 glVertex2f(256.0, 0.0);
313                 glVertex2f(-256.0, -256.0);
314
315                 glVertex2f(256, 0.0);
316                 glVertex2f(256.0, -256.0);
317                 glVertex2f(-256.0, -256.0);
318             glEnd();
319
320             // DEBUG only
321 /*            glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
322             glBegin( GL_LINES );
323                 glVertex2f(0.0, 0.0);
324                 glVertex2f(-256.0, 256.0);
325                 glVertex2f(0.0, 0.0);
326                 glVertex2f(256.0, 256.0);
327             glEnd();*/
328
329             glEnable(GL_BLEND);
330             glEnable(GL_ALPHA_TEST);
331         }
332     glPopMatrix();
333     odg->endCapture( resultTexture->getHandle() );
334 }