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>
29 #include <simgear/math/point3d.hxx>
30 #include <simgear/math/polar3d.hxx>
31 #include <simgear/math/sg_random.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/misc/sg_path.hxx>
38 static ssgSimpleState *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
39 static bool state_initialized = false;
43 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
44 layer_root(new ssgRoot),
45 layer_transform(new ssgTransform),
46 texture_path(tex_path),
50 layer_transition(0.0),
51 layer_coverage(SG_CLOUD_CLEAR),
58 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
59 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
60 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
61 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
63 layer_root->addKid(layer_transform);
68 SGCloudLayer::~SGCloudLayer()
70 delete layer_root; // deletes layer_transform and layer as well
74 SGCloudLayer::getSpan_m () const
80 SGCloudLayer::setSpan_m (float span_m)
82 if (span_m != layer_span) {
89 SGCloudLayer::getElevation_m () const
95 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
97 layer_asl = elevation_m;
100 if (elevation_m > 4000)
101 setSpan_m( elevation_m * 10 );
108 SGCloudLayer::getThickness_m () const
110 return layer_thickness;
114 SGCloudLayer::setThickness_m (float thickness_m)
116 layer_thickness = thickness_m;
120 SGCloudLayer::getTransition_m () const
122 return layer_transition;
126 SGCloudLayer::setTransition_m (float transition_m)
128 layer_transition = transition_m;
131 SGCloudLayer::Coverage
132 SGCloudLayer::getCoverage () const
134 return layer_coverage;
138 SGCloudLayer::setCoverage (Coverage coverage)
140 if (coverage != layer_coverage) {
141 layer_coverage = coverage;
147 // build the cloud object
149 SGCloudLayer::rebuild()
151 // Initialize states and sizes if necessary.
152 if ( !state_initialized ) {
153 state_initialized = true;
155 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
159 cloud_path.set(texture_path.str());
160 cloud_path.append("overcast.rgb");
161 layer_states[SG_CLOUD_OVERCAST] = sgCloudMakeState(cloud_path.str());
163 cloud_path.set(texture_path.str());
164 cloud_path.append("broken.rgba");
165 layer_states[SG_CLOUD_BROKEN]
166 = sgCloudMakeState(cloud_path.str());
168 cloud_path.set(texture_path.str());
169 cloud_path.append("scattered.rgba");
170 layer_states[SG_CLOUD_SCATTERED]
171 = sgCloudMakeState(cloud_path.str());
173 cloud_path.set(texture_path.str());
174 cloud_path.append("few.rgba");
175 layer_states[SG_CLOUD_FEW]
176 = sgCloudMakeState(cloud_path.str());
178 cloud_path.set(texture_path.str());
179 cloud_path.append("cirrus.rgba");
180 layer_states[SG_CLOUD_CIRRUS]
181 = sgCloudMakeState(cloud_path.str());
183 layer_states[SG_CLOUD_CLEAR] = 0;
187 last_lon = last_lat = -999.0f;
190 sgSetVec2( base, sg_random(), sg_random() );
192 // build the cloud layer
197 const float layer_scale = layer_span / scale;
198 const float mpi = SG_PI/4;
199 const float alt_diff = layer_asl * 0.8;
201 for (int i = 0; i < 4; i++)
203 if ( layer[i] != NULL ) {
204 layer_transform->removeKid(layer[i]); // automatic delete
207 vl[i] = new ssgVertexArray( 10 );
208 cl[i] = new ssgColourArray( 10 );
209 tl[i] = new ssgTexCoordArray( 10 );
212 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
213 alt_diff * (sin(i*mpi) - 2) );
215 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
217 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
220 vl[i]->add( vertex );
223 for (int j = 0; j < 4; j++)
225 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
226 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
228 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
229 base[1] + layer_scale * j/4 );
231 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
232 ( (j == 0) || (i == 3)) ?
233 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
236 vl[i]->add( vertex );
240 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
241 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
243 sgSetVec2( tc, base[0] + layer_scale * i/4,
244 base[1] + layer_scale * (j+1)/4 );
246 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
247 ((j == 3) || (i == 0)) ?
248 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
250 vl[i]->add( vertex );
254 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
255 alt_diff * (sin((i+1)*mpi) - 2) );
257 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
258 base[1] + layer_scale );
260 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
263 vl[i]->add( vertex );
266 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
267 layer_transform->addKid( layer[i] );
269 if ( layer_states[layer_coverage] != NULL ) {
270 layer[i]->setState( layer_states[layer_coverage] );
274 // force a repaint of the sky colors with arbitrary defaults
280 // repaint the cloud layer colors
281 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
284 for ( int i = 0; i < 4; i++ )
285 for ( int j = 0; j < 10; ++j ) {
286 color = cl[i]->get( j );
287 sgCopyVec3( color, fog_color );
294 // reposition the cloud layer at the specified origin and orientation
295 // lon specifies a rotation about the Z axis
296 // lat specifies a rotation about the new Y axis
297 // spin specifies a rotation about the new Z axis (and orients the
298 // sunrise/set effects
299 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
300 double alt, double dt )
305 // combine p and asl (meters) to get translation offset
307 sgCopyVec3( asl_offset, up );
308 sgNormalizeVec3( asl_offset );
309 if ( alt <= layer_asl ) {
310 sgScaleVec3( asl_offset, layer_asl );
312 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
314 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
315 // << "," << asl_offset[2] << endl;
316 sgAddVec3( asl_offset, p );
317 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
318 // << "," << asl_offset[2] << endl;
320 // Translate to zero elevation
321 // Point3D zero_elev = current_view.get_cur_zero_elev();
322 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
323 sgMakeTransMat4( T1, asl_offset );
325 // printf(" Translated to %.2f %.2f %.2f\n",
326 // zero_elev.x, zero_elev.y, zero_elev.z );
328 // Rotate to proper orientation
329 // printf(" lon = %.2f lat = %.2f\n",
330 // lon * SGD_RADIANS_TO_DEGREES,
331 // lat * SGD_RADIANS_TO_DEGREES);
332 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
333 sgSetVec3( axis, 0.0, 0.0, 1.0 );
334 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
336 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
338 sgSetVec3( axis, 0.0, 1.0, 0.0 );
339 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
343 sgCopyMat4( TRANSFORM, T1 );
344 sgPreMultMat4( TRANSFORM, LON );
345 sgPreMultMat4( TRANSFORM, LAT );
348 sgSetCoord( &layerpos, TRANSFORM );
350 layer_transform->setTransform( &layerpos );
352 // now calculate update texture coordinates
353 if ( last_lon < -900 ) {
358 double sp_dist = speed*dt;
360 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
361 Point3D start( last_lon, last_lat, 0.0 );
362 Point3D dest( lon, lat, 0.0 );
363 double course = 0.0, dist = 0.0;
365 calc_gc_course_dist( dest, start, &course, &dist );
366 // cout << "course = " << course << ", dist = " << dist << endl;
368 // if start and dest are too close together,
369 // calc_gc_course_dist() can return a course of "nan". If
370 // this happens, lets just use the last known good course.
371 // This is a hack, and it would probably be better to make
372 // calc_gc_course_dist() more robust.
373 if ( isnan(course) ) {
374 course = last_course;
376 last_course = course;
379 // calculate cloud movement due to external forces
380 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
383 ax = cos(course) * dist;
384 ay = sin(course) * dist;
388 bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
389 by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
393 double xoff = (ax + bx) / (2 * scale);
394 double yoff = (ay + by) / (2 * scale);
396 const float layer_scale = layer_span / scale;
398 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
402 base = tl[0]->get( 0 );
405 // the while loops can lead to *long* pauses if base[0] comes
406 // with a bogus value.
407 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
408 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
409 if ( base[0] > -10.0 && base[0] < 10.0 ) {
410 base[0] -= (int)base[0];
412 SG_LOG(SG_ASTRO, SG_DEBUG,
413 "Error: base = " << base[0] << "," << base[1] <<
414 " course = " << course << " dist = " << dist );
419 // the while loops can lead to *long* pauses if base[0] comes
420 // with a bogus value.
421 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
422 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
423 if ( base[1] > -10.0 && base[1] < 10.0 ) {
424 base[1] -= (int)base[1];
426 SG_LOG(SG_ASTRO, SG_ALERT,
427 "Error: base = " << base[0] << "," << base[1] <<
428 " course = " << course << " dist = " << dist );
432 // cout << "base = " << base[0] << "," << base[1] << endl;
434 for (int i = 0; i < 4; i++)
436 tc = tl[i]->get( 0 );
437 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
439 for (int j = 0; j < 4; j++)
441 tc = tl[i]->get( j*2+1 );
442 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
443 base[1] + layer_scale * j/4 );
445 tc = tl[i]->get( (j+1)*2 );
446 sgSetVec2( tc, base[0] + layer_scale * i/4,
447 base[1] + layer_scale * (j+1)/4 );
450 tc = tl[i]->get( 9 );
451 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
452 base[1] + layer_scale );
463 void SGCloudLayer::draw() {
464 if ( layer_coverage != SG_CLOUD_CLEAR ) {
465 ssgCullAndDraw( layer_root );
470 // make an ssgSimpleState for a cloud layer given the named texture
471 ssgSimpleState *sgCloudMakeState( const string &path ) {
472 ssgSimpleState *state = new ssgSimpleState();
474 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
476 state->setTexture( (char *)path.c_str() );
477 state->setShadeModel( GL_SMOOTH );
478 state->disable( GL_LIGHTING );
479 state->disable( GL_CULL_FACE );
480 state->enable( GL_TEXTURE_2D );
481 state->enable( GL_COLOR_MATERIAL );
482 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
483 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
484 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
485 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
486 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
487 state->enable( GL_BLEND );
488 state->enable( GL_ALPHA_TEST );
489 state->setAlphaClamp( 0.01 );
491 // ref() the state so it doesn't get deleted if the last layer of
492 // it's type is deleted.