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>
45 static ssgStateSelector *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
46 static bool state_initialized = false;
50 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
51 layer_root(new ssgRoot),
52 layer_transform(new ssgTransform),
54 texture_path(tex_path),
58 layer_transition(0.0),
59 layer_coverage(SG_CLOUD_CLEAR),
66 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
67 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
68 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
69 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
71 layer_root->addKid(layer_transform);
76 SGCloudLayer::~SGCloudLayer()
78 delete layer_root; // deletes layer_transform and layer as well
82 SGCloudLayer::getSpan_m () const
88 SGCloudLayer::setSpan_m (float span_m)
90 if (span_m != layer_span) {
97 SGCloudLayer::getElevation_m () const
103 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
105 layer_asl = elevation_m;
108 if (elevation_m > 4000)
109 setSpan_m( elevation_m * 10 );
116 SGCloudLayer::getThickness_m () const
118 return layer_thickness;
122 SGCloudLayer::setThickness_m (float thickness_m)
124 layer_thickness = thickness_m;
128 SGCloudLayer::getTransition_m () const
130 return layer_transition;
134 SGCloudLayer::setTransition_m (float transition_m)
136 layer_transition = transition_m;
139 SGCloudLayer::Coverage
140 SGCloudLayer::getCoverage () const
142 return layer_coverage;
146 SGCloudLayer::setCoverage (Coverage coverage)
148 if (coverage != layer_coverage) {
149 layer_coverage = coverage;
155 // build the cloud object
157 SGCloudLayer::rebuild()
159 // Initialize states and sizes if necessary.
160 if ( !state_initialized ) {
161 state_initialized = true;
163 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
166 ssgStateSelector *state_sel;
167 ssgSimpleState *state;
169 state_sel = new ssgStateSelector( 2 );
171 cloud_path.set(texture_path.str());
172 cloud_path.append("overcast.rgb");
173 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
174 cloud_path.set(texture_path.str());
175 cloud_path.append("overcast_top.rgb");
176 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
177 layer_states[SG_CLOUD_OVERCAST] = state_sel;
179 state_sel = new ssgStateSelector( 2 );
181 cloud_path.set(texture_path.str());
182 cloud_path.append("broken.rgba");
183 state = sgCloudMakeState(cloud_path.str());
184 state_sel->setStep( 0, state );
185 state_sel->setStep( 1, state );
186 layer_states[SG_CLOUD_BROKEN] = state_sel;
188 state_sel = new ssgStateSelector( 2 );
190 cloud_path.set(texture_path.str());
191 cloud_path.append("scattered.rgba");
192 state = sgCloudMakeState(cloud_path.str());
193 state_sel->setStep( 0, state );
194 state_sel->setStep( 1, state );
195 layer_states[SG_CLOUD_SCATTERED] = state_sel;
197 state_sel = new ssgStateSelector( 2 );
199 cloud_path.set(texture_path.str());
200 cloud_path.append("few.rgba");
201 state = sgCloudMakeState(cloud_path.str());
202 state_sel->setStep( 0, state );
203 state_sel->setStep( 1, state );
204 layer_states[SG_CLOUD_FEW] = state_sel;
206 state_sel = new ssgStateSelector( 2 );
208 cloud_path.set(texture_path.str());
209 cloud_path.append("cirrus.rgba");
210 state = sgCloudMakeState(cloud_path.str());
211 state_sel->setStep( 0, state );
212 state_sel->setStep( 1, state );
213 layer_states[SG_CLOUD_CIRRUS] = state_sel;
215 layer_states[SG_CLOUD_CLEAR] = 0;
219 last_lon = last_lat = -999.0f;
222 sgSetVec2( base, sg_random(), sg_random() );
224 // build the cloud layer
229 const float layer_scale = layer_span / scale;
230 const float mpi = SG_PI/4;
232 // caclculate the difference between a flat-earth model and
233 // a round earth model given the span and altutude ASL of
234 // the cloud layer. This is the difference in altitude between
235 // the top of the inverted bowl and the edge of the bowl.
236 // const float alt_diff = layer_asl * 0.8;
237 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
238 const float layer_angle = acos( 0.5*layer_span / layer_to_core);
239 const float border_to_core = layer_to_core * sin(layer_angle);
240 const float alt_diff = layer_to_core - border_to_core;
242 for (int i = 0; i < 4; i++)
244 if ( layer[i] != NULL ) {
245 layer_transform->removeKid(layer[i]); // automatic delete
248 vl[i] = new ssgVertexArray( 10 );
249 cl[i] = new ssgColourArray( 10 );
250 tl[i] = new ssgTexCoordArray( 10 );
253 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
254 alt_diff * (sin(i*mpi) - 2) );
256 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
258 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
261 vl[i]->add( vertex );
264 for (int j = 0; j < 4; j++)
266 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
267 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
269 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
270 base[1] + layer_scale * j/4 );
272 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
273 ( (j == 0) || (i == 3)) ?
274 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
277 vl[i]->add( vertex );
281 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
282 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
284 sgSetVec2( tc, base[0] + layer_scale * i/4,
285 base[1] + layer_scale * (j+1)/4 );
287 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
288 ((j == 3) || (i == 0)) ?
289 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
291 vl[i]->add( vertex );
295 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
296 alt_diff * (sin((i+1)*mpi) - 2) );
298 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
299 base[1] + layer_scale );
301 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
304 vl[i]->add( vertex );
307 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
308 layer_transform->addKid( layer[i] );
310 if ( layer_states[layer_coverage] != NULL ) {
311 layer[i]->setState( layer_states[layer_coverage] );
313 state_sel = layer_states[layer_coverage];
316 // force a repaint of the sky colors with arbitrary defaults
322 // repaint the cloud layer colors
323 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
326 for ( int i = 0; i < 4; i++ )
327 for ( int j = 0; j < 10; ++j ) {
328 color = cl[i]->get( j );
329 sgCopyVec3( color, fog_color );
336 // reposition the cloud layer at the specified origin and orientation
337 // lon specifies a rotation about the Z axis
338 // lat specifies a rotation about the new Y axis
339 // spin specifies a rotation about the new Z axis (and orients the
340 // sunrise/set effects
341 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
342 double alt, double dt )
347 // combine p and asl (meters) to get translation offset
349 sgCopyVec3( asl_offset, up );
350 sgNormalizeVec3( asl_offset );
351 if ( alt <= layer_asl ) {
352 sgScaleVec3( asl_offset, layer_asl );
354 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
356 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
357 // << "," << asl_offset[2] << endl;
358 sgAddVec3( asl_offset, p );
359 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
360 // << "," << asl_offset[2] << endl;
362 // Translate to zero elevation
363 // Point3D zero_elev = current_view.get_cur_zero_elev();
364 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
365 sgMakeTransMat4( T1, asl_offset );
367 // printf(" Translated to %.2f %.2f %.2f\n",
368 // zero_elev.x, zero_elev.y, zero_elev.z );
370 // Rotate to proper orientation
371 // printf(" lon = %.2f lat = %.2f\n",
372 // lon * SGD_RADIANS_TO_DEGREES,
373 // lat * SGD_RADIANS_TO_DEGREES);
374 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
375 sgSetVec3( axis, 0.0, 0.0, 1.0 );
376 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
378 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
380 sgSetVec3( axis, 0.0, 1.0, 0.0 );
381 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
385 sgCopyMat4( TRANSFORM, T1 );
386 sgPreMultMat4( TRANSFORM, LON );
387 sgPreMultMat4( TRANSFORM, LAT );
390 sgSetCoord( &layerpos, TRANSFORM );
392 layer_transform->setTransform( &layerpos );
394 // now calculate update texture coordinates
395 if ( last_lon < -900 ) {
400 double sp_dist = speed*dt;
402 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
403 Point3D start( last_lon, last_lat, 0.0 );
404 Point3D dest( lon, lat, 0.0 );
405 double course = 0.0, dist = 0.0;
407 calc_gc_course_dist( dest, start, &course, &dist );
408 // cout << "course = " << course << ", dist = " << dist << endl;
410 // if start and dest are too close together,
411 // calc_gc_course_dist() can return a course of "nan". If
412 // this happens, lets just use the last known good course.
413 // This is a hack, and it would probably be better to make
414 // calc_gc_course_dist() more robust.
415 if ( isnan(course) ) {
416 course = last_course;
418 last_course = course;
421 // calculate cloud movement due to external forces
422 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
425 ax = cos(course) * dist;
426 ay = sin(course) * dist;
430 bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
431 by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
435 double xoff = (ax + bx) / (2 * scale);
436 double yoff = (ay + by) / (2 * scale);
438 const float layer_scale = layer_span / scale;
440 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
444 base = tl[0]->get( 0 );
447 // the while loops can lead to *long* pauses if base[0] comes
448 // with a bogus value.
449 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
450 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
451 if ( base[0] > -10.0 && base[0] < 10.0 ) {
452 base[0] -= (int)base[0];
454 SG_LOG(SG_ASTRO, SG_DEBUG,
455 "Error: base = " << base[0] << "," << base[1] <<
456 " course = " << course << " dist = " << dist );
461 // the while loops can lead to *long* pauses if base[0] comes
462 // with a bogus value.
463 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
464 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
465 if ( base[1] > -10.0 && base[1] < 10.0 ) {
466 base[1] -= (int)base[1];
468 SG_LOG(SG_ASTRO, SG_ALERT,
469 "Error: base = " << base[0] << "," << base[1] <<
470 " course = " << course << " dist = " << dist );
474 // cout << "base = " << base[0] << "," << base[1] << endl;
476 for (int i = 0; i < 4; i++)
478 tc = tl[i]->get( 0 );
479 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
481 for (int j = 0; j < 4; j++)
483 tc = tl[i]->get( j*2+1 );
484 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
485 base[1] + layer_scale * j/4 );
487 tc = tl[i]->get( (j+1)*2 );
488 sgSetVec2( tc, base[0] + layer_scale * i/4,
489 base[1] + layer_scale * (j+1)/4 );
492 tc = tl[i]->get( 9 );
493 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
494 base[1] + layer_scale );
505 void SGCloudLayer::draw( bool top ) {
506 if ( layer_coverage != SG_CLOUD_CLEAR ) {
507 state_sel->selectStep( top ? 1 : 0 );
508 ssgCullAndDraw( layer_root );
513 // make an ssgSimpleState for a cloud layer given the named texture
514 ssgSimpleState *sgCloudMakeState( const string &path ) {
515 ssgSimpleState *state = new ssgSimpleState();
517 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
519 state->setTexture( (char *)path.c_str() );
520 state->setShadeModel( GL_SMOOTH );
521 state->disable( GL_LIGHTING );
522 state->disable( GL_CULL_FACE );
523 state->enable( GL_TEXTURE_2D );
524 state->enable( GL_COLOR_MATERIAL );
525 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
526 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
527 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
528 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
529 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
530 state->enable( GL_BLEND );
531 state->enable( GL_ALPHA_TEST );
532 state->setAlphaClamp( 0.01 );