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 # if __FreeBSD_version < 500000
51 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
57 static ssgStateSelector *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
58 static bool state_initialized = false;
59 static bool bump_mapping = false;
60 static int nb_texture_unit = 0;
61 static ssgTexture *normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
62 static ssgTexture *color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
63 static GLuint normalization_cube_map;
65 static glActiveTextureProc glActiveTexturePtr = 0;
66 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
67 static glBlendColorProc glBlendColorPtr = 0;
69 bool SGCloudLayer::enable_bump_mapping = false;
72 generateNormalizationCubeMap()
74 unsigned char data[ 32 * 32 * 3 ];
76 const float half_size = 16.0f,
79 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
82 unsigned char *ptr = data;
83 for ( j = 0; j < size; j++ ) {
84 for ( i = 0; i < size; i++ ) {
86 sgSetVec3( tmp, half_size,
87 -( j + offset - half_size ),
88 -( i + offset - half_size ) );
89 sgNormalizeVec3( tmp );
90 sgScaleVec3( tmp, 0.5f );
91 sgAddVec3( tmp, zero_normal );
93 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
94 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
95 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
98 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
99 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
102 for ( j = 0; j < size; j++ ) {
103 for ( i = 0; i < size; i++ ) {
105 sgSetVec3( tmp, -half_size,
106 -( j + offset - half_size ),
107 ( i + offset - half_size ) );
108 sgNormalizeVec3( tmp );
109 sgScaleVec3( tmp, 0.5f );
110 sgAddVec3( tmp, zero_normal );
112 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
113 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
114 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
117 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
118 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
121 for ( j = 0; j < size; j++ ) {
122 for ( i = 0; i < size; i++ ) {
124 sgSetVec3( tmp, ( i + offset - half_size ),
126 ( j + offset - half_size ) );
127 sgNormalizeVec3( tmp );
128 sgScaleVec3( tmp, 0.5f );
129 sgAddVec3( tmp, zero_normal );
131 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
132 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
133 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
136 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
137 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
140 for ( j = 0; j < size; j++ ) {
141 for ( i = 0; i < size; i++ ) {
143 sgSetVec3( tmp, ( i + offset - half_size ),
145 -( j + offset - half_size ) );
146 sgNormalizeVec3( tmp );
147 sgScaleVec3( tmp, 0.5f );
148 sgAddVec3( tmp, zero_normal );
150 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
151 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
152 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
155 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
156 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
159 for ( j = 0; j < size; j++ ) {
160 for ( i = 0; i < size; i++ ) {
162 sgSetVec3( tmp, ( i + offset - half_size ),
163 -( j + offset - half_size ),
165 sgNormalizeVec3( tmp );
166 sgScaleVec3( tmp, 0.5f );
167 sgAddVec3( tmp, zero_normal );
169 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
170 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
171 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
174 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
175 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
178 for ( j = 0; j < size; j++ ) {
179 for ( i = 0; i < size; i++ ) {
181 sgSetVec3( tmp, -( i + offset - half_size ),
182 -( j + offset - half_size ),
184 sgNormalizeVec3( tmp );
185 sgScaleVec3( tmp, 0.5f );
186 sgAddVec3( tmp, zero_normal );
188 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
189 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
190 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
193 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
194 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
199 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
200 layer_root(new ssgRoot),
201 layer_transform(new ssgTransform),
203 texture_path(tex_path),
206 layer_thickness(0.0),
207 layer_transition(0.0),
208 layer_coverage(SG_CLOUD_CLEAR),
217 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
218 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
219 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
220 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
222 layer_root->addKid(layer_transform);
227 SGCloudLayer::~SGCloudLayer()
231 delete layer_root; // deletes layer_transform and layer as well
235 SGCloudLayer::getSpan_m () const
241 SGCloudLayer::setSpan_m (float span_m)
243 if (span_m != layer_span) {
250 SGCloudLayer::getElevation_m () const
256 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
258 layer_asl = elevation_m;
261 if (elevation_m > 4000)
262 setSpan_m( elevation_m * 10 );
269 SGCloudLayer::getThickness_m () const
271 return layer_thickness;
275 SGCloudLayer::setThickness_m (float thickness_m)
277 layer_thickness = thickness_m;
281 SGCloudLayer::getTransition_m () const
283 return layer_transition;
287 SGCloudLayer::setTransition_m (float transition_m)
289 layer_transition = transition_m;
292 SGCloudLayer::Coverage
293 SGCloudLayer::getCoverage () const
295 return layer_coverage;
299 SGCloudLayer::setCoverage (Coverage coverage)
301 if (coverage != layer_coverage) {
302 layer_coverage = coverage;
308 // build the cloud object
310 SGCloudLayer::rebuild()
312 // Initialize states and sizes if necessary.
313 if ( !state_initialized ) {
314 state_initialized = true;
316 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
318 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
319 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
320 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
321 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
322 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
324 if ( bump_mapping ) {
325 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
326 if ( nb_texture_unit < 2 ) {
327 bump_mapping = false;
329 //nb_texture_unit = 2; // Force the number of units for now
332 if ( bump_mapping ) {
334 // This bump mapping code was inspired by the tutorial available at
335 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
336 // and a NVidia white paper
337 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
338 // The normal map textures were generated by the normal map Gimp plugin :
339 // http://nifelheim.dyndns.org/~cocidius/normalmap/
343 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
344 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
345 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
347 cloud_path.set(texture_path.str());
348 cloud_path.append("overcast.rgb");
349 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
350 color_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
351 cloud_path.set(texture_path.str());
352 cloud_path.append("overcast_n.rgb");
353 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
354 normal_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
356 cloud_path.set(texture_path.str());
357 cloud_path.append("overcast_top.rgb");
358 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
359 color_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
360 cloud_path.set(texture_path.str());
361 cloud_path.append("overcast_top_n.rgb");
362 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
363 normal_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
365 cloud_path.set(texture_path.str());
366 cloud_path.append("broken.rgba");
367 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
368 color_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
369 cloud_path.set(texture_path.str());
370 cloud_path.append("broken_n.rgb");
371 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
372 normal_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
374 cloud_path.set(texture_path.str());
375 cloud_path.append("scattered.rgba");
376 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
377 color_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
378 cloud_path.set(texture_path.str());
379 cloud_path.append("scattered_n.rgb");
380 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
381 normal_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
383 cloud_path.set(texture_path.str());
384 cloud_path.append("few.rgba");
385 color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
386 color_map[ SG_CLOUD_FEW ][ 0 ]->ref();
387 cloud_path.set(texture_path.str());
388 cloud_path.append("few_n.rgb");
389 normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
390 normal_map[ SG_CLOUD_FEW ][ 0 ]->ref();
392 cloud_path.set(texture_path.str());
393 cloud_path.append("cirrus.rgba");
394 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
395 color_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
396 cloud_path.set(texture_path.str());
397 cloud_path.append("cirrus_n.rgb");
398 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
399 normal_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
401 glGenTextures( 1, &normalization_cube_map );
402 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
403 generateNormalizationCubeMap();
404 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
405 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
406 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
407 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
408 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
411 ssgStateSelector *state_sel;
412 ssgSimpleState *state;
414 state_sel = new ssgStateSelector( 2 );
416 cloud_path.set(texture_path.str());
417 cloud_path.append("overcast.rgb");
418 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
419 cloud_path.set(texture_path.str());
420 cloud_path.append("overcast_top.rgb");
421 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
422 layer_states[SG_CLOUD_OVERCAST] = state_sel;
424 state_sel = new ssgStateSelector( 2 );
426 cloud_path.set(texture_path.str());
427 cloud_path.append("broken.rgba");
428 state = sgCloudMakeState(cloud_path.str());
429 state_sel->setStep( 0, state );
430 state_sel->setStep( 1, state );
431 layer_states[SG_CLOUD_BROKEN] = state_sel;
433 state_sel = new ssgStateSelector( 2 );
435 cloud_path.set(texture_path.str());
436 cloud_path.append("scattered.rgba");
437 state = sgCloudMakeState(cloud_path.str());
438 state_sel->setStep( 0, state );
439 state_sel->setStep( 1, state );
440 layer_states[SG_CLOUD_SCATTERED] = state_sel;
442 state_sel = new ssgStateSelector( 2 );
444 cloud_path.set(texture_path.str());
445 cloud_path.append("few.rgba");
446 state = sgCloudMakeState(cloud_path.str());
447 state_sel->setStep( 0, state );
448 state_sel->setStep( 1, state );
449 layer_states[SG_CLOUD_FEW] = state_sel;
451 state_sel = new ssgStateSelector( 2 );
453 cloud_path.set(texture_path.str());
454 cloud_path.append("cirrus.rgba");
455 state = sgCloudMakeState(cloud_path.str());
456 state_sel->setStep( 0, state );
457 state_sel->setStep( 1, state );
458 layer_states[SG_CLOUD_CIRRUS] = state_sel;
460 layer_states[SG_CLOUD_CLEAR] = 0;
464 if ( bump_mapping ) {
467 vertices = new CloudVertex[ 25 ];
468 indices = new unsigned int[ 40 ];
472 sgSetVec2( base, sg_random(), sg_random() );
474 const float layer_scale = layer_span / scale;
475 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
476 const float half_angle = 0.5 * layer_span / layer_to_core;
479 for ( i = -2; i <= 2; i++ ) {
480 for ( int j = -2; j <= 2; j++ ) {
481 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
482 sgSetVec3( v1.position,
483 0.5 * i * layer_span,
484 0.5 * j * layer_span,
485 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
486 sgSetVec2( v1.texCoord,
487 base[0] + layer_scale * i * 0.25,
488 base[1] + layer_scale * j * 0.25 );
489 sgSetVec3( v1.sTangent,
490 cos( i * half_angle ),
492 -sin( i * half_angle ) );
493 sgSetVec3( v1.tTangent,
495 cos( j * half_angle ),
496 -sin( j * half_angle ) );
497 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
498 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
502 * 0 1 5 6 10 11 15 16 20 21
503 * 1 2 6 7 11 12 16 17 21 22
504 * 2 3 7 8 12 13 17 18 22 23
505 * 3 4 8 9 13 14 18 19 23 24
507 for ( i = 0; i < 4; i++ ) {
508 for ( int j = 0; j < 5; j++ ) {
509 indices[ i*10 + (j*2) ] = i + 5 * j;
510 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
517 last_lon = last_lat = -999.0f;
520 sgSetVec2( base, sg_random(), sg_random() );
522 // build the cloud layer
527 const float layer_scale = layer_span / scale;
528 const float mpi = SG_PI/4;
530 // caclculate the difference between a flat-earth model and
531 // a round earth model given the span and altutude ASL of
532 // the cloud layer. This is the difference in altitude between
533 // the top of the inverted bowl and the edge of the bowl.
534 // const float alt_diff = layer_asl * 0.8;
535 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
536 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
537 const float border_to_core = layer_to_core * cos(layer_angle);
538 const float alt_diff = layer_to_core - border_to_core;
540 for (int i = 0; i < 4; i++)
542 if ( layer[i] != NULL ) {
543 layer_transform->removeKid(layer[i]); // automatic delete
546 vl[i] = new ssgVertexArray( 10 );
547 cl[i] = new ssgColourArray( 10 );
548 tl[i] = new ssgTexCoordArray( 10 );
551 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
552 alt_diff * (sin(i*mpi) - 2) );
554 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
556 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
559 vl[i]->add( vertex );
562 for (int j = 0; j < 4; j++)
564 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
565 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
567 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
568 base[1] + layer_scale * j/4 );
570 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
571 ( (j == 0) || (i == 3)) ?
572 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
575 vl[i]->add( vertex );
579 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
580 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
582 sgSetVec2( tc, base[0] + layer_scale * i/4,
583 base[1] + layer_scale * (j+1)/4 );
585 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
586 ((j == 3) || (i == 0)) ?
587 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
589 vl[i]->add( vertex );
593 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
594 alt_diff * (sin((i+1)*mpi) - 2) );
596 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
597 base[1] + layer_scale );
599 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
602 vl[i]->add( vertex );
605 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
606 layer_transform->addKid( layer[i] );
608 if ( layer_states[layer_coverage] != NULL ) {
609 layer[i]->setState( layer_states[layer_coverage] );
611 state_sel = layer_states[layer_coverage];
614 // force a repaint of the sky colors with arbitrary defaults
620 // repaint the cloud layer colors
621 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
623 if ( bump_mapping && enable_bump_mapping ) {
625 for ( int i = 0; i < 25; i++ ) {
626 sgCopyVec3( vertices[ i ].color, fog_color );
632 for ( int i = 0; i < 4; i++ ) {
633 for ( int j = 0; j < 10; ++j ) {
634 color = cl[i]->get( j );
635 sgCopyVec3( color, fog_color );
643 // reposition the cloud layer at the specified origin and orientation
644 // lon specifies a rotation about the Z axis
645 // lat specifies a rotation about the new Y axis
646 // spin specifies a rotation about the new Z axis (and orients the
647 // sunrise/set effects
648 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
649 double alt, double dt )
654 // combine p and asl (meters) to get translation offset
656 sgCopyVec3( asl_offset, up );
657 sgNormalizeVec3( asl_offset );
658 if ( alt <= layer_asl ) {
659 sgScaleVec3( asl_offset, layer_asl );
661 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
663 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
664 // << "," << asl_offset[2] << endl;
665 sgAddVec3( asl_offset, p );
666 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
667 // << "," << asl_offset[2] << endl;
669 // Translate to zero elevation
670 // Point3D zero_elev = current_view.get_cur_zero_elev();
671 sgMakeTransMat4( T1, asl_offset );
673 // printf(" Translated to %.2f %.2f %.2f\n",
674 // zero_elev.x, zero_elev.y, zero_elev.z );
676 // Rotate to proper orientation
677 // printf(" lon = %.2f lat = %.2f\n",
678 // lon * SGD_RADIANS_TO_DEGREES,
679 // lat * SGD_RADIANS_TO_DEGREES);
680 sgSetVec3( axis, 0.0, 0.0, 1.0 );
681 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
683 sgSetVec3( axis, 0.0, 1.0, 0.0 );
684 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
688 sgCopyMat4( TRANSFORM, T1 );
689 sgPreMultMat4( TRANSFORM, LON );
690 sgPreMultMat4( TRANSFORM, LAT );
693 sgSetCoord( &layerpos, TRANSFORM );
695 layer_transform->setTransform( &layerpos );
697 // now calculate update texture coordinates
698 if ( last_lon < -900 ) {
703 double sp_dist = speed*dt;
705 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
706 Point3D start( last_lon, last_lat, 0.0 );
707 Point3D dest( lon, lat, 0.0 );
708 double course = 0.0, dist = 0.0;
710 calc_gc_course_dist( dest, start, &course, &dist );
711 // cout << "course = " << course << ", dist = " << dist << endl;
713 // if start and dest are too close together,
714 // calc_gc_course_dist() can return a course of "nan". If
715 // this happens, lets just use the last known good course.
716 // This is a hack, and it would probably be better to make
717 // calc_gc_course_dist() more robust.
718 if ( isnan(course) ) {
719 course = last_course;
721 last_course = course;
724 // calculate cloud movement due to external forces
725 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
728 ax = cos(course) * dist;
729 ay = sin(course) * dist;
733 bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
734 by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
738 double xoff = (ax + bx) / (2 * scale);
739 double yoff = (ay + by) / (2 * scale);
741 const float layer_scale = layer_span / scale;
743 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
746 if ( bump_mapping && enable_bump_mapping ) {
747 base = vertices[12].texCoord;
749 base = tl[0]->get( 0 );
753 // the while loops can lead to *long* pauses if base[0] comes
754 // with a bogus value.
755 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
756 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
757 if ( base[0] > -10.0 && base[0] < 10.0 ) {
758 base[0] -= (int)base[0];
760 SG_LOG(SG_ASTRO, SG_DEBUG,
761 "Error: base = " << base[0] << "," << base[1] <<
762 " course = " << course << " dist = " << dist );
767 // the while loops can lead to *long* pauses if base[0] comes
768 // with a bogus value.
769 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
770 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
771 if ( base[1] > -10.0 && base[1] < 10.0 ) {
772 base[1] -= (int)base[1];
774 SG_LOG(SG_ASTRO, SG_ALERT,
775 "Error: base = " << base[0] << "," << base[1] <<
776 " course = " << course << " dist = " << dist );
780 if ( bump_mapping && enable_bump_mapping ) {
782 for ( int i = -2; i <= 2; i++ ) {
783 for ( int j = -2; j <= 2; j++ ) {
784 if ( i == 0 && j == 0 )
785 continue; // Already done on base
786 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
787 sgSetVec2( v1.texCoord,
788 base[0] + layer_scale * i * 0.25,
789 base[1] + layer_scale * j * 0.25 );
794 // cout << "base = " << base[0] << "," << base[1] << endl;
797 for (int i = 0; i < 4; i++) {
798 tc = tl[i]->get( 0 );
799 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
801 for (int j = 0; j < 4; j++)
803 tc = tl[i]->get( j*2+1 );
804 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
805 base[1] + layer_scale * j/4 );
807 tc = tl[i]->get( (j+1)*2 );
808 sgSetVec2( tc, base[0] + layer_scale * i/4,
809 base[1] + layer_scale * (j+1)/4 );
812 tc = tl[i]->get( 9 );
813 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
814 base[1] + layer_scale );
826 void SGCloudLayer::draw( bool top ) {
827 if ( layer_coverage != SG_CLOUD_CLEAR ) {
829 if ( bump_mapping && enable_bump_mapping ) {
834 ssgGetModelviewMatrix( modelview );
835 layer_transform->getTransform( transform );
837 sgTransposeNegateMat4( tmp, transform );
839 sgPostMultMat4( transform, modelview );
840 ssgLoadModelviewMatrix( transform );
843 ssgGetLight( 0 )->getPosition( lightVec );
844 sgNegateVec3( lightVec );
845 sgXformVec3( lightVec, tmp );
847 for ( int i = 0; i < 25; i++ ) {
848 CloudVertex &v = vertices[ i ];
849 sgSetVec3( v.tangentSpLight,
850 sgScalarProductVec3( v.sTangent, lightVec ),
851 sgScalarProductVec3( v.tTangent, lightVec ),
852 sgScalarProductVec3( v.normal, lightVec ) );
855 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
856 if ( top && decal == 0 ) {
857 decal = color_map[ layer_coverage ][ 0 ];
859 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
860 if ( top && normal == 0 ) {
861 normal = normal_map[ layer_coverage ][ 0 ];
864 glDisable( GL_LIGHTING );
865 glDisable( GL_CULL_FACE );
866 // glDisable( GL_ALPHA_TEST );
867 if ( layer_coverage == SG_CLOUD_FEW ) {
868 glEnable( GL_ALPHA_TEST );
869 glAlphaFunc ( GL_GREATER, 0.01 );
871 glEnable( GL_BLEND );
872 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
874 glShadeModel( GL_SMOOTH );
875 glEnable( GL_COLOR_MATERIAL );
879 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
880 emis = ( color[0]+color[1]+color[2] ) / 3.0;
884 sgSetVec4( color, emis, emis, emis, 0.0 );
885 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
886 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
887 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
888 sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
889 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
890 sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
891 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
893 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
895 glActiveTexturePtr( GL_TEXTURE0_ARB );
896 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
897 glEnable( GL_TEXTURE_2D );
899 //Bind normalisation cube map to texture unit 1
900 glActiveTexturePtr( GL_TEXTURE1_ARB );
901 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
902 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
903 glActiveTexturePtr( GL_TEXTURE0_ARB );
905 //Set vertex arrays for cloud
906 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
907 glEnableClientState( GL_VERTEX_ARRAY );
909 if ( nb_texture_unit >= 3 ) {
910 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
911 glEnableClientState( GL_COLOR_ARRAY );
914 //Send texture coords for normal map to unit 0
915 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
916 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
918 //Send tangent space light vectors for normalisation to unit 1
919 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
920 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
921 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
923 //Set up texture environment to do (tex0 dot tex1)*color
924 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
925 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
926 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
927 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
928 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
930 glActiveTexturePtr( GL_TEXTURE1_ARB );
932 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
933 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
934 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
935 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
936 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
937 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
939 if ( nb_texture_unit >= 3 ) {
940 glActiveTexturePtr( GL_TEXTURE2_ARB );
941 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
943 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
944 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
945 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
947 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
948 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
949 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
950 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
952 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
953 glActiveTexturePtr( GL_TEXTURE0_ARB );
956 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
957 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
958 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
959 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
961 glDisable( GL_TEXTURE_2D );
962 glActiveTexturePtr( GL_TEXTURE1_ARB );
963 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
964 glActiveTexturePtr( GL_TEXTURE2_ARB );
965 glDisable( GL_TEXTURE_2D );
966 glActiveTexturePtr( GL_TEXTURE0_ARB );
968 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
969 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
970 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
971 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
972 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
973 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
975 glDisableClientState( GL_COLOR_ARRAY );
976 glEnable( GL_LIGHTING );
978 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
981 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
982 glActiveTexturePtr( GL_TEXTURE0_ARB );
985 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
986 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
987 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
988 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
991 glDisable( GL_TEXTURE_2D );
993 glActiveTexturePtr( GL_TEXTURE1_ARB );
994 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
995 glActiveTexturePtr( GL_TEXTURE0_ARB );
997 //disable vertex arrays
998 glDisableClientState( GL_VERTEX_ARRAY );
1000 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1001 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
1002 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1003 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1005 //Return to standard modulate texenv
1006 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1008 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
1009 glDepthFunc(GL_LEQUAL);
1011 glEnable( GL_LIGHTING );
1013 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1014 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1015 average = 0.15 + average/10;
1016 sgVec4 averageColor;
1017 sgSetVec4( averageColor, average, average, average, 1.0f );
1018 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1020 glBlendColorPtr( average, average, average, 1.0f );
1021 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1023 //Perform a second pass to color the torus
1024 //Bind decal texture
1025 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1026 glEnable(GL_TEXTURE_2D);
1028 //Set vertex arrays for torus
1029 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1030 glEnableClientState( GL_VERTEX_ARRAY );
1032 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1033 //glEnableClientState( GL_COLOR_ARRAY );
1035 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1036 glEnableClientState( GL_NORMAL_ARRAY );
1038 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1039 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1042 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1043 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1044 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1045 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1047 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1049 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1053 glDisable( GL_TEXTURE_2D );
1055 glDisableClientState( GL_VERTEX_ARRAY );
1056 glDisableClientState( GL_NORMAL_ARRAY );
1058 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1059 glEnable( GL_CULL_FACE );
1060 glDepthFunc(GL_LESS);
1062 ssgLoadModelviewMatrix( modelview );
1065 state_sel->selectStep( top ? 1 : 0 );
1066 ssgCullAndDraw( layer_root );
1072 // make an ssgSimpleState for a cloud layer given the named texture
1073 ssgSimpleState *sgCloudMakeState( const string &path ) {
1074 ssgSimpleState *state = new ssgSimpleState();
1076 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1078 state->setTexture( (char *)path.c_str() );
1079 state->setShadeModel( GL_SMOOTH );
1080 state->disable( GL_LIGHTING );
1081 state->disable( GL_CULL_FACE );
1082 state->enable( GL_TEXTURE_2D );
1083 state->enable( GL_COLOR_MATERIAL );
1084 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1085 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1086 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1087 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1088 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1089 state->enable( GL_BLEND );
1090 state->enable( GL_ALPHA_TEST );
1091 state->setAlphaClamp( 0.01 );