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