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 ) :
202 layer_root(new ssgRoot),
203 layer_transform(new ssgTransform),
206 texture_path(tex_path),
209 layer_thickness(0.0),
210 layer_transition(0.0),
211 layer_coverage(SG_CLOUD_CLEAR),
218 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
219 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
220 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
221 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
223 layer_root->addKid(layer_transform);
228 SGCloudLayer::~SGCloudLayer()
232 delete layer_root; // deletes layer_transform and layer as well
236 SGCloudLayer::getSpan_m () const
242 SGCloudLayer::setSpan_m (float span_m)
244 if (span_m != layer_span) {
251 SGCloudLayer::getElevation_m () const
257 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
259 layer_asl = elevation_m;
262 if (elevation_m > 4000)
263 setSpan_m( elevation_m * 10 );
270 SGCloudLayer::getThickness_m () const
272 return layer_thickness;
276 SGCloudLayer::setThickness_m (float thickness_m)
278 layer_thickness = thickness_m;
282 SGCloudLayer::getTransition_m () const
284 return layer_transition;
288 SGCloudLayer::setTransition_m (float transition_m)
290 layer_transition = transition_m;
293 SGCloudLayer::Coverage
294 SGCloudLayer::getCoverage () const
296 return layer_coverage;
300 SGCloudLayer::setCoverage (Coverage coverage)
302 if (coverage != layer_coverage) {
303 layer_coverage = coverage;
309 // build the cloud object
311 SGCloudLayer::rebuild()
313 // Initialize states and sizes if necessary.
314 if ( !state_initialized ) {
315 state_initialized = true;
317 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
319 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
320 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
321 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
322 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
323 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
325 if ( bump_mapping ) {
326 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
327 if ( nb_texture_unit < 2 ) {
328 bump_mapping = false;
330 //nb_texture_unit = 2; // Force the number of units for now
333 if ( bump_mapping ) {
335 // This bump mapping code was inspired by the tutorial available at
336 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
337 // and a NVidia white paper
338 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
339 // The normal map textures were generated by the normal map Gimp plugin :
340 // http://nifelheim.dyndns.org/~cocidius/normalmap/
344 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
345 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
346 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
348 cloud_path.set(texture_path.str());
349 cloud_path.append("overcast.rgb");
350 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
351 color_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
352 cloud_path.set(texture_path.str());
353 cloud_path.append("overcast_n.rgb");
354 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
355 normal_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
357 cloud_path.set(texture_path.str());
358 cloud_path.append("overcast_top.rgb");
359 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
360 color_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
361 cloud_path.set(texture_path.str());
362 cloud_path.append("overcast_top_n.rgb");
363 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
364 normal_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
366 cloud_path.set(texture_path.str());
367 cloud_path.append("broken.rgba");
368 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
369 color_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
370 cloud_path.set(texture_path.str());
371 cloud_path.append("broken_n.rgb");
372 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
373 normal_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
375 cloud_path.set(texture_path.str());
376 cloud_path.append("scattered.rgba");
377 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
378 color_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
379 cloud_path.set(texture_path.str());
380 cloud_path.append("scattered_n.rgb");
381 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
382 normal_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
384 cloud_path.set(texture_path.str());
385 cloud_path.append("few.rgba");
386 color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
387 color_map[ SG_CLOUD_FEW ][ 0 ]->ref();
388 cloud_path.set(texture_path.str());
389 cloud_path.append("few_n.rgb");
390 normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
391 normal_map[ SG_CLOUD_FEW ][ 0 ]->ref();
393 cloud_path.set(texture_path.str());
394 cloud_path.append("cirrus.rgba");
395 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
396 color_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
397 cloud_path.set(texture_path.str());
398 cloud_path.append("cirrus_n.rgb");
399 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
400 normal_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
402 glGenTextures( 1, &normalization_cube_map );
403 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
404 generateNormalizationCubeMap();
405 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
406 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
407 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
408 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
409 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
412 ssgStateSelector *state_sel;
413 ssgSimpleState *state;
415 state_sel = new ssgStateSelector( 2 );
417 cloud_path.set(texture_path.str());
418 cloud_path.append("overcast.rgb");
419 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
420 cloud_path.set(texture_path.str());
421 cloud_path.append("overcast_top.rgb");
422 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
423 layer_states[SG_CLOUD_OVERCAST] = state_sel;
425 state_sel = new ssgStateSelector( 2 );
427 cloud_path.set(texture_path.str());
428 cloud_path.append("broken.rgba");
429 state = sgCloudMakeState(cloud_path.str());
430 state_sel->setStep( 0, state );
431 state_sel->setStep( 1, state );
432 layer_states[SG_CLOUD_BROKEN] = state_sel;
434 state_sel = new ssgStateSelector( 2 );
436 cloud_path.set(texture_path.str());
437 cloud_path.append("scattered.rgba");
438 state = sgCloudMakeState(cloud_path.str());
439 state_sel->setStep( 0, state );
440 state_sel->setStep( 1, state );
441 layer_states[SG_CLOUD_SCATTERED] = state_sel;
443 state_sel = new ssgStateSelector( 2 );
445 cloud_path.set(texture_path.str());
446 cloud_path.append("few.rgba");
447 state = sgCloudMakeState(cloud_path.str());
448 state_sel->setStep( 0, state );
449 state_sel->setStep( 1, state );
450 layer_states[SG_CLOUD_FEW] = state_sel;
452 state_sel = new ssgStateSelector( 2 );
454 cloud_path.set(texture_path.str());
455 cloud_path.append("cirrus.rgba");
456 state = sgCloudMakeState(cloud_path.str());
457 state_sel->setStep( 0, state );
458 state_sel->setStep( 1, state );
459 layer_states[SG_CLOUD_CIRRUS] = state_sel;
461 layer_states[SG_CLOUD_CLEAR] = 0;
465 if ( bump_mapping ) {
468 vertices = new CloudVertex[ 25 ];
469 indices = new unsigned int[ 40 ];
473 sgSetVec2( base, sg_random(), sg_random() );
475 const float layer_scale = layer_span / scale;
476 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
477 const float half_angle = 0.5 * layer_span / layer_to_core;
480 for ( i = -2; i <= 2; i++ ) {
481 for ( int j = -2; j <= 2; j++ ) {
482 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
483 sgSetVec3( v1.position,
484 0.5 * i * layer_span,
485 0.5 * j * layer_span,
486 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
487 sgSetVec2( v1.texCoord,
488 base[0] + layer_scale * i * 0.25,
489 base[1] + layer_scale * j * 0.25 );
490 sgSetVec3( v1.sTangent,
491 cos( i * half_angle ),
493 -sin( i * half_angle ) );
494 sgSetVec3( v1.tTangent,
496 cos( j * half_angle ),
497 -sin( j * half_angle ) );
498 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
499 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : cloud_alpha * 0.15f );
503 * 0 1 5 6 10 11 15 16 20 21
504 * 1 2 6 7 11 12 16 17 21 22
505 * 2 3 7 8 12 13 17 18 22 23
506 * 3 4 8 9 13 14 18 19 23 24
508 for ( i = 0; i < 4; i++ ) {
509 for ( int j = 0; j < 5; j++ ) {
510 indices[ i*10 + (j*2) ] = i + 5 * j;
511 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
518 last_lon = last_lat = -999.0f;
521 sgSetVec2( base, sg_random(), sg_random() );
523 // build the cloud layer
528 const float layer_scale = layer_span / scale;
529 const float mpi = SG_PI/4;
531 // caclculate the difference between a flat-earth model and
532 // a round earth model given the span and altutude ASL of
533 // the cloud layer. This is the difference in altitude between
534 // the top of the inverted bowl and the edge of the bowl.
535 // const float alt_diff = layer_asl * 0.8;
536 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
537 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
538 const float border_to_core = layer_to_core * cos(layer_angle);
539 const float alt_diff = layer_to_core - border_to_core;
541 for (int i = 0; i < 4; i++)
543 if ( layer[i] != NULL ) {
544 layer_transform->removeKid(layer[i]); // automatic delete
547 vl[i] = new ssgVertexArray( 10 );
548 cl[i] = new ssgColourArray( 10 );
549 tl[i] = new ssgTexCoordArray( 10 );
552 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
553 alt_diff * (sin(i*mpi) - 2) );
555 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
557 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
560 vl[i]->add( vertex );
563 for (int j = 0; j < 4; j++)
565 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
566 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
568 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
569 base[1] + layer_scale * j/4 );
571 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
572 ( (j == 0) || (i == 3)) ?
573 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
576 vl[i]->add( vertex );
580 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
581 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
583 sgSetVec2( tc, base[0] + layer_scale * i/4,
584 base[1] + layer_scale * (j+1)/4 );
586 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
587 ((j == 3) || (i == 0)) ?
588 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
590 vl[i]->add( vertex );
594 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
595 alt_diff * (sin((i+1)*mpi) - 2) );
597 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
598 base[1] + layer_scale );
600 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
603 vl[i]->add( vertex );
606 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
607 layer_transform->addKid( layer[i] );
609 if ( layer_states[layer_coverage] != NULL ) {
610 layer[i]->setState( layer_states[layer_coverage] );
612 state_sel = layer_states[layer_coverage];
615 // force a repaint of the sky colors with arbitrary defaults
621 // repaint the cloud layer colors
622 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
624 if ( bump_mapping && enable_bump_mapping ) {
626 for ( int i = 0; i < 25; i++ ) {
627 sgCopyVec3( vertices[ i ].color, fog_color );
633 for ( int i = 0; i < 4; i++ ) {
634 color = cl[i]->get( 0 );
635 sgCopyVec3( color, fog_color );
636 color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
638 for ( int j = 0; j < 4; ++j ) {
639 color = cl[i]->get( (2*j) + 1 );
640 sgCopyVec3( color, fog_color );
642 ((j == 0) || (i == 3)) ?
643 ((j == 0) && (i == 3)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
645 color = cl[i]->get( (2*j) + 2 );
646 sgCopyVec3( color, fog_color );
648 ((j == 3) || (i == 0)) ?
649 ((j == 3) && (i == 0)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
652 color = cl[i]->get( 9 );
653 sgCopyVec3( color, fog_color );
654 color[3] = (i == 3) ? 0.0f : cloud_alpha * 0.15f;
661 // reposition the cloud layer at the specified origin and orientation
662 // lon specifies a rotation about the Z axis
663 // lat specifies a rotation about the new Y axis
664 // spin specifies a rotation about the new Z axis (and orients the
665 // sunrise/set effects
666 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
667 double alt, double dt )
672 // combine p and asl (meters) to get translation offset
674 sgCopyVec3( asl_offset, up );
675 sgNormalizeVec3( asl_offset );
676 if ( alt <= layer_asl ) {
677 sgScaleVec3( asl_offset, layer_asl );
679 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
681 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
682 // << "," << asl_offset[2] << endl;
683 sgAddVec3( asl_offset, p );
684 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
685 // << "," << asl_offset[2] << endl;
687 // Translate to zero elevation
688 // Point3D zero_elev = current_view.get_cur_zero_elev();
689 sgMakeTransMat4( T1, asl_offset );
691 // printf(" Translated to %.2f %.2f %.2f\n",
692 // zero_elev.x, zero_elev.y, zero_elev.z );
694 // Rotate to proper orientation
695 // printf(" lon = %.2f lat = %.2f\n",
696 // lon * SGD_RADIANS_TO_DEGREES,
697 // lat * SGD_RADIANS_TO_DEGREES);
698 sgSetVec3( axis, 0.0, 0.0, 1.0 );
699 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
701 sgSetVec3( axis, 0.0, 1.0, 0.0 );
702 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
706 sgCopyMat4( TRANSFORM, T1 );
707 sgPreMultMat4( TRANSFORM, LON );
708 sgPreMultMat4( TRANSFORM, LAT );
711 sgSetCoord( &layerpos, TRANSFORM );
713 layer_transform->setTransform( &layerpos );
715 // now calculate update texture coordinates
716 if ( last_lon < -900 ) {
721 double sp_dist = speed*dt;
723 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
724 Point3D start( last_lon, last_lat, 0.0 );
725 Point3D dest( lon, lat, 0.0 );
726 double course = 0.0, dist = 0.0;
728 calc_gc_course_dist( dest, start, &course, &dist );
729 // cout << "course = " << course << ", dist = " << dist << endl;
731 // if start and dest are too close together,
732 // calc_gc_course_dist() can return a course of "nan". If
733 // this happens, lets just use the last known good course.
734 // This is a hack, and it would probably be better to make
735 // calc_gc_course_dist() more robust.
736 if ( isnan(course) ) {
737 course = last_course;
739 last_course = course;
742 // calculate cloud movement due to external forces
743 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
746 ax = cos(course) * dist;
747 ay = sin(course) * dist;
751 bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
752 by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
756 double xoff = (ax + bx) / (2 * scale);
757 double yoff = (ay + by) / (2 * scale);
759 const float layer_scale = layer_span / scale;
761 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
764 if ( bump_mapping && enable_bump_mapping ) {
765 base = vertices[12].texCoord;
767 base = tl[0]->get( 0 );
771 // the while loops can lead to *long* pauses if base[0] comes
772 // with a bogus value.
773 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
774 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
775 if ( base[0] > -10.0 && base[0] < 10.0 ) {
776 base[0] -= (int)base[0];
778 SG_LOG(SG_ASTRO, SG_DEBUG,
779 "Error: base = " << base[0] << "," << base[1] <<
780 " course = " << course << " dist = " << dist );
785 // the while loops can lead to *long* pauses if base[0] comes
786 // with a bogus value.
787 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
788 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
789 if ( base[1] > -10.0 && base[1] < 10.0 ) {
790 base[1] -= (int)base[1];
792 SG_LOG(SG_ASTRO, SG_ALERT,
793 "Error: base = " << base[0] << "," << base[1] <<
794 " course = " << course << " dist = " << dist );
798 if ( bump_mapping && enable_bump_mapping ) {
800 for ( int i = -2; i <= 2; i++ ) {
801 for ( int j = -2; j <= 2; j++ ) {
802 if ( i == 0 && j == 0 )
803 continue; // Already done on base
804 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
805 sgSetVec2( v1.texCoord,
806 base[0] + layer_scale * i * 0.25,
807 base[1] + layer_scale * j * 0.25 );
812 // cout << "base = " << base[0] << "," << base[1] << endl;
815 for (int i = 0; i < 4; i++) {
816 tc = tl[i]->get( 0 );
817 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
819 for (int j = 0; j < 4; j++)
821 tc = tl[i]->get( j*2+1 );
822 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
823 base[1] + layer_scale * j/4 );
825 tc = tl[i]->get( (j+1)*2 );
826 sgSetVec2( tc, base[0] + layer_scale * i/4,
827 base[1] + layer_scale * (j+1)/4 );
830 tc = tl[i]->get( 9 );
831 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
832 base[1] + layer_scale );
844 void SGCloudLayer::draw( bool top ) {
845 if ( layer_coverage != SG_CLOUD_CLEAR ) {
847 if ( bump_mapping && enable_bump_mapping ) {
852 ssgGetModelviewMatrix( modelview );
853 layer_transform->getTransform( transform );
855 sgTransposeNegateMat4( tmp, transform );
857 sgPostMultMat4( transform, modelview );
858 ssgLoadModelviewMatrix( transform );
861 ssgGetLight( 0 )->getPosition( lightVec );
862 sgNegateVec3( lightVec );
863 sgXformVec3( lightVec, tmp );
865 for ( int i = 0; i < 25; i++ ) {
866 CloudVertex &v = vertices[ i ];
867 sgSetVec3( v.tangentSpLight,
868 sgScalarProductVec3( v.sTangent, lightVec ),
869 sgScalarProductVec3( v.tTangent, lightVec ),
870 sgScalarProductVec3( v.normal, lightVec ) );
873 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
874 if ( top && decal == 0 ) {
875 decal = color_map[ layer_coverage ][ 0 ];
877 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
878 if ( top && normal == 0 ) {
879 normal = normal_map[ layer_coverage ][ 0 ];
882 glDisable( GL_LIGHTING );
883 glDisable( GL_CULL_FACE );
884 // glDisable( GL_ALPHA_TEST );
885 if ( layer_coverage == SG_CLOUD_FEW ) {
886 glEnable( GL_ALPHA_TEST );
887 glAlphaFunc ( GL_GREATER, 0.01 );
889 glEnable( GL_BLEND );
890 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
892 glShadeModel( GL_SMOOTH );
893 glEnable( GL_COLOR_MATERIAL );
897 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
898 emis = ( color[0]+color[1]+color[2] ) / 3.0;
902 sgSetVec4( color, emis, emis, emis, 0.0 );
903 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
904 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
905 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
906 sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
907 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
908 sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
909 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
911 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
913 glActiveTexturePtr( GL_TEXTURE0_ARB );
914 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
915 glEnable( GL_TEXTURE_2D );
917 //Bind normalisation cube map to texture unit 1
918 glActiveTexturePtr( GL_TEXTURE1_ARB );
919 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
920 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
921 glActiveTexturePtr( GL_TEXTURE0_ARB );
923 //Set vertex arrays for cloud
924 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
925 glEnableClientState( GL_VERTEX_ARRAY );
927 if ( nb_texture_unit >= 3 ) {
928 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
929 glEnableClientState( GL_COLOR_ARRAY );
932 //Send texture coords for normal map to unit 0
933 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
934 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
936 //Send tangent space light vectors for normalisation to unit 1
937 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
938 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
939 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
941 //Set up texture environment to do (tex0 dot tex1)*color
942 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
943 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
944 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
945 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
946 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
948 glActiveTexturePtr( GL_TEXTURE1_ARB );
950 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
951 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
952 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
953 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
954 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
955 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
957 if ( nb_texture_unit >= 3 ) {
958 glActiveTexturePtr( GL_TEXTURE2_ARB );
959 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
961 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
962 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
963 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
965 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
966 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
967 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
968 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
970 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
971 glActiveTexturePtr( GL_TEXTURE0_ARB );
974 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
975 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
976 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
977 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
979 glDisable( GL_TEXTURE_2D );
980 glActiveTexturePtr( GL_TEXTURE1_ARB );
981 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
982 glActiveTexturePtr( GL_TEXTURE2_ARB );
983 glDisable( GL_TEXTURE_2D );
984 glActiveTexturePtr( GL_TEXTURE0_ARB );
986 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
987 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
988 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
989 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
990 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
991 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
993 glDisableClientState( GL_COLOR_ARRAY );
994 glEnable( GL_LIGHTING );
996 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
999 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1000 glActiveTexturePtr( GL_TEXTURE0_ARB );
1003 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1004 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1005 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1006 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1009 glDisable( GL_TEXTURE_2D );
1011 glActiveTexturePtr( GL_TEXTURE1_ARB );
1012 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
1013 glActiveTexturePtr( GL_TEXTURE0_ARB );
1015 //disable vertex arrays
1016 glDisableClientState( GL_VERTEX_ARRAY );
1018 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1019 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
1020 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1021 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1023 //Return to standard modulate texenv
1024 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1026 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
1027 glDepthFunc(GL_LEQUAL);
1029 glEnable( GL_LIGHTING );
1031 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1032 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1033 average = 0.15 + average/10;
1034 sgVec4 averageColor;
1035 sgSetVec4( averageColor, average, average, average, 1.0f );
1036 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1038 glBlendColorPtr( average, average, average, 1.0f );
1039 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1041 //Perform a second pass to color the torus
1042 //Bind decal texture
1043 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1044 glEnable(GL_TEXTURE_2D);
1046 //Set vertex arrays for torus
1047 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1048 glEnableClientState( GL_VERTEX_ARRAY );
1050 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1051 //glEnableClientState( GL_COLOR_ARRAY );
1053 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1054 glEnableClientState( GL_NORMAL_ARRAY );
1056 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1057 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1060 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1061 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1062 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1063 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1065 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1067 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1071 glDisable( GL_TEXTURE_2D );
1073 glDisableClientState( GL_VERTEX_ARRAY );
1074 glDisableClientState( GL_NORMAL_ARRAY );
1076 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1077 glEnable( GL_CULL_FACE );
1078 glDepthFunc(GL_LESS);
1080 ssgLoadModelviewMatrix( modelview );
1083 state_sel->selectStep( top ? 1 : 0 );
1084 ssgCullAndDraw( layer_root );
1090 // make an ssgSimpleState for a cloud layer given the named texture
1091 ssgSimpleState *sgCloudMakeState( const string &path ) {
1092 ssgSimpleState *state = new ssgSimpleState();
1094 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1096 state->setTexture( (char *)path.c_str() );
1097 state->setShadeModel( GL_SMOOTH );
1098 state->disable( GL_LIGHTING );
1099 state->disable( GL_CULL_FACE );
1100 state->enable( GL_TEXTURE_2D );
1101 state->enable( GL_COLOR_MATERIAL );
1102 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1103 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1104 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1105 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1106 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1107 state->enable( GL_BLEND );
1108 state->enable( GL_ALPHA_TEST );
1109 state->setAlphaClamp( 0.01 );