]> git.mxchange.org Git - simgear.git/blob - simgear/environment/visual_enviro.cxx
32140a561e661dc4ad06ad48db7e58689f91165d
[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(void) :
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 familly, 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(familly) {
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(familly) {
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 && (familly == 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(familly) {
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
365         sgVec3 light;
366         sgVec3 min_light = {0.35, 0.35, 0.35};
367         sgAddVec3( light, fog_color, min_light );
368         float da = SG_PI * 2.0f / (float) slices;
369         // low number = faster
370         float speedf = 2.5f - speed / 200.0;
371         if( speedf < 1.0f )
372                 speedf = 1.0f;
373         float lenf = 0.03f + speed / 2000.0;
374         if( lenf > 0.10f )
375                 lenf = 0.10f;
376     float t = fmod((float) elapsed_time, speedf) / speedf;
377 //      t = 0.1f;
378         if( !down )
379                 t = 1.0f - t;
380         float angle = 0.0f;
381         glColor4f(1.0f, 0.7f, 0.7f, 0.9f);
382         glBegin(GL_LINES);
383         int rainpos_indice = 0;
384         for( int i = 0 ; i < slices ; i++ ) {
385                 float x = cos(angle) * baseRadius;
386                 float y = sin(angle) * baseRadius;
387                 angle += da;
388                 sgVec3 dir = {x, -height, y};
389
390                 // rain drops at 2 different speed to simulate depth
391                 float t1 = (i & 1 ? t : t + t) + rainpos[rainpos_indice];
392                 if(t1 > 1.0f)   t1 -= 1.0f;
393                 if(t1 > 1.0f)   t1 -= 1.0f;
394
395                 // distant raindrops are more transparent
396                 float c = (i & 1 ? t1 * 0.5f : t1 * 0.9f);
397                 glColor4f(c * light[0], c * light[1], c * light[2], c);
398                 sgVec3 p1, p2;
399                 sgScaleVec3(p1, dir, t1);
400                 // distant raindrops are shorter
401                 float t2 = t1 + (i & 1 ? lenf : lenf+lenf);
402                 sgScaleVec3(p2, dir, t2);
403
404                 glVertex3f(p1[0], p1[1] + height, p1[2]);
405                 glVertex3f(p2[0], p2[1] + height, p2[2]);
406                 if( ++rainpos_indice >= MAX_RAIN_SLICE )
407                         rainpos_indice = 0;
408         }
409         glEnd();
410 }
411
412 void SGEnviro::drawRain(double pitch, double roll, double heading, double speed, double rain_norm) {
413
414         glBindTexture(GL_TEXTURE_2D, 0);
415
416         glDisable(GL_DEPTH_TEST);
417         glShadeModel(GL_SMOOTH);
418         glEnable(GL_BLEND);
419         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
420         glDisable( GL_FOG );
421         glDisable(GL_LIGHTING);
422
423         int slice_count = static_cast<int>(
424                                 (40.0 + rain_norm*150.0)* precipitation_density / 100.0);
425
426         // www.wonderquest.com/falling-raindrops.htm says that
427         // Raindrop terminal velocity is 5 to 20mph
428         // Rather than model it accurately (temp, pressure, diameter), and make it
429         // smaller than terminal when closer to the precipitation cloud base,
430         // we interpolate in the 5-20mph range according to rain_norm.
431         double raindrop_speed_kts 
432                 = (5.0 + rain_norm*15.0) * SG_MPH_TO_MPS * SG_MPS_TO_KT;
433
434         float angle = atanf(speed / raindrop_speed_kts) * SG_RADIANS_TO_DEGREES;
435         // We assume that the speed is HORIZONTAL airspeed here!!! XXX
436         // DOCUMENT THE CHANGE IN THE PARAMETER speed MEANING BY RENAMING IT!!!
437         glPushMatrix();
438                 // the cone rotate with speed
439                 angle = -pitch - angle;
440                 glRotatef(heading, 0.0, 1.0, 0.0);
441                 glRotatef(roll, 0.0, 0.0, 1.0);
442                 glRotatef(angle, 1.0, 0.0, 0.0);
443
444                 // up cone
445                 DrawCone2(15.0, 30.0, slice_count, true, rain_norm, speed);
446                 // down cone (usually not visible)
447                 if(angle > 0.0 || heading != 0.0)
448                         DrawCone2(15.0, -30.0, slice_count, false, rain_norm, speed);
449
450         glPopMatrix();
451
452         glEnable(GL_LIGHTING);
453         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
454         glEnable( GL_FOG );
455         glEnable(GL_DEPTH_TEST);
456
457 }
458
459 void SGEnviro::set_soundMgr(SGSoundMgr *mgr) {
460         soundMgr = mgr;
461 }
462
463 void SGEnviro::drawPrecipitation(double rain_norm, double snow_norm, double hail_norm, double pitch, double roll, double heading, double speed) {
464         if( precipitation_enable_state && rain_norm > 0.0)
465           if( precipitation_max_alt >= last_alt )
466                 drawRain(pitch, roll, heading, speed, rain_norm);
467 }
468
469
470 SGLightning::SGLightning(double _lon, double _lat, double _alt) :
471         nb_tree(0),
472         lon(_lon),
473         lat(_lat),
474         alt(_alt),
475         age(1.0 + sg_random() * 4.0)
476 {
477 //      sequence_count = 1 + sg_random() * 5.0;
478         lt_build();
479 }
480
481 SGLightning::~SGLightning() {
482 }
483
484 // lightning rendering code
485 void SGLightning::lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize) {
486
487         sgVec3 dir, newdir;
488         int nseg = 0;
489         Point3D pt = start;
490         if( nbseg == 50 )
491                 sgSetVec3( dir, 0.0, -1.0, 0.0 );
492         else {
493                 sgSetVec3( dir, sg_random() - 0.5f, sg_random() - 0.5f, sg_random() - 0.5f);
494                 sgNormaliseVec3(dir);
495         }
496         if( nb_tree >= MAX_LT_TREE_SEG )
497                 return;
498
499         lt_tree[nb_tree].depth = tree_nr;
500         nseg = 0;
501         lt_tree[nb_tree].pt = pt;
502         lt_tree[nb_tree].prev = -1;
503         nb_tree ++;
504
505         // TODO:check agl
506         while(nseg < nbseg && pt.y() > 0.0) {
507         int prev = nb_tree - 1;
508         nseg++;
509                 // add a branch
510         if( energy * sg_random() > 0.8f )
511                         lt_build_tree_branch(tree_nr + 1, pt, energy * 0.9f, nbseg == 50 ? 10 : static_cast<int>(nbseg * 0.4f), segsize * 0.7f);
512
513                 if( nb_tree >= MAX_LT_TREE_SEG )
514                         return;
515                 sgSetVec3(newdir, (sg_random() - 0.5f), (sg_random() - 0.5f) - (nbseg == 50 ? 0.5f : 0.0), (sg_random() - 0.5f));
516                 sgNormaliseVec3(newdir);
517                 sgAddVec3( dir, newdir);
518                 sgNormaliseVec3(dir);
519                 sgVec3 scaleDir;
520                 sgScaleVec3( scaleDir, dir, segsize * energy * 0.5f );
521                 pt[PX] += scaleDir[0];
522                 pt[PY] += scaleDir[1];
523                 pt[PZ] += scaleDir[2];
524
525                 lt_tree[nb_tree].depth = tree_nr;
526                 lt_tree[nb_tree].pt = pt;
527                 lt_tree[nb_tree].prev = prev;
528                 nb_tree ++;
529         }
530 }
531
532 void SGLightning::lt_build(void) {
533     Point3D top;
534     nb_tree = 0;
535     top[PX] = 0 ;
536     top[PY] = alt;
537     top[PZ] = 0;
538     lt_build_tree_branch(0, top, 1.0, 50, top[PY] / 8.0);
539         if( ! sgEnviro.soundMgr )
540                 return;
541         Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
542         Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
543         double course = 0.0, dist = 0.0;
544         calc_gc_course_dist( dest, start, &course, &dist );
545         if( dist < 10000.0 && ! sgEnviro.snd_playing && (dist < sgEnviro.snd_dist || ! sgEnviro.snd_active) ) {
546                 sgEnviro.snd_timer = 0.0;
547                 sgEnviro.snd_wait  = dist / 340;
548                 sgEnviro.snd_dist  = dist;
549                 sgEnviro.snd_pos_lat = lat;
550                 sgEnviro.snd_pos_lon = lon;
551                 sgEnviro.snd_active = true;
552                 sgEnviro.snd_playing = false;
553         }
554 }
555
556
557 void SGLightning::lt_Render(void) {
558         float flash = 0.5;
559         if( fmod(sgEnviro.elapsed_time*100.0, 100.0) > 50.0 )
560                 flash = sg_random() * 0.75f + 0.25f;
561     float h = lt_tree[0].pt[PY];
562         sgVec4 col={0.62f, 0.83f, 1.0f, 1.0f};
563         sgVec4 c;
564
565 #define DRAW_SEG() \
566                         {glColorMaterial(GL_FRONT, GL_EMISSION);  \
567                         glDisable(GL_LINE_SMOOTH); glBegin(GL_LINES); \
568                                 glColor4fv(c); \
569                 glVertex3f(lt_tree[n].pt[PX], lt_tree[n].pt[PZ], lt_tree[n].pt[PY]); \
570                 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]); \
571                         glEnd(); glEnable(GL_LINE_SMOOTH);}
572
573         glDepthMask( GL_FALSE );
574         glEnable(GL_BLEND);
575         glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
576         glBindTexture(GL_TEXTURE_2D, 0);
577
578         glDisable(GL_LIGHTING);
579         glDisable( GL_FOG );
580         glPushMatrix();
581         sgMat4 modelview, tmp;
582     ssgGetModelviewMatrix( modelview );
583         sgCopyMat4( tmp, sgEnviro.transform );
584     sgPostMultMat4( tmp, modelview );
585     ssgLoadModelviewMatrix( tmp );
586
587     Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
588     Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
589     double course = 0.0, dist = 0.0;
590     calc_gc_course_dist( dest, start, &course, &dist );
591     double ax = 0.0, ay = 0.0;
592     ax = cos(course) * dist;
593     ay = sin(course) * dist;
594
595         glTranslatef( ax, ay, -sgEnviro.last_alt );
596
597         sgEnviro.radarEcho.push_back( SGWxRadarEcho ( course, 0.0, 0.0, dist, age, true, 0 ) );
598
599         for( int n = 0 ; n < nb_tree ; n++ ) {
600         if( lt_tree[n].prev < 0 )
601                         continue;
602
603         float t1 = sgLerp(0.5, 1.0, lt_tree[n].pt[PY] / h);
604                 t1 *= flash;
605                 if( lt_tree[n].depth >= 2 ) {
606             glLineWidth(3);
607                         sgScaleVec4(c, col, t1 * 0.6f);
608                         DRAW_SEG();
609                 } else {
610                         if( lt_tree[n].depth == 0 ) {
611                 glLineWidth(12);
612                                 sgScaleVec4(c, col, t1 * 0.5f);
613                                 DRAW_SEG();
614
615                 glLineWidth(6);
616                                 sgScaleVec4(c, col, t1);
617                                 DRAW_SEG();
618                         } else {
619                 glLineWidth(6);
620                                 sgScaleVec4(c, col, t1 * 0.7f);
621                                 DRAW_SEG();
622                         }
623
624             if( lt_tree[n].depth == 0 ) 
625                 glLineWidth(3);
626                         else
627                 glLineWidth(2);
628
629             sgSetVec4(c, t1, t1, t1, t1);
630                         DRAW_SEG();
631                 }
632
633         }
634     glLineWidth(1);
635         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
636         glPopMatrix();
637         glDepthMask( GL_TRUE ); 
638         glEnable( GL_FOG );
639         glEnable(GL_LIGHTING);
640 }
641
642 void SGEnviro::addLightning(double lon, double lat, double alt) {
643         if( lightnings.size() > 10)
644                 return;
645         SGLightning *lt= new SGLightning(lon, lat, alt);
646         lightnings.push_back(lt);
647 }
648
649 void SGEnviro::drawLightning(void) {
650         list_of_lightning::iterator iLightning;
651         // play 'thunder' for lightning
652         if( snd_active )
653                 if( !snd_playing ) {
654                         // wait until sound has reached us
655                         snd_timer += dt;
656                         if( snd_timer >= snd_wait ) {
657                                 snd_playing = true;
658                                 // compute relative position of lightning
659                                 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
660                                 Point3D dest( snd_pos_lon*SG_DEGREES_TO_RADIANS, snd_pos_lat*SG_DEGREES_TO_RADIANS, 0.0 );
661                                 double course = 0.0, dist = 0.0;
662                                 calc_gc_course_dist( dest, start, &course, &dist );
663                                 double ax = 0.0, ay = 0.0;
664                                 ax = cos(course) * dist;
665                                 ay = sin(course) * dist;
666                                 SGSharedPtr<SGSoundSample> snd = soundMgr->find("thunder");
667                                 if( snd ) {
668                                         ALfloat pos[3]={ax, ay, -sgEnviro.last_alt };
669                                         snd->set_source_pos(pos);
670                                         snd->play_once();
671                                 }
672                         }
673                 } else {
674                         if( !soundMgr->is_playing("thunder") ) {
675                                 snd_active = false;
676                                 snd_playing = false;
677                         }
678                 }
679
680         if( ! lightning_enable_state )
681                 return;
682
683         for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
684                 if( dt )
685                         if( sg_random() > 0.95f )
686                                 (*iLightning)->lt_build();
687                 (*iLightning)->lt_Render();
688                 (*iLightning)->age -= dt;
689                 if( (*iLightning)->age < 0.0 ) {
690                         delete (*iLightning);
691                         lightnings.erase( iLightning );
692                         break;
693                 }
694         }
695
696 }
697
698
699 void SGEnviro::setFOV( float w, float h ) {
700         fov_width = w;
701         fov_height = h;
702 }
703
704 void SGEnviro::getFOV( float &w, float &h ) {
705         w = fov_width;
706         h = fov_height;
707 }