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 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;
207 // caclculate the difference between a flat-earth model and
208 // a round earth model given the span and altutude ASL of
209 // the cloud layer. This is the difference in altitude between
210 // the top of the inverted bowl and the edge of the bowl.
211 // const float alt_diff = layer_asl * 0.8;
212 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
213 const float layer_angle = acos( 0.5*layer_span / layer_to_core);
214 const float border_to_core = layer_to_core * sin(layer_angle);
215 const float alt_diff = layer_to_core - border_to_core;
217 for (int i = 0; i < 4; i++)
219 if ( layer[i] != NULL ) {
220 layer_transform->removeKid(layer[i]); // automatic delete
223 vl[i] = new ssgVertexArray( 10 );
224 cl[i] = new ssgColourArray( 10 );
225 tl[i] = new ssgTexCoordArray( 10 );
228 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
229 alt_diff * (sin(i*mpi) - 2) );
231 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
233 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
236 vl[i]->add( vertex );
239 for (int j = 0; j < 4; j++)
241 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
242 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
244 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
245 base[1] + layer_scale * j/4 );
247 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
248 ( (j == 0) || (i == 3)) ?
249 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
252 vl[i]->add( vertex );
256 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
257 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
259 sgSetVec2( tc, base[0] + layer_scale * i/4,
260 base[1] + layer_scale * (j+1)/4 );
262 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
263 ((j == 3) || (i == 0)) ?
264 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
266 vl[i]->add( vertex );
270 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
271 alt_diff * (sin((i+1)*mpi) - 2) );
273 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
274 base[1] + layer_scale );
276 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
279 vl[i]->add( vertex );
282 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
283 layer_transform->addKid( layer[i] );
285 if ( layer_states[layer_coverage] != NULL ) {
286 layer[i]->setState( layer_states[layer_coverage] );
290 // force a repaint of the sky colors with arbitrary defaults
296 // repaint the cloud layer colors
297 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
300 for ( int i = 0; i < 4; i++ )
301 for ( int j = 0; j < 10; ++j ) {
302 color = cl[i]->get( j );
303 sgCopyVec3( color, fog_color );
310 // reposition the cloud layer at the specified origin and orientation
311 // lon specifies a rotation about the Z axis
312 // lat specifies a rotation about the new Y axis
313 // spin specifies a rotation about the new Z axis (and orients the
314 // sunrise/set effects
315 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
316 double alt, double dt )
321 // combine p and asl (meters) to get translation offset
323 sgCopyVec3( asl_offset, up );
324 sgNormalizeVec3( asl_offset );
325 if ( alt <= layer_asl ) {
326 sgScaleVec3( asl_offset, layer_asl );
328 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
330 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
331 // << "," << asl_offset[2] << endl;
332 sgAddVec3( asl_offset, p );
333 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
334 // << "," << asl_offset[2] << endl;
336 // Translate to zero elevation
337 // Point3D zero_elev = current_view.get_cur_zero_elev();
338 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
339 sgMakeTransMat4( T1, asl_offset );
341 // printf(" Translated to %.2f %.2f %.2f\n",
342 // zero_elev.x, zero_elev.y, zero_elev.z );
344 // Rotate to proper orientation
345 // printf(" lon = %.2f lat = %.2f\n",
346 // lon * SGD_RADIANS_TO_DEGREES,
347 // lat * SGD_RADIANS_TO_DEGREES);
348 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
349 sgSetVec3( axis, 0.0, 0.0, 1.0 );
350 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
352 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
354 sgSetVec3( axis, 0.0, 1.0, 0.0 );
355 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
359 sgCopyMat4( TRANSFORM, T1 );
360 sgPreMultMat4( TRANSFORM, LON );
361 sgPreMultMat4( TRANSFORM, LAT );
364 sgSetCoord( &layerpos, TRANSFORM );
366 layer_transform->setTransform( &layerpos );
368 // now calculate update texture coordinates
369 if ( last_lon < -900 ) {
374 double sp_dist = speed*dt;
376 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
377 Point3D start( last_lon, last_lat, 0.0 );
378 Point3D dest( lon, lat, 0.0 );
379 double course = 0.0, dist = 0.0;
381 calc_gc_course_dist( dest, start, &course, &dist );
382 // cout << "course = " << course << ", dist = " << dist << endl;
384 // if start and dest are too close together,
385 // calc_gc_course_dist() can return a course of "nan". If
386 // this happens, lets just use the last known good course.
387 // This is a hack, and it would probably be better to make
388 // calc_gc_course_dist() more robust.
389 if ( isnan(course) ) {
390 course = last_course;
392 last_course = course;
395 // calculate cloud movement due to external forces
396 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
399 ax = cos(course) * dist;
400 ay = sin(course) * dist;
404 bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
405 by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
409 double xoff = (ax + bx) / (2 * scale);
410 double yoff = (ay + by) / (2 * scale);
412 const float layer_scale = layer_span / scale;
414 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
418 base = tl[0]->get( 0 );
421 // the while loops can lead to *long* pauses if base[0] comes
422 // with a bogus value.
423 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
424 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
425 if ( base[0] > -10.0 && base[0] < 10.0 ) {
426 base[0] -= (int)base[0];
428 SG_LOG(SG_ASTRO, SG_DEBUG,
429 "Error: base = " << base[0] << "," << base[1] <<
430 " course = " << course << " dist = " << dist );
435 // the while loops can lead to *long* pauses if base[0] comes
436 // with a bogus value.
437 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
438 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
439 if ( base[1] > -10.0 && base[1] < 10.0 ) {
440 base[1] -= (int)base[1];
442 SG_LOG(SG_ASTRO, SG_ALERT,
443 "Error: base = " << base[0] << "," << base[1] <<
444 " course = " << course << " dist = " << dist );
448 // cout << "base = " << base[0] << "," << base[1] << endl;
450 for (int i = 0; i < 4; i++)
452 tc = tl[i]->get( 0 );
453 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
455 for (int j = 0; j < 4; j++)
457 tc = tl[i]->get( j*2+1 );
458 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
459 base[1] + layer_scale * j/4 );
461 tc = tl[i]->get( (j+1)*2 );
462 sgSetVec2( tc, base[0] + layer_scale * i/4,
463 base[1] + layer_scale * (j+1)/4 );
466 tc = tl[i]->get( 9 );
467 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
468 base[1] + layer_scale );
479 void SGCloudLayer::draw() {
480 if ( layer_coverage != SG_CLOUD_CLEAR ) {
481 ssgCullAndDraw( layer_root );
486 // make an ssgSimpleState for a cloud layer given the named texture
487 ssgSimpleState *sgCloudMakeState( const string &path ) {
488 ssgSimpleState *state = new ssgSimpleState();
490 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
492 state->setTexture( (char *)path.c_str() );
493 state->setShadeModel( GL_SMOOTH );
494 state->disable( GL_LIGHTING );
495 state->disable( GL_CULL_FACE );
496 state->enable( GL_TEXTURE_2D );
497 state->enable( GL_COLOR_MATERIAL );
498 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
499 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
500 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
501 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
502 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
503 state->enable( GL_BLEND );
504 state->enable( GL_ALPHA_TEST );
505 state->setAlphaClamp( 0.01 );
507 // ref() the state so it doesn't get deleted if the last layer of
508 // it's type is deleted.