]> git.mxchange.org Git - simgear.git/blob - simgear/environment/visual_enviro.cxx
1c265f7f1fde3fba4a0d9bc36fea5b730208dc50
[simgear.git] / simgear / environment / visual_enviro.cxx
1 // Visual environment helper class
2 //
3 // Written by Harald JOHNSEN, started April 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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 //
22 #ifdef HAVE_CONFIG_H
23 #  include <simgear_config.h>
24 #endif
25
26 #include <plib/sg.h>
27 #include <simgear/constants.h>
28 #include <simgear/structure/SGReferenced.hxx>
29 #include <simgear/structure/SGSharedPtr.hxx>
30 #include <simgear/math/sg_random.h>
31 #include <simgear/math/sg_geodesy.hxx>
32 #include <simgear/math/point3d.hxx>
33 #include <simgear/math/polar3d.hxx>
34 #include <simgear/sound/soundmgr_openal.hxx>
35 #include <simgear/scene/sky/cloudfield.hxx>
36 #include <simgear/scene/sky/newcloud.hxx>
37 #include "visual_enviro.hxx"
38
39 #include <vector>
40
41 SG_USING_STD(vector);
42
43
44 typedef struct {
45         Point3D         pt;
46         int                     depth;
47         int                     prev;
48 } lt_tree_seg;
49
50 #define MAX_RAIN_SLICE  200
51 static float rainpos[MAX_RAIN_SLICE];
52 #define MAX_LT_TREE_SEG 400
53
54 /**
55  * A class to render lightnings.
56  */
57 class SGLightning {
58 public:
59     /**
60      * Build a new lightning.
61      * The lightning has a limited life time. It will also play a thunder sounder once.
62      * @param lon lon longitude in degree
63      * @param lat lat latitude in degree
64      * @param alt asl of top of lightning
65      */
66         SGLightning(double lon, double lat, double alt);
67         ~SGLightning();
68         void lt_Render(void);
69         void lt_build(void);
70         void lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize);
71
72         // contains all the segments of the lightning
73         lt_tree_seg lt_tree[MAX_LT_TREE_SEG];
74         // segment count
75         int             nb_tree;
76         // position of lightning
77         double  lon, lat, alt;
78         int             sequence_count;
79         // time to live
80         double  age;
81 };
82
83 typedef vector<SGLightning *> list_of_lightning;
84 static list_of_lightning lightnings;
85
86 SGEnviro sgEnviro;
87
88 SGEnviro::SGEnviro() :
89         view_in_cloud(false),
90         precipitation_enable_state(true),
91         precipitation_density(100.0),
92         precipitation_max_alt(0.0),
93         turbulence_enable_state(false),
94         last_cloud_turbulence(0.0),
95         cloud_turbulence(0.0),
96         lightning_enable_state(false),
97         elapsed_time(0.0),
98         dt(0.0),
99         soundMgr(NULL),
100         snd_active(false),
101         snd_dist(0.0),
102         min_time_before_lt(0.0),
103         fov_width(55.0),
104         fov_height(55.0)
105
106 {
107         for(int i = 0; i < MAX_RAIN_SLICE ; i++)
108                 rainpos[i] = sg_random();
109         radarEcho.reserve(100);
110 }
111
112 SGEnviro::~SGEnviro(void) {
113         list_of_lightning::iterator iLightning;
114         for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
115                 delete (*iLightning);
116         }
117         lightnings.clear();
118 }
119
120 void SGEnviro::startOfFrame( sgVec3 p, sgVec3 up, double lon, double lat, double alt, double delta_time) {
121         view_in_cloud = false;
122         // ask the impostor cache to do some cleanup
123         if(SGNewCloud::cldCache)
124                 SGNewCloud::cldCache->startNewFrame();
125         last_cloud_turbulence = cloud_turbulence;
126         cloud_turbulence = 0.0;
127         elapsed_time += delta_time;
128         min_time_before_lt -= delta_time;
129         dt = delta_time;
130
131         sgMat4 T1, LON, LAT;
132     sgVec3 axis;
133
134     sgMakeTransMat4( T1, p );
135
136     sgSetVec3( axis, 0.0, 0.0, 1.0 );
137     sgMakeRotMat4( LON, lon, axis );
138
139     sgSetVec3( axis, 0.0, 1.0, 0.0 );
140     sgMakeRotMat4( LAT, 90.0 - lat, axis );
141
142     sgMat4 TRANSFORM;
143
144     sgCopyMat4( TRANSFORM, T1 );
145     sgPreMultMat4( TRANSFORM, LON );
146     sgPreMultMat4( TRANSFORM, LAT );
147
148     sgCoord pos;
149     sgSetCoord( &pos, TRANSFORM );
150
151         sgMakeCoordMat4( transform, &pos );
152     last_lon = lon;
153     last_lat = lat;
154         last_alt = alt;
155
156         radarEcho.clear();
157         precipitation_max_alt = 400.0;
158 }
159
160 void SGEnviro::endOfFrame(void) {
161 }
162
163 double SGEnviro::get_cloud_turbulence(void) const {
164         return last_cloud_turbulence;
165 }
166
167 // this can be queried to add some turbulence for example
168 bool SGEnviro::is_view_in_cloud(void) const {
169         return view_in_cloud;
170 }
171 void SGEnviro::set_view_in_cloud(bool incloud) {
172         view_in_cloud = incloud;
173 }
174
175 int SGEnviro::get_CacheResolution(void) const {
176         return SGCloudField::get_CacheResolution();
177 }
178
179 int SGEnviro::get_clouds_CacheSize(void) const {
180         return SGCloudField::get_CacheSize();
181 }
182 float SGEnviro::get_clouds_visibility(void) const {
183         return SGCloudField::get_CloudVis();
184 }
185 float SGEnviro::get_clouds_density(void) const {
186         return SGCloudField::get_density();
187 }
188 bool SGEnviro::get_clouds_enable_state(void) const {
189         return SGCloudField::get_enable3dClouds();
190 }
191
192 bool SGEnviro::get_turbulence_enable_state(void) const {
193         return turbulence_enable_state;
194 }
195
196 void SGEnviro::set_CacheResolution(int resolutionPixels) {
197         SGCloudField::set_CacheResolution(resolutionPixels);
198 }
199
200 void SGEnviro::set_clouds_CacheSize(int sizeKb) {
201         SGCloudField::set_CacheSize(sizeKb);
202 }
203 void SGEnviro::set_clouds_visibility(float distance) {
204         SGCloudField::set_CloudVis(distance);
205 }
206 void SGEnviro::set_clouds_density(float density) {
207         SGCloudField::set_density(density);
208 }
209 void SGEnviro::set_clouds_enable_state(bool enable) {
210         SGCloudField::set_enable3dClouds(enable);
211 }
212 void SGEnviro::set_turbulence_enable_state(bool enable) {
213         turbulence_enable_state = enable;
214 }
215 // rain/snow
216 float SGEnviro::get_precipitation_density(void) const {
217         return precipitation_density;
218 }
219 bool SGEnviro::get_precipitation_enable_state(void) const {
220         return precipitation_enable_state;
221 }
222
223 void SGEnviro::set_precipitation_density(float density) {
224         precipitation_density = density;
225 }
226 void SGEnviro::set_precipitation_enable_state(bool enable) {
227         precipitation_enable_state = enable;
228 }
229
230 // others
231 bool SGEnviro::get_lightning_enable_state(void) const {
232         return lightning_enable_state;
233 }
234
235 void SGEnviro::set_lightning_enable_state(bool enable) {
236         lightning_enable_state = enable;
237         if( ! enable ) {
238                 // TODO:cleanup
239         }
240 }
241
242 void SGEnviro::setLight(sgVec4 adj_fog_color) {
243         sgCopyVec4( fog_color, adj_fog_color );
244         if( false ) {
245         //    ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
246         }
247 }
248
249 void SGEnviro::callback_cloud(float heading, float alt, float radius, int family, float dist, int cloudId) {
250         // send data to wx radar
251         // compute turbulence
252         // draw precipitation
253         // draw lightning
254         // compute illumination
255
256         // http://www.pilotfriend.com/flight_training/weather/THUNDERSTORM%20HAZARDS1.htm
257         double turbulence = 0.0;
258         if( dist < radius * radius * 2.25f ) {
259                 switch(family) {
260                         case SGNewCloud::CLFamilly_st:
261                                 turbulence = 0.2;
262                                 break;
263                         case SGNewCloud::CLFamilly_ci:
264                         case SGNewCloud::CLFamilly_cs:
265                         case SGNewCloud::CLFamilly_cc:
266                         case SGNewCloud::CLFamilly_ac:
267                         case SGNewCloud::CLFamilly_as:
268                                 turbulence = 0.1;
269                                 break;
270                         case SGNewCloud::CLFamilly_sc:
271                                 turbulence = 0.3;
272                                 break;
273                         case SGNewCloud::CLFamilly_ns:
274                                 turbulence = 0.4;
275                                 break;
276                         case SGNewCloud::CLFamilly_cu:
277                                 turbulence = 0.5;
278                                 break;
279                         case SGNewCloud::CLFamilly_cb:
280                                 turbulence = 0.6;
281                                 break;
282                 }
283                 // full turbulence inside cloud, half in the vicinity
284                 if( dist > radius * radius )
285                         turbulence *= 0.5;
286                 if( turbulence > cloud_turbulence )
287                         cloud_turbulence = turbulence;
288                 // we can do 'local' precipitations too
289         }
290
291         // convert to LWC for radar (experimental)
292         // http://www-das.uwyo.edu/~geerts/cwx/notes/chap08/moist_cloud.html
293         double LWC = 0.0;
294         switch(family) {
295                 case SGNewCloud::CLFamilly_st:
296                         LWC = 0.29;
297                         break;
298                 case SGNewCloud::CLFamilly_cu:
299                         LWC = 0.27;
300                         break;
301                 case SGNewCloud::CLFamilly_cb:
302                         LWC = 2.0;
303                         break;
304                 case SGNewCloud::CLFamilly_sc:
305                         LWC = 0.44;
306                         break;
307                 case SGNewCloud::CLFamilly_ci:
308                         LWC = 0.03;
309                         break;
310                 // no data
311                 case SGNewCloud::CLFamilly_cs:
312                 case SGNewCloud::CLFamilly_cc:
313                 case SGNewCloud::CLFamilly_ac:
314                 case SGNewCloud::CLFamilly_as:
315                         LWC = 0.03;
316                         break;
317                 case SGNewCloud::CLFamilly_ns:
318                         LWC = 0.29*2.0;
319                         break;
320         }
321         // add to the list for the wxRadar instrument
322         if( LWC > 0.0 )
323                 radarEcho.push_back( SGWxRadarEcho ( heading, alt, radius, dist, LWC, false, cloudId ) );
324
325         // NB:data valid only from cockpit view
326
327         // spawn a new lightning
328         if(lightning_enable_state && min_time_before_lt <= 0.0 && (family == SGNewCloud::CLFamilly_cb) &&
329                 dist < 15000.0 * 15000.0 && sg_random() > 0.9f) {
330                 double lat, lon;
331                 Point3D orig, dest;
332                 orig.setlat(last_lat * SG_DEGREES_TO_RADIANS );
333                 orig.setlon(last_lon * SG_DEGREES_TO_RADIANS );
334                 orig.setelev(0.0);
335                 dist = sgSqrt(dist);
336                 dest = calc_gc_lon_lat(orig, heading, dist);
337                 lon = dest.lon() * SG_RADIANS_TO_DEGREES;
338                 lat = dest.lat() * SG_RADIANS_TO_DEGREES;
339                 addLightning( lon, lat, alt );
340
341                 // reset timer
342                 min_time_before_lt = 5.0 + sg_random() * 30;
343                 // DEBUG only
344 //              min_time_before_lt = 5.0;
345         }
346         if( (alt - radius * 0.1) > precipitation_max_alt )
347                 switch(family) {
348                         case SGNewCloud::CLFamilly_st:
349                         case SGNewCloud::CLFamilly_cu:
350                         case SGNewCloud::CLFamilly_cb:
351                         case SGNewCloud::CLFamilly_ns:
352                         case SGNewCloud::CLFamilly_sc:
353                                 precipitation_max_alt = alt - radius * 0.1;
354                                 break;
355                 }
356 }
357
358 list_of_SGWxRadarEcho *SGEnviro::get_radar_echo(void) {
359         return &radarEcho;
360 }
361
362 // precipitation rendering code
363 void SGEnviro::DrawCone2(float baseRadius, float height, int slices, bool down, double rain_norm, double speed) {
364         sgVec3 light;
365         sgVec3 min_light = {0.35, 0.35, 0.35};
366         sgAddVec3( light, fog_color, min_light );
367         float da = SG_PI * 2.0f / (float) slices;
368         // low number = faster
369         float speedf = 2.5f - speed / 200.0;
370         if( speedf < 1.0f )
371                 speedf = 1.0f;
372         float lenf = 0.03f + speed / 2000.0;
373         if( lenf > 0.10f )
374                 lenf = 0.10f;
375     float t = fmod((float) elapsed_time, speedf) / speedf;
376 //      t = 0.1f;
377         if( !down )
378                 t = 1.0f - t;
379         float angle = 0.0f;
380         glColor4f(1.0f, 0.7f, 0.7f, 0.9f);
381         glBegin(GL_LINES);
382         int rainpos_indice = 0;
383         for( int i = 0 ; i < slices ; i++ ) {
384                 float x = cos(angle) * baseRadius;
385                 float y = sin(angle) * baseRadius;
386                 angle += da;
387                 sgVec3 dir = {x, -height, y};
388
389                 // rain drops at 2 different speed to simulate depth
390                 float t1 = (i & 1 ? t : t + t) + rainpos[rainpos_indice];
391                 if(t1 > 1.0f)   t1 -= 1.0f;
392                 if(t1 > 1.0f)   t1 -= 1.0f;
393
394                 // distant raindrops are more transparent
395                 float c = (i & 1 ? t1 * 0.5f : t1 * 0.9f);
396                 glColor4f(c * light[0], c * light[1], c * light[2], c);
397                 sgVec3 p1, p2;
398                 sgScaleVec3(p1, dir, t1);
399                 // distant raindrops are shorter
400                 float t2 = t1 + (i & 1 ? lenf : lenf+lenf);
401                 sgScaleVec3(p2, dir, t2);
402
403                 glVertex3f(p1[0], p1[1] + height, p1[2]);
404                 glVertex3f(p2[0], p2[1] + height, p2[2]);
405                 if( ++rainpos_indice >= MAX_RAIN_SLICE )
406                         rainpos_indice = 0;
407         }
408         glEnd();
409 }
410
411 void SGEnviro::drawRain(double pitch, double roll, double heading, double hspeed, double rain_norm) {
412
413 #if 0
414         static int debug_period = 0;
415         if (debug_period++ == 50) {
416                 debug_period = 0;
417                 cout << "drawRain(" 
418                         << pitch << ", " 
419                         << roll  << ", " 
420                         << heading << ", " 
421                         << hspeed << ", " 
422                         << rain_norm << ");"
423                         //" angle = " << angle 
424                         //<< " raindrop(KTS) = " << raindrop_speed_kts
425                         << endl;
426         }
427 #endif
428
429
430         glBindTexture(GL_TEXTURE_2D, 0);
431
432         glDisable(GL_DEPTH_TEST);
433         glShadeModel(GL_SMOOTH);
434         glEnable(GL_BLEND);
435         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
436         glDisable( GL_FOG );
437         glDisable(GL_LIGHTING);
438
439         int slice_count = static_cast<int>(
440                                 (40.0 + rain_norm*150.0)* precipitation_density / 100.0);
441
442         // www.wonderquest.com/falling-raindrops.htm says that
443         // Raindrop terminal velocity is 5 to 20mph
444         // Rather than model it accurately (temp, pressure, diameter), and make it
445         // smaller than terminal when closer to the precipitation cloud base,
446         // we interpolate in the 5-20mph range according to rain_norm.
447         double raindrop_speed_kts 
448                 = (5.0 + rain_norm*15.0) * SG_MPH_TO_MPS * SG_MPS_TO_KT;
449
450         float angle = atanf(hspeed / raindrop_speed_kts) * SG_RADIANS_TO_DEGREES;
451         glPushMatrix();
452                 // the cone rotate with hspeed
453                 angle = -pitch - angle;
454                 glRotatef(roll, 0.0, 0.0, 1.0);
455                 glRotatef(heading, 0.0, 1.0, 0.0);
456                 glRotatef(angle, 1.0, 0.0, 0.0);
457
458                 // up cone
459                 DrawCone2(15.0, 30.0, slice_count, true, rain_norm, hspeed);
460                 // down cone (usually not visible)
461                 if(angle > 0.0 || heading != 0.0)
462                         DrawCone2(15.0, -30.0, slice_count, false, rain_norm, hspeed);
463
464         glPopMatrix();
465
466         glEnable(GL_LIGHTING);
467         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
468         glEnable( GL_FOG );
469         glEnable(GL_DEPTH_TEST);
470
471 }
472
473 void SGEnviro::set_soundMgr(SGSoundMgr *mgr) {
474         soundMgr = mgr;
475 }
476
477 void SGEnviro::drawPrecipitation(double rain_norm, double snow_norm, double hail_norm, double pitch, double roll, double heading, double hspeed) {
478         if( precipitation_enable_state && rain_norm > 0.0)
479           if( precipitation_max_alt >= last_alt )
480                 drawRain(pitch, roll, heading, hspeed, rain_norm);
481 }
482
483
484 SGLightning::SGLightning(double _lon, double _lat, double _alt) :
485         nb_tree(0),
486         lon(_lon),
487         lat(_lat),
488         alt(_alt),
489         age(1.0 + sg_random() * 4.0)
490 {
491 //      sequence_count = 1 + sg_random() * 5.0;
492         lt_build();
493 }
494
495 SGLightning::~SGLightning() {
496 }
497
498 // lightning rendering code
499 void SGLightning::lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize) {
500
501         sgVec3 dir, newdir;
502         int nseg = 0;
503         Point3D pt = start;
504         if( nbseg == 50 )
505                 sgSetVec3( dir, 0.0, -1.0, 0.0 );
506         else {
507                 sgSetVec3( dir, sg_random() - 0.5f, sg_random() - 0.5f, sg_random() - 0.5f);
508                 sgNormaliseVec3(dir);
509         }
510         if( nb_tree >= MAX_LT_TREE_SEG )
511                 return;
512
513         lt_tree[nb_tree].depth = tree_nr;
514         nseg = 0;
515         lt_tree[nb_tree].pt = pt;
516         lt_tree[nb_tree].prev = -1;
517         nb_tree ++;
518
519         // TODO:check agl
520         while(nseg < nbseg && pt.y() > 0.0) {
521         int prev = nb_tree - 1;
522         nseg++;
523                 // add a branch
524         if( energy * sg_random() > 0.8f )
525                         lt_build_tree_branch(tree_nr + 1, pt, energy * 0.9f, nbseg == 50 ? 10 : static_cast<int>(nbseg * 0.4f), segsize * 0.7f);
526
527                 if( nb_tree >= MAX_LT_TREE_SEG )
528                         return;
529                 sgSetVec3(newdir, (sg_random() - 0.5f), (sg_random() - 0.5f) - (nbseg == 50 ? 0.5f : 0.0), (sg_random() - 0.5f));
530                 sgNormaliseVec3(newdir);
531                 sgAddVec3( dir, newdir);
532                 sgNormaliseVec3(dir);
533                 sgVec3 scaleDir;
534                 sgScaleVec3( scaleDir, dir, segsize * energy * 0.5f );
535                 pt[PX] += scaleDir[0];
536                 pt[PY] += scaleDir[1];
537                 pt[PZ] += scaleDir[2];
538
539                 lt_tree[nb_tree].depth = tree_nr;
540                 lt_tree[nb_tree].pt = pt;
541                 lt_tree[nb_tree].prev = prev;
542                 nb_tree ++;
543         }
544 }
545
546 void SGLightning::lt_build(void) {
547     Point3D top;
548     nb_tree = 0;
549     top[PX] = 0 ;
550     top[PY] = alt;
551     top[PZ] = 0;
552     lt_build_tree_branch(0, top, 1.0, 50, top[PY] / 8.0);
553         if( ! sgEnviro.soundMgr )
554                 return;
555         Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
556         Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
557         double course = 0.0, dist = 0.0;
558         calc_gc_course_dist( dest, start, &course, &dist );
559         if( dist < 10000.0 && ! sgEnviro.snd_playing && (dist < sgEnviro.snd_dist || ! sgEnviro.snd_active) ) {
560                 sgEnviro.snd_timer = 0.0;
561                 sgEnviro.snd_wait  = dist / 340;
562                 sgEnviro.snd_dist  = dist;
563                 sgEnviro.snd_pos_lat = lat;
564                 sgEnviro.snd_pos_lon = lon;
565                 sgEnviro.snd_active = true;
566                 sgEnviro.snd_playing = false;
567         }
568 }
569
570
571 void SGLightning::lt_Render(void) {
572         float flash = 0.5;
573         if( fmod(sgEnviro.elapsed_time*100.0, 100.0) > 50.0 )
574                 flash = sg_random() * 0.75f + 0.25f;
575     float h = lt_tree[0].pt[PY];
576         sgVec4 col={0.62f, 0.83f, 1.0f, 1.0f};
577         sgVec4 c;
578
579 #define DRAW_SEG() \
580                         {glColorMaterial(GL_FRONT, GL_EMISSION);  \
581                         glDisable(GL_LINE_SMOOTH); glBegin(GL_LINES); \
582                                 glColor4fv(c); \
583                 glVertex3f(lt_tree[n].pt[PX], lt_tree[n].pt[PZ], lt_tree[n].pt[PY]); \
584                 glVertex3f(lt_tree[lt_tree[n].prev].pt[PX], lt_tree[lt_tree[n].prev].pt[PZ], lt_tree[lt_tree[n].prev].pt[PY]); \
585                         glEnd(); glEnable(GL_LINE_SMOOTH);}
586
587         glDepthMask( GL_FALSE );
588         glEnable(GL_BLEND);
589         glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
590         glBindTexture(GL_TEXTURE_2D, 0);
591
592         glDisable(GL_LIGHTING);
593         glDisable( GL_FOG );
594         glPushMatrix();
595         sgMat4 modelview, tmp;
596     ssgGetModelviewMatrix( modelview );
597         sgCopyMat4( tmp, sgEnviro.transform );
598     sgPostMultMat4( tmp, modelview );
599     ssgLoadModelviewMatrix( tmp );
600
601     Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
602     Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
603     double course = 0.0, dist = 0.0;
604     calc_gc_course_dist( dest, start, &course, &dist );
605     double ax = 0.0, ay = 0.0;
606     ax = cos(course) * dist;
607     ay = sin(course) * dist;
608
609         glTranslatef( ax, ay, -sgEnviro.last_alt );
610
611         sgEnviro.radarEcho.push_back( SGWxRadarEcho ( course, 0.0, 0.0, dist, age, true, 0 ) );
612
613         for( int n = 0 ; n < nb_tree ; n++ ) {
614         if( lt_tree[n].prev < 0 )
615                         continue;
616
617         float t1 = sgLerp(0.5, 1.0, lt_tree[n].pt[PY] / h);
618                 t1 *= flash;
619                 if( lt_tree[n].depth >= 2 ) {
620             glLineWidth(3);
621                         sgScaleVec4(c, col, t1 * 0.6f);
622                         DRAW_SEG();
623                 } else {
624                         if( lt_tree[n].depth == 0 ) {
625                 glLineWidth(12);
626                                 sgScaleVec4(c, col, t1 * 0.5f);
627                                 DRAW_SEG();
628
629                 glLineWidth(6);
630                                 sgScaleVec4(c, col, t1);
631                                 DRAW_SEG();
632                         } else {
633                 glLineWidth(6);
634                                 sgScaleVec4(c, col, t1 * 0.7f);
635                                 DRAW_SEG();
636                         }
637
638             if( lt_tree[n].depth == 0 ) 
639                 glLineWidth(3);
640                         else
641                 glLineWidth(2);
642
643             sgSetVec4(c, t1, t1, t1, t1);
644                         DRAW_SEG();
645                 }
646
647         }
648     glLineWidth(1);
649         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
650         glPopMatrix();
651         glDepthMask( GL_TRUE ); 
652         glEnable( GL_FOG );
653         glEnable(GL_LIGHTING);
654 }
655
656 void SGEnviro::addLightning(double lon, double lat, double alt) {
657         if( lightnings.size() > 10)
658                 return;
659         SGLightning *lt= new SGLightning(lon, lat, alt);
660         lightnings.push_back(lt);
661 }
662
663 void SGEnviro::drawLightning(void) {
664         list_of_lightning::iterator iLightning;
665         // play 'thunder' for lightning
666         if( snd_active )
667                 if( !snd_playing ) {
668                         // wait until sound has reached us
669                         snd_timer += dt;
670                         if( snd_timer >= snd_wait ) {
671                                 snd_playing = true;
672                                 // compute relative position of lightning
673                                 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
674                                 Point3D dest( snd_pos_lon*SG_DEGREES_TO_RADIANS, snd_pos_lat*SG_DEGREES_TO_RADIANS, 0.0 );
675                                 double course = 0.0, dist = 0.0;
676                                 calc_gc_course_dist( dest, start, &course, &dist );
677                                 double ax = 0.0, ay = 0.0;
678                                 ax = cos(course) * dist;
679                                 ay = sin(course) * dist;
680                                 SGSharedPtr<SGSoundSample> snd = soundMgr->find("thunder");
681                                 if( snd ) {
682                                         ALfloat pos[3]={ax, ay, -sgEnviro.last_alt };
683                                         snd->set_source_pos(pos);
684                                         snd->play_once();
685                                 }
686                         }
687                 } else {
688                         if( !soundMgr->is_playing("thunder") ) {
689                                 snd_active = false;
690                                 snd_playing = false;
691                         }
692                 }
693
694         if( ! lightning_enable_state )
695                 return;
696
697         for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
698                 if( dt )
699                         if( sg_random() > 0.95f )
700                                 (*iLightning)->lt_build();
701                 (*iLightning)->lt_Render();
702                 (*iLightning)->age -= dt;
703                 if( (*iLightning)->age < 0.0 ) {
704                         delete (*iLightning);
705                         lightnings.erase( iLightning );
706                         break;
707                 }
708         }
709
710 }
711
712
713 void SGEnviro::setFOV( float w, float h ) {
714         fov_width = w;
715         fov_height = h;
716 }
717
718 void SGEnviro::getFOV( float &w, float &h ) {
719         w = fov_width;
720         h = fov_height;
721 }