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