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 library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 # include <simgear_config.h>
27 #include <simgear/compiler.h>
35 #include <simgear/math/point3d.hxx>
36 #include <simgear/math/polar3d.hxx>
37 #include <simgear/math/sg_random.h>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/screen/extensions.hxx>
40 #include <simgear/screen/texture.hxx>
41 #include <simgear/structure/ssgSharedPtr.hxx>
43 #include "newcloud.hxx"
44 #include "cloudfield.hxx"
47 #if defined(__MINGW32__)
48 #define isnan(x) _isnan(x)
51 #if defined (__FreeBSD__)
52 # if __FreeBSD_version < 500000
54 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
60 static ssgSharedPtr<ssgStateSelector> layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
61 static bool state_initialized = false;
62 static bool bump_mapping = false;
63 static GLint nb_texture_unit = 0;
64 static ssgSharedPtr<ssgTexture> normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
65 static ssgSharedPtr<ssgTexture> color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
66 static GLuint normalization_cube_map;
68 static glActiveTextureProc glActiveTexturePtr = 0;
69 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
70 static glBlendColorProc glBlendColorPtr = 0;
72 bool SGCloudLayer::enable_bump_mapping = false;
75 generateNormalizationCubeMap()
77 unsigned char data[ 32 * 32 * 3 ];
79 const float half_size = 16.0f,
82 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
85 unsigned char *ptr = data;
86 for ( j = 0; j < size; j++ ) {
87 for ( i = 0; i < size; i++ ) {
89 sgSetVec3( tmp, half_size,
90 -( j + offset - half_size ),
91 -( i + offset - half_size ) );
92 sgNormalizeVec3( tmp );
93 sgScaleVec3( tmp, 0.5f );
94 sgAddVec3( tmp, zero_normal );
96 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
97 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
98 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
101 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
102 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
105 for ( j = 0; j < size; j++ ) {
106 for ( i = 0; i < size; i++ ) {
108 sgSetVec3( tmp, -half_size,
109 -( j + offset - half_size ),
110 ( i + offset - half_size ) );
111 sgNormalizeVec3( tmp );
112 sgScaleVec3( tmp, 0.5f );
113 sgAddVec3( tmp, zero_normal );
115 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
116 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
117 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
120 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
121 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
124 for ( j = 0; j < size; j++ ) {
125 for ( i = 0; i < size; i++ ) {
127 sgSetVec3( tmp, ( i + offset - half_size ),
129 ( j + offset - half_size ) );
130 sgNormalizeVec3( tmp );
131 sgScaleVec3( tmp, 0.5f );
132 sgAddVec3( tmp, zero_normal );
134 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
135 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
136 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
139 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
140 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
143 for ( j = 0; j < size; j++ ) {
144 for ( i = 0; i < size; i++ ) {
146 sgSetVec3( tmp, ( i + offset - half_size ),
148 -( j + offset - half_size ) );
149 sgNormalizeVec3( tmp );
150 sgScaleVec3( tmp, 0.5f );
151 sgAddVec3( tmp, zero_normal );
153 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
154 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
155 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
158 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
159 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
162 for ( j = 0; j < size; j++ ) {
163 for ( i = 0; i < size; i++ ) {
165 sgSetVec3( tmp, ( i + offset - half_size ),
166 -( j + offset - half_size ),
168 sgNormalizeVec3( tmp );
169 sgScaleVec3( tmp, 0.5f );
170 sgAddVec3( tmp, zero_normal );
172 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
173 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
174 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
177 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
178 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
181 for ( j = 0; j < size; j++ ) {
182 for ( i = 0; i < size; i++ ) {
184 sgSetVec3( tmp, -( i + offset - half_size ),
185 -( j + offset - half_size ),
187 sgNormalizeVec3( tmp );
188 sgScaleVec3( tmp, 0.5f );
189 sgAddVec3( tmp, zero_normal );
191 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
192 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
193 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
196 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
197 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
202 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
205 layer_root(new ssgRoot),
206 layer_transform(new ssgTransform),
209 texture_path(tex_path),
212 layer_thickness(0.0),
213 layer_transition(0.0),
214 layer_coverage(SG_CLOUD_CLEAR),
221 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
222 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
223 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
224 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
226 layer_root->addKid(layer_transform);
227 layer3D = new SGCloudField;
232 SGCloudLayer::~SGCloudLayer()
237 delete layer_root; // deletes layer_transform and layer as well
241 SGCloudLayer::getSpan_m () const
247 SGCloudLayer::setSpan_m (float span_m)
249 if (span_m != layer_span) {
256 SGCloudLayer::getElevation_m () const
262 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
264 layer_asl = elevation_m;
267 if (elevation_m > 4000)
268 setSpan_m( elevation_m * 10 );
275 SGCloudLayer::getThickness_m () const
277 return layer_thickness;
281 SGCloudLayer::setThickness_m (float thickness_m)
283 layer_thickness = thickness_m;
287 SGCloudLayer::getTransition_m () const
289 return layer_transition;
293 SGCloudLayer::setTransition_m (float transition_m)
295 layer_transition = transition_m;
298 SGCloudLayer::Coverage
299 SGCloudLayer::getCoverage () const
301 return layer_coverage;
305 SGCloudLayer::setCoverage (Coverage coverage)
307 if (coverage != layer_coverage) {
308 layer_coverage = coverage;
314 // build the cloud object
316 SGCloudLayer::rebuild()
318 // Initialize states and sizes if necessary.
319 if ( !state_initialized ) {
320 state_initialized = true;
322 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
324 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
325 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
326 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
327 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
328 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
330 if ( bump_mapping ) {
331 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
332 if ( nb_texture_unit < 2 ) {
333 bump_mapping = false;
335 //nb_texture_unit = 2; // Force the number of units for now
338 if ( bump_mapping ) {
340 // This bump mapping code was inspired by the tutorial available at
341 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
342 // and a NVidia white paper
343 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
344 // The normal map textures were generated by the normal map Gimp plugin :
345 // http://nifelheim.dyndns.org/~cocidius/normalmap/
349 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
350 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
351 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
353 cloud_path.set(texture_path.str());
354 cloud_path.append("overcast.rgb");
355 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
356 cloud_path.set(texture_path.str());
357 cloud_path.append("overcast_n.rgb");
358 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
360 cloud_path.set(texture_path.str());
361 cloud_path.append("overcast_top.rgb");
362 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
363 cloud_path.set(texture_path.str());
364 cloud_path.append("overcast_top_n.rgb");
365 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
367 cloud_path.set(texture_path.str());
368 cloud_path.append("broken.rgba");
369 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
370 cloud_path.set(texture_path.str());
371 cloud_path.append("broken_n.rgb");
372 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
374 cloud_path.set(texture_path.str());
375 cloud_path.append("scattered.rgba");
376 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
377 cloud_path.set(texture_path.str());
378 cloud_path.append("scattered_n.rgb");
379 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
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 cloud_path.set(texture_path.str());
385 cloud_path.append("few_n.rgb");
386 normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
388 cloud_path.set(texture_path.str());
389 cloud_path.append("cirrus.rgba");
390 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
391 cloud_path.set(texture_path.str());
392 cloud_path.append("cirrus_n.rgb");
393 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
395 glGenTextures( 1, &normalization_cube_map );
396 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
397 generateNormalizationCubeMap();
398 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
399 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
400 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
401 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
402 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
405 ssgStateSelector *state_sel;
406 ssgSimpleState *state;
408 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 );
418 cloud_path.set(texture_path.str());
419 cloud_path.append("broken.rgba");
420 state = sgCloudMakeState(cloud_path.str());
421 state_sel->setStep( 0, state );
422 state_sel->setStep( 1, state );
423 layer_states[SG_CLOUD_BROKEN] = state_sel;
425 state_sel = new ssgStateSelector( 2 );
426 cloud_path.set(texture_path.str());
427 cloud_path.append("scattered.rgba");
428 state = sgCloudMakeState(cloud_path.str());
429 state_sel->setStep( 0, state );
430 state_sel->setStep( 1, state );
431 layer_states[SG_CLOUD_SCATTERED] = state_sel;
433 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 );
442 cloud_path.set(texture_path.str());
443 cloud_path.append("cirrus.rgba");
444 state = sgCloudMakeState(cloud_path.str());
445 state_sel->setStep( 0, state );
446 state_sel->setStep( 1, state );
447 layer_states[SG_CLOUD_CIRRUS] = state_sel;
449 layer_states[SG_CLOUD_CLEAR] = 0;
451 SGNewCloud::loadTextures(texture_path.str());
452 layer3D->buildTestLayer();
455 if ( bump_mapping ) {
458 vertices = new CloudVertex[ 25 ];
459 indices = new unsigned int[ 40 ];
463 sgSetVec2( base, sg_random(), sg_random() );
465 const float layer_scale = layer_span / scale;
466 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
467 const float half_angle = 0.5 * layer_span / layer_to_core;
470 for ( i = -2; i <= 2; i++ ) {
471 for ( int j = -2; j <= 2; j++ ) {
472 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
473 sgSetVec3( v1.position,
474 0.5 * i * layer_span,
475 0.5 * j * layer_span,
476 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
477 sgSetVec2( v1.texCoord,
478 base[0] + layer_scale * i * 0.25,
479 base[1] + layer_scale * j * 0.25 );
480 sgSetVec3( v1.sTangent,
481 cos( i * half_angle ),
483 -sin( i * half_angle ) );
484 sgSetVec3( v1.tTangent,
486 cos( j * half_angle ),
487 -sin( j * half_angle ) );
488 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
489 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : cloud_alpha * 0.15f );
493 * 0 1 5 6 10 11 15 16 20 21
494 * 1 2 6 7 11 12 16 17 21 22
495 * 2 3 7 8 12 13 17 18 22 23
496 * 3 4 8 9 13 14 18 19 23 24
498 for ( i = 0; i < 4; i++ ) {
499 for ( int j = 0; j < 5; j++ ) {
500 indices[ i*10 + (j*2) ] = i + 5 * j;
501 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
508 last_lon = last_lat = -999.0f;
511 sgSetVec2( base, sg_random(), sg_random() );
513 // build the cloud layer
518 const float layer_scale = layer_span / scale;
519 const float mpi = SG_PI/4;
521 // caclculate the difference between a flat-earth model and
522 // a round earth model given the span and altutude ASL of
523 // the cloud layer. This is the difference in altitude between
524 // the top of the inverted bowl and the edge of the bowl.
525 // const float alt_diff = layer_asl * 0.8;
526 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
527 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
528 const float border_to_core = layer_to_core * cos(layer_angle);
529 const float alt_diff = layer_to_core - border_to_core;
531 for (int i = 0; i < 4; i++)
533 if ( layer[i] != NULL ) {
534 layer_transform->removeKid(layer[i]); // automatic delete
537 vl[i] = new ssgVertexArray( 10 );
538 cl[i] = new ssgColourArray( 10 );
539 tl[i] = new ssgTexCoordArray( 10 );
542 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
543 alt_diff * (sin(i*mpi) - 2) );
545 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
547 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
550 vl[i]->add( vertex );
553 for (int j = 0; j < 4; j++)
555 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
556 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
558 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
559 base[1] + layer_scale * j/4 );
561 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
562 ( (j == 0) || (i == 3)) ?
563 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
566 vl[i]->add( vertex );
570 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
571 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
573 sgSetVec2( tc, base[0] + layer_scale * i/4,
574 base[1] + layer_scale * (j+1)/4 );
576 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
577 ((j == 3) || (i == 0)) ?
578 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
580 vl[i]->add( vertex );
584 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
585 alt_diff * (sin((i+1)*mpi) - 2) );
587 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
588 base[1] + layer_scale );
590 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
593 vl[i]->add( vertex );
596 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
597 layer_transform->addKid( layer[i] );
599 if ( layer_states[layer_coverage] != NULL ) {
600 layer[i]->setState( layer_states[layer_coverage] );
602 state_sel = layer_states[layer_coverage];
605 // force a repaint of the sky colors with arbitrary defaults
611 // repaint the cloud layer colors
612 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
614 if ( bump_mapping && enable_bump_mapping ) {
616 for ( int i = 0; i < 25; i++ ) {
617 sgCopyVec3( vertices[ i ].color, fog_color );
623 for ( int i = 0; i < 4; i++ ) {
624 color = cl[i]->get( 0 );
625 sgCopyVec3( color, fog_color );
626 color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
628 for ( int j = 0; j < 4; ++j ) {
629 color = cl[i]->get( (2*j) + 1 );
630 sgCopyVec3( color, fog_color );
632 ((j == 0) || (i == 3)) ?
633 ((j == 0) && (i == 3)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
635 color = cl[i]->get( (2*j) + 2 );
636 sgCopyVec3( color, fog_color );
638 ((j == 3) || (i == 0)) ?
639 ((j == 3) && (i == 0)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
642 color = cl[i]->get( 9 );
643 sgCopyVec3( color, fog_color );
644 color[3] = (i == 3) ? 0.0f : cloud_alpha * 0.15f;
651 // reposition the cloud layer at the specified origin and orientation
652 // lon specifies a rotation about the Z axis
653 // lat specifies a rotation about the new Y axis
654 // spin specifies a rotation about the new Z axis (and orients the
655 // sunrise/set effects
656 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
657 double alt, double dt )
662 // combine p and asl (meters) to get translation offset
664 sgCopyVec3( asl_offset, up );
665 sgNormalizeVec3( asl_offset );
666 if ( alt <= layer_asl ) {
667 sgScaleVec3( asl_offset, layer_asl );
669 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
671 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
672 // << "," << asl_offset[2] << endl;
673 sgAddVec3( asl_offset, p );
674 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
675 // << "," << asl_offset[2] << endl;
677 // Translate to zero elevation
678 // Point3D zero_elev = current_view.get_cur_zero_elev();
679 sgMakeTransMat4( T1, asl_offset );
681 // printf(" Translated to %.2f %.2f %.2f\n",
682 // zero_elev.x, zero_elev.y, zero_elev.z );
684 // Rotate to proper orientation
685 // printf(" lon = %.2f lat = %.2f\n",
686 // lon * SGD_RADIANS_TO_DEGREES,
687 // lat * SGD_RADIANS_TO_DEGREES);
688 sgSetVec3( axis, 0.0, 0.0, 1.0 );
689 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
691 sgSetVec3( axis, 0.0, 1.0, 0.0 );
692 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
696 sgCopyMat4( TRANSFORM, T1 );
697 sgPreMultMat4( TRANSFORM, LON );
698 sgPreMultMat4( TRANSFORM, LAT );
701 sgSetCoord( &layerpos, TRANSFORM );
703 layer_transform->setTransform( &layerpos );
705 // now calculate update texture coordinates
706 if ( last_lon < -900 ) {
711 double sp_dist = speed*dt;
713 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
714 Point3D start( last_lon, last_lat, 0.0 );
715 Point3D dest( lon, lat, 0.0 );
716 double course = 0.0, dist = 0.0;
718 calc_gc_course_dist( dest, start, &course, &dist );
719 // cout << "course = " << course << ", dist = " << dist << endl;
721 // if start and dest are too close together,
722 // calc_gc_course_dist() can return a course of "nan". If
723 // this happens, lets just use the last known good course.
724 // This is a hack, and it would probably be better to make
725 // calc_gc_course_dist() more robust.
726 if ( isnan(course) ) {
727 course = last_course;
729 last_course = course;
732 // calculate cloud movement due to external forces
733 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
736 ax = cos(course) * dist;
737 ay = sin(course) * dist;
741 bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
742 by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
746 double xoff = (ax + bx) / (2 * scale);
747 double yoff = (ay + by) / (2 * scale);
749 const float layer_scale = layer_span / scale;
751 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
754 if ( bump_mapping && enable_bump_mapping ) {
755 base = vertices[12].texCoord;
757 base = tl[0]->get( 0 );
761 // the while loops can lead to *long* pauses if base[0] comes
762 // with a bogus value.
763 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
764 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
765 if ( base[0] > -10.0 && base[0] < 10.0 ) {
766 base[0] -= (int)base[0];
768 SG_LOG(SG_ASTRO, SG_DEBUG,
769 "Error: base = " << base[0] << "," << base[1] <<
770 " course = " << course << " dist = " << dist );
775 // the while loops can lead to *long* pauses if base[0] comes
776 // with a bogus value.
777 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
778 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
779 if ( base[1] > -10.0 && base[1] < 10.0 ) {
780 base[1] -= (int)base[1];
782 SG_LOG(SG_ASTRO, SG_DEBUG,
783 "Error: base = " << base[0] << "," << base[1] <<
784 " course = " << course << " dist = " << dist );
788 if ( bump_mapping && enable_bump_mapping ) {
790 for ( int i = -2; i <= 2; i++ ) {
791 for ( int j = -2; j <= 2; j++ ) {
792 if ( i == 0 && j == 0 )
793 continue; // Already done on base
794 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
795 sgSetVec2( v1.texCoord,
796 base[0] + layer_scale * i * 0.25,
797 base[1] + layer_scale * j * 0.25 );
802 // cout << "base = " << base[0] << "," << base[1] << endl;
805 for (int i = 0; i < 4; i++) {
806 tc = tl[i]->get( 0 );
807 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
809 for (int j = 0; j < 4; j++)
811 tc = tl[i]->get( j*2+1 );
812 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
813 base[1] + layer_scale * j/4 );
815 tc = tl[i]->get( (j+1)*2 );
816 sgSetVec2( tc, base[0] + layer_scale * i/4,
817 base[1] + layer_scale * (j+1)/4 );
820 tc = tl[i]->get( 9 );
821 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
822 base[1] + layer_scale );
830 layer3D->reposition( p, up, lon, lat, alt, dt, direction, speed);
835 void SGCloudLayer::draw( bool top ) {
836 if ( layer_coverage != SG_CLOUD_CLEAR ) {
838 if ( SGCloudField::enable3D && layer3D->is3D())
841 if ( bump_mapping && enable_bump_mapping ) {
846 ssgGetModelviewMatrix( modelview );
847 layer_transform->getTransform( transform );
849 sgTransposeNegateMat4( tmp, transform );
851 sgPostMultMat4( transform, modelview );
852 ssgLoadModelviewMatrix( transform );
855 ssgGetLight( 0 )->getPosition( lightVec );
856 sgNegateVec3( lightVec );
857 sgXformVec3( lightVec, tmp );
859 for ( int i = 0; i < 25; i++ ) {
860 CloudVertex &v = vertices[ i ];
861 sgSetVec3( v.tangentSpLight,
862 sgScalarProductVec3( v.sTangent, lightVec ),
863 sgScalarProductVec3( v.tTangent, lightVec ),
864 sgScalarProductVec3( v.normal, lightVec ) );
867 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
868 if ( top && decal == 0 ) {
869 decal = color_map[ layer_coverage ][ 0 ];
871 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
872 if ( top && normal == 0 ) {
873 normal = normal_map[ layer_coverage ][ 0 ];
876 glDisable( GL_LIGHTING );
877 glDisable( GL_CULL_FACE );
878 // glDisable( GL_ALPHA_TEST );
879 if ( layer_coverage == SG_CLOUD_FEW ) {
880 glEnable( GL_ALPHA_TEST );
881 glAlphaFunc ( GL_GREATER, 0.01 );
883 glEnable( GL_BLEND );
884 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
886 glShadeModel( GL_SMOOTH );
887 glEnable( GL_COLOR_MATERIAL );
891 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
892 emis = ( color[0]+color[1]+color[2] ) / 3.0;
896 sgSetVec4( color, emis, emis, emis, 0.0 );
897 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
898 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
899 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
900 sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
901 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
902 sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
903 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
905 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
907 glActiveTexturePtr( GL_TEXTURE0_ARB );
908 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
909 glEnable( GL_TEXTURE_2D );
911 //Bind normalisation cube map to texture unit 1
912 glActiveTexturePtr( GL_TEXTURE1_ARB );
913 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
914 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
915 glActiveTexturePtr( GL_TEXTURE0_ARB );
917 //Set vertex arrays for cloud
918 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
919 glEnableClientState( GL_VERTEX_ARRAY );
921 if ( nb_texture_unit >= 3 ) {
922 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
923 glEnableClientState( GL_COLOR_ARRAY );
926 //Send texture coords for normal map to unit 0
927 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
928 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
930 //Send tangent space light vectors for normalisation to unit 1
931 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
932 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
933 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
935 //Set up texture environment to do (tex0 dot tex1)*color
936 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
937 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
938 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
939 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
940 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
942 glActiveTexturePtr( GL_TEXTURE1_ARB );
944 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
945 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
946 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
947 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
948 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
949 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
951 if ( nb_texture_unit >= 3 ) {
952 glActiveTexturePtr( GL_TEXTURE2_ARB );
953 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
955 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
956 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
957 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
959 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
960 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
961 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
962 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
964 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
965 glActiveTexturePtr( GL_TEXTURE0_ARB );
968 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
969 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
970 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
971 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
973 glDisable( GL_TEXTURE_2D );
974 glActiveTexturePtr( GL_TEXTURE1_ARB );
975 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
976 glActiveTexturePtr( GL_TEXTURE2_ARB );
977 glDisable( GL_TEXTURE_2D );
978 glActiveTexturePtr( GL_TEXTURE0_ARB );
980 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
981 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
982 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
983 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
984 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
985 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
987 glDisableClientState( GL_COLOR_ARRAY );
988 glEnable( GL_LIGHTING );
990 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
993 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
994 glActiveTexturePtr( GL_TEXTURE0_ARB );
997 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
998 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
999 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1000 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1003 glDisable( GL_TEXTURE_2D );
1005 glActiveTexturePtr( GL_TEXTURE1_ARB );
1006 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
1007 glActiveTexturePtr( GL_TEXTURE0_ARB );
1009 //disable vertex arrays
1010 glDisableClientState( GL_VERTEX_ARRAY );
1012 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1013 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
1014 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1015 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1017 //Return to standard modulate texenv
1018 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1020 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
1021 glDepthFunc(GL_LEQUAL);
1023 glEnable( GL_LIGHTING );
1025 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1026 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1027 average = 0.15 + average/10;
1028 sgVec4 averageColor;
1029 sgSetVec4( averageColor, average, average, average, 1.0f );
1030 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1032 glBlendColorPtr( average, average, average, 1.0f );
1033 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1035 //Perform a second pass to color the torus
1036 //Bind decal texture
1037 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1038 glEnable(GL_TEXTURE_2D);
1040 //Set vertex arrays for torus
1041 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1042 glEnableClientState( GL_VERTEX_ARRAY );
1044 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1045 //glEnableClientState( GL_COLOR_ARRAY );
1047 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1048 glEnableClientState( GL_NORMAL_ARRAY );
1050 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1051 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1054 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1055 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1056 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1057 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1059 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1061 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1065 glDisable( GL_TEXTURE_2D );
1067 glDisableClientState( GL_VERTEX_ARRAY );
1068 glDisableClientState( GL_NORMAL_ARRAY );
1070 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1071 glEnable( GL_CULL_FACE );
1072 glDepthFunc(GL_LESS);
1074 ssgLoadModelviewMatrix( modelview );
1077 state_sel->selectStep( top ? 1 : 0 );
1078 ssgCullAndDraw( layer_root );
1084 // make an ssgSimpleState for a cloud layer given the named texture
1085 ssgSimpleState *sgCloudMakeState( const string &path ) {
1086 ssgSimpleState *state = new ssgSimpleState();
1088 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1090 state->setTexture( (char *)path.c_str() );
1091 state->setShadeModel( GL_SMOOTH );
1092 state->disable( GL_LIGHTING );
1093 state->disable( GL_CULL_FACE );
1094 state->enable( GL_TEXTURE_2D );
1095 state->enable( GL_COLOR_MATERIAL );
1096 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1097 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1098 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1099 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1100 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1101 state->enable( GL_BLEND );
1102 state->enable( GL_ALPHA_TEST );
1103 state->setAlphaClamp( 0.01 );