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>
27 #include <simgear/math/point3d.hxx>
28 #include <simgear/math/polar3d.hxx>
29 #include <simgear/math/sg_random.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/screen/extensions.hxx>
33 #include <simgear/screen/texture.hxx>
34 #include <simgear/structure/ssgSharedPtr.hxx>
36 #include "newcloud.hxx"
37 #include "cloudfield.hxx"
40 #if defined(__MINGW32__)
41 #define isnan(x) _isnan(x)
44 #if defined (__FreeBSD__)
45 # if __FreeBSD_version < 500000
47 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
53 static ssgSharedPtr<ssgStateSelector> layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
54 static bool state_initialized = false;
55 static bool bump_mapping = false;
56 static GLint nb_texture_unit = 0;
57 static ssgSharedPtr<ssgTexture> normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
58 static ssgSharedPtr<ssgTexture> color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
59 static GLuint normalization_cube_map;
61 static glActiveTextureProc glActiveTexturePtr = 0;
62 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
63 static glBlendColorProc glBlendColorPtr = 0;
65 bool SGCloudLayer::enable_bump_mapping = false;
68 generateNormalizationCubeMap()
70 unsigned char data[ 32 * 32 * 3 ];
72 const float half_size = 16.0f,
75 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
78 unsigned char *ptr = data;
79 for ( j = 0; j < size; j++ ) {
80 for ( i = 0; i < size; i++ ) {
82 sgSetVec3( tmp, half_size,
83 -( j + offset - half_size ),
84 -( i + offset - half_size ) );
85 sgNormalizeVec3( tmp );
86 sgScaleVec3( tmp, 0.5f );
87 sgAddVec3( tmp, zero_normal );
89 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
90 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
91 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
94 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
95 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
98 for ( j = 0; j < size; j++ ) {
99 for ( i = 0; i < size; i++ ) {
101 sgSetVec3( tmp, -half_size,
102 -( j + offset - half_size ),
103 ( i + offset - half_size ) );
104 sgNormalizeVec3( tmp );
105 sgScaleVec3( tmp, 0.5f );
106 sgAddVec3( tmp, zero_normal );
108 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
109 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
110 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
113 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
114 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
117 for ( j = 0; j < size; j++ ) {
118 for ( i = 0; i < size; i++ ) {
120 sgSetVec3( tmp, ( i + offset - half_size ),
122 ( j + offset - half_size ) );
123 sgNormalizeVec3( tmp );
124 sgScaleVec3( tmp, 0.5f );
125 sgAddVec3( tmp, zero_normal );
127 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
128 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
129 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
132 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
133 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
136 for ( j = 0; j < size; j++ ) {
137 for ( i = 0; i < size; i++ ) {
139 sgSetVec3( tmp, ( i + offset - half_size ),
141 -( j + offset - half_size ) );
142 sgNormalizeVec3( tmp );
143 sgScaleVec3( tmp, 0.5f );
144 sgAddVec3( tmp, zero_normal );
146 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
147 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
148 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
151 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
152 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
155 for ( j = 0; j < size; j++ ) {
156 for ( i = 0; i < size; i++ ) {
158 sgSetVec3( tmp, ( i + offset - half_size ),
159 -( j + offset - half_size ),
161 sgNormalizeVec3( tmp );
162 sgScaleVec3( tmp, 0.5f );
163 sgAddVec3( tmp, zero_normal );
165 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
166 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
167 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
170 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
171 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
174 for ( j = 0; j < size; j++ ) {
175 for ( i = 0; i < size; i++ ) {
177 sgSetVec3( tmp, -( i + offset - half_size ),
178 -( j + offset - half_size ),
180 sgNormalizeVec3( tmp );
181 sgScaleVec3( tmp, 0.5f );
182 sgAddVec3( tmp, zero_normal );
184 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
185 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
186 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
189 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
190 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
195 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
198 layer_root(new ssgRoot),
199 layer_transform(new ssgTransform),
202 texture_path(tex_path),
205 layer_thickness(0.0),
206 layer_transition(0.0),
207 layer_coverage(SG_CLOUD_CLEAR),
214 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
215 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
216 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
217 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
219 layer_root->addKid(layer_transform);
220 layer3D = new SGCloudField;
225 SGCloudLayer::~SGCloudLayer()
230 delete layer_root; // deletes layer_transform and layer as well
234 SGCloudLayer::getSpan_m () const
240 SGCloudLayer::setSpan_m (float span_m)
242 if (span_m != layer_span) {
249 SGCloudLayer::getElevation_m () const
255 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
257 layer_asl = elevation_m;
260 if (elevation_m > 4000)
261 setSpan_m( elevation_m * 10 );
268 SGCloudLayer::getThickness_m () const
270 return layer_thickness;
274 SGCloudLayer::setThickness_m (float thickness_m)
276 layer_thickness = thickness_m;
280 SGCloudLayer::getTransition_m () const
282 return layer_transition;
286 SGCloudLayer::setTransition_m (float transition_m)
288 layer_transition = transition_m;
291 SGCloudLayer::Coverage
292 SGCloudLayer::getCoverage () const
294 return layer_coverage;
298 SGCloudLayer::setCoverage (Coverage coverage)
300 if (coverage != layer_coverage) {
301 layer_coverage = coverage;
307 // build the cloud object
309 SGCloudLayer::rebuild()
311 // Initialize states and sizes if necessary.
312 if ( !state_initialized ) {
313 state_initialized = true;
315 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
317 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
318 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
319 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
320 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
321 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
323 if ( bump_mapping ) {
324 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
325 if ( nb_texture_unit < 2 ) {
326 bump_mapping = false;
328 //nb_texture_unit = 2; // Force the number of units for now
331 if ( bump_mapping ) {
333 // This bump mapping code was inspired by the tutorial available at
334 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
335 // and a NVidia white paper
336 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
337 // The normal map textures were generated by the normal map Gimp plugin :
338 // http://nifelheim.dyndns.org/~cocidius/normalmap/
342 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
343 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
344 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
346 cloud_path.set(texture_path.str());
347 cloud_path.append("overcast.rgb");
348 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
349 cloud_path.set(texture_path.str());
350 cloud_path.append("overcast_n.rgb");
351 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
353 cloud_path.set(texture_path.str());
354 cloud_path.append("overcast_top.rgb");
355 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
356 cloud_path.set(texture_path.str());
357 cloud_path.append("overcast_top_n.rgb");
358 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
360 cloud_path.set(texture_path.str());
361 cloud_path.append("broken.rgba");
362 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
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() );
367 cloud_path.set(texture_path.str());
368 cloud_path.append("scattered.rgba");
369 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
370 cloud_path.set(texture_path.str());
371 cloud_path.append("scattered_n.rgb");
372 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
374 cloud_path.set(texture_path.str());
375 cloud_path.append("few.rgba");
376 color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
377 cloud_path.set(texture_path.str());
378 cloud_path.append("few_n.rgb");
379 normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
381 cloud_path.set(texture_path.str());
382 cloud_path.append("cirrus.rgba");
383 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
384 cloud_path.set(texture_path.str());
385 cloud_path.append("cirrus_n.rgb");
386 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
388 glGenTextures( 1, &normalization_cube_map );
389 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
390 generateNormalizationCubeMap();
391 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
392 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
393 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
394 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
395 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
398 ssgStateSelector *state_sel;
399 ssgSimpleState *state;
401 state_sel = new ssgStateSelector( 2 );
402 cloud_path.set(texture_path.str());
403 cloud_path.append("overcast.rgb");
404 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
405 cloud_path.set(texture_path.str());
406 cloud_path.append("overcast_top.rgb");
407 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
408 layer_states[SG_CLOUD_OVERCAST] = state_sel;
410 state_sel = new ssgStateSelector( 2 );
411 cloud_path.set(texture_path.str());
412 cloud_path.append("broken.rgba");
413 state = sgCloudMakeState(cloud_path.str());
414 state_sel->setStep( 0, state );
415 state_sel->setStep( 1, state );
416 layer_states[SG_CLOUD_BROKEN] = state_sel;
418 state_sel = new ssgStateSelector( 2 );
419 cloud_path.set(texture_path.str());
420 cloud_path.append("scattered.rgba");
421 state = sgCloudMakeState(cloud_path.str());
422 state_sel->setStep( 0, state );
423 state_sel->setStep( 1, state );
424 layer_states[SG_CLOUD_SCATTERED] = state_sel;
426 state_sel = new ssgStateSelector( 2 );
427 cloud_path.set(texture_path.str());
428 cloud_path.append("few.rgba");
429 state = sgCloudMakeState(cloud_path.str());
430 state_sel->setStep( 0, state );
431 state_sel->setStep( 1, state );
432 layer_states[SG_CLOUD_FEW] = state_sel;
434 state_sel = new ssgStateSelector( 2 );
435 cloud_path.set(texture_path.str());
436 cloud_path.append("cirrus.rgba");
437 state = sgCloudMakeState(cloud_path.str());
438 state_sel->setStep( 0, state );
439 state_sel->setStep( 1, state );
440 layer_states[SG_CLOUD_CIRRUS] = state_sel;
442 layer_states[SG_CLOUD_CLEAR] = 0;
444 SGNewCloud::loadTextures(texture_path.str());
445 layer3D->buildTestLayer();
448 if ( bump_mapping ) {
451 vertices = new CloudVertex[ 25 ];
452 indices = new unsigned int[ 40 ];
456 sgSetVec2( base, sg_random(), sg_random() );
458 const float layer_scale = layer_span / scale;
459 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
460 const float half_angle = 0.5 * layer_span / layer_to_core;
463 for ( i = -2; i <= 2; i++ ) {
464 for ( int j = -2; j <= 2; j++ ) {
465 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
466 sgSetVec3( v1.position,
467 0.5 * i * layer_span,
468 0.5 * j * layer_span,
469 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
470 sgSetVec2( v1.texCoord,
471 base[0] + layer_scale * i * 0.25,
472 base[1] + layer_scale * j * 0.25 );
473 sgSetVec3( v1.sTangent,
474 cos( i * half_angle ),
476 -sin( i * half_angle ) );
477 sgSetVec3( v1.tTangent,
479 cos( j * half_angle ),
480 -sin( j * half_angle ) );
481 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
482 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : cloud_alpha * 0.15f );
486 * 0 1 5 6 10 11 15 16 20 21
487 * 1 2 6 7 11 12 16 17 21 22
488 * 2 3 7 8 12 13 17 18 22 23
489 * 3 4 8 9 13 14 18 19 23 24
491 for ( i = 0; i < 4; i++ ) {
492 for ( int j = 0; j < 5; j++ ) {
493 indices[ i*10 + (j*2) ] = i + 5 * j;
494 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
501 last_lon = last_lat = -999.0f;
504 sgSetVec2( base, sg_random(), sg_random() );
506 // build the cloud layer
511 const float layer_scale = layer_span / scale;
512 const float mpi = SG_PI/4;
514 // caclculate the difference between a flat-earth model and
515 // a round earth model given the span and altutude ASL of
516 // the cloud layer. This is the difference in altitude between
517 // the top of the inverted bowl and the edge of the bowl.
518 // const float alt_diff = layer_asl * 0.8;
519 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
520 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
521 const float border_to_core = layer_to_core * cos(layer_angle);
522 const float alt_diff = layer_to_core - border_to_core;
524 for (int i = 0; i < 4; i++)
526 if ( layer[i] != NULL ) {
527 layer_transform->removeKid(layer[i]); // automatic delete
530 vl[i] = new ssgVertexArray( 10 );
531 cl[i] = new ssgColourArray( 10 );
532 tl[i] = new ssgTexCoordArray( 10 );
535 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
536 alt_diff * (sin(i*mpi) - 2) );
538 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
540 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
543 vl[i]->add( vertex );
546 for (int j = 0; j < 4; j++)
548 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
549 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
551 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
552 base[1] + layer_scale * j/4 );
554 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
555 ( (j == 0) || (i == 3)) ?
556 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
559 vl[i]->add( vertex );
563 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
564 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
566 sgSetVec2( tc, base[0] + layer_scale * i/4,
567 base[1] + layer_scale * (j+1)/4 );
569 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
570 ((j == 3) || (i == 0)) ?
571 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
573 vl[i]->add( vertex );
577 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
578 alt_diff * (sin((i+1)*mpi) - 2) );
580 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
581 base[1] + layer_scale );
583 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
586 vl[i]->add( vertex );
589 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
590 layer_transform->addKid( layer[i] );
592 if ( layer_states[layer_coverage] != NULL ) {
593 layer[i]->setState( layer_states[layer_coverage] );
595 state_sel = layer_states[layer_coverage];
598 // force a repaint of the sky colors with arbitrary defaults
604 // repaint the cloud layer colors
605 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
607 if ( bump_mapping && enable_bump_mapping ) {
609 for ( int i = 0; i < 25; i++ ) {
610 sgCopyVec3( vertices[ i ].color, fog_color );
616 for ( int i = 0; i < 4; i++ ) {
617 color = cl[i]->get( 0 );
618 sgCopyVec3( color, fog_color );
619 color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
621 for ( int j = 0; j < 4; ++j ) {
622 color = cl[i]->get( (2*j) + 1 );
623 sgCopyVec3( color, fog_color );
625 ((j == 0) || (i == 3)) ?
626 ((j == 0) && (i == 3)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
628 color = cl[i]->get( (2*j) + 2 );
629 sgCopyVec3( color, fog_color );
631 ((j == 3) || (i == 0)) ?
632 ((j == 3) && (i == 0)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
635 color = cl[i]->get( 9 );
636 sgCopyVec3( color, fog_color );
637 color[3] = (i == 3) ? 0.0f : cloud_alpha * 0.15f;
644 // reposition the cloud layer at the specified origin and orientation
645 // lon specifies a rotation about the Z axis
646 // lat specifies a rotation about the new Y axis
647 // spin specifies a rotation about the new Z axis (and orients the
648 // sunrise/set effects
649 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
650 double alt, double dt )
655 // combine p and asl (meters) to get translation offset
657 sgCopyVec3( asl_offset, up );
658 sgNormalizeVec3( asl_offset );
659 if ( alt <= layer_asl ) {
660 sgScaleVec3( asl_offset, layer_asl );
662 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
664 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
665 // << "," << asl_offset[2] << endl;
666 sgAddVec3( asl_offset, p );
667 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
668 // << "," << asl_offset[2] << endl;
670 // Translate to zero elevation
671 // Point3D zero_elev = current_view.get_cur_zero_elev();
672 sgMakeTransMat4( T1, asl_offset );
674 // printf(" Translated to %.2f %.2f %.2f\n",
675 // zero_elev.x, zero_elev.y, zero_elev.z );
677 // Rotate to proper orientation
678 // printf(" lon = %.2f lat = %.2f\n",
679 // lon * SGD_RADIANS_TO_DEGREES,
680 // lat * SGD_RADIANS_TO_DEGREES);
681 sgSetVec3( axis, 0.0, 0.0, 1.0 );
682 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
684 sgSetVec3( axis, 0.0, 1.0, 0.0 );
685 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
689 sgCopyMat4( TRANSFORM, T1 );
690 sgPreMultMat4( TRANSFORM, LON );
691 sgPreMultMat4( TRANSFORM, LAT );
694 sgSetCoord( &layerpos, TRANSFORM );
696 layer_transform->setTransform( &layerpos );
698 // now calculate update texture coordinates
699 if ( last_lon < -900 ) {
704 double sp_dist = speed*dt;
706 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
707 Point3D start( last_lon, last_lat, 0.0 );
708 Point3D dest( lon, lat, 0.0 );
709 double course = 0.0, dist = 0.0;
711 calc_gc_course_dist( dest, start, &course, &dist );
712 // cout << "course = " << course << ", dist = " << dist << endl;
714 // if start and dest are too close together,
715 // calc_gc_course_dist() can return a course of "nan". If
716 // this happens, lets just use the last known good course.
717 // This is a hack, and it would probably be better to make
718 // calc_gc_course_dist() more robust.
719 if ( isnan(course) ) {
720 course = last_course;
722 last_course = course;
725 // calculate cloud movement due to external forces
726 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
729 ax = cos(course) * dist;
730 ay = sin(course) * dist;
734 bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
735 by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
739 double xoff = (ax + bx) / (2 * scale);
740 double yoff = (ay + by) / (2 * scale);
742 const float layer_scale = layer_span / scale;
744 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
747 if ( bump_mapping && enable_bump_mapping ) {
748 base = vertices[12].texCoord;
750 base = tl[0]->get( 0 );
754 // the while loops can lead to *long* pauses if base[0] comes
755 // with a bogus value.
756 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
757 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
758 if ( base[0] > -10.0 && base[0] < 10.0 ) {
759 base[0] -= (int)base[0];
761 SG_LOG(SG_ASTRO, SG_DEBUG,
762 "Error: base = " << base[0] << "," << base[1] <<
763 " course = " << course << " dist = " << dist );
768 // the while loops can lead to *long* pauses if base[0] comes
769 // with a bogus value.
770 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
771 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
772 if ( base[1] > -10.0 && base[1] < 10.0 ) {
773 base[1] -= (int)base[1];
775 SG_LOG(SG_ASTRO, SG_ALERT,
776 "Error: base = " << base[0] << "," << base[1] <<
777 " course = " << course << " dist = " << dist );
781 if ( bump_mapping && enable_bump_mapping ) {
783 for ( int i = -2; i <= 2; i++ ) {
784 for ( int j = -2; j <= 2; j++ ) {
785 if ( i == 0 && j == 0 )
786 continue; // Already done on base
787 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
788 sgSetVec2( v1.texCoord,
789 base[0] + layer_scale * i * 0.25,
790 base[1] + layer_scale * j * 0.25 );
795 // cout << "base = " << base[0] << "," << base[1] << endl;
798 for (int i = 0; i < 4; i++) {
799 tc = tl[i]->get( 0 );
800 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
802 for (int j = 0; j < 4; j++)
804 tc = tl[i]->get( j*2+1 );
805 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
806 base[1] + layer_scale * j/4 );
808 tc = tl[i]->get( (j+1)*2 );
809 sgSetVec2( tc, base[0] + layer_scale * i/4,
810 base[1] + layer_scale * (j+1)/4 );
813 tc = tl[i]->get( 9 );
814 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
815 base[1] + layer_scale );
823 layer3D->reposition( p, up, lon, lat, alt, dt, direction, speed);
828 void SGCloudLayer::draw( bool top ) {
829 if ( layer_coverage != SG_CLOUD_CLEAR ) {
831 if ( SGCloudField::enable3D && layer3D->is3D())
834 if ( bump_mapping && enable_bump_mapping ) {
839 ssgGetModelviewMatrix( modelview );
840 layer_transform->getTransform( transform );
842 sgTransposeNegateMat4( tmp, transform );
844 sgPostMultMat4( transform, modelview );
845 ssgLoadModelviewMatrix( transform );
848 ssgGetLight( 0 )->getPosition( lightVec );
849 sgNegateVec3( lightVec );
850 sgXformVec3( lightVec, tmp );
852 for ( int i = 0; i < 25; i++ ) {
853 CloudVertex &v = vertices[ i ];
854 sgSetVec3( v.tangentSpLight,
855 sgScalarProductVec3( v.sTangent, lightVec ),
856 sgScalarProductVec3( v.tTangent, lightVec ),
857 sgScalarProductVec3( v.normal, lightVec ) );
860 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
861 if ( top && decal == 0 ) {
862 decal = color_map[ layer_coverage ][ 0 ];
864 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
865 if ( top && normal == 0 ) {
866 normal = normal_map[ layer_coverage ][ 0 ];
869 glDisable( GL_LIGHTING );
870 glDisable( GL_CULL_FACE );
871 // glDisable( GL_ALPHA_TEST );
872 if ( layer_coverage == SG_CLOUD_FEW ) {
873 glEnable( GL_ALPHA_TEST );
874 glAlphaFunc ( GL_GREATER, 0.01 );
876 glEnable( GL_BLEND );
877 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
879 glShadeModel( GL_SMOOTH );
880 glEnable( GL_COLOR_MATERIAL );
884 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
885 emis = ( color[0]+color[1]+color[2] ) / 3.0;
889 sgSetVec4( color, emis, emis, emis, 0.0 );
890 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
891 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
892 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
893 sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
894 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
895 sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
896 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
898 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
900 glActiveTexturePtr( GL_TEXTURE0_ARB );
901 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
902 glEnable( GL_TEXTURE_2D );
904 //Bind normalisation cube map to texture unit 1
905 glActiveTexturePtr( GL_TEXTURE1_ARB );
906 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
907 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
908 glActiveTexturePtr( GL_TEXTURE0_ARB );
910 //Set vertex arrays for cloud
911 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
912 glEnableClientState( GL_VERTEX_ARRAY );
914 if ( nb_texture_unit >= 3 ) {
915 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
916 glEnableClientState( GL_COLOR_ARRAY );
919 //Send texture coords for normal map to unit 0
920 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
921 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
923 //Send tangent space light vectors for normalisation to unit 1
924 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
925 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
926 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
928 //Set up texture environment to do (tex0 dot tex1)*color
929 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
930 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
931 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
932 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
933 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
935 glActiveTexturePtr( GL_TEXTURE1_ARB );
937 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
938 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
939 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
940 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
941 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
942 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
944 if ( nb_texture_unit >= 3 ) {
945 glActiveTexturePtr( GL_TEXTURE2_ARB );
946 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
948 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
949 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
950 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
952 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
953 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
954 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
955 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
957 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
958 glActiveTexturePtr( GL_TEXTURE0_ARB );
961 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
962 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
963 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
964 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
966 glDisable( GL_TEXTURE_2D );
967 glActiveTexturePtr( GL_TEXTURE1_ARB );
968 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
969 glActiveTexturePtr( GL_TEXTURE2_ARB );
970 glDisable( GL_TEXTURE_2D );
971 glActiveTexturePtr( GL_TEXTURE0_ARB );
973 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
974 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
975 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
976 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
977 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
978 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
980 glDisableClientState( GL_COLOR_ARRAY );
981 glEnable( GL_LIGHTING );
983 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
986 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
987 glActiveTexturePtr( GL_TEXTURE0_ARB );
990 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
991 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
992 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
993 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
996 glDisable( GL_TEXTURE_2D );
998 glActiveTexturePtr( GL_TEXTURE1_ARB );
999 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
1000 glActiveTexturePtr( GL_TEXTURE0_ARB );
1002 //disable vertex arrays
1003 glDisableClientState( GL_VERTEX_ARRAY );
1005 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1006 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
1007 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1008 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1010 //Return to standard modulate texenv
1011 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1013 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
1014 glDepthFunc(GL_LEQUAL);
1016 glEnable( GL_LIGHTING );
1018 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1019 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1020 average = 0.15 + average/10;
1021 sgVec4 averageColor;
1022 sgSetVec4( averageColor, average, average, average, 1.0f );
1023 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1025 glBlendColorPtr( average, average, average, 1.0f );
1026 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1028 //Perform a second pass to color the torus
1029 //Bind decal texture
1030 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1031 glEnable(GL_TEXTURE_2D);
1033 //Set vertex arrays for torus
1034 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1035 glEnableClientState( GL_VERTEX_ARRAY );
1037 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1038 //glEnableClientState( GL_COLOR_ARRAY );
1040 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1041 glEnableClientState( GL_NORMAL_ARRAY );
1043 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1044 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1047 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1048 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1049 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1050 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1052 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1054 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1058 glDisable( GL_TEXTURE_2D );
1060 glDisableClientState( GL_VERTEX_ARRAY );
1061 glDisableClientState( GL_NORMAL_ARRAY );
1063 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1064 glEnable( GL_CULL_FACE );
1065 glDepthFunc(GL_LESS);
1067 ssgLoadModelviewMatrix( modelview );
1070 state_sel->selectStep( top ? 1 : 0 );
1071 ssgCullAndDraw( layer_root );
1077 // make an ssgSimpleState for a cloud layer given the named texture
1078 ssgSimpleState *sgCloudMakeState( const string &path ) {
1079 ssgSimpleState *state = new ssgSimpleState();
1081 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1083 state->setTexture( (char *)path.c_str() );
1084 state->setShadeModel( GL_SMOOTH );
1085 state->disable( GL_LIGHTING );
1086 state->disable( GL_CULL_FACE );
1087 state->enable( GL_TEXTURE_2D );
1088 state->enable( GL_COLOR_MATERIAL );
1089 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1090 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1091 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1092 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1093 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1094 state->enable( GL_BLEND );
1095 state->enable( GL_ALPHA_TEST );
1096 state->setAlphaClamp( 0.01 );