]> git.mxchange.org Git - simgear.git/blob - simgear/environment/visual_enviro.cxx
8855689adde72a7c6dcc3a65ec4c3392d80f48b3
[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_density(100.0)
103
104 {
105         for(int i = 0; i < MAX_RAIN_SLICE ; i++)
106                 rainpos[i] = sg_random();
107
108 }
109
110 SGEnviro::~SGEnviro(void) {
111         list_of_lightning::iterator iLightning;
112         for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
113                 delete (*iLightning);
114         }
115         lightnings.clear();
116 }
117
118 void SGEnviro::startOfFrame( sgVec3 p, sgVec3 up, double lon, double lat, double alt, double delta_time) {
119         view_in_cloud = false;
120         // ask the impostor cache to do some cleanup
121         if(SGNewCloud::cldCache)
122                 SGNewCloud::cldCache->startNewFrame();
123         last_cloud_turbulence = cloud_turbulence;
124         cloud_turbulence = 0.0;
125         elapsed_time += delta_time;
126         min_time_before_lt -= delta_time;
127         dt = delta_time;
128
129         sgMat4 T1, LON, LAT;
130     sgVec3 axis;
131
132     sgMakeTransMat4( T1, p );
133
134     sgSetVec3( axis, 0.0, 0.0, 1.0 );
135     sgMakeRotMat4( LON, lon, axis );
136
137     sgSetVec3( axis, 0.0, 1.0, 0.0 );
138     sgMakeRotMat4( LAT, 90.0 - lat, axis );
139
140     sgMat4 TRANSFORM;
141
142     sgCopyMat4( TRANSFORM, T1 );
143     sgPreMultMat4( TRANSFORM, LON );
144     sgPreMultMat4( TRANSFORM, LAT );
145
146     sgCoord pos;
147     sgSetCoord( &pos, TRANSFORM );
148
149         sgMakeCoordMat4( transform, &pos );
150     last_lon = lon;
151     last_lat = lat;
152         last_alt = alt;
153 }
154
155 void SGEnviro::endOfFrame(void) {
156 }
157
158 double SGEnviro::get_cloud_turbulence(void) const {
159         return last_cloud_turbulence;
160 }
161
162 // this can be queried to add some turbulence for example
163 bool SGEnviro::is_view_in_cloud(void) const {
164         return view_in_cloud;
165 }
166 void SGEnviro::set_view_in_cloud(bool incloud) {
167         view_in_cloud = incloud;
168 }
169
170 int SGEnviro::get_CacheResolution(void) const {
171         return SGCloudField::get_CacheResolution();
172 }
173
174 int SGEnviro::get_clouds_CacheSize(void) const {
175         return SGCloudField::get_CacheSize();
176 }
177 float SGEnviro::get_clouds_visibility(void) const {
178         return SGCloudField::get_CloudVis();
179 }
180 float SGEnviro::get_clouds_density(void) const {
181         return SGCloudField::get_density();
182 }
183 bool SGEnviro::get_clouds_enable_state(void) const {
184         return SGCloudField::get_enable3dClouds();
185 }
186
187 bool SGEnviro::get_turbulence_enable_state(void) const {
188         return turbulence_enable_state;
189 }
190
191 void SGEnviro::set_CacheResolution(int resolutionPixels) {
192         SGCloudField::set_CacheResolution(resolutionPixels);
193 }
194
195 void SGEnviro::set_clouds_CacheSize(int sizeKb) {
196         SGCloudField::set_CacheSize(sizeKb);
197 }
198 void SGEnviro::set_clouds_visibility(float distance) {
199         SGCloudField::set_CloudVis(distance);
200 }
201 void SGEnviro::set_clouds_density(float density) {
202         SGCloudField::set_density(density);
203 }
204 void SGEnviro::set_clouds_enable_state(bool enable) {
205         SGCloudField::set_enable3dClouds(enable);
206 }
207 void SGEnviro::set_turbulence_enable_state(bool enable) {
208         turbulence_enable_state = enable;
209 }
210 // rain/snow
211 float SGEnviro::get_precipitation_density(void) const {
212         return precipitation_density;
213 }
214 bool SGEnviro::get_precipitation_enable_state(void) const {
215         return precipitation_enable_state;
216 }
217
218 void SGEnviro::set_precipitation_density(float density) {
219         precipitation_density = density;
220 }
221 void SGEnviro::set_precipitation_enable_state(bool enable) {
222         precipitation_enable_state = enable;
223 }
224
225 // others
226 bool SGEnviro::get_lightning_enable_state(void) const {
227         return lightning_enable_state;
228 }
229
230 void SGEnviro::set_lightning_enable_state(bool enable) {
231         lightning_enable_state = enable;
232         if( ! enable ) {
233                 // TODO:cleanup
234         }
235 }
236
237 void SGEnviro::setLight(sgVec4 adj_fog_color) {
238         sgCopyVec4( fog_color, adj_fog_color );
239         if( false ) {
240         //    ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
241         }
242 }
243
244 void SGEnviro::callback_cloud(float heading, float alt, float radius, int familly, float dist) {
245         // send data to wx radar
246         // compute turbulence
247         // draw precipitation
248         // draw lightning
249         // compute illumination
250
251         // http://www.pilotfriend.com/flight_training/weather/THUNDERSTORM%20HAZARDS1.htm
252         double turbulence = 0.0;
253         if( dist < radius * radius * 2.25f ) {
254                 switch(familly) {
255                         case SGNewCloud::CLFamilly_st:
256                                 turbulence = 0.2;
257                                 break;
258                         case SGNewCloud::CLFamilly_ci:
259                         case SGNewCloud::CLFamilly_cs:
260                         case SGNewCloud::CLFamilly_cc:
261                         case SGNewCloud::CLFamilly_ac:
262                         case SGNewCloud::CLFamilly_as:
263                                 turbulence = 0.1;
264                                 break;
265                         case SGNewCloud::CLFamilly_sc:
266                                 turbulence = 0.3;
267                                 break;
268                         case SGNewCloud::CLFamilly_ns:
269                                 turbulence = 0.4;
270                                 break;
271                         case SGNewCloud::CLFamilly_cu:
272                                 turbulence = 0.5;
273                                 break;
274                         case SGNewCloud::CLFamilly_cb:
275                                 turbulence = 0.6;
276                                 break;
277                 }
278                 // full turbulence inside cloud, half in the vicinity
279                 if( dist > radius * radius )
280                         turbulence *= 0.5;
281                 if( turbulence > cloud_turbulence )
282                         cloud_turbulence = turbulence;
283                 // we can do 'local' precipitations too
284         }
285
286         // convert to LWC for radar (experimental)
287         // http://www-das.uwyo.edu/~geerts/cwx/notes/chap08/moist_cloud.html
288         double LWC = 0.0;
289         switch(familly) {
290                 case SGNewCloud::CLFamilly_st:
291                         LWC = 0.29;
292                         break;
293                 case SGNewCloud::CLFamilly_cu:
294                         LWC = 0.27;
295                         break;
296                 case SGNewCloud::CLFamilly_cb:
297                         LWC = 2.0;
298                         break;
299                 case SGNewCloud::CLFamilly_sc:
300                         LWC = 0.44;
301                         break;
302                 case SGNewCloud::CLFamilly_ci:
303                         LWC = 0.03;
304                         break;
305                 // no data
306                 case SGNewCloud::CLFamilly_cs:
307                 case SGNewCloud::CLFamilly_cc:
308                 case SGNewCloud::CLFamilly_ac:
309                 case SGNewCloud::CLFamilly_as:
310                         LWC = 0.03;
311                         break;
312                 case SGNewCloud::CLFamilly_ns:
313                         LWC = 0.29*2.0;
314                         break;
315         }
316         // TODO:send data to radar antenna
317         // NB:data valid only from cockpit view
318
319         // spawn a new lightning
320         if(min_time_before_lt <= 0.0 && (familly == SGNewCloud::CLFamilly_cb) &&
321                 dist < 15000.0 * 15000.0 && sg_random() > 0.9f) {
322                 double lat, lon;
323                 Point3D orig, dest;
324                 orig.setlat(last_lat * SG_DEGREES_TO_RADIANS );
325                 orig.setlon(last_lon * SG_DEGREES_TO_RADIANS );
326                 orig.setelev(0.0);
327                 dist = sgSqrt(dist);
328                 dest = calc_gc_lon_lat(orig, heading, dist);
329                 lon = dest.lon() * SG_RADIANS_TO_DEGREES;
330                 lat = dest.lat() * SG_RADIANS_TO_DEGREES;
331                 addLightning( lon, lat, alt );
332
333                 // reset timer
334                 min_time_before_lt = 5.0 + sg_random() * 30;
335                 // DEBUG only
336 //              min_time_before_lt = 1.0;
337         }
338 }
339
340 // precipitation rendering code
341 void SGEnviro::DrawCone2(float baseRadius, float height, int slices, bool down, double rain_norm, double speed) {
342
343         sgVec3 light;
344         sgVec3 min_light = {0.35, 0.35, 0.35};
345         sgAddVec3( light, fog_color, min_light );
346         float da = SG_PI * 2.0f / (float) slices;
347         // low number = faster
348         float speedf = 2.5f - speed / 200.0;
349         if( speedf < 1.0f )
350                 speedf = 1.0f;
351         float lenf = 0.03f + speed / 2000.0;
352         if( lenf > 0.10f )
353                 lenf = 0.10f;
354     float t = fmod((float) elapsed_time, speedf) / speedf;
355 //      t = 0.1f;
356         if( !down )
357                 t = 1.0f - t;
358         float angle = 0.0f;
359         glColor4f(1.0f, 0.7f, 0.7f, 0.9f);
360         glBegin(GL_LINES);
361         int rainpos_indice = 0;
362         for( int i = 0 ; i < slices ; i++ ) {
363                 float x = cos(angle) * baseRadius;
364                 float y = sin(angle) * baseRadius;
365                 angle += da;
366                 sgVec3 dir = {x, -height, y};
367
368                 // rain drops at 2 different speed to simulate depth
369                 float t1 = (i & 1 ? t : t + t) + rainpos[rainpos_indice];
370                 if(t1 > 1.0f)   t1 -= 1.0f;
371                 if(t1 > 1.0f)   t1 -= 1.0f;
372
373                 // distant raindrops are more transparent
374                 float c = (i & 1 ? t1 * 0.5f : t1 * 0.9f);
375                 glColor4f(c * light[0], c * light[1], c * light[2], c);
376                 sgVec3 p1, p2;
377                 sgScaleVec3(p1, dir, t1);
378                 // distant raindrops are shorter
379                 float t2 = t1 + (i & 1 ? lenf : lenf+lenf);
380                 sgScaleVec3(p2, dir, t2);
381
382                 glVertex3f(p1[0], p1[1] + height, p1[2]);
383                 glVertex3f(p2[0], p2[1] + height, p2[2]);
384                 if( ++rainpos_indice >= MAX_RAIN_SLICE )
385                         rainpos_indice = 0;
386         }
387         glEnd();
388 }
389
390 // TODO:check alt vs layer
391 void SGEnviro::drawRain(double pitch, double roll, double heading, double speed, double rain_norm) {
392
393         glBindTexture(GL_TEXTURE_2D, 0);
394
395         glDisable(GL_DEPTH_TEST);
396         glShadeModel(GL_SMOOTH);
397         glEnable(GL_BLEND);
398         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
399         glDisable( GL_FOG );
400         glDisable(GL_LIGHTING);
401
402         int slice_count = (40.0 + rain_norm*150.0)* precipitation_density / 100.0;
403
404         float angle = speed;
405         if( angle > 90.0 )
406                 angle = 90.0;
407
408         glPushMatrix();
409                 // TODO:find the real view orientation, not the AC one
410                 // the cone rotate with speed
411                 angle = -pitch - angle;
412                 glRotatef(angle, 1.0, 0.0, 0.0);
413                 glRotatef(roll, 0.0, 1.0, 0.0);
414                 glRotatef(heading, 0.0, 0.0, 1.0);
415
416                 // up cone
417                 DrawCone2(15.0, 30.0, slice_count, true, rain_norm, speed);
418                 // down cone (usually not visible)
419                 if(angle > 0.0 || heading != 0.0)
420                         DrawCone2(15.0, -30.0, slice_count, false, rain_norm, speed);
421
422         glPopMatrix();
423
424         glEnable(GL_LIGHTING);
425         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
426         glEnable( GL_FOG );
427         glEnable(GL_DEPTH_TEST);
428
429 }
430
431 void SGEnviro::set_soundMgr(SGSoundMgr *mgr) {
432         soundMgr = mgr;
433 }
434
435 void SGEnviro::drawPrecipitation(double rain_norm, double snow_norm, double hail_norm, double pitch, double roll, double heading, double speed) {
436         // TODO:check alt with right layer (wich layer ?)
437         if( precipitation_enable_state && rain_norm > 0.0)
438                 drawRain(pitch, roll, heading, speed, rain_norm);
439 }
440
441
442 SGLightning::SGLightning(double _lon, double _lat, double _alt) :
443         lon(_lon),
444         lat(_lat),
445         alt(_alt),
446         age(1.0 + sg_random() * 4.0),
447         nb_tree(0)
448 {
449 //      sequence_count = 1 + sg_random() * 5.0;
450         lt_build();
451 }
452
453 SGLightning::~SGLightning() {
454 }
455
456 // lightning rendering code
457 void SGLightning::lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize) {
458
459         sgVec3 dir, newdir;
460         int nseg = 0;
461         Point3D pt = start;
462         if( nbseg == 50 )
463                 sgSetVec3( dir, 0.0, -1.0, 0.0 );
464         else {
465                 sgSetVec3( dir, sg_random() - 0.5f, sg_random() - 0.5f, sg_random() - 0.5f);
466                 sgNormaliseVec3(dir);
467         }
468         if( nb_tree >= MAX_LT_TREE_SEG )
469                 return;
470
471         lt_tree[nb_tree].depth = tree_nr;
472         nseg = 0;
473         lt_tree[nb_tree].pt = pt;
474         lt_tree[nb_tree].prev = -1;
475         nb_tree ++;
476
477         // TODO:check agl
478         while(nseg < nbseg && pt.y() > 0.0) {
479         int prev = nb_tree - 1;
480         nseg++;
481                 // add a branch
482         if( energy * sg_random() > 0.8f )
483                         lt_build_tree_branch(tree_nr + 1, pt, energy * 0.9f, nbseg == 50 ? 10 : nbseg * 0.4f, segsize * 0.7f);
484
485                 if( nb_tree >= MAX_LT_TREE_SEG )
486                         return;
487                 sgSetVec3(newdir, (sg_random() - 0.5f), (sg_random() - 0.5f) - (nbseg == 50 ? 0.5f : 0.0), (sg_random() - 0.5f));
488                 sgNormaliseVec3(newdir);
489                 sgAddVec3( dir, newdir);
490                 sgNormaliseVec3(dir);
491                 sgVec3 scaleDir;
492                 sgScaleVec3( scaleDir, dir, segsize * energy * 0.5f );
493                 pt[PX] += scaleDir[0];
494                 pt[PY] += scaleDir[1];
495                 pt[PZ] += scaleDir[2];
496
497                 lt_tree[nb_tree].depth = tree_nr;
498                 lt_tree[nb_tree].pt = pt;
499                 lt_tree[nb_tree].prev = prev;
500                 nb_tree ++;
501         }
502 }
503
504 void SGLightning::lt_build(void) {
505     Point3D top;
506     nb_tree = 0;
507     top[PX] = 0 ;
508     top[PY] = alt;
509     top[PZ] = 0;
510     lt_build_tree_branch(0, top, 1.0, 50, top[PY] / 8.0);
511         if( ! sgEnviro.soundMgr )
512                 return;
513         Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
514         Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
515         double course = 0.0, dist = 0.0;
516         calc_gc_course_dist( dest, start, &course, &dist );
517         if( dist < 10000.0 && ! sgEnviro.snd_playing && (dist < sgEnviro.snd_dist || ! sgEnviro.snd_active) ) {
518                 sgEnviro.snd_timer = 0.0;
519                 sgEnviro.snd_wait  = dist / 340;
520                 sgEnviro.snd_dist  = dist;
521                 sgEnviro.snd_pos_lat = lat;
522                 sgEnviro.snd_pos_lon = lon;
523                 sgEnviro.snd_active = true;
524                 sgEnviro.snd_playing = false;
525         }
526 }
527
528
529 void SGLightning::lt_Render(void) {
530         float flash = 0.5;
531         if( fmod(sgEnviro.elapsed_time*100.0, 100.0) > 50.0 )
532                 flash = sg_random() * 0.75f + 0.25f;
533     float h = lt_tree[0].pt[PY];
534         sgVec4 col={0.62f, 0.83f, 1.0f, 1.0f};
535         sgVec4 c;
536
537 #define DRAW_SEG() \
538                         {glBegin(GL_LINES); \
539                                 glColor4fv(c); \
540                 glVertex3f(lt_tree[n].pt[PX], lt_tree[n].pt[PZ], lt_tree[n].pt[PY]); \
541                 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]); \
542                         glEnd();}
543
544         glDepthMask( GL_FALSE );
545         glEnable(GL_BLEND);
546         glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
547         glBindTexture(GL_TEXTURE_2D, 0);
548
549         glDisable(GL_LIGHTING);
550         glDisable( GL_FOG );
551         glPushMatrix();
552         sgMat4 modelview, tmp;
553     ssgGetModelviewMatrix( modelview );
554         sgCopyMat4( tmp, sgEnviro.transform );
555     sgPostMultMat4( tmp, modelview );
556     ssgLoadModelviewMatrix( tmp );
557
558     Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
559     Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
560     double course = 0.0, dist = 0.0;
561     calc_gc_course_dist( dest, start, &course, &dist );
562     double ax = 0.0, ay = 0.0;
563     ax = cos(course) * dist;
564     ay = sin(course) * dist;
565
566         glTranslatef( ax, ay, -sgEnviro.last_alt );
567 //      glTranslatef( ax, ay, 0 );
568
569         for( int n = 0 ; n < nb_tree ; n++ ) {
570         if( lt_tree[n].prev < 0 )
571                         continue;
572
573         float t1 = sgLerp(0.5, 1.0, lt_tree[n].pt[PY] / h);
574                 t1 *= flash;
575                 if( lt_tree[n].depth >= 2 ) {
576             glLineWidth(3);
577                         sgScaleVec4(c, col, t1 * 0.6f);
578                         DRAW_SEG();
579                 } else {
580                         if( lt_tree[n].depth == 0 ) {
581                 glLineWidth(12);
582                                 sgScaleVec4(c, col, t1 * 0.5f);
583                                 DRAW_SEG();
584
585                 glLineWidth(6);
586                                 sgScaleVec4(c, col, t1);
587                                 DRAW_SEG();
588                         } else {
589                 glLineWidth(6);
590                                 sgScaleVec4(c, col, t1 * 0.7f);
591                                 DRAW_SEG();
592                         }
593
594             if( lt_tree[n].depth == 0 ) 
595                 glLineWidth(3);
596                         else
597                 glLineWidth(2);
598
599             sgSetVec4(c, t1, t1, t1, t1);
600                         DRAW_SEG();
601                 }
602
603         }
604     glLineWidth(1);
605         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
606         glPopMatrix();
607         glDepthMask( GL_TRUE ); 
608         glEnable( GL_FOG );
609         glEnable(GL_LIGHTING);
610 }
611
612 void SGEnviro::addLightning(double lon, double lat, double alt) {
613         if( lightnings.size() > 10)
614                 return;
615         SGLightning *lt= new SGLightning(lon, lat, alt);
616         lightnings.push_back(lt);
617 }
618
619 void SGEnviro::drawLightning(void) {
620         list_of_lightning::iterator iLightning;
621         // play 'thunder' for lightning
622         if( snd_active )
623                 if( !snd_playing ) {
624                         // wait until sound has reached us
625                         snd_timer += dt;
626                         if( snd_timer >= snd_wait ) {
627                                 snd_playing = true;
628                                 // compute relative position of lightning
629                                 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
630                                 Point3D dest( snd_pos_lon*SG_DEGREES_TO_RADIANS, snd_pos_lat*SG_DEGREES_TO_RADIANS, 0.0 );
631                                 double course = 0.0, dist = 0.0;
632                                 calc_gc_course_dist( dest, start, &course, &dist );
633                                 double ax = 0.0, ay = 0.0;
634                                 ax = cos(course) * dist;
635                                 ay = sin(course) * dist;
636                                 SGSoundSample *snd = soundMgr->find("thunder");
637                                 if( snd ) {
638                                         ALfloat pos[3]={ax, ay, -sgEnviro.last_alt };
639                                         snd->set_source_pos(pos);
640                                         snd->play_once();
641                                 }
642                         }
643                 } else {
644                         if( !soundMgr->is_playing("thunder") ) {
645                                 snd_active = false;
646                                 snd_playing = false;
647                         }
648                 }
649
650         if( ! lightning_enable_state )
651                 return;
652
653         for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
654                 if( dt )
655                         if( sg_random() > 0.95f )
656                                 (*iLightning)->lt_build();
657                 (*iLightning)->lt_Render();
658                 (*iLightning)->age -= dt;
659                 if( (*iLightning)->age < 0.0 ) {
660                         delete (*iLightning);
661                         lightnings.erase( iLightning );
662                         break;
663                 }
664         }
665
666 }
667
668
669 void SGEnviro::setFOV( float w, float h ) {
670         fov_width = w;
671         fov_height = h;
672 }
673
674 void SGEnviro::getFOV( float &w, float &h ) {
675         w = fov_width;
676         h = fov_height;
677 }