1 // cloud.cxx -- model a single cloud layer
3 // Written by Curtis Olson, started June 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
7 // This program is distributed in the hope that it will be useful, but
8 // WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // General Public License for more details.
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <simgear/compiler.h>
24 // #if defined (__APPLE__)
25 // // any C++ header file undefines isinf and isnan
26 // // so this should be included before <iostream>
27 // inline int (isnan)(double r) { return isnan(r); }
30 #if defined(__MINGW32__)
31 #define isnan(x) _isnan(x)
34 // #include STL_IOSTREAM
39 #include <simgear/math/point3d.hxx>
40 #include <simgear/math/polar3d.hxx>
41 #include <simgear/math/sg_random.h>
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/misc/sg_path.hxx>
44 #include <simgear/screen/extensions.hxx>
45 #include <simgear/screen/texture.hxx>
50 static ssgStateSelector *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
51 static bool state_initialized = false;
52 static bool bump_mapping = false;
53 static int nb_texture_unit = 0;
54 static ssgTexture *normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
55 static ssgTexture *color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
56 static GLuint normalization_cube_map;
58 static glActiveTextureProc glActiveTexturePtr = 0;
59 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
60 static glBlendColorProc glBlendColorPtr = 0;
62 bool SGCloudLayer::enable_bump_mapping = false;
65 generateNormalizationCubeMap()
67 unsigned char data[ 32 * 32 * 3 ];
69 const float half_size = 16.0f,
72 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
75 unsigned char *ptr = data;
76 for ( j = 0; j < size; j++ ) {
77 for ( i = 0; i < size; i++ ) {
79 sgSetVec3( tmp, half_size,
80 -( j + offset - half_size ),
81 -( i + offset - half_size ) );
82 sgNormalizeVec3( tmp );
83 sgScaleVec3( tmp, 0.5f );
84 sgAddVec3( tmp, zero_normal );
86 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
87 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
88 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
91 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
92 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
95 for ( j = 0; j < size; j++ ) {
96 for ( i = 0; i < size; i++ ) {
98 sgSetVec3( tmp, -half_size,
99 -( j + offset - half_size ),
100 ( i + offset - half_size ) );
101 sgNormalizeVec3( tmp );
102 sgScaleVec3( tmp, 0.5f );
103 sgAddVec3( tmp, zero_normal );
105 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
106 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
107 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
110 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
111 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
114 for ( j = 0; j < size; j++ ) {
115 for ( i = 0; i < size; i++ ) {
117 sgSetVec3( tmp, ( i + offset - half_size ),
119 ( j + offset - half_size ) );
120 sgNormalizeVec3( tmp );
121 sgScaleVec3( tmp, 0.5f );
122 sgAddVec3( tmp, zero_normal );
124 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
125 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
126 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
129 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
130 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
133 for ( j = 0; j < size; j++ ) {
134 for ( i = 0; i < size; i++ ) {
136 sgSetVec3( tmp, ( i + offset - half_size ),
138 -( j + offset - half_size ) );
139 sgNormalizeVec3( tmp );
140 sgScaleVec3( tmp, 0.5f );
141 sgAddVec3( tmp, zero_normal );
143 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
144 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
145 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
148 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
149 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
152 for ( j = 0; j < size; j++ ) {
153 for ( i = 0; i < size; i++ ) {
155 sgSetVec3( tmp, ( i + offset - half_size ),
156 -( j + offset - half_size ),
158 sgNormalizeVec3( tmp );
159 sgScaleVec3( tmp, 0.5f );
160 sgAddVec3( tmp, zero_normal );
162 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
163 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
164 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
167 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
168 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
171 for ( j = 0; j < size; j++ ) {
172 for ( i = 0; i < size; i++ ) {
174 sgSetVec3( tmp, -( i + offset - half_size ),
175 -( j + offset - half_size ),
177 sgNormalizeVec3( tmp );
178 sgScaleVec3( tmp, 0.5f );
179 sgAddVec3( tmp, zero_normal );
181 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
182 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
183 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
186 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
187 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
192 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
193 layer_root(new ssgRoot),
194 layer_transform(new ssgTransform),
196 texture_path(tex_path),
199 layer_thickness(0.0),
200 layer_transition(0.0),
201 layer_coverage(SG_CLOUD_CLEAR),
210 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
211 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
212 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
213 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
215 layer_root->addKid(layer_transform);
220 SGCloudLayer::~SGCloudLayer()
224 delete layer_root; // deletes layer_transform and layer as well
228 SGCloudLayer::getSpan_m () const
234 SGCloudLayer::setSpan_m (float span_m)
236 if (span_m != layer_span) {
243 SGCloudLayer::getElevation_m () const
249 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
251 layer_asl = elevation_m;
254 if (elevation_m > 4000)
255 setSpan_m( elevation_m * 10 );
262 SGCloudLayer::getThickness_m () const
264 return layer_thickness;
268 SGCloudLayer::setThickness_m (float thickness_m)
270 layer_thickness = thickness_m;
274 SGCloudLayer::getTransition_m () const
276 return layer_transition;
280 SGCloudLayer::setTransition_m (float transition_m)
282 layer_transition = transition_m;
285 SGCloudLayer::Coverage
286 SGCloudLayer::getCoverage () const
288 return layer_coverage;
292 SGCloudLayer::setCoverage (Coverage coverage)
294 if (coverage != layer_coverage) {
295 layer_coverage = coverage;
301 // build the cloud object
303 SGCloudLayer::rebuild()
305 // Initialize states and sizes if necessary.
306 if ( !state_initialized ) {
307 state_initialized = true;
309 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
311 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
312 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
313 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
314 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
315 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
317 if ( bump_mapping ) {
318 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
319 if ( nb_texture_unit < 2 ) {
320 bump_mapping = false;
322 nb_texture_unit = 2; // Force the number of units for now
325 if ( bump_mapping ) {
327 // This bump mapping code was inspired by the tutorial available at
328 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
329 // and a NVidia white paper
330 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
331 // The normal map textures were generated by the normal map Gimp plugin :
332 // http://nifelheim.dyndns.org/~cocidius/normalmap/
336 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
337 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
338 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
340 cloud_path.set(texture_path.str());
341 cloud_path.append("overcast.rgb");
342 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
343 color_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
344 cloud_path.set(texture_path.str());
345 cloud_path.append("overcast_n.rgb");
346 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
347 normal_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
349 cloud_path.set(texture_path.str());
350 cloud_path.append("overcast_top.rgb");
351 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
352 color_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
353 cloud_path.set(texture_path.str());
354 cloud_path.append("overcast_top_n.rgb");
355 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
356 normal_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
358 cloud_path.set(texture_path.str());
359 cloud_path.append("broken.rgba");
360 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
361 color_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
362 cloud_path.set(texture_path.str());
363 cloud_path.append("broken_n.rgb");
364 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
365 normal_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
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 color_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
371 cloud_path.set(texture_path.str());
372 cloud_path.append("scattered_n.rgb");
373 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
374 normal_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
376 cloud_path.set(texture_path.str());
377 cloud_path.append("few.rgba");
378 color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
379 color_map[ SG_CLOUD_FEW ][ 0 ]->ref();
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() );
383 normal_map[ SG_CLOUD_FEW ][ 0 ]->ref();
385 cloud_path.set(texture_path.str());
386 cloud_path.append("cirrus.rgba");
387 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
388 color_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
389 cloud_path.set(texture_path.str());
390 cloud_path.append("cirrus_n.rgb");
391 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
392 normal_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
394 glGenTextures( 1, &normalization_cube_map );
395 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
396 generateNormalizationCubeMap();
397 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
398 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
399 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
400 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
401 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
404 ssgStateSelector *state_sel;
405 ssgSimpleState *state;
407 state_sel = new ssgStateSelector( 2 );
409 cloud_path.set(texture_path.str());
410 cloud_path.append("overcast.rgb");
411 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
412 cloud_path.set(texture_path.str());
413 cloud_path.append("overcast_top.rgb");
414 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
415 layer_states[SG_CLOUD_OVERCAST] = state_sel;
417 state_sel = new ssgStateSelector( 2 );
419 cloud_path.set(texture_path.str());
420 cloud_path.append("broken.rgba");
421 state = sgCloudMakeState(cloud_path.str());
422 state_sel->setStep( 0, state );
423 state_sel->setStep( 1, state );
424 layer_states[SG_CLOUD_BROKEN] = state_sel;
426 state_sel = new ssgStateSelector( 2 );
428 cloud_path.set(texture_path.str());
429 cloud_path.append("scattered.rgba");
430 state = sgCloudMakeState(cloud_path.str());
431 state_sel->setStep( 0, state );
432 state_sel->setStep( 1, state );
433 layer_states[SG_CLOUD_SCATTERED] = state_sel;
435 state_sel = new ssgStateSelector( 2 );
437 cloud_path.set(texture_path.str());
438 cloud_path.append("few.rgba");
439 state = sgCloudMakeState(cloud_path.str());
440 state_sel->setStep( 0, state );
441 state_sel->setStep( 1, state );
442 layer_states[SG_CLOUD_FEW] = state_sel;
444 state_sel = new ssgStateSelector( 2 );
446 cloud_path.set(texture_path.str());
447 cloud_path.append("cirrus.rgba");
448 state = sgCloudMakeState(cloud_path.str());
449 state_sel->setStep( 0, state );
450 state_sel->setStep( 1, state );
451 layer_states[SG_CLOUD_CIRRUS] = state_sel;
453 layer_states[SG_CLOUD_CLEAR] = 0;
457 if ( bump_mapping ) {
460 vertices = new CloudVertex[ 25 ];
461 indices = new unsigned int[ 40 ];
465 sgSetVec2( base, sg_random(), sg_random() );
467 const float layer_scale = layer_span / scale;
468 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
469 const float half_angle = 0.5 * layer_span / layer_to_core;
472 for ( i = -2; i <= 2; i++ ) {
473 for ( int j = -2; j <= 2; j++ ) {
474 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
475 sgSetVec3( v1.position,
476 0.5 * i * layer_span,
477 0.5 * j * layer_span,
478 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
479 sgSetVec2( v1.texCoord,
480 base[0] + layer_scale * i * 0.25,
481 base[1] + layer_scale * j * 0.25 );
482 sgSetVec3( v1.sTangent,
483 cos( i * half_angle ),
485 -sin( i * half_angle ) );
486 sgSetVec3( v1.tTangent,
488 cos( j * half_angle ),
489 -sin( j * half_angle ) );
490 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
491 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
495 * 0 1 5 6 10 11 15 16 20 21
496 * 1 2 6 7 11 12 16 17 21 22
497 * 2 3 7 8 12 13 17 18 22 23
498 * 3 4 8 9 13 14 18 19 23 24
500 for ( i = 0; i < 4; i++ ) {
501 for ( int j = 0; j < 5; j++ ) {
502 indices[ i*10 + (j*2) ] = i + 5 * j;
503 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
510 last_lon = last_lat = -999.0f;
513 sgSetVec2( base, sg_random(), sg_random() );
515 // build the cloud layer
520 const float layer_scale = layer_span / scale;
521 const float mpi = SG_PI/4;
523 // caclculate the difference between a flat-earth model and
524 // a round earth model given the span and altutude ASL of
525 // the cloud layer. This is the difference in altitude between
526 // the top of the inverted bowl and the edge of the bowl.
527 // const float alt_diff = layer_asl * 0.8;
528 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
529 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
530 const float border_to_core = layer_to_core * cos(layer_angle);
531 const float alt_diff = layer_to_core - border_to_core;
533 for (int i = 0; i < 4; i++)
535 if ( layer[i] != NULL ) {
536 layer_transform->removeKid(layer[i]); // automatic delete
539 vl[i] = new ssgVertexArray( 10 );
540 cl[i] = new ssgColourArray( 10 );
541 tl[i] = new ssgTexCoordArray( 10 );
544 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
545 alt_diff * (sin(i*mpi) - 2) );
547 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
549 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
552 vl[i]->add( vertex );
555 for (int j = 0; j < 4; j++)
557 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
558 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
560 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
561 base[1] + layer_scale * j/4 );
563 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
564 ( (j == 0) || (i == 3)) ?
565 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
568 vl[i]->add( vertex );
572 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
573 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
575 sgSetVec2( tc, base[0] + layer_scale * i/4,
576 base[1] + layer_scale * (j+1)/4 );
578 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
579 ((j == 3) || (i == 0)) ?
580 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
582 vl[i]->add( vertex );
586 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
587 alt_diff * (sin((i+1)*mpi) - 2) );
589 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
590 base[1] + layer_scale );
592 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
595 vl[i]->add( vertex );
598 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
599 layer_transform->addKid( layer[i] );
601 if ( layer_states[layer_coverage] != NULL ) {
602 layer[i]->setState( layer_states[layer_coverage] );
604 state_sel = layer_states[layer_coverage];
607 // force a repaint of the sky colors with arbitrary defaults
613 // repaint the cloud layer colors
614 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
616 if ( bump_mapping && enable_bump_mapping ) {
618 for ( int i = 0; i < 25; i++ ) {
619 sgCopyVec3( vertices[ i ].color, fog_color );
625 for ( int i = 0; i < 4; i++ ) {
626 for ( int j = 0; j < 10; ++j ) {
627 color = cl[i]->get( j );
628 sgCopyVec3( color, fog_color );
637 // reposition the cloud layer at the specified origin and orientation
638 // lon specifies a rotation about the Z axis
639 // lat specifies a rotation about the new Y axis
640 // spin specifies a rotation about the new Z axis (and orients the
641 // sunrise/set effects
642 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
643 double alt, double dt )
648 // combine p and asl (meters) to get translation offset
650 sgCopyVec3( asl_offset, up );
651 sgNormalizeVec3( asl_offset );
652 if ( alt <= layer_asl ) {
653 sgScaleVec3( asl_offset, layer_asl );
655 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
657 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
658 // << "," << asl_offset[2] << endl;
659 sgAddVec3( asl_offset, p );
660 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
661 // << "," << asl_offset[2] << endl;
663 // Translate to zero elevation
664 // Point3D zero_elev = current_view.get_cur_zero_elev();
665 sgMakeTransMat4( T1, asl_offset );
667 // printf(" Translated to %.2f %.2f %.2f\n",
668 // zero_elev.x, zero_elev.y, zero_elev.z );
670 // Rotate to proper orientation
671 // printf(" lon = %.2f lat = %.2f\n",
672 // lon * SGD_RADIANS_TO_DEGREES,
673 // lat * SGD_RADIANS_TO_DEGREES);
674 sgSetVec3( axis, 0.0, 0.0, 1.0 );
675 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
677 sgSetVec3( axis, 0.0, 1.0, 0.0 );
678 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
682 sgCopyMat4( TRANSFORM, T1 );
683 sgPreMultMat4( TRANSFORM, LON );
684 sgPreMultMat4( TRANSFORM, LAT );
687 sgSetCoord( &layerpos, TRANSFORM );
689 layer_transform->setTransform( &layerpos );
691 // now calculate update texture coordinates
692 if ( last_lon < -900 ) {
697 double sp_dist = speed*dt;
699 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
700 Point3D start( last_lon, last_lat, 0.0 );
701 Point3D dest( lon, lat, 0.0 );
702 double course = 0.0, dist = 0.0;
704 calc_gc_course_dist( dest, start, &course, &dist );
705 // cout << "course = " << course << ", dist = " << dist << endl;
707 // if start and dest are too close together,
708 // calc_gc_course_dist() can return a course of "nan". If
709 // this happens, lets just use the last known good course.
710 // This is a hack, and it would probably be better to make
711 // calc_gc_course_dist() more robust.
712 if ( isnan(course) ) {
713 course = last_course;
715 last_course = course;
718 // calculate cloud movement due to external forces
719 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
722 ax = cos(course) * dist;
723 ay = sin(course) * dist;
727 bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
728 by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
732 double xoff = (ax + bx) / (2 * scale);
733 double yoff = (ay + by) / (2 * scale);
735 const float layer_scale = layer_span / scale;
737 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
740 if ( bump_mapping && enable_bump_mapping ) {
741 base = vertices[12].texCoord;
743 base = tl[0]->get( 0 );
747 // the while loops can lead to *long* pauses if base[0] comes
748 // with a bogus value.
749 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
750 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
751 if ( base[0] > -10.0 && base[0] < 10.0 ) {
752 base[0] -= (int)base[0];
754 SG_LOG(SG_ASTRO, SG_DEBUG,
755 "Error: base = " << base[0] << "," << base[1] <<
756 " course = " << course << " dist = " << dist );
761 // the while loops can lead to *long* pauses if base[0] comes
762 // with a bogus value.
763 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
764 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
765 if ( base[1] > -10.0 && base[1] < 10.0 ) {
766 base[1] -= (int)base[1];
768 SG_LOG(SG_ASTRO, SG_ALERT,
769 "Error: base = " << base[0] << "," << base[1] <<
770 " course = " << course << " dist = " << dist );
774 if ( bump_mapping && enable_bump_mapping ) {
776 for ( int i = -2; i <= 2; i++ ) {
777 for ( int j = -2; j <= 2; j++ ) {
778 if ( i == 0 && j == 0 )
779 continue; // Already done on base
780 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
781 sgSetVec2( v1.texCoord,
782 base[0] + layer_scale * i * 0.25,
783 base[1] + layer_scale * j * 0.25 );
788 // cout << "base = " << base[0] << "," << base[1] << endl;
791 for (int i = 0; i < 4; i++) {
792 tc = tl[i]->get( 0 );
793 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
795 for (int j = 0; j < 4; j++)
797 tc = tl[i]->get( j*2+1 );
798 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
799 base[1] + layer_scale * j/4 );
801 tc = tl[i]->get( (j+1)*2 );
802 sgSetVec2( tc, base[0] + layer_scale * i/4,
803 base[1] + layer_scale * (j+1)/4 );
806 tc = tl[i]->get( 9 );
807 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
808 base[1] + layer_scale );
820 void SGCloudLayer::draw( bool top ) {
821 if ( layer_coverage != SG_CLOUD_CLEAR ) {
823 if ( bump_mapping && enable_bump_mapping ) {
828 ssgGetModelviewMatrix( modelview );
829 layer_transform->getTransform( transform );
831 sgInvertMat4( tmp, transform );
833 sgPostMultMat4( transform, modelview );
834 ssgLoadModelviewMatrix( transform );
837 ssgGetLight( 0 )->getPosition( lightVec );
838 sgNegateVec3( lightVec );
839 sgXformVec3( lightVec, tmp );
841 for ( int i = 0; i < 25; i++ ) {
842 CloudVertex &v = vertices[ i ];
843 sgSetVec3( v.tangentSpLight,
844 sgScalarProductVec3( v.sTangent, lightVec ),
845 sgScalarProductVec3( v.tTangent, lightVec ),
846 sgScalarProductVec3( v.normal, lightVec ) );
849 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
850 if ( top && decal == 0 ) {
851 decal = color_map[ layer_coverage ][ 0 ];
853 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
854 if ( top && normal == 0 ) {
855 normal = normal_map[ layer_coverage ][ 0 ];
858 glDisable( GL_LIGHTING );
859 glDisable( GL_CULL_FACE );
860 if ( layer_coverage == SG_CLOUD_FEW ) {
861 glEnable( GL_ALPHA_TEST );
862 glAlphaFunc ( GL_GREATER, 0.01 );
864 glEnable( GL_BLEND );
865 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
867 glPolygonMode( GL_FILL, GL_FRONT_AND_BACK );
868 glShadeModel( GL_SMOOTH );
869 glEnable( GL_COLOR_MATERIAL );
871 sgSetVec4( color, 0.05, 0.05, 0.05, 0.0 );
872 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
873 sgSetVec4( color, 0.8, 0.8, 0.8, 0.0 );
874 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
875 sgSetVec4( color, 0.5, 0.5, 0.5, 0.0 );
876 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
877 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
878 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
880 glActiveTexturePtr( GL_TEXTURE0_ARB );
881 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
882 glEnable( GL_TEXTURE_2D );
884 //Bind normalisation cube map to texture unit 1
885 glActiveTexturePtr( GL_TEXTURE1_ARB );
886 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
887 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
888 glActiveTexturePtr( GL_TEXTURE0_ARB );
890 //Set vertex arrays for cloud
891 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
892 glEnableClientState( GL_VERTEX_ARRAY );
894 if ( nb_texture_unit >= 3 ) {
895 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
896 glEnableClientState( GL_COLOR_ARRAY );
899 //Send texture coords for normal map to unit 0
900 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
901 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
903 //Send tangent space light vectors for normalisation to unit 1
904 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
905 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
906 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
908 //Set up texture environment to do (tex0 dot tex1)*color
909 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
910 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
911 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
912 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
913 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
915 glActiveTexturePtr( GL_TEXTURE1_ARB );
917 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
918 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
919 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
920 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
921 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
922 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
924 if ( nb_texture_unit >= 3 ) {
925 glActiveTexturePtr( GL_TEXTURE2_ARB );
926 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
928 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
929 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
930 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
933 if ( top || layer_coverage != SG_CLOUD_OVERCAST ) {
934 sgSetVec4( factors, 0.5f, 0.5f, 0.3f, 1.0f );
936 sgSetVec4( factors, 0.1f, 0.1f, 0.0f, 1.0f );
938 glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, factors );
940 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
941 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE_ARB );
942 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
943 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
944 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB );
946 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
947 glActiveTexturePtr( GL_TEXTURE0_ARB );
950 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
951 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
952 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
953 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
955 glDisable( GL_TEXTURE_2D );
956 glActiveTexturePtr( GL_TEXTURE1_ARB );
957 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
958 glActiveTexturePtr( GL_TEXTURE2_ARB );
959 glDisable( GL_TEXTURE_2D );
960 glActiveTexturePtr( GL_TEXTURE0_ARB );
962 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
963 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
964 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
965 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
966 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
967 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
969 glDisableClientState( GL_COLOR_ARRAY );
970 glEnable( GL_LIGHTING );
972 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
975 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
976 glActiveTexturePtr( GL_TEXTURE0_ARB );
979 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
980 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
981 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
982 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
985 glDisable( GL_TEXTURE_2D );
987 glActiveTexturePtr( GL_TEXTURE1_ARB );
988 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
989 glActiveTexturePtr( GL_TEXTURE0_ARB );
991 //disable vertex arrays
992 glDisableClientState( GL_VERTEX_ARRAY );
994 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
995 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
996 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
997 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
999 //Return to standard modulate texenv
1000 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1002 glDepthFunc(GL_LEQUAL);
1004 glEnable( GL_LIGHTING );
1006 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1007 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1008 sgVec4 averageColor;
1009 sgSetVec4( averageColor, average, average, average, 1.0f );
1010 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1012 glBlendColorPtr( average, average, average, 1.0f );
1013 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1015 //Perform a second pass to color the torus
1016 //Bind decal texture
1017 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1018 glEnable(GL_TEXTURE_2D);
1020 //Set vertex arrays for torus
1021 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1022 glEnableClientState( GL_VERTEX_ARRAY );
1024 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1025 //glEnableClientState( GL_COLOR_ARRAY );
1027 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1028 glEnableClientState( GL_NORMAL_ARRAY );
1030 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1031 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1034 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1035 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1036 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1037 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1039 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1041 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1044 glDisable( GL_TEXTURE_2D );
1046 glDisableClientState( GL_VERTEX_ARRAY );
1047 glDisableClientState( GL_NORMAL_ARRAY );
1049 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1050 glEnable( GL_CULL_FACE );
1051 glDepthFunc(GL_LESS);
1053 ssgLoadModelviewMatrix( modelview );
1056 state_sel->selectStep( top ? 1 : 0 );
1057 ssgCullAndDraw( layer_root );
1063 // make an ssgSimpleState for a cloud layer given the named texture
1064 ssgSimpleState *sgCloudMakeState( const string &path ) {
1065 ssgSimpleState *state = new ssgSimpleState();
1067 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1069 state->setTexture( (char *)path.c_str() );
1070 state->setShadeModel( GL_SMOOTH );
1071 state->disable( GL_LIGHTING );
1072 state->disable( GL_CULL_FACE );
1073 state->enable( GL_TEXTURE_2D );
1074 state->enable( GL_COLOR_MATERIAL );
1075 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1076 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1077 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1078 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1079 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1080 state->enable( GL_BLEND );
1081 state->enable( GL_ALPHA_TEST );
1082 state->setAlphaClamp( 0.01 );