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