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, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
24 # include <simgear_config.h>
28 #include <simgear/constants.h>
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 "visual_enviro.hxx"
49 #define MAX_RAIN_SLICE 200
50 static float rainpos[MAX_RAIN_SLICE];
51 #define MAX_LT_TREE_SEG 400
54 * A class to render lightnings.
59 * Build a new lightning.
60 * The lightning has a limited life time. It will also play a thunder sounder once.
61 * @param lon lon longitude in degree
62 * @param lat lat latitude in degree
63 * @param alt asl of top of lightning
65 SGLightning(double lon, double lat, double alt);
69 void lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize);
71 // contains all the segments of the lightning
72 lt_tree_seg lt_tree[MAX_LT_TREE_SEG];
75 // position of lightning
82 typedef vector<SGLightning *> list_of_lightning;
83 static list_of_lightning lightnings;
87 SGEnviro::SGEnviro(void) :
89 turbulence_enable_state(false),
90 precipitation_enable_state(true),
91 lightning_enable_state(false),
95 last_cloud_turbulence(0.0),
96 cloud_turbulence(0.0),
99 min_time_before_lt(0.0),
102 precipitation_max_alt(0.0),
103 precipitation_density(100.0)
106 for(int i = 0; i < MAX_RAIN_SLICE ; i++)
107 rainpos[i] = sg_random();
108 radarEcho.reserve(100);
111 SGEnviro::~SGEnviro(void) {
112 list_of_lightning::iterator iLightning;
113 for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
114 delete (*iLightning);
119 void SGEnviro::startOfFrame( sgVec3 p, sgVec3 up, double lon, double lat, double alt, double delta_time) {
120 view_in_cloud = false;
121 // ask the impostor cache to do some cleanup
122 if(SGNewCloud::cldCache)
123 SGNewCloud::cldCache->startNewFrame();
124 last_cloud_turbulence = cloud_turbulence;
125 cloud_turbulence = 0.0;
126 elapsed_time += delta_time;
127 min_time_before_lt -= delta_time;
133 sgMakeTransMat4( T1, p );
135 sgSetVec3( axis, 0.0, 0.0, 1.0 );
136 sgMakeRotMat4( LON, lon, axis );
138 sgSetVec3( axis, 0.0, 1.0, 0.0 );
139 sgMakeRotMat4( LAT, 90.0 - lat, axis );
143 sgCopyMat4( TRANSFORM, T1 );
144 sgPreMultMat4( TRANSFORM, LON );
145 sgPreMultMat4( TRANSFORM, LAT );
148 sgSetCoord( &pos, TRANSFORM );
150 sgMakeCoordMat4( transform, &pos );
156 precipitation_max_alt = 400.0;
159 void SGEnviro::endOfFrame(void) {
162 double SGEnviro::get_cloud_turbulence(void) const {
163 return last_cloud_turbulence;
166 // this can be queried to add some turbulence for example
167 bool SGEnviro::is_view_in_cloud(void) const {
168 return view_in_cloud;
170 void SGEnviro::set_view_in_cloud(bool incloud) {
171 view_in_cloud = incloud;
174 int SGEnviro::get_CacheResolution(void) const {
175 return SGCloudField::get_CacheResolution();
178 int SGEnviro::get_clouds_CacheSize(void) const {
179 return SGCloudField::get_CacheSize();
181 float SGEnviro::get_clouds_visibility(void) const {
182 return SGCloudField::get_CloudVis();
184 float SGEnviro::get_clouds_density(void) const {
185 return SGCloudField::get_density();
187 bool SGEnviro::get_clouds_enable_state(void) const {
188 return SGCloudField::get_enable3dClouds();
191 bool SGEnviro::get_turbulence_enable_state(void) const {
192 return turbulence_enable_state;
195 void SGEnviro::set_CacheResolution(int resolutionPixels) {
196 SGCloudField::set_CacheResolution(resolutionPixels);
199 void SGEnviro::set_clouds_CacheSize(int sizeKb) {
200 SGCloudField::set_CacheSize(sizeKb);
202 void SGEnviro::set_clouds_visibility(float distance) {
203 SGCloudField::set_CloudVis(distance);
205 void SGEnviro::set_clouds_density(float density) {
206 SGCloudField::set_density(density);
208 void SGEnviro::set_clouds_enable_state(bool enable) {
209 SGCloudField::set_enable3dClouds(enable);
211 void SGEnviro::set_turbulence_enable_state(bool enable) {
212 turbulence_enable_state = enable;
215 float SGEnviro::get_precipitation_density(void) const {
216 return precipitation_density;
218 bool SGEnviro::get_precipitation_enable_state(void) const {
219 return precipitation_enable_state;
222 void SGEnviro::set_precipitation_density(float density) {
223 precipitation_density = density;
225 void SGEnviro::set_precipitation_enable_state(bool enable) {
226 precipitation_enable_state = enable;
230 bool SGEnviro::get_lightning_enable_state(void) const {
231 return lightning_enable_state;
234 void SGEnviro::set_lightning_enable_state(bool enable) {
235 lightning_enable_state = enable;
241 void SGEnviro::setLight(sgVec4 adj_fog_color) {
242 sgCopyVec4( fog_color, adj_fog_color );
244 // ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
248 void SGEnviro::callback_cloud(float heading, float alt, float radius, int familly, float dist, int cloudId) {
249 // send data to wx radar
250 // compute turbulence
251 // draw precipitation
253 // compute illumination
255 // http://www.pilotfriend.com/flight_training/weather/THUNDERSTORM%20HAZARDS1.htm
256 double turbulence = 0.0;
257 if( dist < radius * radius * 2.25f ) {
259 case SGNewCloud::CLFamilly_st:
262 case SGNewCloud::CLFamilly_ci:
263 case SGNewCloud::CLFamilly_cs:
264 case SGNewCloud::CLFamilly_cc:
265 case SGNewCloud::CLFamilly_ac:
266 case SGNewCloud::CLFamilly_as:
269 case SGNewCloud::CLFamilly_sc:
272 case SGNewCloud::CLFamilly_ns:
275 case SGNewCloud::CLFamilly_cu:
278 case SGNewCloud::CLFamilly_cb:
282 // full turbulence inside cloud, half in the vicinity
283 if( dist > radius * radius )
285 if( turbulence > cloud_turbulence )
286 cloud_turbulence = turbulence;
287 // we can do 'local' precipitations too
290 // convert to LWC for radar (experimental)
291 // http://www-das.uwyo.edu/~geerts/cwx/notes/chap08/moist_cloud.html
294 case SGNewCloud::CLFamilly_st:
297 case SGNewCloud::CLFamilly_cu:
300 case SGNewCloud::CLFamilly_cb:
303 case SGNewCloud::CLFamilly_sc:
306 case SGNewCloud::CLFamilly_ci:
310 case SGNewCloud::CLFamilly_cs:
311 case SGNewCloud::CLFamilly_cc:
312 case SGNewCloud::CLFamilly_ac:
313 case SGNewCloud::CLFamilly_as:
316 case SGNewCloud::CLFamilly_ns:
320 // add to the list for the wxRadar instrument
322 radarEcho.push_back( SGWxRadarEcho ( heading, alt, radius, dist, LWC, false, cloudId ) );
324 // NB:data valid only from cockpit view
326 // spawn a new lightning
327 if(lightning_enable_state && min_time_before_lt <= 0.0 && (familly == SGNewCloud::CLFamilly_cb) &&
328 dist < 15000.0 * 15000.0 && sg_random() > 0.9f) {
331 orig.setlat(last_lat * SG_DEGREES_TO_RADIANS );
332 orig.setlon(last_lon * SG_DEGREES_TO_RADIANS );
335 dest = calc_gc_lon_lat(orig, heading, dist);
336 lon = dest.lon() * SG_RADIANS_TO_DEGREES;
337 lat = dest.lat() * SG_RADIANS_TO_DEGREES;
338 addLightning( lon, lat, alt );
341 min_time_before_lt = 5.0 + sg_random() * 30;
343 // min_time_before_lt = 5.0;
345 if( (alt - radius * 0.1) > precipitation_max_alt )
347 case SGNewCloud::CLFamilly_st:
348 case SGNewCloud::CLFamilly_cu:
349 case SGNewCloud::CLFamilly_cb:
350 case SGNewCloud::CLFamilly_ns:
351 case SGNewCloud::CLFamilly_sc:
352 precipitation_max_alt = alt - radius * 0.1;
357 list_of_SGWxRadarEcho *SGEnviro::get_radar_echo(void) {
361 // precipitation rendering code
362 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 speed, double rain_norm) {
413 glBindTexture(GL_TEXTURE_2D, 0);
415 glDisable(GL_DEPTH_TEST);
416 glShadeModel(GL_SMOOTH);
418 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
420 glDisable(GL_LIGHTING);
422 int slice_count = (40.0 + rain_norm*150.0)* precipitation_density / 100.0;
429 // TODO:find the real view orientation, not the AC one
430 // the cone rotate with speed
431 angle = -pitch - angle;
432 glRotatef(angle, 1.0, 0.0, 0.0);
433 glRotatef(roll, 0.0, 1.0, 0.0);
434 glRotatef(heading, 0.0, 0.0, 1.0);
437 DrawCone2(15.0, 30.0, slice_count, true, rain_norm, speed);
438 // down cone (usually not visible)
439 if(angle > 0.0 || heading != 0.0)
440 DrawCone2(15.0, -30.0, slice_count, false, rain_norm, speed);
444 glEnable(GL_LIGHTING);
445 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
447 glEnable(GL_DEPTH_TEST);
451 void SGEnviro::set_soundMgr(SGSoundMgr *mgr) {
455 void SGEnviro::drawPrecipitation(double rain_norm, double snow_norm, double hail_norm, double pitch, double roll, double heading, double speed) {
456 if( precipitation_enable_state && rain_norm > 0.0)
457 if( precipitation_max_alt >= last_alt )
458 drawRain(pitch, roll, heading, speed, rain_norm);
462 SGLightning::SGLightning(double _lon, double _lat, double _alt) :
466 age(1.0 + sg_random() * 4.0),
469 // sequence_count = 1 + sg_random() * 5.0;
473 SGLightning::~SGLightning() {
476 // lightning rendering code
477 void SGLightning::lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize) {
483 sgSetVec3( dir, 0.0, -1.0, 0.0 );
485 sgSetVec3( dir, sg_random() - 0.5f, sg_random() - 0.5f, sg_random() - 0.5f);
486 sgNormaliseVec3(dir);
488 if( nb_tree >= MAX_LT_TREE_SEG )
491 lt_tree[nb_tree].depth = tree_nr;
493 lt_tree[nb_tree].pt = pt;
494 lt_tree[nb_tree].prev = -1;
498 while(nseg < nbseg && pt.y() > 0.0) {
499 int prev = nb_tree - 1;
502 if( energy * sg_random() > 0.8f )
503 lt_build_tree_branch(tree_nr + 1, pt, energy * 0.9f, nbseg == 50 ? 10 : nbseg * 0.4f, segsize * 0.7f);
505 if( nb_tree >= MAX_LT_TREE_SEG )
507 sgSetVec3(newdir, (sg_random() - 0.5f), (sg_random() - 0.5f) - (nbseg == 50 ? 0.5f : 0.0), (sg_random() - 0.5f));
508 sgNormaliseVec3(newdir);
509 sgAddVec3( dir, newdir);
510 sgNormaliseVec3(dir);
512 sgScaleVec3( scaleDir, dir, segsize * energy * 0.5f );
513 pt[PX] += scaleDir[0];
514 pt[PY] += scaleDir[1];
515 pt[PZ] += scaleDir[2];
517 lt_tree[nb_tree].depth = tree_nr;
518 lt_tree[nb_tree].pt = pt;
519 lt_tree[nb_tree].prev = prev;
524 void SGLightning::lt_build(void) {
530 lt_build_tree_branch(0, top, 1.0, 50, top[PY] / 8.0);
531 if( ! sgEnviro.soundMgr )
533 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
534 Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
535 double course = 0.0, dist = 0.0;
536 calc_gc_course_dist( dest, start, &course, &dist );
537 if( dist < 10000.0 && ! sgEnviro.snd_playing && (dist < sgEnviro.snd_dist || ! sgEnviro.snd_active) ) {
538 sgEnviro.snd_timer = 0.0;
539 sgEnviro.snd_wait = dist / 340;
540 sgEnviro.snd_dist = dist;
541 sgEnviro.snd_pos_lat = lat;
542 sgEnviro.snd_pos_lon = lon;
543 sgEnviro.snd_active = true;
544 sgEnviro.snd_playing = false;
549 void SGLightning::lt_Render(void) {
551 if( fmod(sgEnviro.elapsed_time*100.0, 100.0) > 50.0 )
552 flash = sg_random() * 0.75f + 0.25f;
553 float h = lt_tree[0].pt[PY];
554 sgVec4 col={0.62f, 0.83f, 1.0f, 1.0f};
558 {glBegin(GL_LINES); \
560 glVertex3f(lt_tree[n].pt[PX], lt_tree[n].pt[PZ], lt_tree[n].pt[PY]); \
561 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]); \
564 glDepthMask( GL_FALSE );
566 glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
567 glBindTexture(GL_TEXTURE_2D, 0);
569 glDisable(GL_LIGHTING);
572 sgMat4 modelview, tmp;
573 ssgGetModelviewMatrix( modelview );
574 sgCopyMat4( tmp, sgEnviro.transform );
575 sgPostMultMat4( tmp, modelview );
576 ssgLoadModelviewMatrix( tmp );
578 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
579 Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
580 double course = 0.0, dist = 0.0;
581 calc_gc_course_dist( dest, start, &course, &dist );
582 double ax = 0.0, ay = 0.0;
583 ax = cos(course) * dist;
584 ay = sin(course) * dist;
586 glTranslatef( ax, ay, -sgEnviro.last_alt );
588 sgEnviro.radarEcho.push_back( SGWxRadarEcho ( course, 0.0, 0.0, dist, age, true, 0 ) );
590 for( int n = 0 ; n < nb_tree ; n++ ) {
591 if( lt_tree[n].prev < 0 )
594 float t1 = sgLerp(0.5, 1.0, lt_tree[n].pt[PY] / h);
596 if( lt_tree[n].depth >= 2 ) {
598 sgScaleVec4(c, col, t1 * 0.6f);
601 if( lt_tree[n].depth == 0 ) {
603 sgScaleVec4(c, col, t1 * 0.5f);
607 sgScaleVec4(c, col, t1);
611 sgScaleVec4(c, col, t1 * 0.7f);
615 if( lt_tree[n].depth == 0 )
620 sgSetVec4(c, t1, t1, t1, t1);
626 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
628 glDepthMask( GL_TRUE );
630 glEnable(GL_LIGHTING);
633 void SGEnviro::addLightning(double lon, double lat, double alt) {
634 if( lightnings.size() > 10)
636 SGLightning *lt= new SGLightning(lon, lat, alt);
637 lightnings.push_back(lt);
640 void SGEnviro::drawLightning(void) {
641 list_of_lightning::iterator iLightning;
642 // play 'thunder' for lightning
645 // wait until sound has reached us
647 if( snd_timer >= snd_wait ) {
649 // compute relative position of lightning
650 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
651 Point3D dest( snd_pos_lon*SG_DEGREES_TO_RADIANS, snd_pos_lat*SG_DEGREES_TO_RADIANS, 0.0 );
652 double course = 0.0, dist = 0.0;
653 calc_gc_course_dist( dest, start, &course, &dist );
654 double ax = 0.0, ay = 0.0;
655 ax = cos(course) * dist;
656 ay = sin(course) * dist;
657 SGSoundSample *snd = soundMgr->find("thunder");
659 ALfloat pos[3]={ax, ay, -sgEnviro.last_alt };
660 snd->set_source_pos(pos);
665 if( !soundMgr->is_playing("thunder") ) {
671 if( ! lightning_enable_state )
674 for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
676 if( sg_random() > 0.95f )
677 (*iLightning)->lt_build();
678 (*iLightning)->lt_Render();
679 (*iLightning)->age -= dt;
680 if( (*iLightning)->age < 0.0 ) {
681 delete (*iLightning);
682 lightnings.erase( iLightning );
690 void SGEnviro::setFOV( float w, float h ) {
695 void SGEnviro::getFOV( float &w, float &h ) {