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/structure/SGReferenced.hxx>
30 #include <simgear/structure/SGSharedPtr.hxx>
31 #include <simgear/math/sg_random.h>
32 #include <simgear/math/sg_geodesy.hxx>
33 #include <simgear/math/point3d.hxx>
34 #include <simgear/math/polar3d.hxx>
35 #include <simgear/sound/soundmgr_openal.hxx>
36 #include <simgear/scene/sky/cloudfield.hxx>
37 #include <simgear/scene/sky/newcloud.hxx>
38 #include "visual_enviro.hxx"
51 #define MAX_RAIN_SLICE 200
52 static float rainpos[MAX_RAIN_SLICE];
53 #define MAX_LT_TREE_SEG 400
56 * A class to render lightnings.
61 * Build a new lightning.
62 * The lightning has a limited life time. It will also play a thunder sounder once.
63 * @param lon lon longitude in degree
64 * @param lat lat latitude in degree
65 * @param alt asl of top of lightning
67 SGLightning(double lon, double lat, double alt);
71 void lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize);
73 // contains all the segments of the lightning
74 lt_tree_seg lt_tree[MAX_LT_TREE_SEG];
77 // position of lightning
84 typedef vector<SGLightning *> list_of_lightning;
85 static list_of_lightning lightnings;
89 SGEnviro::SGEnviro(void) :
91 turbulence_enable_state(false),
92 precipitation_enable_state(true),
93 lightning_enable_state(false),
97 last_cloud_turbulence(0.0),
98 cloud_turbulence(0.0),
101 min_time_before_lt(0.0),
104 precipitation_max_alt(0.0),
105 precipitation_density(100.0)
108 for(int i = 0; i < MAX_RAIN_SLICE ; i++)
109 rainpos[i] = sg_random();
110 radarEcho.reserve(100);
113 SGEnviro::~SGEnviro(void) {
114 list_of_lightning::iterator iLightning;
115 for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
116 delete (*iLightning);
121 void SGEnviro::startOfFrame( sgVec3 p, sgVec3 up, double lon, double lat, double alt, double delta_time) {
122 view_in_cloud = false;
123 // ask the impostor cache to do some cleanup
124 if(SGNewCloud::cldCache)
125 SGNewCloud::cldCache->startNewFrame();
126 last_cloud_turbulence = cloud_turbulence;
127 cloud_turbulence = 0.0;
128 elapsed_time += delta_time;
129 min_time_before_lt -= delta_time;
135 sgMakeTransMat4( T1, p );
137 sgSetVec3( axis, 0.0, 0.0, 1.0 );
138 sgMakeRotMat4( LON, lon, axis );
140 sgSetVec3( axis, 0.0, 1.0, 0.0 );
141 sgMakeRotMat4( LAT, 90.0 - lat, axis );
145 sgCopyMat4( TRANSFORM, T1 );
146 sgPreMultMat4( TRANSFORM, LON );
147 sgPreMultMat4( TRANSFORM, LAT );
150 sgSetCoord( &pos, TRANSFORM );
152 sgMakeCoordMat4( transform, &pos );
158 precipitation_max_alt = 400.0;
161 void SGEnviro::endOfFrame(void) {
164 double SGEnviro::get_cloud_turbulence(void) const {
165 return last_cloud_turbulence;
168 // this can be queried to add some turbulence for example
169 bool SGEnviro::is_view_in_cloud(void) const {
170 return view_in_cloud;
172 void SGEnviro::set_view_in_cloud(bool incloud) {
173 view_in_cloud = incloud;
176 int SGEnviro::get_CacheResolution(void) const {
177 return SGCloudField::get_CacheResolution();
180 int SGEnviro::get_clouds_CacheSize(void) const {
181 return SGCloudField::get_CacheSize();
183 float SGEnviro::get_clouds_visibility(void) const {
184 return SGCloudField::get_CloudVis();
186 float SGEnviro::get_clouds_density(void) const {
187 return SGCloudField::get_density();
189 bool SGEnviro::get_clouds_enable_state(void) const {
190 return SGCloudField::get_enable3dClouds();
193 bool SGEnviro::get_turbulence_enable_state(void) const {
194 return turbulence_enable_state;
197 void SGEnviro::set_CacheResolution(int resolutionPixels) {
198 SGCloudField::set_CacheResolution(resolutionPixels);
201 void SGEnviro::set_clouds_CacheSize(int sizeKb) {
202 SGCloudField::set_CacheSize(sizeKb);
204 void SGEnviro::set_clouds_visibility(float distance) {
205 SGCloudField::set_CloudVis(distance);
207 void SGEnviro::set_clouds_density(float density) {
208 SGCloudField::set_density(density);
210 void SGEnviro::set_clouds_enable_state(bool enable) {
211 SGCloudField::set_enable3dClouds(enable);
213 void SGEnviro::set_turbulence_enable_state(bool enable) {
214 turbulence_enable_state = enable;
217 float SGEnviro::get_precipitation_density(void) const {
218 return precipitation_density;
220 bool SGEnviro::get_precipitation_enable_state(void) const {
221 return precipitation_enable_state;
224 void SGEnviro::set_precipitation_density(float density) {
225 precipitation_density = density;
227 void SGEnviro::set_precipitation_enable_state(bool enable) {
228 precipitation_enable_state = enable;
232 bool SGEnviro::get_lightning_enable_state(void) const {
233 return lightning_enable_state;
236 void SGEnviro::set_lightning_enable_state(bool enable) {
237 lightning_enable_state = enable;
243 void SGEnviro::setLight(sgVec4 adj_fog_color) {
244 sgCopyVec4( fog_color, adj_fog_color );
246 // ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
250 void SGEnviro::callback_cloud(float heading, float alt, float radius, int familly, float dist, int cloudId) {
251 // send data to wx radar
252 // compute turbulence
253 // draw precipitation
255 // compute illumination
257 // http://www.pilotfriend.com/flight_training/weather/THUNDERSTORM%20HAZARDS1.htm
258 double turbulence = 0.0;
259 if( dist < radius * radius * 2.25f ) {
261 case SGNewCloud::CLFamilly_st:
264 case SGNewCloud::CLFamilly_ci:
265 case SGNewCloud::CLFamilly_cs:
266 case SGNewCloud::CLFamilly_cc:
267 case SGNewCloud::CLFamilly_ac:
268 case SGNewCloud::CLFamilly_as:
271 case SGNewCloud::CLFamilly_sc:
274 case SGNewCloud::CLFamilly_ns:
277 case SGNewCloud::CLFamilly_cu:
280 case SGNewCloud::CLFamilly_cb:
284 // full turbulence inside cloud, half in the vicinity
285 if( dist > radius * radius )
287 if( turbulence > cloud_turbulence )
288 cloud_turbulence = turbulence;
289 // we can do 'local' precipitations too
292 // convert to LWC for radar (experimental)
293 // http://www-das.uwyo.edu/~geerts/cwx/notes/chap08/moist_cloud.html
296 case SGNewCloud::CLFamilly_st:
299 case SGNewCloud::CLFamilly_cu:
302 case SGNewCloud::CLFamilly_cb:
305 case SGNewCloud::CLFamilly_sc:
308 case SGNewCloud::CLFamilly_ci:
312 case SGNewCloud::CLFamilly_cs:
313 case SGNewCloud::CLFamilly_cc:
314 case SGNewCloud::CLFamilly_ac:
315 case SGNewCloud::CLFamilly_as:
318 case SGNewCloud::CLFamilly_ns:
322 // add to the list for the wxRadar instrument
324 radarEcho.push_back( SGWxRadarEcho ( heading, alt, radius, dist, LWC, false, cloudId ) );
326 // NB:data valid only from cockpit view
328 // spawn a new lightning
329 if(lightning_enable_state && min_time_before_lt <= 0.0 && (familly == SGNewCloud::CLFamilly_cb) &&
330 dist < 15000.0 * 15000.0 && sg_random() > 0.9f) {
333 orig.setlat(last_lat * SG_DEGREES_TO_RADIANS );
334 orig.setlon(last_lon * SG_DEGREES_TO_RADIANS );
337 dest = calc_gc_lon_lat(orig, heading, dist);
338 lon = dest.lon() * SG_RADIANS_TO_DEGREES;
339 lat = dest.lat() * SG_RADIANS_TO_DEGREES;
340 addLightning( lon, lat, alt );
343 min_time_before_lt = 5.0 + sg_random() * 30;
345 // min_time_before_lt = 5.0;
347 if( (alt - radius * 0.1) > precipitation_max_alt )
349 case SGNewCloud::CLFamilly_st:
350 case SGNewCloud::CLFamilly_cu:
351 case SGNewCloud::CLFamilly_cb:
352 case SGNewCloud::CLFamilly_ns:
353 case SGNewCloud::CLFamilly_sc:
354 precipitation_max_alt = alt - radius * 0.1;
359 list_of_SGWxRadarEcho *SGEnviro::get_radar_echo(void) {
363 // precipitation rendering code
364 void SGEnviro::DrawCone2(float baseRadius, float height, int slices, bool down, double rain_norm, double speed) {
367 sgVec3 min_light = {0.35, 0.35, 0.35};
368 sgAddVec3( light, fog_color, min_light );
369 float da = SG_PI * 2.0f / (float) slices;
370 // low number = faster
371 float speedf = 2.5f - speed / 200.0;
374 float lenf = 0.03f + speed / 2000.0;
377 float t = fmod((float) elapsed_time, speedf) / speedf;
382 glColor4f(1.0f, 0.7f, 0.7f, 0.9f);
384 int rainpos_indice = 0;
385 for( int i = 0 ; i < slices ; i++ ) {
386 float x = cos(angle) * baseRadius;
387 float y = sin(angle) * baseRadius;
389 sgVec3 dir = {x, -height, y};
391 // rain drops at 2 different speed to simulate depth
392 float t1 = (i & 1 ? t : t + t) + rainpos[rainpos_indice];
393 if(t1 > 1.0f) t1 -= 1.0f;
394 if(t1 > 1.0f) t1 -= 1.0f;
396 // distant raindrops are more transparent
397 float c = (i & 1 ? t1 * 0.5f : t1 * 0.9f);
398 glColor4f(c * light[0], c * light[1], c * light[2], c);
400 sgScaleVec3(p1, dir, t1);
401 // distant raindrops are shorter
402 float t2 = t1 + (i & 1 ? lenf : lenf+lenf);
403 sgScaleVec3(p2, dir, t2);
405 glVertex3f(p1[0], p1[1] + height, p1[2]);
406 glVertex3f(p2[0], p2[1] + height, p2[2]);
407 if( ++rainpos_indice >= MAX_RAIN_SLICE )
413 void SGEnviro::drawRain(double pitch, double roll, double heading, double speed, double rain_norm) {
415 glBindTexture(GL_TEXTURE_2D, 0);
417 glDisable(GL_DEPTH_TEST);
418 glShadeModel(GL_SMOOTH);
420 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
422 glDisable(GL_LIGHTING);
424 int slice_count = static_cast<int>(
425 (40.0 + rain_norm*150.0)* precipitation_density / 100.0);
432 // TODO:find the real view orientation, not the AC one
433 // the cone rotate with speed
434 angle = -pitch - angle;
435 glRotatef(angle, 1.0, 0.0, 0.0);
436 glRotatef(roll, 0.0, 1.0, 0.0);
437 glRotatef(heading, 0.0, 0.0, 1.0);
440 DrawCone2(15.0, 30.0, slice_count, true, rain_norm, speed);
441 // down cone (usually not visible)
442 if(angle > 0.0 || heading != 0.0)
443 DrawCone2(15.0, -30.0, slice_count, false, rain_norm, speed);
447 glEnable(GL_LIGHTING);
448 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
450 glEnable(GL_DEPTH_TEST);
454 void SGEnviro::set_soundMgr(SGSoundMgr *mgr) {
458 void SGEnviro::drawPrecipitation(double rain_norm, double snow_norm, double hail_norm, double pitch, double roll, double heading, double speed) {
459 if( precipitation_enable_state && rain_norm > 0.0)
460 if( precipitation_max_alt >= last_alt )
461 drawRain(pitch, roll, heading, speed, rain_norm);
465 SGLightning::SGLightning(double _lon, double _lat, double _alt) :
469 age(1.0 + sg_random() * 4.0),
472 // sequence_count = 1 + sg_random() * 5.0;
476 SGLightning::~SGLightning() {
479 // lightning rendering code
480 void SGLightning::lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize) {
486 sgSetVec3( dir, 0.0, -1.0, 0.0 );
488 sgSetVec3( dir, sg_random() - 0.5f, sg_random() - 0.5f, sg_random() - 0.5f);
489 sgNormaliseVec3(dir);
491 if( nb_tree >= MAX_LT_TREE_SEG )
494 lt_tree[nb_tree].depth = tree_nr;
496 lt_tree[nb_tree].pt = pt;
497 lt_tree[nb_tree].prev = -1;
501 while(nseg < nbseg && pt.y() > 0.0) {
502 int prev = nb_tree - 1;
505 if( energy * sg_random() > 0.8f )
506 lt_build_tree_branch(tree_nr + 1, pt, energy * 0.9f, nbseg == 50 ? 10 : static_cast<int>(nbseg * 0.4f), segsize * 0.7f);
508 if( nb_tree >= MAX_LT_TREE_SEG )
510 sgSetVec3(newdir, (sg_random() - 0.5f), (sg_random() - 0.5f) - (nbseg == 50 ? 0.5f : 0.0), (sg_random() - 0.5f));
511 sgNormaliseVec3(newdir);
512 sgAddVec3( dir, newdir);
513 sgNormaliseVec3(dir);
515 sgScaleVec3( scaleDir, dir, segsize * energy * 0.5f );
516 pt[PX] += scaleDir[0];
517 pt[PY] += scaleDir[1];
518 pt[PZ] += scaleDir[2];
520 lt_tree[nb_tree].depth = tree_nr;
521 lt_tree[nb_tree].pt = pt;
522 lt_tree[nb_tree].prev = prev;
527 void SGLightning::lt_build(void) {
533 lt_build_tree_branch(0, top, 1.0, 50, top[PY] / 8.0);
534 if( ! sgEnviro.soundMgr )
536 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
537 Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
538 double course = 0.0, dist = 0.0;
539 calc_gc_course_dist( dest, start, &course, &dist );
540 if( dist < 10000.0 && ! sgEnviro.snd_playing && (dist < sgEnviro.snd_dist || ! sgEnviro.snd_active) ) {
541 sgEnviro.snd_timer = 0.0;
542 sgEnviro.snd_wait = dist / 340;
543 sgEnviro.snd_dist = dist;
544 sgEnviro.snd_pos_lat = lat;
545 sgEnviro.snd_pos_lon = lon;
546 sgEnviro.snd_active = true;
547 sgEnviro.snd_playing = false;
552 void SGLightning::lt_Render(void) {
554 if( fmod(sgEnviro.elapsed_time*100.0, 100.0) > 50.0 )
555 flash = sg_random() * 0.75f + 0.25f;
556 float h = lt_tree[0].pt[PY];
557 sgVec4 col={0.62f, 0.83f, 1.0f, 1.0f};
561 {glBegin(GL_LINES); \
563 glVertex3f(lt_tree[n].pt[PX], lt_tree[n].pt[PZ], lt_tree[n].pt[PY]); \
564 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]); \
567 glDepthMask( GL_FALSE );
569 glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
570 glBindTexture(GL_TEXTURE_2D, 0);
572 glDisable(GL_LIGHTING);
575 sgMat4 modelview, tmp;
576 ssgGetModelviewMatrix( modelview );
577 sgCopyMat4( tmp, sgEnviro.transform );
578 sgPostMultMat4( tmp, modelview );
579 ssgLoadModelviewMatrix( tmp );
581 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
582 Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
583 double course = 0.0, dist = 0.0;
584 calc_gc_course_dist( dest, start, &course, &dist );
585 double ax = 0.0, ay = 0.0;
586 ax = cos(course) * dist;
587 ay = sin(course) * dist;
589 glTranslatef( ax, ay, -sgEnviro.last_alt );
591 sgEnviro.radarEcho.push_back( SGWxRadarEcho ( course, 0.0, 0.0, dist, age, true, 0 ) );
593 for( int n = 0 ; n < nb_tree ; n++ ) {
594 if( lt_tree[n].prev < 0 )
597 float t1 = sgLerp(0.5, 1.0, lt_tree[n].pt[PY] / h);
599 if( lt_tree[n].depth >= 2 ) {
601 sgScaleVec4(c, col, t1 * 0.6f);
604 if( lt_tree[n].depth == 0 ) {
606 sgScaleVec4(c, col, t1 * 0.5f);
610 sgScaleVec4(c, col, t1);
614 sgScaleVec4(c, col, t1 * 0.7f);
618 if( lt_tree[n].depth == 0 )
623 sgSetVec4(c, t1, t1, t1, t1);
629 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
631 glDepthMask( GL_TRUE );
633 glEnable(GL_LIGHTING);
636 void SGEnviro::addLightning(double lon, double lat, double alt) {
637 if( lightnings.size() > 10)
639 SGLightning *lt= new SGLightning(lon, lat, alt);
640 lightnings.push_back(lt);
643 void SGEnviro::drawLightning(void) {
644 list_of_lightning::iterator iLightning;
645 // play 'thunder' for lightning
648 // wait until sound has reached us
650 if( snd_timer >= snd_wait ) {
652 // compute relative position of lightning
653 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
654 Point3D dest( snd_pos_lon*SG_DEGREES_TO_RADIANS, snd_pos_lat*SG_DEGREES_TO_RADIANS, 0.0 );
655 double course = 0.0, dist = 0.0;
656 calc_gc_course_dist( dest, start, &course, &dist );
657 double ax = 0.0, ay = 0.0;
658 ax = cos(course) * dist;
659 ay = sin(course) * dist;
660 SGSharedPtr<SGSoundSample> snd = soundMgr->find("thunder");
662 ALfloat pos[3]={ax, ay, -sgEnviro.last_alt };
663 snd->set_source_pos(pos);
668 if( !soundMgr->is_playing("thunder") ) {
674 if( ! lightning_enable_state )
677 for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
679 if( sg_random() > 0.95f )
680 (*iLightning)->lt_build();
681 (*iLightning)->lt_Render();
682 (*iLightning)->age -= dt;
683 if( (*iLightning)->age < 0.0 ) {
684 delete (*iLightning);
685 lightnings.erase( iLightning );
693 void SGEnviro::setFOV( float w, float h ) {
698 void SGEnviro::getFOV( float &w, float &h ) {