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() :
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 family, 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 && (family == 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) {
365 sgVec3 min_light = {0.35, 0.35, 0.35};
366 sgAddVec3( light, fog_color, min_light );
367 float da = SG_PI * 2.0f / (float) slices;
368 // low number = faster
369 float speedf = 2.5f - speed / 200.0;
372 float lenf = 0.03f + speed / 2000.0;
375 float t = fmod((float) elapsed_time, speedf) / speedf;
380 glColor4f(1.0f, 0.7f, 0.7f, 0.9f);
382 int rainpos_indice = 0;
383 for( int i = 0 ; i < slices ; i++ ) {
384 float x = cos(angle) * baseRadius;
385 float y = sin(angle) * baseRadius;
387 sgVec3 dir = {x, -height, y};
389 // rain drops at 2 different speed to simulate depth
390 float t1 = (i & 1 ? t : t + t) + rainpos[rainpos_indice];
391 if(t1 > 1.0f) t1 -= 1.0f;
392 if(t1 > 1.0f) t1 -= 1.0f;
394 // distant raindrops are more transparent
395 float c = (i & 1 ? t1 * 0.5f : t1 * 0.9f);
396 glColor4f(c * light[0], c * light[1], c * light[2], c);
398 sgScaleVec3(p1, dir, t1);
399 // distant raindrops are shorter
400 float t2 = t1 + (i & 1 ? lenf : lenf+lenf);
401 sgScaleVec3(p2, dir, t2);
403 glVertex3f(p1[0], p1[1] + height, p1[2]);
404 glVertex3f(p2[0], p2[1] + height, p2[2]);
405 if( ++rainpos_indice >= MAX_RAIN_SLICE )
411 void SGEnviro::drawRain(double pitch, double roll, double heading, double hspeed, double rain_norm) {
414 static int debug_period = 0;
415 if (debug_period++ == 50) {
423 //" angle = " << angle
424 //<< " raindrop(KTS) = " << raindrop_speed_kts
430 glBindTexture(GL_TEXTURE_2D, 0);
432 glDisable(GL_DEPTH_TEST);
433 glShadeModel(GL_SMOOTH);
435 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
437 glDisable(GL_LIGHTING);
439 int slice_count = static_cast<int>(
440 (40.0 + rain_norm*150.0)* precipitation_density / 100.0);
442 // www.wonderquest.com/falling-raindrops.htm says that
443 // Raindrop terminal velocity is 5 to 20mph
444 // Rather than model it accurately (temp, pressure, diameter), and make it
445 // smaller than terminal when closer to the precipitation cloud base,
446 // we interpolate in the 5-20mph range according to rain_norm.
447 double raindrop_speed_kts
448 = (5.0 + rain_norm*15.0) * SG_MPH_TO_MPS * SG_MPS_TO_KT;
450 float angle = atanf(hspeed / raindrop_speed_kts) * SG_RADIANS_TO_DEGREES;
452 // the cone rotate with hspeed
453 angle = -pitch - angle;
454 glRotatef(roll, 0.0, 0.0, 1.0);
455 glRotatef(heading, 0.0, 1.0, 0.0);
456 glRotatef(angle, 1.0, 0.0, 0.0);
459 DrawCone2(15.0, 30.0, slice_count, true, rain_norm, hspeed);
460 // down cone (usually not visible)
461 if(angle > 0.0 || heading != 0.0)
462 DrawCone2(15.0, -30.0, slice_count, false, rain_norm, hspeed);
466 glEnable(GL_LIGHTING);
467 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
469 glEnable(GL_DEPTH_TEST);
473 void SGEnviro::set_soundMgr(SGSoundMgr *mgr) {
477 void SGEnviro::drawPrecipitation(double rain_norm, double snow_norm, double hail_norm, double pitch, double roll, double heading, double hspeed) {
478 if( precipitation_enable_state && rain_norm > 0.0)
479 if( precipitation_max_alt >= last_alt )
480 drawRain(pitch, roll, heading, hspeed, rain_norm);
484 SGLightning::SGLightning(double _lon, double _lat, double _alt) :
489 age(1.0 + sg_random() * 4.0)
491 // sequence_count = 1 + sg_random() * 5.0;
495 SGLightning::~SGLightning() {
498 // lightning rendering code
499 void SGLightning::lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize) {
505 sgSetVec3( dir, 0.0, -1.0, 0.0 );
507 sgSetVec3( dir, sg_random() - 0.5f, sg_random() - 0.5f, sg_random() - 0.5f);
508 sgNormaliseVec3(dir);
510 if( nb_tree >= MAX_LT_TREE_SEG )
513 lt_tree[nb_tree].depth = tree_nr;
515 lt_tree[nb_tree].pt = pt;
516 lt_tree[nb_tree].prev = -1;
520 while(nseg < nbseg && pt.y() > 0.0) {
521 int prev = nb_tree - 1;
524 if( energy * sg_random() > 0.8f )
525 lt_build_tree_branch(tree_nr + 1, pt, energy * 0.9f, nbseg == 50 ? 10 : static_cast<int>(nbseg * 0.4f), segsize * 0.7f);
527 if( nb_tree >= MAX_LT_TREE_SEG )
529 sgSetVec3(newdir, (sg_random() - 0.5f), (sg_random() - 0.5f) - (nbseg == 50 ? 0.5f : 0.0), (sg_random() - 0.5f));
530 sgNormaliseVec3(newdir);
531 sgAddVec3( dir, newdir);
532 sgNormaliseVec3(dir);
534 sgScaleVec3( scaleDir, dir, segsize * energy * 0.5f );
535 pt[PX] += scaleDir[0];
536 pt[PY] += scaleDir[1];
537 pt[PZ] += scaleDir[2];
539 lt_tree[nb_tree].depth = tree_nr;
540 lt_tree[nb_tree].pt = pt;
541 lt_tree[nb_tree].prev = prev;
546 void SGLightning::lt_build(void) {
552 lt_build_tree_branch(0, top, 1.0, 50, top[PY] / 8.0);
553 if( ! sgEnviro.soundMgr )
555 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
556 Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
557 double course = 0.0, dist = 0.0;
558 calc_gc_course_dist( dest, start, &course, &dist );
559 if( dist < 10000.0 && ! sgEnviro.snd_playing && (dist < sgEnviro.snd_dist || ! sgEnviro.snd_active) ) {
560 sgEnviro.snd_timer = 0.0;
561 sgEnviro.snd_wait = dist / 340;
562 sgEnviro.snd_dist = dist;
563 sgEnviro.snd_pos_lat = lat;
564 sgEnviro.snd_pos_lon = lon;
565 sgEnviro.snd_active = true;
566 sgEnviro.snd_playing = false;
571 void SGLightning::lt_Render(void) {
573 if( fmod(sgEnviro.elapsed_time*100.0, 100.0) > 50.0 )
574 flash = sg_random() * 0.75f + 0.25f;
575 float h = lt_tree[0].pt[PY];
576 sgVec4 col={0.62f, 0.83f, 1.0f, 1.0f};
580 {glColorMaterial(GL_FRONT, GL_EMISSION); \
581 glDisable(GL_LINE_SMOOTH); glBegin(GL_LINES); \
583 glVertex3f(lt_tree[n].pt[PX], lt_tree[n].pt[PZ], lt_tree[n].pt[PY]); \
584 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]); \
585 glEnd(); glEnable(GL_LINE_SMOOTH);}
587 glDepthMask( GL_FALSE );
589 glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
590 glBindTexture(GL_TEXTURE_2D, 0);
592 glDisable(GL_LIGHTING);
595 sgMat4 modelview, tmp;
596 ssgGetModelviewMatrix( modelview );
597 sgCopyMat4( tmp, sgEnviro.transform );
598 sgPostMultMat4( tmp, modelview );
599 ssgLoadModelviewMatrix( tmp );
601 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
602 Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
603 double course = 0.0, dist = 0.0;
604 calc_gc_course_dist( dest, start, &course, &dist );
605 double ax = 0.0, ay = 0.0;
606 ax = cos(course) * dist;
607 ay = sin(course) * dist;
609 glTranslatef( ax, ay, -sgEnviro.last_alt );
611 sgEnviro.radarEcho.push_back( SGWxRadarEcho ( course, 0.0, 0.0, dist, age, true, 0 ) );
613 for( int n = 0 ; n < nb_tree ; n++ ) {
614 if( lt_tree[n].prev < 0 )
617 float t1 = sgLerp(0.5, 1.0, lt_tree[n].pt[PY] / h);
619 if( lt_tree[n].depth >= 2 ) {
621 sgScaleVec4(c, col, t1 * 0.6f);
624 if( lt_tree[n].depth == 0 ) {
626 sgScaleVec4(c, col, t1 * 0.5f);
630 sgScaleVec4(c, col, t1);
634 sgScaleVec4(c, col, t1 * 0.7f);
638 if( lt_tree[n].depth == 0 )
643 sgSetVec4(c, t1, t1, t1, t1);
649 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
651 glDepthMask( GL_TRUE );
653 glEnable(GL_LIGHTING);
656 void SGEnviro::addLightning(double lon, double lat, double alt) {
657 if( lightnings.size() > 10)
659 SGLightning *lt= new SGLightning(lon, lat, alt);
660 lightnings.push_back(lt);
663 void SGEnviro::drawLightning(void) {
664 list_of_lightning::iterator iLightning;
665 // play 'thunder' for lightning
668 // wait until sound has reached us
670 if( snd_timer >= snd_wait ) {
672 // compute relative position of lightning
673 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
674 Point3D dest( snd_pos_lon*SG_DEGREES_TO_RADIANS, snd_pos_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 SGSharedPtr<SGSoundSample> snd = soundMgr->find("thunder");
682 ALfloat pos[3]={ax, ay, -sgEnviro.last_alt };
683 snd->set_source_pos(pos);
688 if( !soundMgr->is_playing("thunder") ) {
694 if( ! lightning_enable_state )
697 for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
699 if( sg_random() > 0.95f )
700 (*iLightning)->lt_build();
701 (*iLightning)->lt_Render();
702 (*iLightning)->age -= dt;
703 if( (*iLightning)->age < 0.0 ) {
704 delete (*iLightning);
705 lightnings.erase( iLightning );
713 void SGEnviro::setFOV( float w, float h ) {
718 void SGEnviro::getFOV( float &w, float &h ) {