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