1 // cloud.cxx -- model a single cloud layer
3 // Written by Curtis Olson, started June 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
7 // This program is distributed in the hope that it will be useful, but
8 // WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // General Public License for more details.
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <simgear/compiler.h>
24 // #if defined (__APPLE__)
25 // // any C++ header file undefines isinf and isnan
26 // // so this should be included before <iostream>
27 // inline int (isinf)(double r) { return isinf(r); }
28 // inline int (isnan)(double r) { return isnan(r); }
31 // #include STL_IOSTREAM
36 #include <simgear/math/point3d.hxx>
37 #include <simgear/math/polar3d.hxx>
38 #include <simgear/math/sg_random.h>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/misc/sg_path.hxx>
44 #if defined(_MSC_VER) || defined(__MINGW32__)
45 #define isnan(x) _isnan(x)
49 static ssgStateSelector *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
50 static bool state_initialized = false;
54 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
55 layer_root(new ssgRoot),
56 layer_transform(new ssgTransform),
58 texture_path(tex_path),
62 layer_transition(0.0),
63 layer_coverage(SG_CLOUD_CLEAR),
70 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
71 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
72 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
73 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
75 layer_root->addKid(layer_transform);
80 SGCloudLayer::~SGCloudLayer()
82 delete layer_root; // deletes layer_transform and layer as well
86 SGCloudLayer::getSpan_m () const
92 SGCloudLayer::setSpan_m (float span_m)
94 if (span_m != layer_span) {
101 SGCloudLayer::getElevation_m () const
107 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
109 layer_asl = elevation_m;
112 if (elevation_m > 4000)
113 setSpan_m( elevation_m * 10 );
120 SGCloudLayer::getThickness_m () const
122 return layer_thickness;
126 SGCloudLayer::setThickness_m (float thickness_m)
128 layer_thickness = thickness_m;
132 SGCloudLayer::getTransition_m () const
134 return layer_transition;
138 SGCloudLayer::setTransition_m (float transition_m)
140 layer_transition = transition_m;
143 SGCloudLayer::Coverage
144 SGCloudLayer::getCoverage () const
146 return layer_coverage;
150 SGCloudLayer::setCoverage (Coverage coverage)
152 if (coverage != layer_coverage) {
153 layer_coverage = coverage;
159 // build the cloud object
161 SGCloudLayer::rebuild()
163 // Initialize states and sizes if necessary.
164 if ( !state_initialized ) {
165 state_initialized = true;
167 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
170 ssgStateSelector *state_sel;
171 ssgSimpleState *state;
173 state_sel = new ssgStateSelector( 2 );
175 cloud_path.set(texture_path.str());
176 cloud_path.append("overcast.rgb");
177 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
178 cloud_path.set(texture_path.str());
179 cloud_path.append("overcast_top.rgb");
180 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
181 layer_states[SG_CLOUD_OVERCAST] = state_sel;
183 state_sel = new ssgStateSelector( 2 );
185 cloud_path.set(texture_path.str());
186 cloud_path.append("broken.rgba");
187 state = sgCloudMakeState(cloud_path.str());
188 state_sel->setStep( 0, state );
189 state_sel->setStep( 1, state );
190 layer_states[SG_CLOUD_BROKEN] = state_sel;
192 state_sel = new ssgStateSelector( 2 );
194 cloud_path.set(texture_path.str());
195 cloud_path.append("scattered.rgba");
196 state = sgCloudMakeState(cloud_path.str());
197 state_sel->setStep( 0, state );
198 state_sel->setStep( 1, state );
199 layer_states[SG_CLOUD_SCATTERED] = state_sel;
201 state_sel = new ssgStateSelector( 2 );
203 cloud_path.set(texture_path.str());
204 cloud_path.append("few.rgba");
205 state = sgCloudMakeState(cloud_path.str());
206 state_sel->setStep( 0, state );
207 state_sel->setStep( 1, state );
208 layer_states[SG_CLOUD_FEW] = state_sel;
210 state_sel = new ssgStateSelector( 2 );
212 cloud_path.set(texture_path.str());
213 cloud_path.append("cirrus.rgba");
214 state = sgCloudMakeState(cloud_path.str());
215 state_sel->setStep( 0, state );
216 state_sel->setStep( 1, state );
217 layer_states[SG_CLOUD_CIRRUS] = state_sel;
219 layer_states[SG_CLOUD_CLEAR] = 0;
223 last_lon = last_lat = -999.0f;
226 sgSetVec2( base, sg_random(), sg_random() );
228 // build the cloud layer
233 const float layer_scale = layer_span / scale;
234 const float mpi = SG_PI/4;
236 // caclculate the difference between a flat-earth model and
237 // a round earth model given the span and altutude ASL of
238 // the cloud layer. This is the difference in altitude between
239 // the top of the inverted bowl and the edge of the bowl.
240 // const float alt_diff = layer_asl * 0.8;
241 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
242 const float layer_angle = acos( 0.5*layer_span / layer_to_core);
243 const float border_to_core = layer_to_core * sin(layer_angle);
244 const float alt_diff = layer_to_core - border_to_core;
246 for (int i = 0; i < 4; i++)
248 if ( layer[i] != NULL ) {
249 layer_transform->removeKid(layer[i]); // automatic delete
252 vl[i] = new ssgVertexArray( 10 );
253 cl[i] = new ssgColourArray( 10 );
254 tl[i] = new ssgTexCoordArray( 10 );
257 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
258 alt_diff * (sin(i*mpi) - 2) );
260 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
262 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
265 vl[i]->add( vertex );
268 for (int j = 0; j < 4; j++)
270 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
271 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
273 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
274 base[1] + layer_scale * j/4 );
276 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
277 ( (j == 0) || (i == 3)) ?
278 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
281 vl[i]->add( vertex );
285 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
286 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
288 sgSetVec2( tc, base[0] + layer_scale * i/4,
289 base[1] + layer_scale * (j+1)/4 );
291 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
292 ((j == 3) || (i == 0)) ?
293 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
295 vl[i]->add( vertex );
299 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
300 alt_diff * (sin((i+1)*mpi) - 2) );
302 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
303 base[1] + layer_scale );
305 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
308 vl[i]->add( vertex );
311 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
312 layer_transform->addKid( layer[i] );
314 if ( layer_states[layer_coverage] != NULL ) {
315 layer[i]->setState( layer_states[layer_coverage] );
317 state_sel = layer_states[layer_coverage];
320 // force a repaint of the sky colors with arbitrary defaults
326 // repaint the cloud layer colors
327 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
330 for ( int i = 0; i < 4; i++ )
331 for ( int j = 0; j < 10; ++j ) {
332 color = cl[i]->get( j );
333 sgCopyVec3( color, fog_color );
340 // reposition the cloud layer at the specified origin and orientation
341 // lon specifies a rotation about the Z axis
342 // lat specifies a rotation about the new Y axis
343 // spin specifies a rotation about the new Z axis (and orients the
344 // sunrise/set effects
345 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
346 double alt, double dt )
351 // combine p and asl (meters) to get translation offset
353 sgCopyVec3( asl_offset, up );
354 sgNormalizeVec3( asl_offset );
355 if ( alt <= layer_asl ) {
356 sgScaleVec3( asl_offset, layer_asl );
358 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
360 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
361 // << "," << asl_offset[2] << endl;
362 sgAddVec3( asl_offset, p );
363 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
364 // << "," << asl_offset[2] << endl;
366 // Translate to zero elevation
367 // Point3D zero_elev = current_view.get_cur_zero_elev();
368 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
369 sgMakeTransMat4( T1, asl_offset );
371 // printf(" Translated to %.2f %.2f %.2f\n",
372 // zero_elev.x, zero_elev.y, zero_elev.z );
374 // Rotate to proper orientation
375 // printf(" lon = %.2f lat = %.2f\n",
376 // lon * SGD_RADIANS_TO_DEGREES,
377 // lat * SGD_RADIANS_TO_DEGREES);
378 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
379 sgSetVec3( axis, 0.0, 0.0, 1.0 );
380 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
382 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
384 sgSetVec3( axis, 0.0, 1.0, 0.0 );
385 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
389 sgCopyMat4( TRANSFORM, T1 );
390 sgPreMultMat4( TRANSFORM, LON );
391 sgPreMultMat4( TRANSFORM, LAT );
394 sgSetCoord( &layerpos, TRANSFORM );
396 layer_transform->setTransform( &layerpos );
398 // now calculate update texture coordinates
399 if ( last_lon < -900 ) {
404 double sp_dist = speed*dt;
406 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
407 Point3D start( last_lon, last_lat, 0.0 );
408 Point3D dest( lon, lat, 0.0 );
409 double course = 0.0, dist = 0.0;
411 calc_gc_course_dist( dest, start, &course, &dist );
412 // cout << "course = " << course << ", dist = " << dist << endl;
414 // if start and dest are too close together,
415 // calc_gc_course_dist() can return a course of "nan". If
416 // this happens, lets just use the last known good course.
417 // This is a hack, and it would probably be better to make
418 // calc_gc_course_dist() more robust.
419 if ( isnan(course) ) {
420 course = last_course;
422 last_course = course;
425 // calculate cloud movement due to external forces
426 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
429 ax = cos(course) * dist;
430 ay = sin(course) * dist;
434 bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
435 by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
439 double xoff = (ax + bx) / (2 * scale);
440 double yoff = (ay + by) / (2 * scale);
442 const float layer_scale = layer_span / scale;
444 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
448 base = tl[0]->get( 0 );
451 // the while loops can lead to *long* pauses if base[0] comes
452 // with a bogus value.
453 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
454 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
455 if ( base[0] > -10.0 && base[0] < 10.0 ) {
456 base[0] -= (int)base[0];
458 SG_LOG(SG_ASTRO, SG_DEBUG,
459 "Error: base = " << base[0] << "," << base[1] <<
460 " course = " << course << " dist = " << dist );
465 // the while loops can lead to *long* pauses if base[0] comes
466 // with a bogus value.
467 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
468 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
469 if ( base[1] > -10.0 && base[1] < 10.0 ) {
470 base[1] -= (int)base[1];
472 SG_LOG(SG_ASTRO, SG_ALERT,
473 "Error: base = " << base[0] << "," << base[1] <<
474 " course = " << course << " dist = " << dist );
478 // cout << "base = " << base[0] << "," << base[1] << endl;
480 for (int i = 0; i < 4; i++)
482 tc = tl[i]->get( 0 );
483 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
485 for (int j = 0; j < 4; j++)
487 tc = tl[i]->get( j*2+1 );
488 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
489 base[1] + layer_scale * j/4 );
491 tc = tl[i]->get( (j+1)*2 );
492 sgSetVec2( tc, base[0] + layer_scale * i/4,
493 base[1] + layer_scale * (j+1)/4 );
496 tc = tl[i]->get( 9 );
497 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
498 base[1] + layer_scale );
509 void SGCloudLayer::draw( bool top ) {
510 if ( layer_coverage != SG_CLOUD_CLEAR ) {
511 state_sel->selectStep( top ? 1 : 0 );
512 ssgCullAndDraw( layer_root );
517 // make an ssgSimpleState for a cloud layer given the named texture
518 ssgSimpleState *sgCloudMakeState( const string &path ) {
519 ssgSimpleState *state = new ssgSimpleState();
521 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
523 state->setTexture( (char *)path.c_str() );
524 state->setShadeModel( GL_SMOOTH );
525 state->disable( GL_LIGHTING );
526 state->disable( GL_CULL_FACE );
527 state->enable( GL_TEXTURE_2D );
528 state->enable( GL_COLOR_MATERIAL );
529 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
530 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
531 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
532 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
533 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
534 state->enable( GL_BLEND );
535 state->enable( GL_ALPHA_TEST );
536 state->setAlphaClamp( 0.01 );