1 // Visual environment helper class
3 // Written by Harald JOHNSEN, started April 2005.
5 // Copyright (C) 2005 Harald JOHNSEN - hjohnsen@evc.net
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.
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.
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.
23 # include <simgear_config.h>
27 #include <simgear/constants.h>
28 #include <simgear/structure/SGReferenced.hxx>
29 #include <simgear/structure/SGSharedPtr.hxx>
30 #include <simgear/math/sg_random.h>
31 #include <simgear/math/sg_geodesy.hxx>
32 #include <simgear/math/point3d.hxx>
33 #include <simgear/math/polar3d.hxx>
34 #include <simgear/sound/soundmgr_openal.hxx>
35 #include <simgear/scene/sky/cloudfield.hxx>
36 #include <simgear/scene/sky/newcloud.hxx>
37 #include "visual_enviro.hxx"
50 #define MAX_RAIN_SLICE 200
51 static float rainpos[MAX_RAIN_SLICE];
52 #define MAX_LT_TREE_SEG 400
55 * A class to render lightnings.
60 * Build a new lightning.
61 * The lightning has a limited life time. It will also play a thunder sounder once.
62 * @param lon lon longitude in degree
63 * @param lat lat latitude in degree
64 * @param alt asl of top of lightning
66 SGLightning(double lon, double lat, double alt);
70 void lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize);
72 // contains all the segments of the lightning
73 lt_tree_seg lt_tree[MAX_LT_TREE_SEG];
76 // position of lightning
83 typedef vector<SGLightning *> list_of_lightning;
84 static list_of_lightning lightnings;
88 SGEnviro::SGEnviro(void) :
90 precipitation_enable_state(true),
91 precipitation_density(100.0),
92 precipitation_max_alt(0.0),
93 turbulence_enable_state(false),
94 last_cloud_turbulence(0.0),
95 cloud_turbulence(0.0),
96 lightning_enable_state(false),
102 min_time_before_lt(0.0),
107 for(int i = 0; i < MAX_RAIN_SLICE ; i++)
108 rainpos[i] = sg_random();
109 radarEcho.reserve(100);
112 SGEnviro::~SGEnviro(void) {
113 list_of_lightning::iterator iLightning;
114 for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
115 delete (*iLightning);
120 void SGEnviro::startOfFrame( sgVec3 p, sgVec3 up, double lon, double lat, double alt, double delta_time) {
121 view_in_cloud = false;
122 // ask the impostor cache to do some cleanup
123 if(SGNewCloud::cldCache)
124 SGNewCloud::cldCache->startNewFrame();
125 last_cloud_turbulence = cloud_turbulence;
126 cloud_turbulence = 0.0;
127 elapsed_time += delta_time;
128 min_time_before_lt -= delta_time;
134 sgMakeTransMat4( T1, p );
136 sgSetVec3( axis, 0.0, 0.0, 1.0 );
137 sgMakeRotMat4( LON, lon, axis );
139 sgSetVec3( axis, 0.0, 1.0, 0.0 );
140 sgMakeRotMat4( LAT, 90.0 - lat, axis );
144 sgCopyMat4( TRANSFORM, T1 );
145 sgPreMultMat4( TRANSFORM, LON );
146 sgPreMultMat4( TRANSFORM, LAT );
149 sgSetCoord( &pos, TRANSFORM );
151 sgMakeCoordMat4( transform, &pos );
157 precipitation_max_alt = 400.0;
160 void SGEnviro::endOfFrame(void) {
163 double SGEnviro::get_cloud_turbulence(void) const {
164 return last_cloud_turbulence;
167 // this can be queried to add some turbulence for example
168 bool SGEnviro::is_view_in_cloud(void) const {
169 return view_in_cloud;
171 void SGEnviro::set_view_in_cloud(bool incloud) {
172 view_in_cloud = incloud;
175 int SGEnviro::get_CacheResolution(void) const {
176 return SGCloudField::get_CacheResolution();
179 int SGEnviro::get_clouds_CacheSize(void) const {
180 return SGCloudField::get_CacheSize();
182 float SGEnviro::get_clouds_visibility(void) const {
183 return SGCloudField::get_CloudVis();
185 float SGEnviro::get_clouds_density(void) const {
186 return SGCloudField::get_density();
188 bool SGEnviro::get_clouds_enable_state(void) const {
189 return SGCloudField::get_enable3dClouds();
192 bool SGEnviro::get_turbulence_enable_state(void) const {
193 return turbulence_enable_state;
196 void SGEnviro::set_CacheResolution(int resolutionPixels) {
197 SGCloudField::set_CacheResolution(resolutionPixels);
200 void SGEnviro::set_clouds_CacheSize(int sizeKb) {
201 SGCloudField::set_CacheSize(sizeKb);
203 void SGEnviro::set_clouds_visibility(float distance) {
204 SGCloudField::set_CloudVis(distance);
206 void SGEnviro::set_clouds_density(float density) {
207 SGCloudField::set_density(density);
209 void SGEnviro::set_clouds_enable_state(bool enable) {
210 SGCloudField::set_enable3dClouds(enable);
212 void SGEnviro::set_turbulence_enable_state(bool enable) {
213 turbulence_enable_state = enable;
216 float SGEnviro::get_precipitation_density(void) const {
217 return precipitation_density;
219 bool SGEnviro::get_precipitation_enable_state(void) const {
220 return precipitation_enable_state;
223 void SGEnviro::set_precipitation_density(float density) {
224 precipitation_density = density;
226 void SGEnviro::set_precipitation_enable_state(bool enable) {
227 precipitation_enable_state = enable;
231 bool SGEnviro::get_lightning_enable_state(void) const {
232 return lightning_enable_state;
235 void SGEnviro::set_lightning_enable_state(bool enable) {
236 lightning_enable_state = enable;
242 void SGEnviro::setLight(sgVec4 adj_fog_color) {
243 sgCopyVec4( fog_color, adj_fog_color );
245 // ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
249 void SGEnviro::callback_cloud(float heading, float alt, float radius, int familly, float dist, int cloudId) {
250 // send data to wx radar
251 // compute turbulence
252 // draw precipitation
254 // compute illumination
256 // http://www.pilotfriend.com/flight_training/weather/THUNDERSTORM%20HAZARDS1.htm
257 double turbulence = 0.0;
258 if( dist < radius * radius * 2.25f ) {
260 case SGNewCloud::CLFamilly_st:
263 case SGNewCloud::CLFamilly_ci:
264 case SGNewCloud::CLFamilly_cs:
265 case SGNewCloud::CLFamilly_cc:
266 case SGNewCloud::CLFamilly_ac:
267 case SGNewCloud::CLFamilly_as:
270 case SGNewCloud::CLFamilly_sc:
273 case SGNewCloud::CLFamilly_ns:
276 case SGNewCloud::CLFamilly_cu:
279 case SGNewCloud::CLFamilly_cb:
283 // full turbulence inside cloud, half in the vicinity
284 if( dist > radius * radius )
286 if( turbulence > cloud_turbulence )
287 cloud_turbulence = turbulence;
288 // we can do 'local' precipitations too
291 // convert to LWC for radar (experimental)
292 // http://www-das.uwyo.edu/~geerts/cwx/notes/chap08/moist_cloud.html
295 case SGNewCloud::CLFamilly_st:
298 case SGNewCloud::CLFamilly_cu:
301 case SGNewCloud::CLFamilly_cb:
304 case SGNewCloud::CLFamilly_sc:
307 case SGNewCloud::CLFamilly_ci:
311 case SGNewCloud::CLFamilly_cs:
312 case SGNewCloud::CLFamilly_cc:
313 case SGNewCloud::CLFamilly_ac:
314 case SGNewCloud::CLFamilly_as:
317 case SGNewCloud::CLFamilly_ns:
321 // add to the list for the wxRadar instrument
323 radarEcho.push_back( SGWxRadarEcho ( heading, alt, radius, dist, LWC, false, cloudId ) );
325 // NB:data valid only from cockpit view
327 // spawn a new lightning
328 if(lightning_enable_state && min_time_before_lt <= 0.0 && (familly == SGNewCloud::CLFamilly_cb) &&
329 dist < 15000.0 * 15000.0 && sg_random() > 0.9f) {
332 orig.setlat(last_lat * SG_DEGREES_TO_RADIANS );
333 orig.setlon(last_lon * SG_DEGREES_TO_RADIANS );
336 dest = calc_gc_lon_lat(orig, heading, dist);
337 lon = dest.lon() * SG_RADIANS_TO_DEGREES;
338 lat = dest.lat() * SG_RADIANS_TO_DEGREES;
339 addLightning( lon, lat, alt );
342 min_time_before_lt = 5.0 + sg_random() * 30;
344 // min_time_before_lt = 5.0;
346 if( (alt - radius * 0.1) > precipitation_max_alt )
348 case SGNewCloud::CLFamilly_st:
349 case SGNewCloud::CLFamilly_cu:
350 case SGNewCloud::CLFamilly_cb:
351 case SGNewCloud::CLFamilly_ns:
352 case SGNewCloud::CLFamilly_sc:
353 precipitation_max_alt = alt - radius * 0.1;
358 list_of_SGWxRadarEcho *SGEnviro::get_radar_echo(void) {
362 // precipitation rendering code
363 void SGEnviro::DrawCone2(float baseRadius, float height, int slices, bool down, double rain_norm, double speed) {
366 sgVec3 min_light = {0.35, 0.35, 0.35};
367 sgAddVec3( light, fog_color, min_light );
368 float da = SG_PI * 2.0f / (float) slices;
369 // low number = faster
370 float speedf = 2.5f - speed / 200.0;
373 float lenf = 0.03f + speed / 2000.0;
376 float t = fmod((float) elapsed_time, speedf) / speedf;
381 glColor4f(1.0f, 0.7f, 0.7f, 0.9f);
383 int rainpos_indice = 0;
384 for( int i = 0 ; i < slices ; i++ ) {
385 float x = cos(angle) * baseRadius;
386 float y = sin(angle) * baseRadius;
388 sgVec3 dir = {x, -height, y};
390 // rain drops at 2 different speed to simulate depth
391 float t1 = (i & 1 ? t : t + t) + rainpos[rainpos_indice];
392 if(t1 > 1.0f) t1 -= 1.0f;
393 if(t1 > 1.0f) t1 -= 1.0f;
395 // distant raindrops are more transparent
396 float c = (i & 1 ? t1 * 0.5f : t1 * 0.9f);
397 glColor4f(c * light[0], c * light[1], c * light[2], c);
399 sgScaleVec3(p1, dir, t1);
400 // distant raindrops are shorter
401 float t2 = t1 + (i & 1 ? lenf : lenf+lenf);
402 sgScaleVec3(p2, dir, t2);
404 glVertex3f(p1[0], p1[1] + height, p1[2]);
405 glVertex3f(p2[0], p2[1] + height, p2[2]);
406 if( ++rainpos_indice >= MAX_RAIN_SLICE )
412 void SGEnviro::drawRain(double pitch, double roll, double heading, double speed, double rain_norm) {
414 glBindTexture(GL_TEXTURE_2D, 0);
416 glDisable(GL_DEPTH_TEST);
417 glShadeModel(GL_SMOOTH);
419 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
421 glDisable(GL_LIGHTING);
423 int slice_count = static_cast<int>(
424 (40.0 + rain_norm*150.0)* precipitation_density / 100.0);
426 // www.wonderquest.com/falling-raindrops.htm says that
427 // Raindrop terminal velocity is 5 to 20mph
428 // Rather than model it accurately (temp, pressure, diameter), and make it
429 // smaller than terminal when closer to the precipitation cloud base,
430 // we interpolate in the 5-20mph range according to rain_norm.
431 double raindrop_speed_kts
432 = (5.0 + rain_norm*15.0) * SG_MPH_TO_MPS * SG_MPS_TO_KT;
434 float angle = atanf(speed / raindrop_speed_kts) * SG_RADIANS_TO_DEGREES;
435 // We assume that the speed is HORIZONTAL airspeed here!!! XXX
436 // DOCUMENT THE CHANGE IN THE PARAMETER speed MEANING BY RENAMING IT!!!
438 // the cone rotate with speed
439 angle = -pitch - angle;
440 glRotatef(heading, 0.0, 1.0, 0.0);
441 glRotatef(roll, 0.0, 0.0, 1.0);
442 glRotatef(angle, 1.0, 0.0, 0.0);
445 DrawCone2(15.0, 30.0, slice_count, true, rain_norm, speed);
446 // down cone (usually not visible)
447 if(angle > 0.0 || heading != 0.0)
448 DrawCone2(15.0, -30.0, slice_count, false, rain_norm, speed);
452 glEnable(GL_LIGHTING);
453 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
455 glEnable(GL_DEPTH_TEST);
459 void SGEnviro::set_soundMgr(SGSoundMgr *mgr) {
463 void SGEnviro::drawPrecipitation(double rain_norm, double snow_norm, double hail_norm, double pitch, double roll, double heading, double speed) {
464 if( precipitation_enable_state && rain_norm > 0.0)
465 if( precipitation_max_alt >= last_alt )
466 drawRain(pitch, roll, heading, speed, rain_norm);
470 SGLightning::SGLightning(double _lon, double _lat, double _alt) :
475 age(1.0 + sg_random() * 4.0)
477 // sequence_count = 1 + sg_random() * 5.0;
481 SGLightning::~SGLightning() {
484 // lightning rendering code
485 void SGLightning::lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize) {
491 sgSetVec3( dir, 0.0, -1.0, 0.0 );
493 sgSetVec3( dir, sg_random() - 0.5f, sg_random() - 0.5f, sg_random() - 0.5f);
494 sgNormaliseVec3(dir);
496 if( nb_tree >= MAX_LT_TREE_SEG )
499 lt_tree[nb_tree].depth = tree_nr;
501 lt_tree[nb_tree].pt = pt;
502 lt_tree[nb_tree].prev = -1;
506 while(nseg < nbseg && pt.y() > 0.0) {
507 int prev = nb_tree - 1;
510 if( energy * sg_random() > 0.8f )
511 lt_build_tree_branch(tree_nr + 1, pt, energy * 0.9f, nbseg == 50 ? 10 : static_cast<int>(nbseg * 0.4f), segsize * 0.7f);
513 if( nb_tree >= MAX_LT_TREE_SEG )
515 sgSetVec3(newdir, (sg_random() - 0.5f), (sg_random() - 0.5f) - (nbseg == 50 ? 0.5f : 0.0), (sg_random() - 0.5f));
516 sgNormaliseVec3(newdir);
517 sgAddVec3( dir, newdir);
518 sgNormaliseVec3(dir);
520 sgScaleVec3( scaleDir, dir, segsize * energy * 0.5f );
521 pt[PX] += scaleDir[0];
522 pt[PY] += scaleDir[1];
523 pt[PZ] += scaleDir[2];
525 lt_tree[nb_tree].depth = tree_nr;
526 lt_tree[nb_tree].pt = pt;
527 lt_tree[nb_tree].prev = prev;
532 void SGLightning::lt_build(void) {
538 lt_build_tree_branch(0, top, 1.0, 50, top[PY] / 8.0);
539 if( ! sgEnviro.soundMgr )
541 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
542 Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
543 double course = 0.0, dist = 0.0;
544 calc_gc_course_dist( dest, start, &course, &dist );
545 if( dist < 10000.0 && ! sgEnviro.snd_playing && (dist < sgEnviro.snd_dist || ! sgEnviro.snd_active) ) {
546 sgEnviro.snd_timer = 0.0;
547 sgEnviro.snd_wait = dist / 340;
548 sgEnviro.snd_dist = dist;
549 sgEnviro.snd_pos_lat = lat;
550 sgEnviro.snd_pos_lon = lon;
551 sgEnviro.snd_active = true;
552 sgEnviro.snd_playing = false;
557 void SGLightning::lt_Render(void) {
559 if( fmod(sgEnviro.elapsed_time*100.0, 100.0) > 50.0 )
560 flash = sg_random() * 0.75f + 0.25f;
561 float h = lt_tree[0].pt[PY];
562 sgVec4 col={0.62f, 0.83f, 1.0f, 1.0f};
566 {glColorMaterial(GL_FRONT, GL_EMISSION); \
567 glDisable(GL_LINE_SMOOTH); glBegin(GL_LINES); \
569 glVertex3f(lt_tree[n].pt[PX], lt_tree[n].pt[PZ], lt_tree[n].pt[PY]); \
570 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]); \
571 glEnd(); glEnable(GL_LINE_SMOOTH);}
573 glDepthMask( GL_FALSE );
575 glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
576 glBindTexture(GL_TEXTURE_2D, 0);
578 glDisable(GL_LIGHTING);
581 sgMat4 modelview, tmp;
582 ssgGetModelviewMatrix( modelview );
583 sgCopyMat4( tmp, sgEnviro.transform );
584 sgPostMultMat4( tmp, modelview );
585 ssgLoadModelviewMatrix( tmp );
587 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
588 Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
589 double course = 0.0, dist = 0.0;
590 calc_gc_course_dist( dest, start, &course, &dist );
591 double ax = 0.0, ay = 0.0;
592 ax = cos(course) * dist;
593 ay = sin(course) * dist;
595 glTranslatef( ax, ay, -sgEnviro.last_alt );
597 sgEnviro.radarEcho.push_back( SGWxRadarEcho ( course, 0.0, 0.0, dist, age, true, 0 ) );
599 for( int n = 0 ; n < nb_tree ; n++ ) {
600 if( lt_tree[n].prev < 0 )
603 float t1 = sgLerp(0.5, 1.0, lt_tree[n].pt[PY] / h);
605 if( lt_tree[n].depth >= 2 ) {
607 sgScaleVec4(c, col, t1 * 0.6f);
610 if( lt_tree[n].depth == 0 ) {
612 sgScaleVec4(c, col, t1 * 0.5f);
616 sgScaleVec4(c, col, t1);
620 sgScaleVec4(c, col, t1 * 0.7f);
624 if( lt_tree[n].depth == 0 )
629 sgSetVec4(c, t1, t1, t1, t1);
635 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
637 glDepthMask( GL_TRUE );
639 glEnable(GL_LIGHTING);
642 void SGEnviro::addLightning(double lon, double lat, double alt) {
643 if( lightnings.size() > 10)
645 SGLightning *lt= new SGLightning(lon, lat, alt);
646 lightnings.push_back(lt);
649 void SGEnviro::drawLightning(void) {
650 list_of_lightning::iterator iLightning;
651 // play 'thunder' for lightning
654 // wait until sound has reached us
656 if( snd_timer >= snd_wait ) {
658 // compute relative position of lightning
659 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
660 Point3D dest( snd_pos_lon*SG_DEGREES_TO_RADIANS, snd_pos_lat*SG_DEGREES_TO_RADIANS, 0.0 );
661 double course = 0.0, dist = 0.0;
662 calc_gc_course_dist( dest, start, &course, &dist );
663 double ax = 0.0, ay = 0.0;
664 ax = cos(course) * dist;
665 ay = sin(course) * dist;
666 SGSharedPtr<SGSoundSample> snd = soundMgr->find("thunder");
668 ALfloat pos[3]={ax, ay, -sgEnviro.last_alt };
669 snd->set_source_pos(pos);
674 if( !soundMgr->is_playing("thunder") ) {
680 if( ! lightning_enable_state )
683 for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
685 if( sg_random() > 0.95f )
686 (*iLightning)->lt_build();
687 (*iLightning)->lt_Render();
688 (*iLightning)->age -= dt;
689 if( (*iLightning)->age < 0.0 ) {
690 delete (*iLightning);
691 lightnings.erase( iLightning );
699 void SGEnviro::setFOV( float w, float h ) {
704 void SGEnviro::getFOV( float &w, float &h ) {