1 // cloud.cxx -- model a single cloud layer
3 // Written by Curtis Olson, started June 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is distributed in the hope that it will be useful, but
8 // WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // General Public License for more details.
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <simgear/compiler.h>
27 #include <simgear/math/point3d.hxx>
28 #include <simgear/math/polar3d.hxx>
29 #include <simgear/math/sg_random.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/screen/extensions.hxx>
33 #include <simgear/screen/texture.hxx>
35 #include "newcloud.hxx"
36 #include "cloudfield.hxx"
39 #if defined(__MINGW32__)
40 #define isnan(x) _isnan(x)
43 #if defined (__FreeBSD__)
44 # if __FreeBSD_version < 500000
46 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
52 static ssgStateSelector *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
53 static bool state_initialized = false;
54 static bool bump_mapping = false;
55 static GLint nb_texture_unit = 0;
56 static ssgTexture *normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
57 static ssgTexture *color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
58 static GLuint normalization_cube_map;
60 static glActiveTextureProc glActiveTexturePtr = 0;
61 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
62 static glBlendColorProc glBlendColorPtr = 0;
64 bool SGCloudLayer::enable_bump_mapping = false;
67 generateNormalizationCubeMap()
69 unsigned char data[ 32 * 32 * 3 ];
71 const float half_size = 16.0f,
74 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
77 unsigned char *ptr = data;
78 for ( j = 0; j < size; j++ ) {
79 for ( i = 0; i < size; i++ ) {
81 sgSetVec3( tmp, half_size,
82 -( j + offset - half_size ),
83 -( i + offset - half_size ) );
84 sgNormalizeVec3( tmp );
85 sgScaleVec3( tmp, 0.5f );
86 sgAddVec3( tmp, zero_normal );
88 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
89 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
90 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
93 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
94 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
97 for ( j = 0; j < size; j++ ) {
98 for ( i = 0; i < size; i++ ) {
100 sgSetVec3( tmp, -half_size,
101 -( j + offset - half_size ),
102 ( i + offset - half_size ) );
103 sgNormalizeVec3( tmp );
104 sgScaleVec3( tmp, 0.5f );
105 sgAddVec3( tmp, zero_normal );
107 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
108 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
109 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
112 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
113 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
116 for ( j = 0; j < size; j++ ) {
117 for ( i = 0; i < size; i++ ) {
119 sgSetVec3( tmp, ( i + offset - half_size ),
121 ( j + offset - half_size ) );
122 sgNormalizeVec3( tmp );
123 sgScaleVec3( tmp, 0.5f );
124 sgAddVec3( tmp, zero_normal );
126 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
127 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
128 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
131 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
132 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
135 for ( j = 0; j < size; j++ ) {
136 for ( i = 0; i < size; i++ ) {
138 sgSetVec3( tmp, ( i + offset - half_size ),
140 -( j + offset - half_size ) );
141 sgNormalizeVec3( tmp );
142 sgScaleVec3( tmp, 0.5f );
143 sgAddVec3( tmp, zero_normal );
145 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
146 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
147 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
150 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
151 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
154 for ( j = 0; j < size; j++ ) {
155 for ( i = 0; i < size; i++ ) {
157 sgSetVec3( tmp, ( i + offset - half_size ),
158 -( j + offset - half_size ),
160 sgNormalizeVec3( tmp );
161 sgScaleVec3( tmp, 0.5f );
162 sgAddVec3( tmp, zero_normal );
164 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
165 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
166 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
169 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
170 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
173 for ( j = 0; j < size; j++ ) {
174 for ( i = 0; i < size; i++ ) {
176 sgSetVec3( tmp, -( i + offset - half_size ),
177 -( j + offset - half_size ),
179 sgNormalizeVec3( tmp );
180 sgScaleVec3( tmp, 0.5f );
181 sgAddVec3( tmp, zero_normal );
183 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
184 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
185 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
188 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
189 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
194 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
197 layer_root(new ssgRoot),
198 layer_transform(new ssgTransform),
201 texture_path(tex_path),
204 layer_thickness(0.0),
205 layer_transition(0.0),
206 layer_coverage(SG_CLOUD_CLEAR),
213 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
214 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
215 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
216 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
218 layer_root->addKid(layer_transform);
219 layer3D = new SGCloudField;
224 SGCloudLayer::~SGCloudLayer()
229 delete layer_root; // deletes layer_transform and layer as well
233 SGCloudLayer::getSpan_m () const
239 SGCloudLayer::setSpan_m (float span_m)
241 if (span_m != layer_span) {
248 SGCloudLayer::getElevation_m () const
254 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
256 layer_asl = elevation_m;
259 if (elevation_m > 4000)
260 setSpan_m( elevation_m * 10 );
267 SGCloudLayer::getThickness_m () const
269 return layer_thickness;
273 SGCloudLayer::setThickness_m (float thickness_m)
275 layer_thickness = thickness_m;
279 SGCloudLayer::getTransition_m () const
281 return layer_transition;
285 SGCloudLayer::setTransition_m (float transition_m)
287 layer_transition = transition_m;
290 SGCloudLayer::Coverage
291 SGCloudLayer::getCoverage () const
293 return layer_coverage;
297 SGCloudLayer::setCoverage (Coverage coverage)
299 if (coverage != layer_coverage) {
300 layer_coverage = coverage;
306 // build the cloud object
308 SGCloudLayer::rebuild()
310 // Initialize states and sizes if necessary.
311 if ( !state_initialized ) {
312 state_initialized = true;
314 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
316 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
317 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
318 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
319 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
320 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
322 if ( bump_mapping ) {
323 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
324 if ( nb_texture_unit < 2 ) {
325 bump_mapping = false;
327 //nb_texture_unit = 2; // Force the number of units for now
330 if ( bump_mapping ) {
332 // This bump mapping code was inspired by the tutorial available at
333 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
334 // and a NVidia white paper
335 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
336 // The normal map textures were generated by the normal map Gimp plugin :
337 // http://nifelheim.dyndns.org/~cocidius/normalmap/
341 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
342 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
343 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
345 cloud_path.set(texture_path.str());
346 cloud_path.append("overcast.rgb");
347 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
348 color_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
349 cloud_path.set(texture_path.str());
350 cloud_path.append("overcast_n.rgb");
351 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
352 normal_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
354 cloud_path.set(texture_path.str());
355 cloud_path.append("overcast_top.rgb");
356 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
357 color_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
358 cloud_path.set(texture_path.str());
359 cloud_path.append("overcast_top_n.rgb");
360 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
361 normal_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
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 color_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
367 cloud_path.set(texture_path.str());
368 cloud_path.append("broken_n.rgb");
369 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
370 normal_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
372 cloud_path.set(texture_path.str());
373 cloud_path.append("scattered.rgba");
374 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
375 color_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
376 cloud_path.set(texture_path.str());
377 cloud_path.append("scattered_n.rgb");
378 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
379 normal_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
381 cloud_path.set(texture_path.str());
382 cloud_path.append("few.rgba");
383 color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
384 color_map[ SG_CLOUD_FEW ][ 0 ]->ref();
385 cloud_path.set(texture_path.str());
386 cloud_path.append("few_n.rgb");
387 normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
388 normal_map[ SG_CLOUD_FEW ][ 0 ]->ref();
390 cloud_path.set(texture_path.str());
391 cloud_path.append("cirrus.rgba");
392 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
393 color_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
394 cloud_path.set(texture_path.str());
395 cloud_path.append("cirrus_n.rgb");
396 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
397 normal_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
399 glGenTextures( 1, &normalization_cube_map );
400 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
401 generateNormalizationCubeMap();
402 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
403 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
404 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
405 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
406 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
409 ssgStateSelector *state_sel;
410 ssgSimpleState *state;
412 state_sel = new ssgStateSelector( 2 );
414 cloud_path.set(texture_path.str());
415 cloud_path.append("overcast.rgb");
416 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
417 cloud_path.set(texture_path.str());
418 cloud_path.append("overcast_top.rgb");
419 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
420 layer_states[SG_CLOUD_OVERCAST] = state_sel;
422 state_sel = new ssgStateSelector( 2 );
424 cloud_path.set(texture_path.str());
425 cloud_path.append("broken.rgba");
426 state = sgCloudMakeState(cloud_path.str());
427 state_sel->setStep( 0, state );
428 state_sel->setStep( 1, state );
429 layer_states[SG_CLOUD_BROKEN] = state_sel;
431 state_sel = new ssgStateSelector( 2 );
433 cloud_path.set(texture_path.str());
434 cloud_path.append("scattered.rgba");
435 state = sgCloudMakeState(cloud_path.str());
436 state_sel->setStep( 0, state );
437 state_sel->setStep( 1, state );
438 layer_states[SG_CLOUD_SCATTERED] = state_sel;
440 state_sel = new ssgStateSelector( 2 );
442 cloud_path.set(texture_path.str());
443 cloud_path.append("few.rgba");
444 state = sgCloudMakeState(cloud_path.str());
445 state_sel->setStep( 0, state );
446 state_sel->setStep( 1, state );
447 layer_states[SG_CLOUD_FEW] = state_sel;
449 state_sel = new ssgStateSelector( 2 );
451 cloud_path.set(texture_path.str());
452 cloud_path.append("cirrus.rgba");
453 state = sgCloudMakeState(cloud_path.str());
454 state_sel->setStep( 0, state );
455 state_sel->setStep( 1, state );
456 layer_states[SG_CLOUD_CIRRUS] = state_sel;
458 layer_states[SG_CLOUD_CLEAR] = 0;
460 SGNewCloud::loadTextures(texture_path.str());
461 layer3D->buildTestLayer();
464 if ( bump_mapping ) {
467 vertices = new CloudVertex[ 25 ];
468 indices = new unsigned int[ 40 ];
472 sgSetVec2( base, sg_random(), sg_random() );
474 const float layer_scale = layer_span / scale;
475 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
476 const float half_angle = 0.5 * layer_span / layer_to_core;
479 for ( i = -2; i <= 2; i++ ) {
480 for ( int j = -2; j <= 2; j++ ) {
481 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
482 sgSetVec3( v1.position,
483 0.5 * i * layer_span,
484 0.5 * j * layer_span,
485 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
486 sgSetVec2( v1.texCoord,
487 base[0] + layer_scale * i * 0.25,
488 base[1] + layer_scale * j * 0.25 );
489 sgSetVec3( v1.sTangent,
490 cos( i * half_angle ),
492 -sin( i * half_angle ) );
493 sgSetVec3( v1.tTangent,
495 cos( j * half_angle ),
496 -sin( j * half_angle ) );
497 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
498 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : cloud_alpha * 0.15f );
502 * 0 1 5 6 10 11 15 16 20 21
503 * 1 2 6 7 11 12 16 17 21 22
504 * 2 3 7 8 12 13 17 18 22 23
505 * 3 4 8 9 13 14 18 19 23 24
507 for ( i = 0; i < 4; i++ ) {
508 for ( int j = 0; j < 5; j++ ) {
509 indices[ i*10 + (j*2) ] = i + 5 * j;
510 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
517 last_lon = last_lat = -999.0f;
520 sgSetVec2( base, sg_random(), sg_random() );
522 // build the cloud layer
527 const float layer_scale = layer_span / scale;
528 const float mpi = SG_PI/4;
530 // caclculate the difference between a flat-earth model and
531 // a round earth model given the span and altutude ASL of
532 // the cloud layer. This is the difference in altitude between
533 // the top of the inverted bowl and the edge of the bowl.
534 // const float alt_diff = layer_asl * 0.8;
535 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
536 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
537 const float border_to_core = layer_to_core * cos(layer_angle);
538 const float alt_diff = layer_to_core - border_to_core;
540 for (int i = 0; i < 4; i++)
542 if ( layer[i] != NULL ) {
543 layer_transform->removeKid(layer[i]); // automatic delete
546 vl[i] = new ssgVertexArray( 10 );
547 cl[i] = new ssgColourArray( 10 );
548 tl[i] = new ssgTexCoordArray( 10 );
551 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
552 alt_diff * (sin(i*mpi) - 2) );
554 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
556 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
559 vl[i]->add( vertex );
562 for (int j = 0; j < 4; j++)
564 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
565 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
567 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
568 base[1] + layer_scale * j/4 );
570 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
571 ( (j == 0) || (i == 3)) ?
572 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
575 vl[i]->add( vertex );
579 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
580 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
582 sgSetVec2( tc, base[0] + layer_scale * i/4,
583 base[1] + layer_scale * (j+1)/4 );
585 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
586 ((j == 3) || (i == 0)) ?
587 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
589 vl[i]->add( vertex );
593 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
594 alt_diff * (sin((i+1)*mpi) - 2) );
596 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
597 base[1] + layer_scale );
599 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
602 vl[i]->add( vertex );
605 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
606 layer_transform->addKid( layer[i] );
608 if ( layer_states[layer_coverage] != NULL ) {
609 layer[i]->setState( layer_states[layer_coverage] );
611 state_sel = layer_states[layer_coverage];
614 // force a repaint of the sky colors with arbitrary defaults
620 // repaint the cloud layer colors
621 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
623 if ( bump_mapping && enable_bump_mapping ) {
625 for ( int i = 0; i < 25; i++ ) {
626 sgCopyVec3( vertices[ i ].color, fog_color );
632 for ( int i = 0; i < 4; i++ ) {
633 color = cl[i]->get( 0 );
634 sgCopyVec3( color, fog_color );
635 color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
637 for ( int j = 0; j < 4; ++j ) {
638 color = cl[i]->get( (2*j) + 1 );
639 sgCopyVec3( color, fog_color );
641 ((j == 0) || (i == 3)) ?
642 ((j == 0) && (i == 3)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
644 color = cl[i]->get( (2*j) + 2 );
645 sgCopyVec3( color, fog_color );
647 ((j == 3) || (i == 0)) ?
648 ((j == 3) && (i == 0)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
651 color = cl[i]->get( 9 );
652 sgCopyVec3( color, fog_color );
653 color[3] = (i == 3) ? 0.0f : cloud_alpha * 0.15f;
660 // reposition the cloud layer at the specified origin and orientation
661 // lon specifies a rotation about the Z axis
662 // lat specifies a rotation about the new Y axis
663 // spin specifies a rotation about the new Z axis (and orients the
664 // sunrise/set effects
665 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
666 double alt, double dt )
671 // combine p and asl (meters) to get translation offset
673 sgCopyVec3( asl_offset, up );
674 sgNormalizeVec3( asl_offset );
675 if ( alt <= layer_asl ) {
676 sgScaleVec3( asl_offset, layer_asl );
678 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
680 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
681 // << "," << asl_offset[2] << endl;
682 sgAddVec3( asl_offset, p );
683 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
684 // << "," << asl_offset[2] << endl;
686 // Translate to zero elevation
687 // Point3D zero_elev = current_view.get_cur_zero_elev();
688 sgMakeTransMat4( T1, asl_offset );
690 // printf(" Translated to %.2f %.2f %.2f\n",
691 // zero_elev.x, zero_elev.y, zero_elev.z );
693 // Rotate to proper orientation
694 // printf(" lon = %.2f lat = %.2f\n",
695 // lon * SGD_RADIANS_TO_DEGREES,
696 // lat * SGD_RADIANS_TO_DEGREES);
697 sgSetVec3( axis, 0.0, 0.0, 1.0 );
698 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
700 sgSetVec3( axis, 0.0, 1.0, 0.0 );
701 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
705 sgCopyMat4( TRANSFORM, T1 );
706 sgPreMultMat4( TRANSFORM, LON );
707 sgPreMultMat4( TRANSFORM, LAT );
710 sgSetCoord( &layerpos, TRANSFORM );
712 layer_transform->setTransform( &layerpos );
714 // now calculate update texture coordinates
715 if ( last_lon < -900 ) {
720 double sp_dist = speed*dt;
722 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
723 Point3D start( last_lon, last_lat, 0.0 );
724 Point3D dest( lon, lat, 0.0 );
725 double course = 0.0, dist = 0.0;
727 calc_gc_course_dist( dest, start, &course, &dist );
728 // cout << "course = " << course << ", dist = " << dist << endl;
730 // if start and dest are too close together,
731 // calc_gc_course_dist() can return a course of "nan". If
732 // this happens, lets just use the last known good course.
733 // This is a hack, and it would probably be better to make
734 // calc_gc_course_dist() more robust.
735 if ( isnan(course) ) {
736 course = last_course;
738 last_course = course;
741 // calculate cloud movement due to external forces
742 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
745 ax = cos(course) * dist;
746 ay = sin(course) * dist;
750 bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
751 by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
755 double xoff = (ax + bx) / (2 * scale);
756 double yoff = (ay + by) / (2 * scale);
758 const float layer_scale = layer_span / scale;
760 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
763 if ( bump_mapping && enable_bump_mapping ) {
764 base = vertices[12].texCoord;
766 base = tl[0]->get( 0 );
770 // the while loops can lead to *long* pauses if base[0] comes
771 // with a bogus value.
772 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
773 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
774 if ( base[0] > -10.0 && base[0] < 10.0 ) {
775 base[0] -= (int)base[0];
777 SG_LOG(SG_ASTRO, SG_DEBUG,
778 "Error: base = " << base[0] << "," << base[1] <<
779 " course = " << course << " dist = " << dist );
784 // the while loops can lead to *long* pauses if base[0] comes
785 // with a bogus value.
786 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
787 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
788 if ( base[1] > -10.0 && base[1] < 10.0 ) {
789 base[1] -= (int)base[1];
791 SG_LOG(SG_ASTRO, SG_ALERT,
792 "Error: base = " << base[0] << "," << base[1] <<
793 " course = " << course << " dist = " << dist );
797 if ( bump_mapping && enable_bump_mapping ) {
799 for ( int i = -2; i <= 2; i++ ) {
800 for ( int j = -2; j <= 2; j++ ) {
801 if ( i == 0 && j == 0 )
802 continue; // Already done on base
803 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
804 sgSetVec2( v1.texCoord,
805 base[0] + layer_scale * i * 0.25,
806 base[1] + layer_scale * j * 0.25 );
811 // cout << "base = " << base[0] << "," << base[1] << endl;
814 for (int i = 0; i < 4; i++) {
815 tc = tl[i]->get( 0 );
816 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
818 for (int j = 0; j < 4; j++)
820 tc = tl[i]->get( j*2+1 );
821 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
822 base[1] + layer_scale * j/4 );
824 tc = tl[i]->get( (j+1)*2 );
825 sgSetVec2( tc, base[0] + layer_scale * i/4,
826 base[1] + layer_scale * (j+1)/4 );
829 tc = tl[i]->get( 9 );
830 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
831 base[1] + layer_scale );
839 layer3D->reposition( p, up, lon, lat, alt, dt, direction, speed);
844 void SGCloudLayer::draw( bool top ) {
845 if ( layer_coverage != SG_CLOUD_CLEAR ) {
847 if ( SGCloudField::enable3D && layer3D->is3D())
850 if ( bump_mapping && enable_bump_mapping ) {
855 ssgGetModelviewMatrix( modelview );
856 layer_transform->getTransform( transform );
858 sgTransposeNegateMat4( tmp, transform );
860 sgPostMultMat4( transform, modelview );
861 ssgLoadModelviewMatrix( transform );
864 ssgGetLight( 0 )->getPosition( lightVec );
865 sgNegateVec3( lightVec );
866 sgXformVec3( lightVec, tmp );
868 for ( int i = 0; i < 25; i++ ) {
869 CloudVertex &v = vertices[ i ];
870 sgSetVec3( v.tangentSpLight,
871 sgScalarProductVec3( v.sTangent, lightVec ),
872 sgScalarProductVec3( v.tTangent, lightVec ),
873 sgScalarProductVec3( v.normal, lightVec ) );
876 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
877 if ( top && decal == 0 ) {
878 decal = color_map[ layer_coverage ][ 0 ];
880 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
881 if ( top && normal == 0 ) {
882 normal = normal_map[ layer_coverage ][ 0 ];
885 glDisable( GL_LIGHTING );
886 glDisable( GL_CULL_FACE );
887 // glDisable( GL_ALPHA_TEST );
888 if ( layer_coverage == SG_CLOUD_FEW ) {
889 glEnable( GL_ALPHA_TEST );
890 glAlphaFunc ( GL_GREATER, 0.01 );
892 glEnable( GL_BLEND );
893 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
895 glShadeModel( GL_SMOOTH );
896 glEnable( GL_COLOR_MATERIAL );
900 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
901 emis = ( color[0]+color[1]+color[2] ) / 3.0;
905 sgSetVec4( color, emis, emis, emis, 0.0 );
906 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
907 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
908 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
909 sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
910 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
911 sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
912 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
914 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
916 glActiveTexturePtr( GL_TEXTURE0_ARB );
917 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
918 glEnable( GL_TEXTURE_2D );
920 //Bind normalisation cube map to texture unit 1
921 glActiveTexturePtr( GL_TEXTURE1_ARB );
922 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
923 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
924 glActiveTexturePtr( GL_TEXTURE0_ARB );
926 //Set vertex arrays for cloud
927 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
928 glEnableClientState( GL_VERTEX_ARRAY );
930 if ( nb_texture_unit >= 3 ) {
931 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
932 glEnableClientState( GL_COLOR_ARRAY );
935 //Send texture coords for normal map to unit 0
936 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
937 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
939 //Send tangent space light vectors for normalisation to unit 1
940 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
941 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
942 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
944 //Set up texture environment to do (tex0 dot tex1)*color
945 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
946 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
947 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
948 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
949 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
951 glActiveTexturePtr( GL_TEXTURE1_ARB );
953 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
954 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
955 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
956 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
957 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
958 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
960 if ( nb_texture_unit >= 3 ) {
961 glActiveTexturePtr( GL_TEXTURE2_ARB );
962 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
964 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
965 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
966 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
968 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
969 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
970 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
971 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
973 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
974 glActiveTexturePtr( GL_TEXTURE0_ARB );
977 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
978 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
979 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
980 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
982 glDisable( GL_TEXTURE_2D );
983 glActiveTexturePtr( GL_TEXTURE1_ARB );
984 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
985 glActiveTexturePtr( GL_TEXTURE2_ARB );
986 glDisable( GL_TEXTURE_2D );
987 glActiveTexturePtr( GL_TEXTURE0_ARB );
989 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
990 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
991 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
992 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
993 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
994 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
996 glDisableClientState( GL_COLOR_ARRAY );
997 glEnable( GL_LIGHTING );
999 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1002 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1003 glActiveTexturePtr( GL_TEXTURE0_ARB );
1006 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1007 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1008 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1009 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1012 glDisable( GL_TEXTURE_2D );
1014 glActiveTexturePtr( GL_TEXTURE1_ARB );
1015 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
1016 glActiveTexturePtr( GL_TEXTURE0_ARB );
1018 //disable vertex arrays
1019 glDisableClientState( GL_VERTEX_ARRAY );
1021 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1022 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
1023 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1024 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1026 //Return to standard modulate texenv
1027 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1029 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
1030 glDepthFunc(GL_LEQUAL);
1032 glEnable( GL_LIGHTING );
1034 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1035 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1036 average = 0.15 + average/10;
1037 sgVec4 averageColor;
1038 sgSetVec4( averageColor, average, average, average, 1.0f );
1039 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1041 glBlendColorPtr( average, average, average, 1.0f );
1042 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1044 //Perform a second pass to color the torus
1045 //Bind decal texture
1046 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1047 glEnable(GL_TEXTURE_2D);
1049 //Set vertex arrays for torus
1050 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1051 glEnableClientState( GL_VERTEX_ARRAY );
1053 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1054 //glEnableClientState( GL_COLOR_ARRAY );
1056 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1057 glEnableClientState( GL_NORMAL_ARRAY );
1059 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1060 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1063 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1064 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1065 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1066 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1068 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1070 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1074 glDisable( GL_TEXTURE_2D );
1076 glDisableClientState( GL_VERTEX_ARRAY );
1077 glDisableClientState( GL_NORMAL_ARRAY );
1079 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1080 glEnable( GL_CULL_FACE );
1081 glDepthFunc(GL_LESS);
1083 ssgLoadModelviewMatrix( modelview );
1086 state_sel->selectStep( top ? 1 : 0 );
1087 ssgCullAndDraw( layer_root );
1093 // make an ssgSimpleState for a cloud layer given the named texture
1094 ssgSimpleState *sgCloudMakeState( const string &path ) {
1095 ssgSimpleState *state = new ssgSimpleState();
1097 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1099 state->setTexture( (char *)path.c_str() );
1100 state->setShadeModel( GL_SMOOTH );
1101 state->disable( GL_LIGHTING );
1102 state->disable( GL_CULL_FACE );
1103 state->enable( GL_TEXTURE_2D );
1104 state->enable( GL_COLOR_MATERIAL );
1105 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1106 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1107 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1108 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1109 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1110 state->enable( GL_BLEND );
1111 state->enable( GL_ALPHA_TEST );
1112 state->setAlphaClamp( 0.01 );