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 precipitation_enable_state(true),
92 precipitation_density(100.0),
93 precipitation_max_alt(0.0),
94 turbulence_enable_state(false),
95 last_cloud_turbulence(0.0),
96 cloud_turbulence(0.0),
97 lightning_enable_state(false),
103 min_time_before_lt(0.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);
427 // www.wonderquest.com/falling-raindrops.htm says that
428 // Raindrop terminal velocity is 5 to 20mph
429 // Rather than model it accurately (temp, pressure, diameter), and make it
430 // smaller than terminal when closer to the precipitation cloud base,
431 // we interpolate in the 5-20mph range according to rain_norm.
432 double raindrop_speed_kts
433 = (5.0 + rain_norm*15.0) * SG_MPH_TO_MPS * SG_MPS_TO_KT;
435 float angle = atanf(speed / raindrop_speed_kts) * SG_RADIANS_TO_DEGREES;
436 // We assume that the speed is HORIZONTAL airspeed here!!! XXX
437 // DOCUMENT THE CHANGE IN THE PARAMETER speed MEANING BY RENAMING IT!!!
439 // the cone rotate with speed
440 angle = -pitch - angle;
441 glRotatef(heading, 0.0, 1.0, 0.0);
442 glRotatef(roll, 0.0, 0.0, 1.0);
443 glRotatef(angle, 1.0, 0.0, 0.0);
446 DrawCone2(15.0, 30.0, slice_count, true, rain_norm, speed);
447 // down cone (usually not visible)
448 if(angle > 0.0 || heading != 0.0)
449 DrawCone2(15.0, -30.0, slice_count, false, rain_norm, speed);
453 glEnable(GL_LIGHTING);
454 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
456 glEnable(GL_DEPTH_TEST);
460 void SGEnviro::set_soundMgr(SGSoundMgr *mgr) {
464 void SGEnviro::drawPrecipitation(double rain_norm, double snow_norm, double hail_norm, double pitch, double roll, double heading, double speed) {
465 if( precipitation_enable_state && rain_norm > 0.0)
466 if( precipitation_max_alt >= last_alt )
467 drawRain(pitch, roll, heading, speed, rain_norm);
471 SGLightning::SGLightning(double _lon, double _lat, double _alt) :
476 age(1.0 + sg_random() * 4.0)
478 // sequence_count = 1 + sg_random() * 5.0;
482 SGLightning::~SGLightning() {
485 // lightning rendering code
486 void SGLightning::lt_build_tree_branch(int tree_nr, Point3D &start, float energy, int nbseg, float segsize) {
492 sgSetVec3( dir, 0.0, -1.0, 0.0 );
494 sgSetVec3( dir, sg_random() - 0.5f, sg_random() - 0.5f, sg_random() - 0.5f);
495 sgNormaliseVec3(dir);
497 if( nb_tree >= MAX_LT_TREE_SEG )
500 lt_tree[nb_tree].depth = tree_nr;
502 lt_tree[nb_tree].pt = pt;
503 lt_tree[nb_tree].prev = -1;
507 while(nseg < nbseg && pt.y() > 0.0) {
508 int prev = nb_tree - 1;
511 if( energy * sg_random() > 0.8f )
512 lt_build_tree_branch(tree_nr + 1, pt, energy * 0.9f, nbseg == 50 ? 10 : static_cast<int>(nbseg * 0.4f), segsize * 0.7f);
514 if( nb_tree >= MAX_LT_TREE_SEG )
516 sgSetVec3(newdir, (sg_random() - 0.5f), (sg_random() - 0.5f) - (nbseg == 50 ? 0.5f : 0.0), (sg_random() - 0.5f));
517 sgNormaliseVec3(newdir);
518 sgAddVec3( dir, newdir);
519 sgNormaliseVec3(dir);
521 sgScaleVec3( scaleDir, dir, segsize * energy * 0.5f );
522 pt[PX] += scaleDir[0];
523 pt[PY] += scaleDir[1];
524 pt[PZ] += scaleDir[2];
526 lt_tree[nb_tree].depth = tree_nr;
527 lt_tree[nb_tree].pt = pt;
528 lt_tree[nb_tree].prev = prev;
533 void SGLightning::lt_build(void) {
539 lt_build_tree_branch(0, top, 1.0, 50, top[PY] / 8.0);
540 if( ! sgEnviro.soundMgr )
542 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
543 Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
544 double course = 0.0, dist = 0.0;
545 calc_gc_course_dist( dest, start, &course, &dist );
546 if( dist < 10000.0 && ! sgEnviro.snd_playing && (dist < sgEnviro.snd_dist || ! sgEnviro.snd_active) ) {
547 sgEnviro.snd_timer = 0.0;
548 sgEnviro.snd_wait = dist / 340;
549 sgEnviro.snd_dist = dist;
550 sgEnviro.snd_pos_lat = lat;
551 sgEnviro.snd_pos_lon = lon;
552 sgEnviro.snd_active = true;
553 sgEnviro.snd_playing = false;
558 void SGLightning::lt_Render(void) {
560 if( fmod(sgEnviro.elapsed_time*100.0, 100.0) > 50.0 )
561 flash = sg_random() * 0.75f + 0.25f;
562 float h = lt_tree[0].pt[PY];
563 sgVec4 col={0.62f, 0.83f, 1.0f, 1.0f};
567 {glColorMaterial(GL_FRONT, GL_EMISSION); \
568 glDisable(GL_LINE_SMOOTH); glBegin(GL_LINES); \
570 glVertex3f(lt_tree[n].pt[PX], lt_tree[n].pt[PZ], lt_tree[n].pt[PY]); \
571 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]); \
572 glEnd(); glEnable(GL_LINE_SMOOTH);}
574 glDepthMask( GL_FALSE );
576 glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
577 glBindTexture(GL_TEXTURE_2D, 0);
579 glDisable(GL_LIGHTING);
582 sgMat4 modelview, tmp;
583 ssgGetModelviewMatrix( modelview );
584 sgCopyMat4( tmp, sgEnviro.transform );
585 sgPostMultMat4( tmp, modelview );
586 ssgLoadModelviewMatrix( tmp );
588 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
589 Point3D dest( lon*SG_DEGREES_TO_RADIANS, lat*SG_DEGREES_TO_RADIANS, 0.0 );
590 double course = 0.0, dist = 0.0;
591 calc_gc_course_dist( dest, start, &course, &dist );
592 double ax = 0.0, ay = 0.0;
593 ax = cos(course) * dist;
594 ay = sin(course) * dist;
596 glTranslatef( ax, ay, -sgEnviro.last_alt );
598 sgEnviro.radarEcho.push_back( SGWxRadarEcho ( course, 0.0, 0.0, dist, age, true, 0 ) );
600 for( int n = 0 ; n < nb_tree ; n++ ) {
601 if( lt_tree[n].prev < 0 )
604 float t1 = sgLerp(0.5, 1.0, lt_tree[n].pt[PY] / h);
606 if( lt_tree[n].depth >= 2 ) {
608 sgScaleVec4(c, col, t1 * 0.6f);
611 if( lt_tree[n].depth == 0 ) {
613 sgScaleVec4(c, col, t1 * 0.5f);
617 sgScaleVec4(c, col, t1);
621 sgScaleVec4(c, col, t1 * 0.7f);
625 if( lt_tree[n].depth == 0 )
630 sgSetVec4(c, t1, t1, t1, t1);
636 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
638 glDepthMask( GL_TRUE );
640 glEnable(GL_LIGHTING);
643 void SGEnviro::addLightning(double lon, double lat, double alt) {
644 if( lightnings.size() > 10)
646 SGLightning *lt= new SGLightning(lon, lat, alt);
647 lightnings.push_back(lt);
650 void SGEnviro::drawLightning(void) {
651 list_of_lightning::iterator iLightning;
652 // play 'thunder' for lightning
655 // wait until sound has reached us
657 if( snd_timer >= snd_wait ) {
659 // compute relative position of lightning
660 Point3D start( sgEnviro.last_lon*SG_DEGREES_TO_RADIANS, sgEnviro.last_lat*SG_DEGREES_TO_RADIANS, 0.0 );
661 Point3D dest( snd_pos_lon*SG_DEGREES_TO_RADIANS, snd_pos_lat*SG_DEGREES_TO_RADIANS, 0.0 );
662 double course = 0.0, dist = 0.0;
663 calc_gc_course_dist( dest, start, &course, &dist );
664 double ax = 0.0, ay = 0.0;
665 ax = cos(course) * dist;
666 ay = sin(course) * dist;
667 SGSharedPtr<SGSoundSample> snd = soundMgr->find("thunder");
669 ALfloat pos[3]={ax, ay, -sgEnviro.last_alt };
670 snd->set_source_pos(pos);
675 if( !soundMgr->is_playing("thunder") ) {
681 if( ! lightning_enable_state )
684 for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
686 if( sg_random() > 0.95f )
687 (*iLightning)->lt_build();
688 (*iLightning)->lt_Render();
689 (*iLightning)->age -= dt;
690 if( (*iLightning)->age < 0.0 ) {
691 delete (*iLightning);
692 lightnings.erase( iLightning );
700 void SGEnviro::setFOV( float w, float h ) {
705 void SGEnviro::getFOV( float &w, float &h ) {