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