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