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)(Real r) { return isinf(r); }
28 // inline int (isnan)(Real 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 ssgSimpleState *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),
53 texture_path(tex_path),
57 layer_transition(0.0),
58 layer_coverage(SG_CLOUD_CLEAR),
65 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
66 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
67 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
68 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
70 layer_root->addKid(layer_transform);
75 SGCloudLayer::~SGCloudLayer()
77 delete layer_root; // deletes layer_transform and layer as well
81 SGCloudLayer::getSpan_m () const
87 SGCloudLayer::setSpan_m (float span_m)
89 if (span_m != layer_span) {
96 SGCloudLayer::getElevation_m () const
102 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
104 layer_asl = elevation_m;
107 if (elevation_m > 4000)
108 setSpan_m( elevation_m * 10 );
115 SGCloudLayer::getThickness_m () const
117 return layer_thickness;
121 SGCloudLayer::setThickness_m (float thickness_m)
123 layer_thickness = thickness_m;
127 SGCloudLayer::getTransition_m () const
129 return layer_transition;
133 SGCloudLayer::setTransition_m (float transition_m)
135 layer_transition = transition_m;
138 SGCloudLayer::Coverage
139 SGCloudLayer::getCoverage () const
141 return layer_coverage;
145 SGCloudLayer::setCoverage (Coverage coverage)
147 if (coverage != layer_coverage) {
148 layer_coverage = coverage;
154 // build the cloud object
156 SGCloudLayer::rebuild()
158 // Initialize states and sizes if necessary.
159 if ( !state_initialized ) {
160 state_initialized = true;
162 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
166 cloud_path.set(texture_path.str());
167 cloud_path.append("overcast.rgb");
168 layer_states[SG_CLOUD_OVERCAST] = sgCloudMakeState(cloud_path.str());
170 cloud_path.set(texture_path.str());
171 cloud_path.append("broken.rgba");
172 layer_states[SG_CLOUD_BROKEN]
173 = sgCloudMakeState(cloud_path.str());
175 cloud_path.set(texture_path.str());
176 cloud_path.append("scattered.rgba");
177 layer_states[SG_CLOUD_SCATTERED]
178 = sgCloudMakeState(cloud_path.str());
180 cloud_path.set(texture_path.str());
181 cloud_path.append("few.rgba");
182 layer_states[SG_CLOUD_FEW]
183 = sgCloudMakeState(cloud_path.str());
185 cloud_path.set(texture_path.str());
186 cloud_path.append("cirrus.rgba");
187 layer_states[SG_CLOUD_CIRRUS]
188 = sgCloudMakeState(cloud_path.str());
190 layer_states[SG_CLOUD_CLEAR] = 0;
194 last_lon = last_lat = -999.0f;
197 sgSetVec2( base, sg_random(), sg_random() );
199 // build the cloud layer
204 const float layer_scale = layer_span / scale;
205 const float mpi = SG_PI/4;
206 const float alt_diff = layer_asl * 0.8;
208 for (int i = 0; i < 4; i++)
210 if ( layer[i] != NULL ) {
211 layer_transform->removeKid(layer[i]); // automatic delete
214 vl[i] = new ssgVertexArray( 10 );
215 cl[i] = new ssgColourArray( 10 );
216 tl[i] = new ssgTexCoordArray( 10 );
219 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
220 alt_diff * (sin(i*mpi) - 2) );
222 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
224 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
227 vl[i]->add( vertex );
230 for (int j = 0; j < 4; j++)
232 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
233 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
235 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
236 base[1] + layer_scale * j/4 );
238 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
239 ( (j == 0) || (i == 3)) ?
240 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
243 vl[i]->add( vertex );
247 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
248 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
250 sgSetVec2( tc, base[0] + layer_scale * i/4,
251 base[1] + layer_scale * (j+1)/4 );
253 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
254 ((j == 3) || (i == 0)) ?
255 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
257 vl[i]->add( vertex );
261 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
262 alt_diff * (sin((i+1)*mpi) - 2) );
264 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
265 base[1] + layer_scale );
267 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
270 vl[i]->add( vertex );
273 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
274 layer_transform->addKid( layer[i] );
276 if ( layer_states[layer_coverage] != NULL ) {
277 layer[i]->setState( layer_states[layer_coverage] );
281 // force a repaint of the sky colors with arbitrary defaults
287 // repaint the cloud layer colors
288 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
291 for ( int i = 0; i < 4; i++ )
292 for ( int j = 0; j < 10; ++j ) {
293 color = cl[i]->get( j );
294 sgCopyVec3( color, fog_color );
301 // reposition the cloud layer at the specified origin and orientation
302 // lon specifies a rotation about the Z axis
303 // lat specifies a rotation about the new Y axis
304 // spin specifies a rotation about the new Z axis (and orients the
305 // sunrise/set effects
306 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
307 double alt, double dt )
312 // combine p and asl (meters) to get translation offset
314 sgCopyVec3( asl_offset, up );
315 sgNormalizeVec3( asl_offset );
316 if ( alt <= layer_asl ) {
317 sgScaleVec3( asl_offset, layer_asl );
319 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
321 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
322 // << "," << asl_offset[2] << endl;
323 sgAddVec3( asl_offset, p );
324 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
325 // << "," << asl_offset[2] << endl;
327 // Translate to zero elevation
328 // Point3D zero_elev = current_view.get_cur_zero_elev();
329 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
330 sgMakeTransMat4( T1, asl_offset );
332 // printf(" Translated to %.2f %.2f %.2f\n",
333 // zero_elev.x, zero_elev.y, zero_elev.z );
335 // Rotate to proper orientation
336 // printf(" lon = %.2f lat = %.2f\n",
337 // lon * SGD_RADIANS_TO_DEGREES,
338 // lat * SGD_RADIANS_TO_DEGREES);
339 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
340 sgSetVec3( axis, 0.0, 0.0, 1.0 );
341 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
343 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
345 sgSetVec3( axis, 0.0, 1.0, 0.0 );
346 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
350 sgCopyMat4( TRANSFORM, T1 );
351 sgPreMultMat4( TRANSFORM, LON );
352 sgPreMultMat4( TRANSFORM, LAT );
355 sgSetCoord( &layerpos, TRANSFORM );
357 layer_transform->setTransform( &layerpos );
359 // now calculate update texture coordinates
360 if ( last_lon < -900 ) {
365 double sp_dist = speed*dt;
367 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
368 Point3D start( last_lon, last_lat, 0.0 );
369 Point3D dest( lon, lat, 0.0 );
370 double course = 0.0, dist = 0.0;
372 calc_gc_course_dist( dest, start, &course, &dist );
373 // cout << "course = " << course << ", dist = " << dist << endl;
375 // if start and dest are too close together,
376 // calc_gc_course_dist() can return a course of "nan". If
377 // this happens, lets just use the last known good course.
378 // This is a hack, and it would probably be better to make
379 // calc_gc_course_dist() more robust.
380 if ( isnan(course) ) {
381 course = last_course;
383 last_course = course;
386 // calculate cloud movement due to external forces
387 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
390 ax = cos(course) * dist;
391 ay = sin(course) * dist;
395 bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
396 by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
400 double xoff = (ax + bx) / (2 * scale);
401 double yoff = (ay + by) / (2 * scale);
403 const float layer_scale = layer_span / scale;
405 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
409 base = tl[0]->get( 0 );
412 // the while loops can lead to *long* pauses if base[0] comes
413 // with a bogus value.
414 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
415 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
416 if ( base[0] > -10.0 && base[0] < 10.0 ) {
417 base[0] -= (int)base[0];
419 SG_LOG(SG_ASTRO, SG_DEBUG,
420 "Error: base = " << base[0] << "," << base[1] <<
421 " course = " << course << " dist = " << dist );
426 // the while loops can lead to *long* pauses if base[0] comes
427 // with a bogus value.
428 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
429 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
430 if ( base[1] > -10.0 && base[1] < 10.0 ) {
431 base[1] -= (int)base[1];
433 SG_LOG(SG_ASTRO, SG_ALERT,
434 "Error: base = " << base[0] << "," << base[1] <<
435 " course = " << course << " dist = " << dist );
439 // cout << "base = " << base[0] << "," << base[1] << endl;
441 for (int i = 0; i < 4; i++)
443 tc = tl[i]->get( 0 );
444 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
446 for (int j = 0; j < 4; j++)
448 tc = tl[i]->get( j*2+1 );
449 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
450 base[1] + layer_scale * j/4 );
452 tc = tl[i]->get( (j+1)*2 );
453 sgSetVec2( tc, base[0] + layer_scale * i/4,
454 base[1] + layer_scale * (j+1)/4 );
457 tc = tl[i]->get( 9 );
458 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
459 base[1] + layer_scale );
470 void SGCloudLayer::draw() {
471 if ( layer_coverage != SG_CLOUD_CLEAR ) {
472 ssgCullAndDraw( layer_root );
477 // make an ssgSimpleState for a cloud layer given the named texture
478 ssgSimpleState *sgCloudMakeState( const string &path ) {
479 ssgSimpleState *state = new ssgSimpleState();
481 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
483 state->setTexture( (char *)path.c_str() );
484 state->setShadeModel( GL_SMOOTH );
485 state->disable( GL_LIGHTING );
486 state->disable( GL_CULL_FACE );
487 state->enable( GL_TEXTURE_2D );
488 state->enable( GL_COLOR_MATERIAL );
489 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
490 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
491 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
492 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
493 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
494 state->enable( GL_BLEND );
495 state->enable( GL_ALPHA_TEST );
496 state->setAlphaClamp( 0.01 );
498 // ref() the state so it doesn't get deleted if the last layer of
499 // it's type is deleted.