1 // Wx Radar background texture
3 // Written by Harald JOHNSEN, started May 2005.
5 // Copyright (C) 2005 Harald JOHNSEN - hjohnsen@evc.net
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 ()
79 branch = "/instrumentation/" + name;
81 _Instrument = fgGetNode(branch.c_str(), num, true );
82 _serviceable_node = _Instrument->getChild("serviceable", 0, true);
83 resultTexture = FGTextureManager::createTexture( odgauge_name );
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);
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
105 FGInstrumentMgr *imgr = (FGInstrumentMgr *) globals->get_subsystem("instrumentation");
106 odg = (FGODGauge *) imgr->get_subsystem("od_gauge");
110 wxRadarBg::update (double delta_time_sec)
112 if( ! sim_init_done ) {
113 if( ! fgGetBool("sim/sceneryloaded", false) )
115 sim_init_done = true;
117 if ( !odg || ! _serviceable_node->getBoolValue() ) {
118 _Instrument->setStringValue("status","");
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;
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;
135 odg->beginCapture(256);
137 glMatrixMode(GL_MODELVIEW);
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...
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;
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 );
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);
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);
190 if( iradarEcho->LWC >= 0.5 && iradarEcho->LWC <= 0.6)
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 )
199 // compute the relative angle from the view direction
200 float angle = ( view_heading + iradarEcho->heading );
203 if( angle < - 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);
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;
234 float angle = (view_heading - iradarEcho->heading);
237 if( angle < - 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;
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);
257 // erase what is out of sight of antenna
267 float yOffset = 180.0f, xOffset = 256.0f;
268 if( display_mode != "arc" ) {
273 glColor4f(1.0f, 0.0f, 0.0f, 0.01f);
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);
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);
290 glBegin( GL_TRIANGLES );
291 glVertex2f(0.0, 0.0);
292 glVertex2f(-256.0, 0.0);
293 glVertex2f(-256.0, 256.0);
295 glVertex2f(0.0, 0.0);
296 glVertex2f(256.0, 0.0);
297 glVertex2f(256.0, 256.0);
299 glVertex2f(-256, 0.0);
300 glVertex2f(256.0, 0.0);
301 glVertex2f(-256.0, -256.0);
303 glVertex2f(256, 0.0);
304 glVertex2f(256.0, -256.0);
305 glVertex2f(-256.0, -256.0);
309 /* glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
311 glVertex2f(0.0, 0.0);
312 glVertex2f(-256.0, 256.0);
313 glVertex2f(0.0, 0.0);
314 glVertex2f(256.0, 256.0);
318 glEnable(GL_ALPHA_TEST);
321 odg->endCapture( resultTexture->getHandle() );