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>
41 #include <simgear/screen/extensions.hxx>
42 #include <simgear/screen/texture.hxx>
46 #if defined(__MINGW32__)
47 #define isnan(x) _isnan(x)
51 static ssgStateSelector *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
52 static bool state_initialized = false;
53 static bool bump_mapping = false;
54 static int nb_texture_unit = 0;
55 static ssgTexture *normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
56 static ssgTexture *color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
57 static GLuint normalization_cube_map;
59 static glActiveTextureProc glActiveTexturePtr = 0;
60 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
61 static glBlendColorProc glBlendColorPtr = 0;
63 bool SGCloudLayer::enable_bump_mapping = false;
66 generateNormalizationCubeMap()
68 unsigned char data[ 32 * 32 * 3 ];
70 const float half_size = 16.0f,
73 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
76 unsigned char *ptr = data;
77 for ( j = 0; j < size; j++ ) {
78 for ( i = 0; i < size; i++ ) {
80 sgSetVec3( tmp, half_size,
81 -( j + offset - half_size ),
82 -( i + offset - half_size ) );
83 sgNormalizeVec3( tmp );
84 sgScaleVec3( tmp, 0.5f );
85 sgAddVec3( tmp, zero_normal );
87 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
88 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
89 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
92 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
93 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
96 for ( j = 0; j < size; j++ ) {
97 for ( i = 0; i < size; i++ ) {
99 sgSetVec3( tmp, -half_size,
100 -( j + offset - half_size ),
101 ( i + offset - half_size ) );
102 sgNormalizeVec3( tmp );
103 sgScaleVec3( tmp, 0.5f );
104 sgAddVec3( tmp, zero_normal );
106 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
107 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
108 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
111 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
112 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
115 for ( j = 0; j < size; j++ ) {
116 for ( i = 0; i < size; i++ ) {
118 sgSetVec3( tmp, ( i + offset - half_size ),
120 ( j + offset - half_size ) );
121 sgNormalizeVec3( tmp );
122 sgScaleVec3( tmp, 0.5f );
123 sgAddVec3( tmp, zero_normal );
125 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
126 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
127 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
130 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
131 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
134 for ( j = 0; j < size; j++ ) {
135 for ( i = 0; i < size; i++ ) {
137 sgSetVec3( tmp, ( i + offset - half_size ),
139 -( j + offset - half_size ) );
140 sgNormalizeVec3( tmp );
141 sgScaleVec3( tmp, 0.5f );
142 sgAddVec3( tmp, zero_normal );
144 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
145 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
146 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
149 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
150 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
153 for ( j = 0; j < size; j++ ) {
154 for ( i = 0; i < size; i++ ) {
156 sgSetVec3( tmp, ( i + offset - half_size ),
157 -( j + offset - half_size ),
159 sgNormalizeVec3( tmp );
160 sgScaleVec3( tmp, 0.5f );
161 sgAddVec3( tmp, zero_normal );
163 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
164 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
165 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
168 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
169 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
172 for ( j = 0; j < size; j++ ) {
173 for ( i = 0; i < size; i++ ) {
175 sgSetVec3( tmp, -( i + offset - half_size ),
176 -( j + offset - half_size ),
178 sgNormalizeVec3( tmp );
179 sgScaleVec3( tmp, 0.5f );
180 sgAddVec3( tmp, zero_normal );
182 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
183 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
184 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
187 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
188 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
193 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
194 layer_root(new ssgRoot),
195 layer_transform(new ssgTransform),
197 texture_path(tex_path),
200 layer_thickness(0.0),
201 layer_transition(0.0),
202 layer_coverage(SG_CLOUD_CLEAR),
211 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
212 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
213 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
214 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
216 layer_root->addKid(layer_transform);
221 SGCloudLayer::~SGCloudLayer()
225 delete layer_root; // deletes layer_transform and layer as well
229 SGCloudLayer::getSpan_m () const
235 SGCloudLayer::setSpan_m (float span_m)
237 if (span_m != layer_span) {
244 SGCloudLayer::getElevation_m () const
250 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
252 layer_asl = elevation_m;
255 if (elevation_m > 4000)
256 setSpan_m( elevation_m * 10 );
263 SGCloudLayer::getThickness_m () const
265 return layer_thickness;
269 SGCloudLayer::setThickness_m (float thickness_m)
271 layer_thickness = thickness_m;
275 SGCloudLayer::getTransition_m () const
277 return layer_transition;
281 SGCloudLayer::setTransition_m (float transition_m)
283 layer_transition = transition_m;
286 SGCloudLayer::Coverage
287 SGCloudLayer::getCoverage () const
289 return layer_coverage;
293 SGCloudLayer::setCoverage (Coverage coverage)
295 if (coverage != layer_coverage) {
296 layer_coverage = coverage;
302 // build the cloud object
304 SGCloudLayer::rebuild()
306 // Initialize states and sizes if necessary.
307 if ( !state_initialized ) {
308 state_initialized = true;
310 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
312 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
313 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
314 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
315 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
316 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
318 if ( bump_mapping ) {
319 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
320 if ( nb_texture_unit < 2 ) {
321 bump_mapping = false;
323 nb_texture_unit = 2; // Force the number of units for now
326 if ( bump_mapping ) {
328 // This bump mapping code was inspired by the tutorial available at
329 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
330 // and a NVidia white paper
331 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
332 // The normal map textures were generated by the normal map Gimp plugin :
333 // http://nifelheim.dyndns.org/~cocidius/normalmap/
337 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
338 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
339 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
341 cloud_path.set(texture_path.str());
342 cloud_path.append("overcast.rgb");
343 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
344 color_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
345 cloud_path.set(texture_path.str());
346 cloud_path.append("overcast_n.rgb");
347 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
348 normal_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
350 cloud_path.set(texture_path.str());
351 cloud_path.append("overcast_top.rgb");
352 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
353 color_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
354 cloud_path.set(texture_path.str());
355 cloud_path.append("overcast_top_n.rgb");
356 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
357 normal_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
359 cloud_path.set(texture_path.str());
360 cloud_path.append("broken.rgba");
361 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
362 color_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
363 cloud_path.set(texture_path.str());
364 cloud_path.append("broken_n.rgb");
365 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
366 normal_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
368 cloud_path.set(texture_path.str());
369 cloud_path.append("scattered.rgba");
370 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
371 color_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
372 cloud_path.set(texture_path.str());
373 cloud_path.append("scattered_n.rgb");
374 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
375 normal_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
377 cloud_path.set(texture_path.str());
378 cloud_path.append("few.rgba");
379 color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
380 color_map[ SG_CLOUD_FEW ][ 0 ]->ref();
381 cloud_path.set(texture_path.str());
382 cloud_path.append("few_n.rgb");
383 normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
384 normal_map[ SG_CLOUD_FEW ][ 0 ]->ref();
386 cloud_path.set(texture_path.str());
387 cloud_path.append("cirrus.rgba");
388 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
389 color_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
390 cloud_path.set(texture_path.str());
391 cloud_path.append("cirrus_n.rgb");
392 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
393 normal_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
395 glGenTextures( 1, &normalization_cube_map );
396 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
397 generateNormalizationCubeMap();
398 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
399 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
400 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
401 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
402 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
405 ssgStateSelector *state_sel;
406 ssgSimpleState *state;
408 state_sel = new ssgStateSelector( 2 );
410 cloud_path.set(texture_path.str());
411 cloud_path.append("overcast.rgb");
412 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
413 cloud_path.set(texture_path.str());
414 cloud_path.append("overcast_top.rgb");
415 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
416 layer_states[SG_CLOUD_OVERCAST] = state_sel;
418 state_sel = new ssgStateSelector( 2 );
420 cloud_path.set(texture_path.str());
421 cloud_path.append("broken.rgba");
422 state = sgCloudMakeState(cloud_path.str());
423 state_sel->setStep( 0, state );
424 state_sel->setStep( 1, state );
425 layer_states[SG_CLOUD_BROKEN] = state_sel;
427 state_sel = new ssgStateSelector( 2 );
429 cloud_path.set(texture_path.str());
430 cloud_path.append("scattered.rgba");
431 state = sgCloudMakeState(cloud_path.str());
432 state_sel->setStep( 0, state );
433 state_sel->setStep( 1, state );
434 layer_states[SG_CLOUD_SCATTERED] = state_sel;
436 state_sel = new ssgStateSelector( 2 );
438 cloud_path.set(texture_path.str());
439 cloud_path.append("few.rgba");
440 state = sgCloudMakeState(cloud_path.str());
441 state_sel->setStep( 0, state );
442 state_sel->setStep( 1, state );
443 layer_states[SG_CLOUD_FEW] = state_sel;
445 state_sel = new ssgStateSelector( 2 );
447 cloud_path.set(texture_path.str());
448 cloud_path.append("cirrus.rgba");
449 state = sgCloudMakeState(cloud_path.str());
450 state_sel->setStep( 0, state );
451 state_sel->setStep( 1, state );
452 layer_states[SG_CLOUD_CIRRUS] = state_sel;
454 layer_states[SG_CLOUD_CLEAR] = 0;
458 if ( bump_mapping ) {
461 vertices = new CloudVertex[ 25 ];
462 indices = new unsigned int[ 40 ];
466 sgSetVec2( base, sg_random(), sg_random() );
468 const float layer_scale = layer_span / scale;
469 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
470 const float half_angle = 0.5 * layer_span / layer_to_core;
473 for ( i = -2; i <= 2; i++ ) {
474 for ( int j = -2; j <= 2; j++ ) {
475 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
476 sgSetVec3( v1.position,
477 0.5 * i * layer_span,
478 0.5 * j * layer_span,
479 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
480 sgSetVec2( v1.texCoord,
481 base[0] + layer_scale * i * 0.25,
482 base[1] + layer_scale * j * 0.25 );
483 sgSetVec3( v1.sTangent,
484 cos( i * half_angle ),
486 -sin( i * half_angle ) );
487 sgSetVec3( v1.tTangent,
489 cos( j * half_angle ),
490 -sin( j * half_angle ) );
491 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
492 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
496 * 0 1 5 6 10 11 15 16 20 21
497 * 1 2 6 7 11 12 16 17 21 22
498 * 2 3 7 8 12 13 17 18 22 23
499 * 3 4 8 9 13 14 18 19 23 24
501 for ( i = 0; i < 4; i++ ) {
502 for ( int j = 0; j < 5; j++ ) {
503 indices[ i*10 + (j*2) ] = i + 5 * j;
504 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
511 last_lon = last_lat = -999.0f;
514 sgSetVec2( base, sg_random(), sg_random() );
516 // build the cloud layer
521 const float layer_scale = layer_span / scale;
522 const float mpi = SG_PI/4;
524 // caclculate the difference between a flat-earth model and
525 // a round earth model given the span and altutude ASL of
526 // the cloud layer. This is the difference in altitude between
527 // the top of the inverted bowl and the edge of the bowl.
528 // const float alt_diff = layer_asl * 0.8;
529 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
530 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
531 const float border_to_core = layer_to_core * cos(layer_angle);
532 const float alt_diff = layer_to_core - border_to_core;
534 for (int i = 0; i < 4; i++)
536 if ( layer[i] != NULL ) {
537 layer_transform->removeKid(layer[i]); // automatic delete
540 vl[i] = new ssgVertexArray( 10 );
541 cl[i] = new ssgColourArray( 10 );
542 tl[i] = new ssgTexCoordArray( 10 );
545 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
546 alt_diff * (sin(i*mpi) - 2) );
548 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
550 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
553 vl[i]->add( vertex );
556 for (int j = 0; j < 4; j++)
558 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
559 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
561 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
562 base[1] + layer_scale * j/4 );
564 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
565 ( (j == 0) || (i == 3)) ?
566 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
569 vl[i]->add( vertex );
573 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
574 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
576 sgSetVec2( tc, base[0] + layer_scale * i/4,
577 base[1] + layer_scale * (j+1)/4 );
579 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
580 ((j == 3) || (i == 0)) ?
581 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
583 vl[i]->add( vertex );
587 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
588 alt_diff * (sin((i+1)*mpi) - 2) );
590 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
591 base[1] + layer_scale );
593 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
596 vl[i]->add( vertex );
599 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
600 layer_transform->addKid( layer[i] );
602 if ( layer_states[layer_coverage] != NULL ) {
603 layer[i]->setState( layer_states[layer_coverage] );
605 state_sel = layer_states[layer_coverage];
608 // force a repaint of the sky colors with arbitrary defaults
614 // repaint the cloud layer colors
615 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
617 if ( bump_mapping && enable_bump_mapping ) {
619 for ( int i = 0; i < 25; i++ ) {
620 sgCopyVec3( vertices[ i ].color, fog_color );
626 for ( int i = 0; i < 4; i++ ) {
627 for ( int j = 0; j < 10; ++j ) {
628 color = cl[i]->get( j );
629 sgCopyVec3( color, fog_color );
638 // reposition the cloud layer at the specified origin and orientation
639 // lon specifies a rotation about the Z axis
640 // lat specifies a rotation about the new Y axis
641 // spin specifies a rotation about the new Z axis (and orients the
642 // sunrise/set effects
643 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
644 double alt, double dt )
649 // combine p and asl (meters) to get translation offset
651 sgCopyVec3( asl_offset, up );
652 sgNormalizeVec3( asl_offset );
653 if ( alt <= layer_asl ) {
654 sgScaleVec3( asl_offset, layer_asl );
656 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
658 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
659 // << "," << asl_offset[2] << endl;
660 sgAddVec3( asl_offset, p );
661 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
662 // << "," << asl_offset[2] << endl;
664 // Translate to zero elevation
665 // Point3D zero_elev = current_view.get_cur_zero_elev();
666 sgMakeTransMat4( T1, asl_offset );
668 // printf(" Translated to %.2f %.2f %.2f\n",
669 // zero_elev.x, zero_elev.y, zero_elev.z );
671 // Rotate to proper orientation
672 // printf(" lon = %.2f lat = %.2f\n",
673 // lon * SGD_RADIANS_TO_DEGREES,
674 // lat * SGD_RADIANS_TO_DEGREES);
675 sgSetVec3( axis, 0.0, 0.0, 1.0 );
676 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
678 sgSetVec3( axis, 0.0, 1.0, 0.0 );
679 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
683 sgCopyMat4( TRANSFORM, T1 );
684 sgPreMultMat4( TRANSFORM, LON );
685 sgPreMultMat4( TRANSFORM, LAT );
688 sgSetCoord( &layerpos, TRANSFORM );
690 layer_transform->setTransform( &layerpos );
692 // now calculate update texture coordinates
693 if ( last_lon < -900 ) {
698 double sp_dist = speed*dt;
700 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
701 Point3D start( last_lon, last_lat, 0.0 );
702 Point3D dest( lon, lat, 0.0 );
703 double course = 0.0, dist = 0.0;
705 calc_gc_course_dist( dest, start, &course, &dist );
706 // cout << "course = " << course << ", dist = " << dist << endl;
708 // if start and dest are too close together,
709 // calc_gc_course_dist() can return a course of "nan". If
710 // this happens, lets just use the last known good course.
711 // This is a hack, and it would probably be better to make
712 // calc_gc_course_dist() more robust.
713 if ( isnan(course) ) {
714 course = last_course;
716 last_course = course;
719 // calculate cloud movement due to external forces
720 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
723 ax = cos(course) * dist;
724 ay = sin(course) * dist;
728 bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
729 by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
733 double xoff = (ax + bx) / (2 * scale);
734 double yoff = (ay + by) / (2 * scale);
736 const float layer_scale = layer_span / scale;
738 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
741 if ( bump_mapping && enable_bump_mapping ) {
742 base = vertices[12].texCoord;
744 base = tl[0]->get( 0 );
748 // the while loops can lead to *long* pauses if base[0] comes
749 // with a bogus value.
750 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
751 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
752 if ( base[0] > -10.0 && base[0] < 10.0 ) {
753 base[0] -= (int)base[0];
755 SG_LOG(SG_ASTRO, SG_DEBUG,
756 "Error: base = " << base[0] << "," << base[1] <<
757 " course = " << course << " dist = " << dist );
762 // the while loops can lead to *long* pauses if base[0] comes
763 // with a bogus value.
764 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
765 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
766 if ( base[1] > -10.0 && base[1] < 10.0 ) {
767 base[1] -= (int)base[1];
769 SG_LOG(SG_ASTRO, SG_ALERT,
770 "Error: base = " << base[0] << "," << base[1] <<
771 " course = " << course << " dist = " << dist );
775 if ( bump_mapping && enable_bump_mapping ) {
777 for ( int i = -2; i <= 2; i++ ) {
778 for ( int j = -2; j <= 2; j++ ) {
779 if ( i == 0 && j == 0 )
780 continue; // Already done on base
781 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
782 sgSetVec2( v1.texCoord,
783 base[0] + layer_scale * i * 0.25,
784 base[1] + layer_scale * j * 0.25 );
789 // cout << "base = " << base[0] << "," << base[1] << endl;
792 for (int i = 0; i < 4; i++) {
793 tc = tl[i]->get( 0 );
794 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
796 for (int j = 0; j < 4; j++)
798 tc = tl[i]->get( j*2+1 );
799 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
800 base[1] + layer_scale * j/4 );
802 tc = tl[i]->get( (j+1)*2 );
803 sgSetVec2( tc, base[0] + layer_scale * i/4,
804 base[1] + layer_scale * (j+1)/4 );
807 tc = tl[i]->get( 9 );
808 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
809 base[1] + layer_scale );
821 void SGCloudLayer::draw( bool top ) {
822 if ( layer_coverage != SG_CLOUD_CLEAR ) {
824 if ( bump_mapping && enable_bump_mapping ) {
829 ssgGetModelviewMatrix( modelview );
830 layer_transform->getTransform( transform );
832 sgInvertMat4( tmp, transform );
834 sgPostMultMat4( transform, modelview );
835 ssgLoadModelviewMatrix( transform );
838 ssgGetLight( 0 )->getPosition( lightVec );
839 sgNegateVec3( lightVec );
840 sgXformVec3( lightVec, tmp );
842 for ( int i = 0; i < 25; i++ ) {
843 CloudVertex &v = vertices[ i ];
844 sgSetVec3( v.tangentSpLight,
845 sgScalarProductVec3( v.sTangent, lightVec ),
846 sgScalarProductVec3( v.tTangent, lightVec ),
847 sgScalarProductVec3( v.normal, lightVec ) );
850 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
851 if ( top && decal == 0 ) {
852 decal = color_map[ layer_coverage ][ 0 ];
854 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
855 if ( top && normal == 0 ) {
856 normal = normal_map[ layer_coverage ][ 0 ];
859 glDisable( GL_LIGHTING );
860 glDisable( GL_CULL_FACE );
861 if ( layer_coverage == SG_CLOUD_FEW ) {
862 glEnable( GL_ALPHA_TEST );
863 glAlphaFunc ( GL_GREATER, 0.01 );
865 glEnable( GL_BLEND );
866 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
868 glShadeModel( GL_SMOOTH );
869 glEnable( GL_COLOR_MATERIAL );
873 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
874 emis = ( color[0]+color[1]+color[2] ) / 3.0;
878 sgSetVec4( color, emis, emis, emis, 0.0 );
879 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
880 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
881 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
882 sgSetVec4( color, 0.5, 0.5, 0.5, 0.0 );
883 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
884 sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
885 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
887 glActiveTexturePtr( GL_TEXTURE0_ARB );
888 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
889 glEnable( GL_TEXTURE_2D );
891 //Bind normalisation cube map to texture unit 1
892 glActiveTexturePtr( GL_TEXTURE1_ARB );
893 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
894 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
895 glActiveTexturePtr( GL_TEXTURE0_ARB );
897 //Set vertex arrays for cloud
898 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
899 glEnableClientState( GL_VERTEX_ARRAY );
901 if ( nb_texture_unit >= 3 ) {
902 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
903 glEnableClientState( GL_COLOR_ARRAY );
906 //Send texture coords for normal map to unit 0
907 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
908 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
910 //Send tangent space light vectors for normalisation to unit 1
911 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
912 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
913 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
915 //Set up texture environment to do (tex0 dot tex1)*color
916 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
917 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
918 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
919 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
920 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
922 glActiveTexturePtr( GL_TEXTURE1_ARB );
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_DOT3_RGB_ARB );
927 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
928 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
929 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
931 if ( nb_texture_unit >= 3 ) {
932 glActiveTexturePtr( GL_TEXTURE2_ARB );
933 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
935 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
936 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
937 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
940 if ( top || layer_coverage != SG_CLOUD_OVERCAST ) {
941 sgSetVec4( factors, 0.5f, 0.5f, 0.3f, 1.0f );
943 sgSetVec4( factors, 0.1f, 0.1f, 0.0f, 1.0f );
945 glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, factors );
947 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
948 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE_ARB );
949 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
950 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
951 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB );
953 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
954 glActiveTexturePtr( GL_TEXTURE0_ARB );
957 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
958 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
959 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
960 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
962 glDisable( GL_TEXTURE_2D );
963 glActiveTexturePtr( GL_TEXTURE1_ARB );
964 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
965 glActiveTexturePtr( GL_TEXTURE2_ARB );
966 glDisable( GL_TEXTURE_2D );
967 glActiveTexturePtr( GL_TEXTURE0_ARB );
969 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
970 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
971 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
972 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
973 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
974 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
976 glDisableClientState( GL_COLOR_ARRAY );
977 glEnable( GL_LIGHTING );
979 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
982 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
983 glActiveTexturePtr( GL_TEXTURE0_ARB );
986 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
987 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
988 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
989 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
992 glDisable( GL_TEXTURE_2D );
994 glActiveTexturePtr( GL_TEXTURE1_ARB );
995 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
996 glActiveTexturePtr( GL_TEXTURE0_ARB );
998 //disable vertex arrays
999 glDisableClientState( GL_VERTEX_ARRAY );
1001 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1002 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
1003 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1004 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1006 //Return to standard modulate texenv
1007 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
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 );
1052 glDisable( GL_TEXTURE_2D );
1054 glDisableClientState( GL_VERTEX_ARRAY );
1055 glDisableClientState( GL_NORMAL_ARRAY );
1057 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1058 glEnable( GL_CULL_FACE );
1059 glDepthFunc(GL_LESS);
1061 ssgLoadModelviewMatrix( modelview );
1064 state_sel->selectStep( top ? 1 : 0 );
1065 ssgCullAndDraw( layer_root );
1071 // make an ssgSimpleState for a cloud layer given the named texture
1072 ssgSimpleState *sgCloudMakeState( const string &path ) {
1073 ssgSimpleState *state = new ssgSimpleState();
1075 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1077 state->setTexture( (char *)path.c_str() );
1078 state->setShadeModel( GL_SMOOTH );
1079 state->disable( GL_LIGHTING );
1080 state->disable( GL_CULL_FACE );
1081 state->enable( GL_TEXTURE_2D );
1082 state->enable( GL_COLOR_MATERIAL );
1083 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1084 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1085 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1086 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1087 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1088 state->enable( GL_BLEND );
1089 state->enable( GL_ALPHA_TEST );
1090 state->setAlphaClamp( 0.01 );