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_config.h>
22 #include <simgear/compiler.h>
30 #include <simgear/math/point3d.hxx>
31 #include <simgear/math/polar3d.hxx>
32 #include <simgear/math/sg_random.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/screen/extensions.hxx>
36 #include <simgear/screen/texture.hxx>
37 #include <simgear/structure/ssgSharedPtr.hxx>
39 #include "newcloud.hxx"
40 #include "cloudfield.hxx"
43 #if defined(__MINGW32__)
44 #define isnan(x) _isnan(x)
47 #if defined (__FreeBSD__)
48 # if __FreeBSD_version < 500000
50 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
56 static ssgSharedPtr<ssgStateSelector> layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
57 static bool state_initialized = false;
58 static bool bump_mapping = false;
59 static GLint nb_texture_unit = 0;
60 static ssgSharedPtr<ssgTexture> normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
61 static ssgSharedPtr<ssgTexture> color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
62 static GLuint normalization_cube_map;
64 static glActiveTextureProc glActiveTexturePtr = 0;
65 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
66 static glBlendColorProc glBlendColorPtr = 0;
68 bool SGCloudLayer::enable_bump_mapping = false;
71 generateNormalizationCubeMap()
73 unsigned char data[ 32 * 32 * 3 ];
75 const float half_size = 16.0f,
78 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
81 unsigned char *ptr = data;
82 for ( j = 0; j < size; j++ ) {
83 for ( i = 0; i < size; i++ ) {
85 sgSetVec3( tmp, half_size,
86 -( j + offset - half_size ),
87 -( i + offset - half_size ) );
88 sgNormalizeVec3( tmp );
89 sgScaleVec3( tmp, 0.5f );
90 sgAddVec3( tmp, zero_normal );
92 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
93 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
94 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
97 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
98 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
101 for ( j = 0; j < size; j++ ) {
102 for ( i = 0; i < size; i++ ) {
104 sgSetVec3( tmp, -half_size,
105 -( j + offset - half_size ),
106 ( i + offset - half_size ) );
107 sgNormalizeVec3( tmp );
108 sgScaleVec3( tmp, 0.5f );
109 sgAddVec3( tmp, zero_normal );
111 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
112 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
113 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
116 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
117 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
120 for ( j = 0; j < size; j++ ) {
121 for ( i = 0; i < size; i++ ) {
123 sgSetVec3( tmp, ( i + offset - half_size ),
125 ( j + offset - half_size ) );
126 sgNormalizeVec3( tmp );
127 sgScaleVec3( tmp, 0.5f );
128 sgAddVec3( tmp, zero_normal );
130 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
131 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
132 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
135 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
136 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
139 for ( j = 0; j < size; j++ ) {
140 for ( i = 0; i < size; i++ ) {
142 sgSetVec3( tmp, ( i + offset - half_size ),
144 -( j + offset - half_size ) );
145 sgNormalizeVec3( tmp );
146 sgScaleVec3( tmp, 0.5f );
147 sgAddVec3( tmp, zero_normal );
149 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
150 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
151 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
154 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
155 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
158 for ( j = 0; j < size; j++ ) {
159 for ( i = 0; i < size; i++ ) {
161 sgSetVec3( tmp, ( i + offset - half_size ),
162 -( j + offset - half_size ),
164 sgNormalizeVec3( tmp );
165 sgScaleVec3( tmp, 0.5f );
166 sgAddVec3( tmp, zero_normal );
168 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
169 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
170 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
173 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
174 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
177 for ( j = 0; j < size; j++ ) {
178 for ( i = 0; i < size; i++ ) {
180 sgSetVec3( tmp, -( i + offset - half_size ),
181 -( j + offset - half_size ),
183 sgNormalizeVec3( tmp );
184 sgScaleVec3( tmp, 0.5f );
185 sgAddVec3( tmp, zero_normal );
187 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
188 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
189 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
192 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
193 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
198 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
201 layer_root(new ssgRoot),
202 layer_transform(new ssgTransform),
205 texture_path(tex_path),
208 layer_thickness(0.0),
209 layer_transition(0.0),
210 layer_coverage(SG_CLOUD_CLEAR),
217 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
218 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
219 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
220 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
222 layer_root->addKid(layer_transform);
223 layer3D = new SGCloudField;
228 SGCloudLayer::~SGCloudLayer()
233 delete layer_root; // deletes layer_transform and layer as well
237 SGCloudLayer::getSpan_m () const
243 SGCloudLayer::setSpan_m (float span_m)
245 if (span_m != layer_span) {
252 SGCloudLayer::getElevation_m () const
258 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
260 layer_asl = elevation_m;
263 if (elevation_m > 4000)
264 setSpan_m( elevation_m * 10 );
271 SGCloudLayer::getThickness_m () const
273 return layer_thickness;
277 SGCloudLayer::setThickness_m (float thickness_m)
279 layer_thickness = thickness_m;
283 SGCloudLayer::getTransition_m () const
285 return layer_transition;
289 SGCloudLayer::setTransition_m (float transition_m)
291 layer_transition = transition_m;
294 SGCloudLayer::Coverage
295 SGCloudLayer::getCoverage () const
297 return layer_coverage;
301 SGCloudLayer::setCoverage (Coverage coverage)
303 if (coverage != layer_coverage) {
304 layer_coverage = coverage;
310 // build the cloud object
312 SGCloudLayer::rebuild()
314 // Initialize states and sizes if necessary.
315 if ( !state_initialized ) {
316 state_initialized = true;
318 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
320 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
321 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
322 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
323 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
324 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
326 if ( bump_mapping ) {
327 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
328 if ( nb_texture_unit < 2 ) {
329 bump_mapping = false;
331 //nb_texture_unit = 2; // Force the number of units for now
334 if ( bump_mapping ) {
336 // This bump mapping code was inspired by the tutorial available at
337 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
338 // and a NVidia white paper
339 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
340 // The normal map textures were generated by the normal map Gimp plugin :
341 // http://nifelheim.dyndns.org/~cocidius/normalmap/
345 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
346 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
347 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
349 cloud_path.set(texture_path.str());
350 cloud_path.append("overcast.rgb");
351 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
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() );
356 cloud_path.set(texture_path.str());
357 cloud_path.append("overcast_top.rgb");
358 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
359 cloud_path.set(texture_path.str());
360 cloud_path.append("overcast_top_n.rgb");
361 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
363 cloud_path.set(texture_path.str());
364 cloud_path.append("broken.rgba");
365 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
366 cloud_path.set(texture_path.str());
367 cloud_path.append("broken_n.rgb");
368 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
370 cloud_path.set(texture_path.str());
371 cloud_path.append("scattered.rgba");
372 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
373 cloud_path.set(texture_path.str());
374 cloud_path.append("scattered_n.rgb");
375 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
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 cloud_path.set(texture_path.str());
381 cloud_path.append("few_n.rgb");
382 normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
384 cloud_path.set(texture_path.str());
385 cloud_path.append("cirrus.rgba");
386 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
387 cloud_path.set(texture_path.str());
388 cloud_path.append("cirrus_n.rgb");
389 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
391 glGenTextures( 1, &normalization_cube_map );
392 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
393 generateNormalizationCubeMap();
394 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
395 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
396 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
397 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
398 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
401 ssgStateSelector *state_sel;
402 ssgSimpleState *state;
404 state_sel = new ssgStateSelector( 2 );
405 cloud_path.set(texture_path.str());
406 cloud_path.append("overcast.rgb");
407 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
408 cloud_path.set(texture_path.str());
409 cloud_path.append("overcast_top.rgb");
410 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
411 layer_states[SG_CLOUD_OVERCAST] = state_sel;
413 state_sel = new ssgStateSelector( 2 );
414 cloud_path.set(texture_path.str());
415 cloud_path.append("broken.rgba");
416 state = sgCloudMakeState(cloud_path.str());
417 state_sel->setStep( 0, state );
418 state_sel->setStep( 1, state );
419 layer_states[SG_CLOUD_BROKEN] = state_sel;
421 state_sel = new ssgStateSelector( 2 );
422 cloud_path.set(texture_path.str());
423 cloud_path.append("scattered.rgba");
424 state = sgCloudMakeState(cloud_path.str());
425 state_sel->setStep( 0, state );
426 state_sel->setStep( 1, state );
427 layer_states[SG_CLOUD_SCATTERED] = state_sel;
429 state_sel = new ssgStateSelector( 2 );
430 cloud_path.set(texture_path.str());
431 cloud_path.append("few.rgba");
432 state = sgCloudMakeState(cloud_path.str());
433 state_sel->setStep( 0, state );
434 state_sel->setStep( 1, state );
435 layer_states[SG_CLOUD_FEW] = state_sel;
437 state_sel = new ssgStateSelector( 2 );
438 cloud_path.set(texture_path.str());
439 cloud_path.append("cirrus.rgba");
440 state = sgCloudMakeState(cloud_path.str());
441 state_sel->setStep( 0, state );
442 state_sel->setStep( 1, state );
443 layer_states[SG_CLOUD_CIRRUS] = state_sel;
445 layer_states[SG_CLOUD_CLEAR] = 0;
447 SGNewCloud::loadTextures(texture_path.str());
448 layer3D->buildTestLayer();
451 if ( bump_mapping ) {
454 vertices = new CloudVertex[ 25 ];
455 indices = new unsigned int[ 40 ];
459 sgSetVec2( base, sg_random(), sg_random() );
461 const float layer_scale = layer_span / scale;
462 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
463 const float half_angle = 0.5 * layer_span / layer_to_core;
466 for ( i = -2; i <= 2; i++ ) {
467 for ( int j = -2; j <= 2; j++ ) {
468 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
469 sgSetVec3( v1.position,
470 0.5 * i * layer_span,
471 0.5 * j * layer_span,
472 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
473 sgSetVec2( v1.texCoord,
474 base[0] + layer_scale * i * 0.25,
475 base[1] + layer_scale * j * 0.25 );
476 sgSetVec3( v1.sTangent,
477 cos( i * half_angle ),
479 -sin( i * half_angle ) );
480 sgSetVec3( v1.tTangent,
482 cos( j * half_angle ),
483 -sin( j * half_angle ) );
484 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
485 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : cloud_alpha * 0.15f );
489 * 0 1 5 6 10 11 15 16 20 21
490 * 1 2 6 7 11 12 16 17 21 22
491 * 2 3 7 8 12 13 17 18 22 23
492 * 3 4 8 9 13 14 18 19 23 24
494 for ( i = 0; i < 4; i++ ) {
495 for ( int j = 0; j < 5; j++ ) {
496 indices[ i*10 + (j*2) ] = i + 5 * j;
497 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
504 last_lon = last_lat = -999.0f;
507 sgSetVec2( base, sg_random(), sg_random() );
509 // build the cloud layer
514 const float layer_scale = layer_span / scale;
515 const float mpi = SG_PI/4;
517 // caclculate the difference between a flat-earth model and
518 // a round earth model given the span and altutude ASL of
519 // the cloud layer. This is the difference in altitude between
520 // the top of the inverted bowl and the edge of the bowl.
521 // const float alt_diff = layer_asl * 0.8;
522 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
523 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
524 const float border_to_core = layer_to_core * cos(layer_angle);
525 const float alt_diff = layer_to_core - border_to_core;
527 for (int i = 0; i < 4; i++)
529 if ( layer[i] != NULL ) {
530 layer_transform->removeKid(layer[i]); // automatic delete
533 vl[i] = new ssgVertexArray( 10 );
534 cl[i] = new ssgColourArray( 10 );
535 tl[i] = new ssgTexCoordArray( 10 );
538 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
539 alt_diff * (sin(i*mpi) - 2) );
541 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
543 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
546 vl[i]->add( vertex );
549 for (int j = 0; j < 4; j++)
551 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
552 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
554 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
555 base[1] + layer_scale * j/4 );
557 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
558 ( (j == 0) || (i == 3)) ?
559 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
562 vl[i]->add( vertex );
566 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
567 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
569 sgSetVec2( tc, base[0] + layer_scale * i/4,
570 base[1] + layer_scale * (j+1)/4 );
572 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
573 ((j == 3) || (i == 0)) ?
574 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
576 vl[i]->add( vertex );
580 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
581 alt_diff * (sin((i+1)*mpi) - 2) );
583 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
584 base[1] + layer_scale );
586 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
589 vl[i]->add( vertex );
592 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
593 layer_transform->addKid( layer[i] );
595 if ( layer_states[layer_coverage] != NULL ) {
596 layer[i]->setState( layer_states[layer_coverage] );
598 state_sel = layer_states[layer_coverage];
601 // force a repaint of the sky colors with arbitrary defaults
607 // repaint the cloud layer colors
608 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
610 if ( bump_mapping && enable_bump_mapping ) {
612 for ( int i = 0; i < 25; i++ ) {
613 sgCopyVec3( vertices[ i ].color, fog_color );
619 for ( int i = 0; i < 4; i++ ) {
620 color = cl[i]->get( 0 );
621 sgCopyVec3( color, fog_color );
622 color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
624 for ( int j = 0; j < 4; ++j ) {
625 color = cl[i]->get( (2*j) + 1 );
626 sgCopyVec3( color, fog_color );
628 ((j == 0) || (i == 3)) ?
629 ((j == 0) && (i == 3)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
631 color = cl[i]->get( (2*j) + 2 );
632 sgCopyVec3( color, fog_color );
634 ((j == 3) || (i == 0)) ?
635 ((j == 3) && (i == 0)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
638 color = cl[i]->get( 9 );
639 sgCopyVec3( color, fog_color );
640 color[3] = (i == 3) ? 0.0f : cloud_alpha * 0.15f;
647 // reposition the cloud layer at the specified origin and orientation
648 // lon specifies a rotation about the Z axis
649 // lat specifies a rotation about the new Y axis
650 // spin specifies a rotation about the new Z axis (and orients the
651 // sunrise/set effects
652 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
653 double alt, double dt )
658 // combine p and asl (meters) to get translation offset
660 sgCopyVec3( asl_offset, up );
661 sgNormalizeVec3( asl_offset );
662 if ( alt <= layer_asl ) {
663 sgScaleVec3( asl_offset, layer_asl );
665 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
667 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
668 // << "," << asl_offset[2] << endl;
669 sgAddVec3( asl_offset, p );
670 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
671 // << "," << asl_offset[2] << endl;
673 // Translate to zero elevation
674 // Point3D zero_elev = current_view.get_cur_zero_elev();
675 sgMakeTransMat4( T1, asl_offset );
677 // printf(" Translated to %.2f %.2f %.2f\n",
678 // zero_elev.x, zero_elev.y, zero_elev.z );
680 // Rotate to proper orientation
681 // printf(" lon = %.2f lat = %.2f\n",
682 // lon * SGD_RADIANS_TO_DEGREES,
683 // lat * SGD_RADIANS_TO_DEGREES);
684 sgSetVec3( axis, 0.0, 0.0, 1.0 );
685 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
687 sgSetVec3( axis, 0.0, 1.0, 0.0 );
688 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
692 sgCopyMat4( TRANSFORM, T1 );
693 sgPreMultMat4( TRANSFORM, LON );
694 sgPreMultMat4( TRANSFORM, LAT );
697 sgSetCoord( &layerpos, TRANSFORM );
699 layer_transform->setTransform( &layerpos );
701 // now calculate update texture coordinates
702 if ( last_lon < -900 ) {
707 double sp_dist = speed*dt;
709 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
710 Point3D start( last_lon, last_lat, 0.0 );
711 Point3D dest( lon, lat, 0.0 );
712 double course = 0.0, dist = 0.0;
714 calc_gc_course_dist( dest, start, &course, &dist );
715 // cout << "course = " << course << ", dist = " << dist << endl;
717 // if start and dest are too close together,
718 // calc_gc_course_dist() can return a course of "nan". If
719 // this happens, lets just use the last known good course.
720 // This is a hack, and it would probably be better to make
721 // calc_gc_course_dist() more robust.
722 if ( isnan(course) ) {
723 course = last_course;
725 last_course = course;
728 // calculate cloud movement due to external forces
729 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
732 ax = cos(course) * dist;
733 ay = sin(course) * dist;
737 bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
738 by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
742 double xoff = (ax + bx) / (2 * scale);
743 double yoff = (ay + by) / (2 * scale);
745 const float layer_scale = layer_span / scale;
747 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
750 if ( bump_mapping && enable_bump_mapping ) {
751 base = vertices[12].texCoord;
753 base = tl[0]->get( 0 );
757 // the while loops can lead to *long* pauses if base[0] comes
758 // with a bogus value.
759 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
760 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
761 if ( base[0] > -10.0 && base[0] < 10.0 ) {
762 base[0] -= (int)base[0];
764 SG_LOG(SG_ASTRO, SG_DEBUG,
765 "Error: base = " << base[0] << "," << base[1] <<
766 " course = " << course << " dist = " << dist );
771 // the while loops can lead to *long* pauses if base[0] comes
772 // with a bogus value.
773 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
774 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
775 if ( base[1] > -10.0 && base[1] < 10.0 ) {
776 base[1] -= (int)base[1];
778 SG_LOG(SG_ASTRO, SG_ALERT,
779 "Error: base = " << base[0] << "," << base[1] <<
780 " course = " << course << " dist = " << dist );
784 if ( bump_mapping && enable_bump_mapping ) {
786 for ( int i = -2; i <= 2; i++ ) {
787 for ( int j = -2; j <= 2; j++ ) {
788 if ( i == 0 && j == 0 )
789 continue; // Already done on base
790 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
791 sgSetVec2( v1.texCoord,
792 base[0] + layer_scale * i * 0.25,
793 base[1] + layer_scale * j * 0.25 );
798 // cout << "base = " << base[0] << "," << base[1] << endl;
801 for (int i = 0; i < 4; i++) {
802 tc = tl[i]->get( 0 );
803 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
805 for (int j = 0; j < 4; j++)
807 tc = tl[i]->get( j*2+1 );
808 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
809 base[1] + layer_scale * j/4 );
811 tc = tl[i]->get( (j+1)*2 );
812 sgSetVec2( tc, base[0] + layer_scale * i/4,
813 base[1] + layer_scale * (j+1)/4 );
816 tc = tl[i]->get( 9 );
817 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
818 base[1] + layer_scale );
826 layer3D->reposition( p, up, lon, lat, alt, dt, direction, speed);
831 void SGCloudLayer::draw( bool top ) {
832 if ( layer_coverage != SG_CLOUD_CLEAR ) {
834 if ( SGCloudField::enable3D && layer3D->is3D())
837 if ( bump_mapping && enable_bump_mapping ) {
842 ssgGetModelviewMatrix( modelview );
843 layer_transform->getTransform( transform );
845 sgTransposeNegateMat4( tmp, transform );
847 sgPostMultMat4( transform, modelview );
848 ssgLoadModelviewMatrix( transform );
851 ssgGetLight( 0 )->getPosition( lightVec );
852 sgNegateVec3( lightVec );
853 sgXformVec3( lightVec, tmp );
855 for ( int i = 0; i < 25; i++ ) {
856 CloudVertex &v = vertices[ i ];
857 sgSetVec3( v.tangentSpLight,
858 sgScalarProductVec3( v.sTangent, lightVec ),
859 sgScalarProductVec3( v.tTangent, lightVec ),
860 sgScalarProductVec3( v.normal, lightVec ) );
863 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
864 if ( top && decal == 0 ) {
865 decal = color_map[ layer_coverage ][ 0 ];
867 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
868 if ( top && normal == 0 ) {
869 normal = normal_map[ layer_coverage ][ 0 ];
872 glDisable( GL_LIGHTING );
873 glDisable( GL_CULL_FACE );
874 // glDisable( GL_ALPHA_TEST );
875 if ( layer_coverage == SG_CLOUD_FEW ) {
876 glEnable( GL_ALPHA_TEST );
877 glAlphaFunc ( GL_GREATER, 0.01 );
879 glEnable( GL_BLEND );
880 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
882 glShadeModel( GL_SMOOTH );
883 glEnable( GL_COLOR_MATERIAL );
887 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
888 emis = ( color[0]+color[1]+color[2] ) / 3.0;
892 sgSetVec4( color, emis, emis, emis, 0.0 );
893 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
894 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
895 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
896 sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
897 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
898 sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
899 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
901 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
903 glActiveTexturePtr( GL_TEXTURE0_ARB );
904 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
905 glEnable( GL_TEXTURE_2D );
907 //Bind normalisation cube map to texture unit 1
908 glActiveTexturePtr( GL_TEXTURE1_ARB );
909 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
910 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
911 glActiveTexturePtr( GL_TEXTURE0_ARB );
913 //Set vertex arrays for cloud
914 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
915 glEnableClientState( GL_VERTEX_ARRAY );
917 if ( nb_texture_unit >= 3 ) {
918 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
919 glEnableClientState( GL_COLOR_ARRAY );
922 //Send texture coords for normal map to unit 0
923 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
924 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
926 //Send tangent space light vectors for normalisation to unit 1
927 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
928 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
929 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
931 //Set up texture environment to do (tex0 dot tex1)*color
932 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
933 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
934 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
935 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
936 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
938 glActiveTexturePtr( GL_TEXTURE1_ARB );
940 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
941 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
942 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
943 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
944 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
945 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
947 if ( nb_texture_unit >= 3 ) {
948 glActiveTexturePtr( GL_TEXTURE2_ARB );
949 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
951 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
952 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
953 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
955 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
956 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
957 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
958 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
960 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
961 glActiveTexturePtr( GL_TEXTURE0_ARB );
964 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
965 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
966 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
967 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
969 glDisable( GL_TEXTURE_2D );
970 glActiveTexturePtr( GL_TEXTURE1_ARB );
971 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
972 glActiveTexturePtr( GL_TEXTURE2_ARB );
973 glDisable( GL_TEXTURE_2D );
974 glActiveTexturePtr( GL_TEXTURE0_ARB );
976 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
977 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
978 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
979 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
980 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
981 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
983 glDisableClientState( GL_COLOR_ARRAY );
984 glEnable( GL_LIGHTING );
986 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
989 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
990 glActiveTexturePtr( GL_TEXTURE0_ARB );
993 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
994 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
995 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
996 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
999 glDisable( GL_TEXTURE_2D );
1001 glActiveTexturePtr( GL_TEXTURE1_ARB );
1002 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
1003 glActiveTexturePtr( GL_TEXTURE0_ARB );
1005 //disable vertex arrays
1006 glDisableClientState( GL_VERTEX_ARRAY );
1008 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1009 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
1010 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1011 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1013 //Return to standard modulate texenv
1014 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1016 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
1017 glDepthFunc(GL_LEQUAL);
1019 glEnable( GL_LIGHTING );
1021 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1022 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1023 average = 0.15 + average/10;
1024 sgVec4 averageColor;
1025 sgSetVec4( averageColor, average, average, average, 1.0f );
1026 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1028 glBlendColorPtr( average, average, average, 1.0f );
1029 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1031 //Perform a second pass to color the torus
1032 //Bind decal texture
1033 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1034 glEnable(GL_TEXTURE_2D);
1036 //Set vertex arrays for torus
1037 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1038 glEnableClientState( GL_VERTEX_ARRAY );
1040 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1041 //glEnableClientState( GL_COLOR_ARRAY );
1043 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1044 glEnableClientState( GL_NORMAL_ARRAY );
1046 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1047 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1050 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1051 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1052 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1053 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1055 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1057 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1061 glDisable( GL_TEXTURE_2D );
1063 glDisableClientState( GL_VERTEX_ARRAY );
1064 glDisableClientState( GL_NORMAL_ARRAY );
1066 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1067 glEnable( GL_CULL_FACE );
1068 glDepthFunc(GL_LESS);
1070 ssgLoadModelviewMatrix( modelview );
1073 state_sel->selectStep( top ? 1 : 0 );
1074 ssgCullAndDraw( layer_root );
1080 // make an ssgSimpleState for a cloud layer given the named texture
1081 ssgSimpleState *sgCloudMakeState( const string &path ) {
1082 ssgSimpleState *state = new ssgSimpleState();
1084 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1086 state->setTexture( (char *)path.c_str() );
1087 state->setShadeModel( GL_SMOOTH );
1088 state->disable( GL_LIGHTING );
1089 state->disable( GL_CULL_FACE );
1090 state->enable( GL_TEXTURE_2D );
1091 state->enable( GL_COLOR_MATERIAL );
1092 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1093 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1094 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1095 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1096 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1097 state->enable( GL_BLEND );
1098 state->enable( GL_ALPHA_TEST );
1099 state->setAlphaClamp( 0.01 );