1 // cloud.cxx -- model a single cloud layer
3 // Written by Curtis Olson, started June 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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); }
34 #include <simgear/math/point3d.hxx>
35 #include <simgear/math/polar3d.hxx>
36 #include <simgear/math/sg_random.h>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/misc/sg_path.hxx>
39 #include <simgear/screen/extensions.hxx>
40 #include <simgear/screen/texture.hxx>
44 #if defined(__MINGW32__)
45 #define isnan(x) _isnan(x)
48 #if defined (__FreeBSD__)
49 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
54 static ssgStateSelector *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
55 static bool state_initialized = false;
56 static bool bump_mapping = false;
57 static int nb_texture_unit = 0;
58 static ssgTexture *normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
59 static ssgTexture *color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
60 static GLuint normalization_cube_map;
62 static glActiveTextureProc glActiveTexturePtr = 0;
63 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
64 static glBlendColorProc glBlendColorPtr = 0;
66 bool SGCloudLayer::enable_bump_mapping = false;
69 generateNormalizationCubeMap()
71 unsigned char data[ 32 * 32 * 3 ];
73 const float half_size = 16.0f,
76 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
79 unsigned char *ptr = data;
80 for ( j = 0; j < size; j++ ) {
81 for ( i = 0; i < size; i++ ) {
83 sgSetVec3( tmp, half_size,
84 -( j + offset - half_size ),
85 -( i + offset - half_size ) );
86 sgNormalizeVec3( tmp );
87 sgScaleVec3( tmp, 0.5f );
88 sgAddVec3( tmp, zero_normal );
90 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
91 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
92 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
95 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
96 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
99 for ( j = 0; j < size; j++ ) {
100 for ( i = 0; i < size; i++ ) {
102 sgSetVec3( tmp, -half_size,
103 -( j + offset - half_size ),
104 ( i + offset - half_size ) );
105 sgNormalizeVec3( tmp );
106 sgScaleVec3( tmp, 0.5f );
107 sgAddVec3( tmp, zero_normal );
109 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
110 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
111 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
114 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
115 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
118 for ( j = 0; j < size; j++ ) {
119 for ( i = 0; i < size; i++ ) {
121 sgSetVec3( tmp, ( i + offset - half_size ),
123 ( j + offset - half_size ) );
124 sgNormalizeVec3( tmp );
125 sgScaleVec3( tmp, 0.5f );
126 sgAddVec3( tmp, zero_normal );
128 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
129 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
130 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
133 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
134 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
137 for ( j = 0; j < size; j++ ) {
138 for ( i = 0; i < size; i++ ) {
140 sgSetVec3( tmp, ( i + offset - half_size ),
142 -( j + offset - half_size ) );
143 sgNormalizeVec3( tmp );
144 sgScaleVec3( tmp, 0.5f );
145 sgAddVec3( tmp, zero_normal );
147 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
148 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
149 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
152 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
153 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
156 for ( j = 0; j < size; j++ ) {
157 for ( i = 0; i < size; i++ ) {
159 sgSetVec3( tmp, ( i + offset - half_size ),
160 -( j + offset - half_size ),
162 sgNormalizeVec3( tmp );
163 sgScaleVec3( tmp, 0.5f );
164 sgAddVec3( tmp, zero_normal );
166 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
167 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
168 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
171 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
172 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
175 for ( j = 0; j < size; j++ ) {
176 for ( i = 0; i < size; i++ ) {
178 sgSetVec3( tmp, -( i + offset - half_size ),
179 -( j + offset - half_size ),
181 sgNormalizeVec3( tmp );
182 sgScaleVec3( tmp, 0.5f );
183 sgAddVec3( tmp, zero_normal );
185 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
186 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
187 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
190 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
191 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
196 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
197 layer_root(new ssgRoot),
198 layer_transform(new ssgTransform),
200 texture_path(tex_path),
203 layer_thickness(0.0),
204 layer_transition(0.0),
205 layer_coverage(SG_CLOUD_CLEAR),
214 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
215 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
216 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
217 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
219 layer_root->addKid(layer_transform);
224 SGCloudLayer::~SGCloudLayer()
228 delete layer_root; // deletes layer_transform and layer as well
232 SGCloudLayer::getSpan_m () const
238 SGCloudLayer::setSpan_m (float span_m)
240 if (span_m != layer_span) {
247 SGCloudLayer::getElevation_m () const
253 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
255 layer_asl = elevation_m;
258 if (elevation_m > 4000)
259 setSpan_m( elevation_m * 10 );
266 SGCloudLayer::getThickness_m () const
268 return layer_thickness;
272 SGCloudLayer::setThickness_m (float thickness_m)
274 layer_thickness = thickness_m;
278 SGCloudLayer::getTransition_m () const
280 return layer_transition;
284 SGCloudLayer::setTransition_m (float transition_m)
286 layer_transition = transition_m;
289 SGCloudLayer::Coverage
290 SGCloudLayer::getCoverage () const
292 return layer_coverage;
296 SGCloudLayer::setCoverage (Coverage coverage)
298 if (coverage != layer_coverage) {
299 layer_coverage = coverage;
305 // build the cloud object
307 SGCloudLayer::rebuild()
309 // Initialize states and sizes if necessary.
310 if ( !state_initialized ) {
311 state_initialized = true;
313 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
315 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
316 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
317 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
318 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
319 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
321 if ( bump_mapping ) {
322 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
323 if ( nb_texture_unit < 2 ) {
324 bump_mapping = false;
326 //nb_texture_unit = 2; // Force the number of units for now
329 if ( bump_mapping ) {
331 // This bump mapping code was inspired by the tutorial available at
332 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
333 // and a NVidia white paper
334 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
335 // The normal map textures were generated by the normal map Gimp plugin :
336 // http://nifelheim.dyndns.org/~cocidius/normalmap/
340 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
341 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
342 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
344 cloud_path.set(texture_path.str());
345 cloud_path.append("overcast.rgb");
346 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
347 color_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
348 cloud_path.set(texture_path.str());
349 cloud_path.append("overcast_n.rgb");
350 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
351 normal_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
353 cloud_path.set(texture_path.str());
354 cloud_path.append("overcast_top.rgb");
355 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
356 color_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
357 cloud_path.set(texture_path.str());
358 cloud_path.append("overcast_top_n.rgb");
359 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
360 normal_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
362 cloud_path.set(texture_path.str());
363 cloud_path.append("broken.rgba");
364 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
365 color_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
366 cloud_path.set(texture_path.str());
367 cloud_path.append("broken_n.rgb");
368 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
369 normal_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
371 cloud_path.set(texture_path.str());
372 cloud_path.append("scattered.rgba");
373 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
374 color_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
375 cloud_path.set(texture_path.str());
376 cloud_path.append("scattered_n.rgb");
377 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
378 normal_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
380 cloud_path.set(texture_path.str());
381 cloud_path.append("few.rgba");
382 color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
383 color_map[ SG_CLOUD_FEW ][ 0 ]->ref();
384 cloud_path.set(texture_path.str());
385 cloud_path.append("few_n.rgb");
386 normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
387 normal_map[ SG_CLOUD_FEW ][ 0 ]->ref();
389 cloud_path.set(texture_path.str());
390 cloud_path.append("cirrus.rgba");
391 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
392 color_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
393 cloud_path.set(texture_path.str());
394 cloud_path.append("cirrus_n.rgb");
395 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
396 normal_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
398 glGenTextures( 1, &normalization_cube_map );
399 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
400 generateNormalizationCubeMap();
401 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
402 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
403 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
404 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
405 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
408 ssgStateSelector *state_sel;
409 ssgSimpleState *state;
411 state_sel = new ssgStateSelector( 2 );
413 cloud_path.set(texture_path.str());
414 cloud_path.append("overcast.rgb");
415 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
416 cloud_path.set(texture_path.str());
417 cloud_path.append("overcast_top.rgb");
418 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
419 layer_states[SG_CLOUD_OVERCAST] = state_sel;
421 state_sel = new ssgStateSelector( 2 );
423 cloud_path.set(texture_path.str());
424 cloud_path.append("broken.rgba");
425 state = sgCloudMakeState(cloud_path.str());
426 state_sel->setStep( 0, state );
427 state_sel->setStep( 1, state );
428 layer_states[SG_CLOUD_BROKEN] = state_sel;
430 state_sel = new ssgStateSelector( 2 );
432 cloud_path.set(texture_path.str());
433 cloud_path.append("scattered.rgba");
434 state = sgCloudMakeState(cloud_path.str());
435 state_sel->setStep( 0, state );
436 state_sel->setStep( 1, state );
437 layer_states[SG_CLOUD_SCATTERED] = state_sel;
439 state_sel = new ssgStateSelector( 2 );
441 cloud_path.set(texture_path.str());
442 cloud_path.append("few.rgba");
443 state = sgCloudMakeState(cloud_path.str());
444 state_sel->setStep( 0, state );
445 state_sel->setStep( 1, state );
446 layer_states[SG_CLOUD_FEW] = state_sel;
448 state_sel = new ssgStateSelector( 2 );
450 cloud_path.set(texture_path.str());
451 cloud_path.append("cirrus.rgba");
452 state = sgCloudMakeState(cloud_path.str());
453 state_sel->setStep( 0, state );
454 state_sel->setStep( 1, state );
455 layer_states[SG_CLOUD_CIRRUS] = state_sel;
457 layer_states[SG_CLOUD_CLEAR] = 0;
461 if ( bump_mapping ) {
464 vertices = new CloudVertex[ 25 ];
465 indices = new unsigned int[ 40 ];
469 sgSetVec2( base, sg_random(), sg_random() );
471 const float layer_scale = layer_span / scale;
472 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
473 const float half_angle = 0.5 * layer_span / layer_to_core;
476 for ( i = -2; i <= 2; i++ ) {
477 for ( int j = -2; j <= 2; j++ ) {
478 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
479 sgSetVec3( v1.position,
480 0.5 * i * layer_span,
481 0.5 * j * layer_span,
482 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
483 sgSetVec2( v1.texCoord,
484 base[0] + layer_scale * i * 0.25,
485 base[1] + layer_scale * j * 0.25 );
486 sgSetVec3( v1.sTangent,
487 cos( i * half_angle ),
489 -sin( i * half_angle ) );
490 sgSetVec3( v1.tTangent,
492 cos( j * half_angle ),
493 -sin( j * half_angle ) );
494 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
495 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
499 * 0 1 5 6 10 11 15 16 20 21
500 * 1 2 6 7 11 12 16 17 21 22
501 * 2 3 7 8 12 13 17 18 22 23
502 * 3 4 8 9 13 14 18 19 23 24
504 for ( i = 0; i < 4; i++ ) {
505 for ( int j = 0; j < 5; j++ ) {
506 indices[ i*10 + (j*2) ] = i + 5 * j;
507 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
514 last_lon = last_lat = -999.0f;
517 sgSetVec2( base, sg_random(), sg_random() );
519 // build the cloud layer
524 const float layer_scale = layer_span / scale;
525 const float mpi = SG_PI/4;
527 // caclculate the difference between a flat-earth model and
528 // a round earth model given the span and altutude ASL of
529 // the cloud layer. This is the difference in altitude between
530 // the top of the inverted bowl and the edge of the bowl.
531 // const float alt_diff = layer_asl * 0.8;
532 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
533 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
534 const float border_to_core = layer_to_core * cos(layer_angle);
535 const float alt_diff = layer_to_core - border_to_core;
537 for (int i = 0; i < 4; i++)
539 if ( layer[i] != NULL ) {
540 layer_transform->removeKid(layer[i]); // automatic delete
543 vl[i] = new ssgVertexArray( 10 );
544 cl[i] = new ssgColourArray( 10 );
545 tl[i] = new ssgTexCoordArray( 10 );
548 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
549 alt_diff * (sin(i*mpi) - 2) );
551 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
553 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
556 vl[i]->add( vertex );
559 for (int j = 0; j < 4; j++)
561 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
562 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
564 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
565 base[1] + layer_scale * j/4 );
567 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
568 ( (j == 0) || (i == 3)) ?
569 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
572 vl[i]->add( vertex );
576 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
577 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
579 sgSetVec2( tc, base[0] + layer_scale * i/4,
580 base[1] + layer_scale * (j+1)/4 );
582 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
583 ((j == 3) || (i == 0)) ?
584 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
586 vl[i]->add( vertex );
590 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
591 alt_diff * (sin((i+1)*mpi) - 2) );
593 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
594 base[1] + layer_scale );
596 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
599 vl[i]->add( vertex );
602 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
603 layer_transform->addKid( layer[i] );
605 if ( layer_states[layer_coverage] != NULL ) {
606 layer[i]->setState( layer_states[layer_coverage] );
608 state_sel = layer_states[layer_coverage];
611 // force a repaint of the sky colors with arbitrary defaults
617 // repaint the cloud layer colors
618 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
620 if ( bump_mapping && enable_bump_mapping ) {
622 for ( int i = 0; i < 25; i++ ) {
623 sgCopyVec3( vertices[ i ].color, fog_color );
629 for ( int i = 0; i < 4; i++ ) {
630 for ( int j = 0; j < 10; ++j ) {
631 color = cl[i]->get( j );
632 sgCopyVec3( color, fog_color );
640 // reposition the cloud layer at the specified origin and orientation
641 // lon specifies a rotation about the Z axis
642 // lat specifies a rotation about the new Y axis
643 // spin specifies a rotation about the new Z axis (and orients the
644 // sunrise/set effects
645 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
646 double alt, double dt )
651 // combine p and asl (meters) to get translation offset
653 sgCopyVec3( asl_offset, up );
654 sgNormalizeVec3( asl_offset );
655 if ( alt <= layer_asl ) {
656 sgScaleVec3( asl_offset, layer_asl );
658 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
660 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
661 // << "," << asl_offset[2] << endl;
662 sgAddVec3( asl_offset, p );
663 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
664 // << "," << asl_offset[2] << endl;
666 // Translate to zero elevation
667 // Point3D zero_elev = current_view.get_cur_zero_elev();
668 sgMakeTransMat4( T1, asl_offset );
670 // printf(" Translated to %.2f %.2f %.2f\n",
671 // zero_elev.x, zero_elev.y, zero_elev.z );
673 // Rotate to proper orientation
674 // printf(" lon = %.2f lat = %.2f\n",
675 // lon * SGD_RADIANS_TO_DEGREES,
676 // lat * SGD_RADIANS_TO_DEGREES);
677 sgSetVec3( axis, 0.0, 0.0, 1.0 );
678 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
680 sgSetVec3( axis, 0.0, 1.0, 0.0 );
681 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
685 sgCopyMat4( TRANSFORM, T1 );
686 sgPreMultMat4( TRANSFORM, LON );
687 sgPreMultMat4( TRANSFORM, LAT );
690 sgSetCoord( &layerpos, TRANSFORM );
692 layer_transform->setTransform( &layerpos );
694 // now calculate update texture coordinates
695 if ( last_lon < -900 ) {
700 double sp_dist = speed*dt;
702 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
703 Point3D start( last_lon, last_lat, 0.0 );
704 Point3D dest( lon, lat, 0.0 );
705 double course = 0.0, dist = 0.0;
707 calc_gc_course_dist( dest, start, &course, &dist );
708 // cout << "course = " << course << ", dist = " << dist << endl;
710 // if start and dest are too close together,
711 // calc_gc_course_dist() can return a course of "nan". If
712 // this happens, lets just use the last known good course.
713 // This is a hack, and it would probably be better to make
714 // calc_gc_course_dist() more robust.
715 if ( isnan(course) ) {
716 course = last_course;
718 last_course = course;
721 // calculate cloud movement due to external forces
722 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
725 ax = cos(course) * dist;
726 ay = sin(course) * dist;
730 bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
731 by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
735 double xoff = (ax + bx) / (2 * scale);
736 double yoff = (ay + by) / (2 * scale);
738 const float layer_scale = layer_span / scale;
740 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
743 if ( bump_mapping && enable_bump_mapping ) {
744 base = vertices[12].texCoord;
746 base = tl[0]->get( 0 );
750 // the while loops can lead to *long* pauses if base[0] comes
751 // with a bogus value.
752 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
753 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
754 if ( base[0] > -10.0 && base[0] < 10.0 ) {
755 base[0] -= (int)base[0];
757 SG_LOG(SG_ASTRO, SG_DEBUG,
758 "Error: base = " << base[0] << "," << base[1] <<
759 " course = " << course << " dist = " << dist );
764 // the while loops can lead to *long* pauses if base[0] comes
765 // with a bogus value.
766 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
767 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
768 if ( base[1] > -10.0 && base[1] < 10.0 ) {
769 base[1] -= (int)base[1];
771 SG_LOG(SG_ASTRO, SG_ALERT,
772 "Error: base = " << base[0] << "," << base[1] <<
773 " course = " << course << " dist = " << dist );
777 if ( bump_mapping && enable_bump_mapping ) {
779 for ( int i = -2; i <= 2; i++ ) {
780 for ( int j = -2; j <= 2; j++ ) {
781 if ( i == 0 && j == 0 )
782 continue; // Already done on base
783 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
784 sgSetVec2( v1.texCoord,
785 base[0] + layer_scale * i * 0.25,
786 base[1] + layer_scale * j * 0.25 );
791 // cout << "base = " << base[0] << "," << base[1] << endl;
794 for (int i = 0; i < 4; i++) {
795 tc = tl[i]->get( 0 );
796 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
798 for (int j = 0; j < 4; j++)
800 tc = tl[i]->get( j*2+1 );
801 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
802 base[1] + layer_scale * j/4 );
804 tc = tl[i]->get( (j+1)*2 );
805 sgSetVec2( tc, base[0] + layer_scale * i/4,
806 base[1] + layer_scale * (j+1)/4 );
809 tc = tl[i]->get( 9 );
810 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
811 base[1] + layer_scale );
823 void SGCloudLayer::draw( bool top ) {
824 if ( layer_coverage != SG_CLOUD_CLEAR ) {
826 if ( bump_mapping && enable_bump_mapping ) {
831 ssgGetModelviewMatrix( modelview );
832 layer_transform->getTransform( transform );
834 sgTransposeNegateMat4( tmp, transform );
836 sgPostMultMat4( transform, modelview );
837 ssgLoadModelviewMatrix( transform );
840 ssgGetLight( 0 )->getPosition( lightVec );
841 sgNegateVec3( lightVec );
842 sgXformVec3( lightVec, tmp );
844 for ( int i = 0; i < 25; i++ ) {
845 CloudVertex &v = vertices[ i ];
846 sgSetVec3( v.tangentSpLight,
847 sgScalarProductVec3( v.sTangent, lightVec ),
848 sgScalarProductVec3( v.tTangent, lightVec ),
849 sgScalarProductVec3( v.normal, lightVec ) );
852 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
853 if ( top && decal == 0 ) {
854 decal = color_map[ layer_coverage ][ 0 ];
856 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
857 if ( top && normal == 0 ) {
858 normal = normal_map[ layer_coverage ][ 0 ];
861 glDisable( GL_LIGHTING );
862 glDisable( GL_CULL_FACE );
863 // glDisable( GL_ALPHA_TEST );
864 if ( layer_coverage == SG_CLOUD_FEW ) {
865 glEnable( GL_ALPHA_TEST );
866 glAlphaFunc ( GL_GREATER, 0.01 );
868 glEnable( GL_BLEND );
869 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
871 glShadeModel( GL_SMOOTH );
872 glEnable( GL_COLOR_MATERIAL );
876 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
877 emis = ( color[0]+color[1]+color[2] ) / 3.0;
881 sgSetVec4( color, emis, emis, emis, 0.0 );
882 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
883 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
884 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
885 sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
886 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
887 sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
888 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
890 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
892 glActiveTexturePtr( GL_TEXTURE0_ARB );
893 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
894 glEnable( GL_TEXTURE_2D );
896 //Bind normalisation cube map to texture unit 1
897 glActiveTexturePtr( GL_TEXTURE1_ARB );
898 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
899 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
900 glActiveTexturePtr( GL_TEXTURE0_ARB );
902 //Set vertex arrays for cloud
903 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
904 glEnableClientState( GL_VERTEX_ARRAY );
906 if ( nb_texture_unit >= 3 ) {
907 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
908 glEnableClientState( GL_COLOR_ARRAY );
911 //Send texture coords for normal map to unit 0
912 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
913 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
915 //Send tangent space light vectors for normalisation to unit 1
916 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
917 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
918 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
920 //Set up texture environment to do (tex0 dot tex1)*color
921 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
922 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
923 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
924 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
925 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
927 glActiveTexturePtr( GL_TEXTURE1_ARB );
929 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
930 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
931 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
932 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
933 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
934 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
936 if ( nb_texture_unit >= 3 ) {
937 glActiveTexturePtr( GL_TEXTURE2_ARB );
938 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
940 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
941 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
942 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
944 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
945 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
946 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
947 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
949 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
950 glActiveTexturePtr( GL_TEXTURE0_ARB );
953 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
954 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
955 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
956 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
958 glDisable( GL_TEXTURE_2D );
959 glActiveTexturePtr( GL_TEXTURE1_ARB );
960 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
961 glActiveTexturePtr( GL_TEXTURE2_ARB );
962 glDisable( GL_TEXTURE_2D );
963 glActiveTexturePtr( GL_TEXTURE0_ARB );
965 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
966 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
967 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
968 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
969 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
970 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
972 glDisableClientState( GL_COLOR_ARRAY );
973 glEnable( GL_LIGHTING );
975 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
978 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
979 glActiveTexturePtr( GL_TEXTURE0_ARB );
982 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
983 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
984 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
985 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
988 glDisable( GL_TEXTURE_2D );
990 glActiveTexturePtr( GL_TEXTURE1_ARB );
991 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
992 glActiveTexturePtr( GL_TEXTURE0_ARB );
994 //disable vertex arrays
995 glDisableClientState( GL_VERTEX_ARRAY );
997 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
998 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
999 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1000 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1002 //Return to standard modulate texenv
1003 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1005 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
1006 glDepthFunc(GL_LEQUAL);
1008 glEnable( GL_LIGHTING );
1010 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1011 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1012 average = 0.15 + average/10;
1013 sgVec4 averageColor;
1014 sgSetVec4( averageColor, average, average, average, 1.0f );
1015 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1017 glBlendColorPtr( average, average, average, 1.0f );
1018 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1020 //Perform a second pass to color the torus
1021 //Bind decal texture
1022 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1023 glEnable(GL_TEXTURE_2D);
1025 //Set vertex arrays for torus
1026 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1027 glEnableClientState( GL_VERTEX_ARRAY );
1029 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1030 //glEnableClientState( GL_COLOR_ARRAY );
1032 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1033 glEnableClientState( GL_NORMAL_ARRAY );
1035 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1036 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1039 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1040 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1041 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1042 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1044 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1046 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1050 glDisable( GL_TEXTURE_2D );
1052 glDisableClientState( GL_VERTEX_ARRAY );
1053 glDisableClientState( GL_NORMAL_ARRAY );
1055 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1056 glEnable( GL_CULL_FACE );
1057 glDepthFunc(GL_LESS);
1059 ssgLoadModelviewMatrix( modelview );
1062 state_sel->selectStep( top ? 1 : 0 );
1063 ssgCullAndDraw( layer_root );
1069 // make an ssgSimpleState for a cloud layer given the named texture
1070 ssgSimpleState *sgCloudMakeState( const string &path ) {
1071 ssgSimpleState *state = new ssgSimpleState();
1073 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1075 state->setTexture( (char *)path.c_str() );
1076 state->setShadeModel( GL_SMOOTH );
1077 state->disable( GL_LIGHTING );
1078 state->disable( GL_CULL_FACE );
1079 state->enable( GL_TEXTURE_2D );
1080 state->enable( GL_COLOR_MATERIAL );
1081 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1082 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1083 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1084 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1085 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1086 state->enable( GL_BLEND );
1087 state->enable( GL_ALPHA_TEST );
1088 state->setAlphaClamp( 0.01 );