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>
26 #include <simgear/constants.h>
27 #include <simgear/structure/SGReferenced.hxx>
28 #include <simgear/structure/SGSharedPtr.hxx>
29 #include <simgear/math/sg_random.h>
30 #include <simgear/math/sg_geodesy.hxx>
31 #include <simgear/sound/sample_group.hxx>
32 #include <simgear/scene/sky/cloudfield.hxx>
33 #include <simgear/scene/sky/newcloud.hxx>
34 #include <simgear/props/props.hxx>
35 #include "visual_enviro.hxx"
48 #define MAX_RAIN_SLICE 200
49 static float rainpos[MAX_RAIN_SLICE];
50 #define MAX_LT_TREE_SEG 400
52 #define DFL_MIN_LIGHT 0.35
53 sgVec3 SGEnviro::min_light = {DFL_MIN_LIGHT, DFL_MIN_LIGHT, DFL_MIN_LIGHT};
54 #define DFL_STREAK_BRIGHT_NEARMOST_LAYER 0.9
55 SGfloat SGEnviro::streak_bright_nearmost_layer = DFL_STREAK_BRIGHT_NEARMOST_LAYER;
56 #define DFL_STREAK_BRIGHT_FARMOST_LAYER 0.5
57 SGfloat SGEnviro::streak_bright_farmost_layer = DFL_STREAK_BRIGHT_FARMOST_LAYER;
58 #define DFL_STREAK_PERIOD_MAX 2.5
59 SGfloat SGEnviro::streak_period_max = DFL_STREAK_PERIOD_MAX;
60 #define DFL_STREAK_PERIOD_CHANGE_PER_KT 0.005
61 SGfloat SGEnviro::streak_period_change_per_kt = DFL_STREAK_PERIOD_CHANGE_PER_KT;
62 #define DFL_STREAK_PERIOD_MIN 1.0
63 SGfloat SGEnviro::streak_period_min = DFL_STREAK_PERIOD_MIN;
64 #define DFL_STREAK_LENGTH_MIN 0.03
65 SGfloat SGEnviro::streak_length_min = DFL_STREAK_LENGTH_MIN;
66 #define DFL_STREAK_LENGTH_CHANGE_PER_KT 0.0005
67 SGfloat SGEnviro::streak_length_change_per_kt = DFL_STREAK_LENGTH_CHANGE_PER_KT;
68 #define DFL_STREAK_LENGTH_MAX 0.1
69 SGfloat SGEnviro::streak_length_max = DFL_STREAK_LENGTH_MAX;
70 #define DFL_STREAK_COUNT_MIN 40
71 int SGEnviro::streak_count_min = DFL_STREAK_COUNT_MIN;
72 #define DFL_STREAK_COUNT_MAX 190
73 #if (DFL_STREAK_COUNT_MAX > MAX_RAIN_SLICE)
76 int SGEnviro::streak_count_max = DFL_STREAK_COUNT_MAX;
77 #define DFL_CONE_BASE_RADIUS 15.0
78 SGfloat SGEnviro::cone_base_radius = DFL_CONE_BASE_RADIUS;
79 #define DFL_CONE_HEIGHT 30.0
80 SGfloat SGEnviro::cone_height = DFL_CONE_HEIGHT;
83 void SGEnviro::config(const SGPropertyNode* n)
88 const float ml = n->getFloatValue("min-light", DFL_MIN_LIGHT);
89 sgSetVec3(min_light, ml, ml, ml);
91 streak_bright_nearmost_layer = n->getFloatValue(
92 "streak-brightness-nearmost-layer",
93 DFL_STREAK_BRIGHT_NEARMOST_LAYER);
94 streak_bright_farmost_layer = n->getFloatValue(
95 "streak-brightness-farmost-layer",
96 DFL_STREAK_BRIGHT_FARMOST_LAYER);
98 streak_period_max = n->getFloatValue(
100 DFL_STREAK_PERIOD_MAX);
101 streak_period_min = n->getFloatValue(
103 DFL_STREAK_PERIOD_MIN);
104 streak_period_change_per_kt = n->getFloatValue(
105 "streak-period-change-per-kt",
106 DFL_STREAK_PERIOD_CHANGE_PER_KT);
108 streak_length_max = n->getFloatValue(
110 DFL_STREAK_LENGTH_MAX);
111 streak_length_min = n->getFloatValue(
113 DFL_STREAK_LENGTH_MIN);
114 streak_length_change_per_kt = n->getFloatValue(
115 "streak-length-change-per-kt",
116 DFL_STREAK_LENGTH_CHANGE_PER_KT);
118 streak_count_min = n->getIntValue(
119 "streak-count-min", DFL_STREAK_COUNT_MIN);
120 streak_count_max = n->getIntValue(
121 "streak-count-max", DFL_STREAK_COUNT_MAX);
122 if (streak_count_max > MAX_RAIN_SLICE)
123 streak_count_max = MAX_RAIN_SLICE;
125 cone_base_radius = n->getFloatValue(
126 "cone-base-radius", DFL_CONE_BASE_RADIUS);
127 cone_height = n->getFloatValue("cone_height", DFL_CONE_HEIGHT);
132 * A class to render lightnings.
137 * Build a new lightning.
138 * The lightning has a limited life time. It will also play a thunder sounder once.
139 * @param lon lon longitude in degree
140 * @param lat lat latitude in degree
141 * @param alt asl of top of lightning
143 SGLightning(double lon, double lat, double alt);
145 void lt_Render(void);
147 void lt_build_tree_branch(int tree_nr, SGVec3d &start, float energy, int nbseg, float segsize);
149 // contains all the segments of the lightning
150 lt_tree_seg lt_tree[MAX_LT_TREE_SEG];
153 // position of lightning
154 double lon, lat, alt;
160 typedef vector<SGLightning *> list_of_lightning;
161 static list_of_lightning lightnings;
165 SGEnviro::SGEnviro() :
166 view_in_cloud(false),
167 precipitation_enable_state(true),
168 precipitation_density(100.0),
169 precipitation_max_alt(0.0),
170 turbulence_enable_state(false),
171 last_cloud_turbulence(0.0),
172 cloud_turbulence(0.0),
173 lightning_enable_state(false),
179 min_time_before_lt(0.0),
184 for(int i = 0; i < MAX_RAIN_SLICE ; i++)
185 rainpos[i] = sg_random();
186 radarEcho.reserve(100);
189 SGEnviro::~SGEnviro(void) {
190 if (sampleGroup) delete sampleGroup;
194 list_of_lightning::iterator iLightning;
195 for( iLightning = lightnings.begin() ; iLightning != lightnings.end() ; iLightning++ ) {
196 delete (*iLightning);
201 void SGEnviro::startOfFrame( sgVec3 p, sgVec3 up, double lon, double lat, double alt, double delta_time) {
204 view_in_cloud = false;
205 // ask the impostor cache to do some cleanup
206 last_cloud_turbulence = cloud_turbulence;
207 cloud_turbulence = 0.0;
208 elapsed_time += delta_time;
209 min_time_before_lt -= delta_time;
215 sgMakeTransMat4( T1, p );
217 sgSetVec3( axis, 0.0, 0.0, 1.0 );
218 sgMakeRotMat4( LON, lon, axis );
220 sgSetVec3( axis, 0.0, 1.0, 0.0 );
221 sgMakeRotMat4( LAT, 90.0 - lat, axis );
225 sgCopyMat4( TRANSFORM, T1 );
226 sgPreMultMat4( TRANSFORM, LON );
227 sgPreMultMat4( TRANSFORM, LAT );
230 sgSetCoord( &pos, TRANSFORM );
232 sgMakeCoordMat4( transform, &pos );
238 precipitation_max_alt = 400.0;
241 void SGEnviro::endOfFrame(void) {
244 double SGEnviro::get_cloud_turbulence(void) const {
245 return last_cloud_turbulence;
248 // this can be queried to add some turbulence for example
249 bool SGEnviro::is_view_in_cloud(void) const {
250 return view_in_cloud;
252 void SGEnviro::set_view_in_cloud(bool incloud) {
253 view_in_cloud = incloud;
256 bool SGEnviro::get_turbulence_enable_state(void) const {
257 return turbulence_enable_state;
260 void SGEnviro::set_turbulence_enable_state(bool enable) {
261 turbulence_enable_state = enable;
264 float SGEnviro::get_precipitation_density(void) const {
265 return precipitation_density;
267 bool SGEnviro::get_precipitation_enable_state(void) const {
268 return precipitation_enable_state;
271 void SGEnviro::set_precipitation_density(float density) {
272 precipitation_density = density;
274 void SGEnviro::set_precipitation_enable_state(bool enable) {
275 precipitation_enable_state = enable;
279 bool SGEnviro::get_lightning_enable_state(void) const {
280 return lightning_enable_state;
283 void SGEnviro::set_lightning_enable_state(bool enable) {
284 lightning_enable_state = enable;
290 void SGEnviro::setLight(sgVec4 adj_fog_color) {
293 sgCopyVec4( fog_color, adj_fog_color );
295 // ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
299 void SGEnviro::callback_cloud(float heading, float alt, float radius, int family, float dist, int cloudId) {
300 // send data to wx radar
301 // compute turbulence
302 // draw precipitation
304 // compute illumination
306 // http://www.pilotfriend.com/flight_training/weather/THUNDERSTORM%20HAZARDS1.htm
307 double turbulence = 0.0;
308 if( dist < radius * radius * 2.25f ) {
310 case SGNewCloud::CLFamilly_st:
313 case SGNewCloud::CLFamilly_ci:
314 case SGNewCloud::CLFamilly_cs:
315 case SGNewCloud::CLFamilly_cc:
316 case SGNewCloud::CLFamilly_ac:
317 case SGNewCloud::CLFamilly_as:
320 case SGNewCloud::CLFamilly_sc:
323 case SGNewCloud::CLFamilly_ns:
326 case SGNewCloud::CLFamilly_cu:
329 case SGNewCloud::CLFamilly_cb:
333 // full turbulence inside cloud, half in the vicinity
334 if( dist > radius * radius )
336 if( turbulence > cloud_turbulence )
337 cloud_turbulence = turbulence;
338 // we can do 'local' precipitations too
341 // convert to LWC for radar (experimental)
342 // http://www-das.uwyo.edu/~geerts/cwx/notes/chap08/moist_cloud.html
345 case SGNewCloud::CLFamilly_st:
348 case SGNewCloud::CLFamilly_cu:
351 case SGNewCloud::CLFamilly_cb:
354 case SGNewCloud::CLFamilly_sc:
357 case SGNewCloud::CLFamilly_ci:
361 case SGNewCloud::CLFamilly_cs:
362 case SGNewCloud::CLFamilly_cc:
363 case SGNewCloud::CLFamilly_ac:
364 case SGNewCloud::CLFamilly_as:
367 case SGNewCloud::CLFamilly_ns:
372 // add to the list for the wxRadar instrument
374 radarEcho.push_back( SGWxRadarEcho ( heading, alt, radius, dist, LWC, false, cloudId ) );
376 // NB:data valid only from cockpit view
378 // spawn a new lightning
379 if(lightning_enable_state && min_time_before_lt <= 0.0 && (family == SGNewCloud::CLFamilly_cb) &&
380 dist < 15000.0 * 15000.0 && sg_random() > 0.9f) {
383 orig.setlat(last_lat * SG_DEGREES_TO_RADIANS );
384 orig.setlon(last_lon * SG_DEGREES_TO_RADIANS );
387 dest = calc_gc_lon_lat(orig, heading, dist);
388 lon = dest.lon() * SG_RADIANS_TO_DEGREES;
389 lat = dest.lat() * SG_RADIANS_TO_DEGREES;
390 addLightning( lon, lat, alt );
393 min_time_before_lt = 5.0 + sg_random() * 30;
395 // min_time_before_lt = 5.0;
397 if( (alt - radius * 0.1) > precipitation_max_alt )
399 case SGNewCloud::CLFamilly_st:
400 case SGNewCloud::CLFamilly_cu:
401 case SGNewCloud::CLFamilly_cb:
402 case SGNewCloud::CLFamilly_ns:
403 case SGNewCloud::CLFamilly_sc:
404 precipitation_max_alt = alt - radius * 0.1;
411 list_of_SGWxRadarEcho *SGEnviro::get_radar_echo(void) {
415 // precipitation rendering code
416 void SGEnviro::DrawCone2(float baseRadius, float height, int slices, bool down, double rain_norm, double speed) {
420 sgAddVec3( light, fog_color, min_light );
421 float da = SG_PI * 2.0f / (float) slices;
422 // low number = faster
423 float speedf = streak_period_max - speed * streak_period_change_per_kt;
424 if( speedf < streak_period_min )
425 speedf = streak_period_min;
426 float lenf = streak_length_min + speed * streak_length_change_per_kt;
427 if( lenf > streak_length_max )
428 lenf = streak_length_max;
429 float t = fmod((float) elapsed_time, speedf) / speedf;
434 //glColor4f(1.0f, 0.7f, 0.7f, 0.9f); // XXX unneeded? overriden below
436 if (slices > MAX_RAIN_SLICE)
437 slices = MAX_RAIN_SLICE; // should never happen
438 for( int i = 0 ; i < slices ; i++ ) {
439 float x = cos(angle) * baseRadius;
440 float y = sin(angle) * baseRadius;
442 sgVec3 dir = {x, -height, y};
444 // rain drops at 2 different speed to simulate depth
445 float t1 = (i & 1 ? t : t + t) + rainpos[i];
446 if(t1 > 1.0f) t1 -= 1.0f;
447 if(t1 > 1.0f) t1 -= 1.0f;
449 // distant raindrops are more transparent
450 float c = t1 * (i & 1 ?
451 streak_bright_farmost_layer
452 : streak_bright_nearmost_layer);
453 glColor4f(c * light[0], c * light[1], c * light[2], c);
455 sgScaleVec3(p1, dir, t1);
456 // distant raindrops are shorter
457 float t2 = t1 + (i & 1 ? lenf : lenf+lenf);
458 sgScaleVec3(p2, dir, t2);
460 glVertex3f(p1[0], p1[1] + height, p1[2]);
461 glVertex3f(p2[0], p2[1] + height, p2[2]);
466 void SGEnviro::drawRain(double pitch, double roll, double heading, double hspeed, double rain_norm) {
471 static int debug_period = 0;
472 if (debug_period++ == 50) {
480 //" angle = " << angle
481 //<< " raindrop(KTS) = " << raindrop_speed_kts
487 glBindTexture(GL_TEXTURE_2D, 0);
489 glDisable(GL_DEPTH_TEST);
490 glShadeModel(GL_SMOOTH);
492 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
494 glDisable(GL_LIGHTING);
496 int slice_count = static_cast<int>(
497 (streak_count_min + rain_norm*(streak_count_max-streak_count_min))
498 * precipitation_density / 100.0);
500 // www.wonderquest.com/falling-raindrops.htm says that
501 // Raindrop terminal velocity is 5 to 20mph
502 // Rather than model it accurately (temp, pressure, diameter), and make it
503 // smaller than terminal when closer to the precipitation cloud base,
504 // we interpolate in the 5-20mph range according to rain_norm.
505 double raindrop_speed_kts
506 = (5.0 + rain_norm*15.0) * SG_MPH_TO_MPS * SG_MPS_TO_KT;
508 float angle = atanf(hspeed / raindrop_speed_kts) * SG_RADIANS_TO_DEGREES;
510 // the cone rotate with hspeed
511 angle = -pitch - angle;
512 glRotatef(roll, 0.0, 0.0, 1.0);
513 glRotatef(heading, 0.0, 1.0, 0.0);
514 glRotatef(angle, 1.0, 0.0, 0.0);
517 DrawCone2(cone_base_radius, cone_height,
518 slice_count, true, rain_norm, hspeed);
519 // down cone (usually not visible)
520 if(angle > 0.0 || heading != 0.0)
521 DrawCone2(cone_base_radius, -cone_height,
522 slice_count, false, rain_norm, hspeed);
526 glEnable(GL_LIGHTING);
527 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
529 glEnable(GL_DEPTH_TEST);
533 void SGEnviro::set_sampleGroup(SGSampleGroup *sgr) {
537 void SGEnviro::drawPrecipitation(double rain_norm, double snow_norm, double hail_norm, double pitch, double roll, double heading, double hspeed) {
540 if( precipitation_enable_state && rain_norm > 0.0)
541 if( precipitation_max_alt >= last_alt )
542 drawRain(pitch, roll, heading, hspeed, rain_norm);
546 SGLightning::SGLightning(double _lon, double _lat, double _alt) :
551 age(1.0 + sg_random() * 4.0)
553 // sequence_count = 1 + sg_random() * 5.0;
557 SGLightning::~SGLightning() {
560 // lightning rendering code
561 void SGLightning::lt_build_tree_branch(int tree_nr, SGVec3d &start, float energy, int nbseg, float segsize) {
566 void SGLightning::lt_build(void) {
573 void SGLightning::lt_Render(void) {
578 void SGEnviro::addLightning(double lon, double lat, double alt) {
581 if( lightnings.size() > 10)
583 SGLightning *lt= new SGLightning(lon, lat, alt);
584 lightnings.push_back(lt);
587 void SGEnviro::drawLightning(void) {
593 void SGEnviro::setFOV( float w, float h ) {
598 void SGEnviro::getFOV( float &w, float &h ) {