1 // cloud.cxx -- model a single cloud layer
3 // Written by Curtis Olson, started June 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
7 // This program is distributed in the hope that it will be useful, but
8 // WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // General Public License for more details.
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <simgear/compiler.h>
24 // #if defined (__APPLE__)
25 // // any C++ header file undefines isinf and isnan
26 // // so this should be included before <iostream>
27 // inline int (isinf)(double r) { return isinf(r); }
28 // inline int (isnan)(double r) { return isnan(r); }
31 // #include STL_IOSTREAM
36 #include <simgear/math/point3d.hxx>
37 #include <simgear/math/polar3d.hxx>
38 #include <simgear/math/sg_random.h>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/misc/sg_path.hxx>
41 #include <simgear/screen/extensions.hxx>
42 #include <simgear/screen/texture.hxx>
47 static ssgStateSelector *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
48 static bool state_initialized = false;
49 static bool bump_mapping = false;
50 static int nb_texture_unit = 0;
51 static ssgTexture *normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
52 static ssgTexture *color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
53 static GLuint normalization_cube_map;
55 static glActiveTextureProc glActiveTexturePtr = 0;
56 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
57 static glBlendColorProc glBlendColorPtr = 0;
59 bool SGCloudLayer::enable_bump_mapping = false;
62 generateNormalizationCubeMap()
64 unsigned char data[ 32 * 32 * 3 ];
66 const float half_size = 16.0f,
69 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
72 unsigned char *ptr = data;
73 for ( j = 0; j < size; j++ ) {
74 for ( i = 0; i < size; i++ ) {
76 sgSetVec3( tmp, half_size,
77 -( j + offset - half_size ),
78 -( i + offset - half_size ) );
79 sgNormalizeVec3( tmp );
80 sgScaleVec3( tmp, 0.5f );
81 sgAddVec3( tmp, zero_normal );
83 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
84 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
85 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
88 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
89 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
92 for ( j = 0; j < size; j++ ) {
93 for ( i = 0; i < size; i++ ) {
95 sgSetVec3( tmp, -half_size,
96 -( j + offset - half_size ),
97 ( i + offset - half_size ) );
98 sgNormalizeVec3( tmp );
99 sgScaleVec3( tmp, 0.5f );
100 sgAddVec3( tmp, zero_normal );
102 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
103 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
104 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
107 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
108 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
111 for ( j = 0; j < size; j++ ) {
112 for ( i = 0; i < size; i++ ) {
114 sgSetVec3( tmp, ( i + offset - half_size ),
116 ( j + offset - half_size ) );
117 sgNormalizeVec3( tmp );
118 sgScaleVec3( tmp, 0.5f );
119 sgAddVec3( tmp, zero_normal );
121 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
122 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
123 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
126 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
127 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
130 for ( j = 0; j < size; j++ ) {
131 for ( i = 0; i < size; i++ ) {
133 sgSetVec3( tmp, ( i + offset - half_size ),
135 -( j + offset - half_size ) );
136 sgNormalizeVec3( tmp );
137 sgScaleVec3( tmp, 0.5f );
138 sgAddVec3( tmp, zero_normal );
140 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
141 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
142 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
145 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
146 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
149 for ( j = 0; j < size; j++ ) {
150 for ( i = 0; i < size; i++ ) {
152 sgSetVec3( tmp, ( i + offset - half_size ),
153 -( j + offset - half_size ),
155 sgNormalizeVec3( tmp );
156 sgScaleVec3( tmp, 0.5f );
157 sgAddVec3( tmp, zero_normal );
159 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
160 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
161 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
164 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
165 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
168 for ( j = 0; j < size; j++ ) {
169 for ( i = 0; i < size; i++ ) {
171 sgSetVec3( tmp, -( i + offset - half_size ),
172 -( j + offset - half_size ),
174 sgNormalizeVec3( tmp );
175 sgScaleVec3( tmp, 0.5f );
176 sgAddVec3( tmp, zero_normal );
178 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
179 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
180 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
183 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
184 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
189 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
190 layer_root(new ssgRoot),
191 layer_transform(new ssgTransform),
193 texture_path(tex_path),
196 layer_thickness(0.0),
197 layer_transition(0.0),
198 layer_coverage(SG_CLOUD_CLEAR),
207 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
208 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
209 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
210 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
212 layer_root->addKid(layer_transform);
217 SGCloudLayer::~SGCloudLayer()
221 delete layer_root; // deletes layer_transform and layer as well
225 SGCloudLayer::getSpan_m () const
231 SGCloudLayer::setSpan_m (float span_m)
233 if (span_m != layer_span) {
240 SGCloudLayer::getElevation_m () const
246 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
248 layer_asl = elevation_m;
251 if (elevation_m > 4000)
252 setSpan_m( elevation_m * 10 );
259 SGCloudLayer::getThickness_m () const
261 return layer_thickness;
265 SGCloudLayer::setThickness_m (float thickness_m)
267 layer_thickness = thickness_m;
271 SGCloudLayer::getTransition_m () const
273 return layer_transition;
277 SGCloudLayer::setTransition_m (float transition_m)
279 layer_transition = transition_m;
282 SGCloudLayer::Coverage
283 SGCloudLayer::getCoverage () const
285 return layer_coverage;
289 SGCloudLayer::setCoverage (Coverage coverage)
291 if (coverage != layer_coverage) {
292 layer_coverage = coverage;
298 // build the cloud object
300 SGCloudLayer::rebuild()
302 // Initialize states and sizes if necessary.
303 if ( !state_initialized ) {
304 state_initialized = true;
306 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
308 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
309 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
310 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
311 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
312 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
314 if ( bump_mapping ) {
315 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
316 if ( nb_texture_unit < 2 ) {
317 bump_mapping = false;
319 nb_texture_unit = 2; // Force the number of units for now
322 if ( bump_mapping ) {
324 // This bump mapping code was inspired by the tutorial available at
325 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
326 // and a NVidia white paper
327 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
328 // The normal map textures were generated by the normal map Gimp plugin :
329 // http://nifelheim.dyndns.org/~cocidius/normalmap/
333 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
334 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
335 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
337 cloud_path.set(texture_path.str());
338 cloud_path.append("overcast.rgb");
339 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
340 color_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
341 cloud_path.set(texture_path.str());
342 cloud_path.append("overcast_n.rgb");
343 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
344 normal_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
346 cloud_path.set(texture_path.str());
347 cloud_path.append("overcast_top.rgb");
348 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
349 color_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
350 cloud_path.set(texture_path.str());
351 cloud_path.append("overcast_top_n.rgb");
352 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
353 normal_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
355 cloud_path.set(texture_path.str());
356 cloud_path.append("broken.rgba");
357 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
358 color_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
359 cloud_path.set(texture_path.str());
360 cloud_path.append("broken_n.rgb");
361 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
362 normal_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
364 cloud_path.set(texture_path.str());
365 cloud_path.append("scattered.rgba");
366 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
367 color_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
368 cloud_path.set(texture_path.str());
369 cloud_path.append("scattered_n.rgb");
370 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
371 normal_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
373 cloud_path.set(texture_path.str());
374 cloud_path.append("few.rgba");
375 color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
376 color_map[ SG_CLOUD_FEW ][ 0 ]->ref();
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() );
380 normal_map[ SG_CLOUD_FEW ][ 0 ]->ref();
382 cloud_path.set(texture_path.str());
383 cloud_path.append("cirrus.rgba");
384 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
385 color_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
386 cloud_path.set(texture_path.str());
387 cloud_path.append("cirrus_n.rgb");
388 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
389 normal_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
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 );
406 cloud_path.set(texture_path.str());
407 cloud_path.append("overcast.rgb");
408 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
409 cloud_path.set(texture_path.str());
410 cloud_path.append("overcast_top.rgb");
411 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
412 layer_states[SG_CLOUD_OVERCAST] = state_sel;
414 state_sel = new ssgStateSelector( 2 );
416 cloud_path.set(texture_path.str());
417 cloud_path.append("broken.rgba");
418 state = sgCloudMakeState(cloud_path.str());
419 state_sel->setStep( 0, state );
420 state_sel->setStep( 1, state );
421 layer_states[SG_CLOUD_BROKEN] = state_sel;
423 state_sel = new ssgStateSelector( 2 );
425 cloud_path.set(texture_path.str());
426 cloud_path.append("scattered.rgba");
427 state = sgCloudMakeState(cloud_path.str());
428 state_sel->setStep( 0, state );
429 state_sel->setStep( 1, state );
430 layer_states[SG_CLOUD_SCATTERED] = state_sel;
432 state_sel = new ssgStateSelector( 2 );
434 cloud_path.set(texture_path.str());
435 cloud_path.append("few.rgba");
436 state = sgCloudMakeState(cloud_path.str());
437 state_sel->setStep( 0, state );
438 state_sel->setStep( 1, state );
439 layer_states[SG_CLOUD_FEW] = state_sel;
441 state_sel = new ssgStateSelector( 2 );
443 cloud_path.set(texture_path.str());
444 cloud_path.append("cirrus.rgba");
445 state = sgCloudMakeState(cloud_path.str());
446 state_sel->setStep( 0, state );
447 state_sel->setStep( 1, state );
448 layer_states[SG_CLOUD_CIRRUS] = state_sel;
450 layer_states[SG_CLOUD_CLEAR] = 0;
454 if ( bump_mapping ) {
457 vertices = new CloudVertex[ 25 ];
458 indices = new unsigned int[ 40 ];
462 sgSetVec2( base, sg_random(), sg_random() );
464 const float layer_scale = layer_span / scale;
465 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
466 const float half_angle = 0.5 * layer_span / layer_to_core;
469 for ( i = -2; i <= 2; i++ ) {
470 for ( int j = -2; j <= 2; j++ ) {
471 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
472 sgSetVec3( v1.position,
473 0.5 * i * layer_span,
474 0.5 * j * layer_span,
475 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
476 sgSetVec2( v1.texCoord,
477 base[0] + layer_scale * i * 0.25,
478 base[1] + layer_scale * j * 0.25 );
479 sgSetVec3( v1.sTangent,
480 cos( i * half_angle ),
482 -sin( i * half_angle ) );
483 sgSetVec3( v1.tTangent,
485 cos( j * half_angle ),
486 -sin( j * half_angle ) );
487 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
488 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
492 * 0 1 5 6 10 11 15 16 20 21
493 * 1 2 6 7 11 12 16 17 21 22
494 * 2 3 7 8 12 13 17 18 22 23
495 * 3 4 8 9 13 14 18 19 23 24
497 for ( i = 0; i < 4; i++ ) {
498 for ( int j = 0; j < 5; j++ ) {
499 indices[ i*10 + (j*2) ] = i + 5 * j;
500 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
507 last_lon = last_lat = -999.0f;
510 sgSetVec2( base, sg_random(), sg_random() );
512 // build the cloud layer
517 const float layer_scale = layer_span / scale;
518 const float mpi = SG_PI/4;
520 // caclculate the difference between a flat-earth model and
521 // a round earth model given the span and altutude ASL of
522 // the cloud layer. This is the difference in altitude between
523 // the top of the inverted bowl and the edge of the bowl.
524 // const float alt_diff = layer_asl * 0.8;
525 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
526 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
527 const float border_to_core = layer_to_core * cos(layer_angle);
528 const float alt_diff = layer_to_core - border_to_core;
530 for (int i = 0; i < 4; i++)
532 if ( layer[i] != NULL ) {
533 layer_transform->removeKid(layer[i]); // automatic delete
536 vl[i] = new ssgVertexArray( 10 );
537 cl[i] = new ssgColourArray( 10 );
538 tl[i] = new ssgTexCoordArray( 10 );
541 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
542 alt_diff * (sin(i*mpi) - 2) );
544 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
546 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
549 vl[i]->add( vertex );
552 for (int j = 0; j < 4; j++)
554 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
555 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
557 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
558 base[1] + layer_scale * j/4 );
560 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
561 ( (j == 0) || (i == 3)) ?
562 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
565 vl[i]->add( vertex );
569 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
570 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
572 sgSetVec2( tc, base[0] + layer_scale * i/4,
573 base[1] + layer_scale * (j+1)/4 );
575 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
576 ((j == 3) || (i == 0)) ?
577 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
579 vl[i]->add( vertex );
583 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
584 alt_diff * (sin((i+1)*mpi) - 2) );
586 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
587 base[1] + layer_scale );
589 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
592 vl[i]->add( vertex );
595 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
596 layer_transform->addKid( layer[i] );
598 if ( layer_states[layer_coverage] != NULL ) {
599 layer[i]->setState( layer_states[layer_coverage] );
601 state_sel = layer_states[layer_coverage];
604 // force a repaint of the sky colors with arbitrary defaults
610 // repaint the cloud layer colors
611 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
613 if ( bump_mapping && enable_bump_mapping ) {
615 for ( int i = 0; i < 25; i++ ) {
616 sgCopyVec3( vertices[ i ].color, fog_color );
622 for ( int i = 0; i < 4; i++ ) {
623 for ( int j = 0; j < 10; ++j ) {
624 color = cl[i]->get( j );
625 sgCopyVec3( color, fog_color );
634 // reposition the cloud layer at the specified origin and orientation
635 // lon specifies a rotation about the Z axis
636 // lat specifies a rotation about the new Y axis
637 // spin specifies a rotation about the new Z axis (and orients the
638 // sunrise/set effects
639 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
640 double alt, double dt )
645 // combine p and asl (meters) to get translation offset
647 sgCopyVec3( asl_offset, up );
648 sgNormalizeVec3( asl_offset );
649 if ( alt <= layer_asl ) {
650 sgScaleVec3( asl_offset, layer_asl );
652 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
654 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
655 // << "," << asl_offset[2] << endl;
656 sgAddVec3( asl_offset, p );
657 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
658 // << "," << asl_offset[2] << endl;
660 // Translate to zero elevation
661 // Point3D zero_elev = current_view.get_cur_zero_elev();
662 sgMakeTransMat4( T1, asl_offset );
664 // printf(" Translated to %.2f %.2f %.2f\n",
665 // zero_elev.x, zero_elev.y, zero_elev.z );
667 // Rotate to proper orientation
668 // printf(" lon = %.2f lat = %.2f\n",
669 // lon * SGD_RADIANS_TO_DEGREES,
670 // lat * SGD_RADIANS_TO_DEGREES);
671 sgSetVec3( axis, 0.0, 0.0, 1.0 );
672 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
674 sgSetVec3( axis, 0.0, 1.0, 0.0 );
675 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
679 sgCopyMat4( TRANSFORM, T1 );
680 sgPreMultMat4( TRANSFORM, LON );
681 sgPreMultMat4( TRANSFORM, LAT );
684 sgSetCoord( &layerpos, TRANSFORM );
686 layer_transform->setTransform( &layerpos );
688 // now calculate update texture coordinates
689 if ( last_lon < -900 ) {
694 double sp_dist = speed*dt;
696 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
697 Point3D start( last_lon, last_lat, 0.0 );
698 Point3D dest( lon, lat, 0.0 );
699 double course = 0.0, dist = 0.0;
701 calc_gc_course_dist( dest, start, &course, &dist );
702 // cout << "course = " << course << ", dist = " << dist << endl;
704 // if start and dest are too close together,
705 // calc_gc_course_dist() can return a course of "nan". If
706 // this happens, lets just use the last known good course.
707 // This is a hack, and it would probably be better to make
708 // calc_gc_course_dist() more robust.
709 if ( isnan(course) ) {
710 course = last_course;
712 last_course = course;
715 // calculate cloud movement due to external forces
716 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
719 ax = cos(course) * dist;
720 ay = sin(course) * dist;
724 bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
725 by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
729 double xoff = (ax + bx) / (2 * scale);
730 double yoff = (ay + by) / (2 * scale);
732 const float layer_scale = layer_span / scale;
734 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
737 if ( bump_mapping && enable_bump_mapping ) {
738 base = vertices[12].texCoord;
740 base = tl[0]->get( 0 );
744 // the while loops can lead to *long* pauses if base[0] comes
745 // with a bogus value.
746 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
747 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
748 if ( base[0] > -10.0 && base[0] < 10.0 ) {
749 base[0] -= (int)base[0];
751 SG_LOG(SG_ASTRO, SG_DEBUG,
752 "Error: base = " << base[0] << "," << base[1] <<
753 " course = " << course << " dist = " << dist );
758 // the while loops can lead to *long* pauses if base[0] comes
759 // with a bogus value.
760 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
761 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
762 if ( base[1] > -10.0 && base[1] < 10.0 ) {
763 base[1] -= (int)base[1];
765 SG_LOG(SG_ASTRO, SG_ALERT,
766 "Error: base = " << base[0] << "," << base[1] <<
767 " course = " << course << " dist = " << dist );
771 if ( bump_mapping && enable_bump_mapping ) {
773 for ( int i = -2; i <= 2; i++ ) {
774 for ( int j = -2; j <= 2; j++ ) {
775 if ( i == 0 && j == 0 )
776 continue; // Already done on base
777 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
778 sgSetVec2( v1.texCoord,
779 base[0] + layer_scale * i * 0.25,
780 base[1] + layer_scale * j * 0.25 );
785 // cout << "base = " << base[0] << "," << base[1] << endl;
788 for (int i = 0; i < 4; i++) {
789 tc = tl[i]->get( 0 );
790 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
792 for (int j = 0; j < 4; j++)
794 tc = tl[i]->get( j*2+1 );
795 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
796 base[1] + layer_scale * j/4 );
798 tc = tl[i]->get( (j+1)*2 );
799 sgSetVec2( tc, base[0] + layer_scale * i/4,
800 base[1] + layer_scale * (j+1)/4 );
803 tc = tl[i]->get( 9 );
804 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
805 base[1] + layer_scale );
817 void SGCloudLayer::draw( bool top ) {
818 if ( layer_coverage != SG_CLOUD_CLEAR ) {
820 if ( bump_mapping && enable_bump_mapping ) {
825 ssgGetModelviewMatrix( modelview );
826 layer_transform->getTransform( transform );
828 sgInvertMat4( tmp, transform );
830 sgPostMultMat4( transform, modelview );
831 ssgLoadModelviewMatrix( transform );
834 ssgGetLight( 0 )->getPosition( lightVec );
835 sgNegateVec3( lightVec );
836 sgXformVec3( lightVec, tmp );
838 for ( int i = 0; i < 25; i++ ) {
839 CloudVertex &v = vertices[ i ];
840 sgSetVec3( v.tangentSpLight,
841 sgScalarProductVec3( v.sTangent, lightVec ),
842 sgScalarProductVec3( v.tTangent, lightVec ),
843 sgScalarProductVec3( v.normal, lightVec ) );
846 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
847 if ( top && decal == 0 ) {
848 decal = color_map[ layer_coverage ][ 0 ];
850 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
851 if ( top && normal == 0 ) {
852 normal = normal_map[ layer_coverage ][ 0 ];
855 glDisable( GL_LIGHTING );
856 glDisable( GL_CULL_FACE );
857 if ( layer_coverage == SG_CLOUD_FEW ) {
858 glEnable( GL_ALPHA_TEST );
859 glAlphaFunc ( GL_GREATER, 0.01 );
861 glEnable( GL_BLEND );
862 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
864 glPolygonMode( GL_FILL, GL_FRONT_AND_BACK );
865 glShadeModel( GL_SMOOTH );
866 glEnable( GL_COLOR_MATERIAL );
868 sgSetVec4( color, 0.05, 0.05, 0.05, 0.0 );
869 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
870 sgSetVec4( color, 0.8, 0.8, 0.8, 0.0 );
871 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
872 sgSetVec4( color, 0.5, 0.5, 0.5, 0.0 );
873 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
874 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
875 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
877 glActiveTexturePtr( GL_TEXTURE0_ARB );
878 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
879 glEnable( GL_TEXTURE_2D );
881 //Bind normalisation cube map to texture unit 1
882 glActiveTexturePtr( GL_TEXTURE1_ARB );
883 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
884 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
885 glActiveTexturePtr( GL_TEXTURE0_ARB );
887 //Set vertex arrays for cloud
888 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
889 glEnableClientState( GL_VERTEX_ARRAY );
891 if ( nb_texture_unit >= 3 ) {
892 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
893 glEnableClientState( GL_COLOR_ARRAY );
896 //Send texture coords for normal map to unit 0
897 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
898 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
900 //Send tangent space light vectors for normalisation to unit 1
901 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
902 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
903 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
905 //Set up texture environment to do (tex0 dot tex1)*color
906 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
907 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
908 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
909 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
910 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
912 glActiveTexturePtr( GL_TEXTURE1_ARB );
914 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
915 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
916 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
917 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
918 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
919 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
921 if ( nb_texture_unit >= 3 ) {
922 glActiveTexturePtr( GL_TEXTURE2_ARB );
923 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
925 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
926 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
927 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
930 if ( top || layer_coverage != SG_CLOUD_OVERCAST ) {
931 sgSetVec4( factors, 0.5f, 0.5f, 0.3f, 1.0f );
933 sgSetVec4( factors, 0.1f, 0.1f, 0.0f, 1.0f );
935 glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, factors );
937 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
938 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE_ARB );
939 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
940 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
941 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB );
943 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
944 glActiveTexturePtr( GL_TEXTURE0_ARB );
947 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
948 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
949 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
950 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
952 glDisable( GL_TEXTURE_2D );
953 glActiveTexturePtr( GL_TEXTURE1_ARB );
954 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
955 glActiveTexturePtr( GL_TEXTURE2_ARB );
956 glDisable( GL_TEXTURE_2D );
957 glActiveTexturePtr( GL_TEXTURE0_ARB );
959 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
960 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
961 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
962 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
963 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
964 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
966 glDisableClientState( GL_COLOR_ARRAY );
967 glEnable( GL_LIGHTING );
969 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
972 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
973 glActiveTexturePtr( GL_TEXTURE0_ARB );
976 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
977 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
978 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
979 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
982 glDisable( GL_TEXTURE_2D );
984 glActiveTexturePtr( GL_TEXTURE1_ARB );
985 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
986 glActiveTexturePtr( GL_TEXTURE0_ARB );
988 //disable vertex arrays
989 glDisableClientState( GL_VERTEX_ARRAY );
991 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
992 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
993 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
994 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
996 //Return to standard modulate texenv
997 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
999 glDepthFunc(GL_LEQUAL);
1001 glEnable( GL_LIGHTING );
1003 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1004 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1005 sgVec4 averageColor;
1006 sgSetVec4( averageColor, average, average, average, 1.0f );
1007 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1009 glBlendColorPtr( average, average, average, 1.0f );
1010 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1012 //Perform a second pass to color the torus
1013 //Bind decal texture
1014 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1015 glEnable(GL_TEXTURE_2D);
1017 //Set vertex arrays for torus
1018 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1019 glEnableClientState( GL_VERTEX_ARRAY );
1021 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1022 //glEnableClientState( GL_COLOR_ARRAY );
1024 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1025 glEnableClientState( GL_NORMAL_ARRAY );
1027 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1028 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1031 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1032 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1033 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1034 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1036 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1038 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1041 glDisable( GL_TEXTURE_2D );
1043 glDisableClientState( GL_VERTEX_ARRAY );
1044 glDisableClientState( GL_NORMAL_ARRAY );
1046 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1047 glEnable( GL_CULL_FACE );
1048 glDepthFunc(GL_LESS);
1050 ssgLoadModelviewMatrix( modelview );
1053 state_sel->selectStep( top ? 1 : 0 );
1054 ssgCullAndDraw( layer_root );
1060 // make an ssgSimpleState for a cloud layer given the named texture
1061 ssgSimpleState *sgCloudMakeState( const string &path ) {
1062 ssgSimpleState *state = new ssgSimpleState();
1064 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1066 state->setTexture( (char *)path.c_str() );
1067 state->setShadeModel( GL_SMOOTH );
1068 state->disable( GL_LIGHTING );
1069 state->disable( GL_CULL_FACE );
1070 state->enable( GL_TEXTURE_2D );
1071 state->enable( GL_COLOR_MATERIAL );
1072 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1073 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1074 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1075 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1076 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1077 state->enable( GL_BLEND );
1078 state->enable( GL_ALPHA_TEST );
1079 state->setAlphaClamp( 0.01 );