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); }
59 #if defined (__CYGWIN__)
63 static ssgSharedPtr<ssgStateSelector> layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
64 static bool state_initialized = false;
65 static bool bump_mapping = false;
66 static GLint nb_texture_unit = 0;
67 static ssgSharedPtr<ssgTexture> normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
68 static ssgSharedPtr<ssgTexture> color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
69 static GLuint normalization_cube_map;
71 static glActiveTextureProc glActiveTexturePtr = 0;
72 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
73 static glBlendColorProc glBlendColorPtr = 0;
75 bool SGCloudLayer::enable_bump_mapping = false;
78 generateNormalizationCubeMap()
80 unsigned char data[ 32 * 32 * 3 ];
82 const float half_size = 16.0f,
85 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
88 unsigned char *ptr = data;
89 for ( j = 0; j < size; j++ ) {
90 for ( i = 0; i < size; i++ ) {
92 sgSetVec3( tmp, half_size,
93 -( j + offset - half_size ),
94 -( i + offset - half_size ) );
95 sgNormalizeVec3( tmp );
96 sgScaleVec3( tmp, 0.5f );
97 sgAddVec3( tmp, zero_normal );
99 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
100 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
101 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
104 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
105 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
108 for ( j = 0; j < size; j++ ) {
109 for ( i = 0; i < size; i++ ) {
111 sgSetVec3( tmp, -half_size,
112 -( j + offset - half_size ),
113 ( i + offset - half_size ) );
114 sgNormalizeVec3( tmp );
115 sgScaleVec3( tmp, 0.5f );
116 sgAddVec3( tmp, zero_normal );
118 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
119 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
120 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
123 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
124 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
127 for ( j = 0; j < size; j++ ) {
128 for ( i = 0; i < size; i++ ) {
130 sgSetVec3( tmp, ( i + offset - half_size ),
132 ( j + offset - half_size ) );
133 sgNormalizeVec3( tmp );
134 sgScaleVec3( tmp, 0.5f );
135 sgAddVec3( tmp, zero_normal );
137 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
138 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
139 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
142 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
143 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
146 for ( j = 0; j < size; j++ ) {
147 for ( i = 0; i < size; i++ ) {
149 sgSetVec3( tmp, ( i + offset - half_size ),
151 -( j + offset - half_size ) );
152 sgNormalizeVec3( tmp );
153 sgScaleVec3( tmp, 0.5f );
154 sgAddVec3( tmp, zero_normal );
156 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
157 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
158 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
161 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
162 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
165 for ( j = 0; j < size; j++ ) {
166 for ( i = 0; i < size; i++ ) {
168 sgSetVec3( tmp, ( i + offset - half_size ),
169 -( j + offset - half_size ),
171 sgNormalizeVec3( tmp );
172 sgScaleVec3( tmp, 0.5f );
173 sgAddVec3( tmp, zero_normal );
175 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
176 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
177 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
180 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
181 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
184 for ( j = 0; j < size; j++ ) {
185 for ( i = 0; i < size; i++ ) {
187 sgSetVec3( tmp, -( i + offset - half_size ),
188 -( j + offset - half_size ),
190 sgNormalizeVec3( tmp );
191 sgScaleVec3( tmp, 0.5f );
192 sgAddVec3( tmp, zero_normal );
194 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
195 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
196 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
199 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
200 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
205 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
208 layer_root(new ssgRoot),
209 layer_transform(new ssgTransform),
212 texture_path(tex_path),
215 layer_thickness(0.0),
216 layer_transition(0.0),
217 layer_coverage(SG_CLOUD_CLEAR),
224 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
225 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
226 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
227 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
229 layer_root->addKid(layer_transform);
230 layer3D = new SGCloudField;
235 SGCloudLayer::~SGCloudLayer()
240 delete layer_root; // deletes layer_transform and layer as well
244 SGCloudLayer::getSpan_m () const
250 SGCloudLayer::setSpan_m (float span_m)
252 if (span_m != layer_span) {
259 SGCloudLayer::getElevation_m () const
265 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
267 layer_asl = elevation_m;
270 if (elevation_m > 4000)
271 setSpan_m( elevation_m * 10 );
278 SGCloudLayer::getThickness_m () const
280 return layer_thickness;
284 SGCloudLayer::setThickness_m (float thickness_m)
286 layer_thickness = thickness_m;
290 SGCloudLayer::getTransition_m () const
292 return layer_transition;
296 SGCloudLayer::setTransition_m (float transition_m)
298 layer_transition = transition_m;
301 SGCloudLayer::Coverage
302 SGCloudLayer::getCoverage () const
304 return layer_coverage;
308 SGCloudLayer::setCoverage (Coverage coverage)
310 if (coverage != layer_coverage) {
311 layer_coverage = coverage;
317 // build the cloud object
319 SGCloudLayer::rebuild()
321 // Initialize states and sizes if necessary.
322 if ( !state_initialized ) {
323 state_initialized = true;
325 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
327 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
328 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
329 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
330 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
331 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
333 if ( bump_mapping ) {
334 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
335 if ( nb_texture_unit < 2 ) {
336 bump_mapping = false;
338 //nb_texture_unit = 2; // Force the number of units for now
341 if ( bump_mapping ) {
343 // This bump mapping code was inspired by the tutorial available at
344 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
345 // and a NVidia white paper
346 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
347 // The normal map textures were generated by the normal map Gimp plugin :
348 // http://nifelheim.dyndns.org/~cocidius/normalmap/
352 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
353 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
354 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
356 cloud_path.set(texture_path.str());
357 cloud_path.append("overcast.rgb");
358 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
359 cloud_path.set(texture_path.str());
360 cloud_path.append("overcast_n.rgb");
361 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
363 cloud_path.set(texture_path.str());
364 cloud_path.append("overcast_top.rgb");
365 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
366 cloud_path.set(texture_path.str());
367 cloud_path.append("overcast_top_n.rgb");
368 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
370 cloud_path.set(texture_path.str());
371 cloud_path.append("broken.rgba");
372 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
373 cloud_path.set(texture_path.str());
374 cloud_path.append("broken_n.rgb");
375 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
377 cloud_path.set(texture_path.str());
378 cloud_path.append("scattered.rgba");
379 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
380 cloud_path.set(texture_path.str());
381 cloud_path.append("scattered_n.rgb");
382 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
384 cloud_path.set(texture_path.str());
385 cloud_path.append("few.rgba");
386 color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
387 cloud_path.set(texture_path.str());
388 cloud_path.append("few_n.rgb");
389 normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
391 cloud_path.set(texture_path.str());
392 cloud_path.append("cirrus.rgba");
393 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
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() );
398 glGenTextures( 1, &normalization_cube_map );
399 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
400 generateNormalizationCubeMap();
401 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
402 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
403 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
404 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
405 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
408 ssgStateSelector *state_sel;
409 ssgSimpleState *state;
411 state_sel = new ssgStateSelector( 2 );
412 cloud_path.set(texture_path.str());
413 cloud_path.append("overcast.rgb");
414 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
415 cloud_path.set(texture_path.str());
416 cloud_path.append("overcast_top.rgb");
417 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
418 layer_states[SG_CLOUD_OVERCAST] = state_sel;
420 state_sel = new ssgStateSelector( 2 );
421 cloud_path.set(texture_path.str());
422 cloud_path.append("broken.rgba");
423 state = sgCloudMakeState(cloud_path.str());
424 state_sel->setStep( 0, state );
425 state_sel->setStep( 1, state );
426 layer_states[SG_CLOUD_BROKEN] = state_sel;
428 state_sel = new ssgStateSelector( 2 );
429 cloud_path.set(texture_path.str());
430 cloud_path.append("scattered.rgba");
431 state = sgCloudMakeState(cloud_path.str());
432 state_sel->setStep( 0, state );
433 state_sel->setStep( 1, state );
434 layer_states[SG_CLOUD_SCATTERED] = state_sel;
436 state_sel = new ssgStateSelector( 2 );
437 cloud_path.set(texture_path.str());
438 cloud_path.append("few.rgba");
439 state = sgCloudMakeState(cloud_path.str());
440 state_sel->setStep( 0, state );
441 state_sel->setStep( 1, state );
442 layer_states[SG_CLOUD_FEW] = state_sel;
444 state_sel = new ssgStateSelector( 2 );
445 cloud_path.set(texture_path.str());
446 cloud_path.append("cirrus.rgba");
447 state = sgCloudMakeState(cloud_path.str());
448 state_sel->setStep( 0, state );
449 state_sel->setStep( 1, state );
450 layer_states[SG_CLOUD_CIRRUS] = state_sel;
452 layer_states[SG_CLOUD_CLEAR] = 0;
454 SGNewCloud::loadTextures(texture_path.str());
455 layer3D->buildTestLayer();
458 if ( bump_mapping ) {
461 vertices = new CloudVertex[ 25 ];
462 indices = new unsigned int[ 40 ];
466 sgSetVec2( base, sg_random(), sg_random() );
468 const float layer_scale = layer_span / scale;
469 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
470 const float half_angle = 0.5 * layer_span / layer_to_core;
473 for ( i = -2; i <= 2; i++ ) {
474 for ( int j = -2; j <= 2; j++ ) {
475 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
476 sgSetVec3( v1.position,
477 0.5 * i * layer_span,
478 0.5 * j * layer_span,
479 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
480 sgSetVec2( v1.texCoord,
481 base[0] + layer_scale * i * 0.25,
482 base[1] + layer_scale * j * 0.25 );
483 sgSetVec3( v1.sTangent,
484 cos( i * half_angle ),
486 -sin( i * half_angle ) );
487 sgSetVec3( v1.tTangent,
489 cos( j * half_angle ),
490 -sin( j * half_angle ) );
491 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
492 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : cloud_alpha * 0.15f );
496 * 0 1 5 6 10 11 15 16 20 21
497 * 1 2 6 7 11 12 16 17 21 22
498 * 2 3 7 8 12 13 17 18 22 23
499 * 3 4 8 9 13 14 18 19 23 24
501 for ( i = 0; i < 4; i++ ) {
502 for ( int j = 0; j < 5; j++ ) {
503 indices[ i*10 + (j*2) ] = i + 5 * j;
504 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
511 last_lon = last_lat = -999.0f;
514 sgSetVec2( base, sg_random(), sg_random() );
516 // build the cloud layer
521 const float layer_scale = layer_span / scale;
522 const float mpi = SG_PI/4;
524 // caclculate the difference between a flat-earth model and
525 // a round earth model given the span and altutude ASL of
526 // the cloud layer. This is the difference in altitude between
527 // the top of the inverted bowl and the edge of the bowl.
528 // const float alt_diff = layer_asl * 0.8;
529 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
530 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
531 const float border_to_core = layer_to_core * cos(layer_angle);
532 const float alt_diff = layer_to_core - border_to_core;
534 for (int i = 0; i < 4; i++)
536 if ( layer[i] != NULL ) {
537 layer_transform->removeKid(layer[i]); // automatic delete
540 vl[i] = new ssgVertexArray( 10 );
541 cl[i] = new ssgColourArray( 10 );
542 tl[i] = new ssgTexCoordArray( 10 );
545 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
546 alt_diff * (sin(i*mpi) - 2) );
548 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
550 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
553 vl[i]->add( vertex );
556 for (int j = 0; j < 4; j++)
558 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
559 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
561 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
562 base[1] + layer_scale * j/4 );
564 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
565 ( (j == 0) || (i == 3)) ?
566 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
569 vl[i]->add( vertex );
573 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
574 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
576 sgSetVec2( tc, base[0] + layer_scale * i/4,
577 base[1] + layer_scale * (j+1)/4 );
579 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
580 ((j == 3) || (i == 0)) ?
581 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
583 vl[i]->add( vertex );
587 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
588 alt_diff * (sin((i+1)*mpi) - 2) );
590 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
591 base[1] + layer_scale );
593 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
596 vl[i]->add( vertex );
599 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
600 layer_transform->addKid( layer[i] );
602 if ( layer_states[layer_coverage] != NULL ) {
603 layer[i]->setState( layer_states[layer_coverage] );
605 state_sel = layer_states[layer_coverage];
608 // force a repaint of the sky colors with arbitrary defaults
614 // repaint the cloud layer colors
615 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
617 if ( bump_mapping && enable_bump_mapping ) {
619 for ( int i = 0; i < 25; i++ ) {
620 sgCopyVec3( vertices[ i ].color, fog_color );
626 for ( int i = 0; i < 4; i++ ) {
627 color = cl[i]->get( 0 );
628 sgCopyVec3( color, fog_color );
629 color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
631 for ( int j = 0; j < 4; ++j ) {
632 color = cl[i]->get( (2*j) + 1 );
633 sgCopyVec3( color, fog_color );
635 ((j == 0) || (i == 3)) ?
636 ((j == 0) && (i == 3)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
638 color = cl[i]->get( (2*j) + 2 );
639 sgCopyVec3( color, fog_color );
641 ((j == 3) || (i == 0)) ?
642 ((j == 3) && (i == 0)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
645 color = cl[i]->get( 9 );
646 sgCopyVec3( color, fog_color );
647 color[3] = (i == 3) ? 0.0f : cloud_alpha * 0.15f;
654 // reposition the cloud layer at the specified origin and orientation
655 // lon specifies a rotation about the Z axis
656 // lat specifies a rotation about the new Y axis
657 // spin specifies a rotation about the new Z axis (and orients the
658 // sunrise/set effects
659 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
660 double alt, double dt )
665 // combine p and asl (meters) to get translation offset
667 sgCopyVec3( asl_offset, up );
668 sgNormalizeVec3( asl_offset );
669 if ( alt <= layer_asl ) {
670 sgScaleVec3( asl_offset, layer_asl );
672 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
674 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
675 // << "," << asl_offset[2] << endl;
676 sgAddVec3( asl_offset, p );
677 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
678 // << "," << asl_offset[2] << endl;
680 // Translate to zero elevation
681 // Point3D zero_elev = current_view.get_cur_zero_elev();
682 sgMakeTransMat4( T1, asl_offset );
684 // printf(" Translated to %.2f %.2f %.2f\n",
685 // zero_elev.x, zero_elev.y, zero_elev.z );
687 // Rotate to proper orientation
688 // printf(" lon = %.2f lat = %.2f\n",
689 // lon * SGD_RADIANS_TO_DEGREES,
690 // lat * SGD_RADIANS_TO_DEGREES);
691 sgSetVec3( axis, 0.0, 0.0, 1.0 );
692 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
694 sgSetVec3( axis, 0.0, 1.0, 0.0 );
695 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
699 sgCopyMat4( TRANSFORM, T1 );
700 sgPreMultMat4( TRANSFORM, LON );
701 sgPreMultMat4( TRANSFORM, LAT );
704 sgSetCoord( &layerpos, TRANSFORM );
706 layer_transform->setTransform( &layerpos );
708 // now calculate update texture coordinates
709 if ( last_lon < -900 ) {
714 double sp_dist = speed*dt;
716 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
717 Point3D start( last_lon, last_lat, 0.0 );
718 Point3D dest( lon, lat, 0.0 );
719 double course = 0.0, dist = 0.0;
721 calc_gc_course_dist( dest, start, &course, &dist );
722 // cout << "course = " << course << ", dist = " << dist << endl;
724 // if start and dest are too close together,
725 // calc_gc_course_dist() can return a course of "nan". If
726 // this happens, lets just use the last known good course.
727 // This is a hack, and it would probably be better to make
728 // calc_gc_course_dist() more robust.
729 if ( isnan(course) ) {
730 course = last_course;
732 last_course = course;
735 // calculate cloud movement due to external forces
736 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
739 ax = cos(course) * dist;
740 ay = sin(course) * dist;
744 bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
745 by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
749 double xoff = (ax + bx) / (2 * scale);
750 double yoff = (ay + by) / (2 * scale);
752 const float layer_scale = layer_span / scale;
754 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
757 if ( bump_mapping && enable_bump_mapping ) {
758 base = vertices[12].texCoord;
760 base = tl[0]->get( 0 );
764 // the while loops can lead to *long* pauses if base[0] comes
765 // with a bogus value.
766 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
767 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
768 if ( base[0] > -10.0 && base[0] < 10.0 ) {
769 base[0] -= (int)base[0];
771 SG_LOG(SG_ASTRO, SG_DEBUG,
772 "Error: base = " << base[0] << "," << base[1] <<
773 " course = " << course << " dist = " << dist );
778 // the while loops can lead to *long* pauses if base[0] comes
779 // with a bogus value.
780 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
781 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
782 if ( base[1] > -10.0 && base[1] < 10.0 ) {
783 base[1] -= (int)base[1];
785 SG_LOG(SG_ASTRO, SG_DEBUG,
786 "Error: base = " << base[0] << "," << base[1] <<
787 " course = " << course << " dist = " << dist );
791 if ( bump_mapping && enable_bump_mapping ) {
793 for ( int i = -2; i <= 2; i++ ) {
794 for ( int j = -2; j <= 2; j++ ) {
795 if ( i == 0 && j == 0 )
796 continue; // Already done on base
797 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
798 sgSetVec2( v1.texCoord,
799 base[0] + layer_scale * i * 0.25,
800 base[1] + layer_scale * j * 0.25 );
805 // cout << "base = " << base[0] << "," << base[1] << endl;
808 for (int i = 0; i < 4; i++) {
809 tc = tl[i]->get( 0 );
810 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
812 for (int j = 0; j < 4; j++)
814 tc = tl[i]->get( j*2+1 );
815 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
816 base[1] + layer_scale * j/4 );
818 tc = tl[i]->get( (j+1)*2 );
819 sgSetVec2( tc, base[0] + layer_scale * i/4,
820 base[1] + layer_scale * (j+1)/4 );
823 tc = tl[i]->get( 9 );
824 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
825 base[1] + layer_scale );
833 layer3D->reposition( p, up, lon, lat, alt, dt, direction, speed);
838 void SGCloudLayer::draw( bool top ) {
839 if ( layer_coverage != SG_CLOUD_CLEAR ) {
841 if ( SGCloudField::enable3D && layer3D->is3D())
844 if ( bump_mapping && enable_bump_mapping ) {
849 ssgGetModelviewMatrix( modelview );
850 layer_transform->getTransform( transform );
852 sgTransposeNegateMat4( tmp, transform );
854 sgPostMultMat4( transform, modelview );
855 ssgLoadModelviewMatrix( transform );
858 ssgGetLight( 0 )->getPosition( lightVec );
859 sgNegateVec3( lightVec );
860 sgXformVec3( lightVec, tmp );
862 for ( int i = 0; i < 25; i++ ) {
863 CloudVertex &v = vertices[ i ];
864 sgSetVec3( v.tangentSpLight,
865 sgScalarProductVec3( v.sTangent, lightVec ),
866 sgScalarProductVec3( v.tTangent, lightVec ),
867 sgScalarProductVec3( v.normal, lightVec ) );
870 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
871 if ( top && decal == 0 ) {
872 decal = color_map[ layer_coverage ][ 0 ];
874 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
875 if ( top && normal == 0 ) {
876 normal = normal_map[ layer_coverage ][ 0 ];
879 glDisable( GL_LIGHTING );
880 glDisable( GL_CULL_FACE );
881 // glDisable( GL_ALPHA_TEST );
882 if ( layer_coverage == SG_CLOUD_FEW ) {
883 glEnable( GL_ALPHA_TEST );
884 glAlphaFunc ( GL_GREATER, 0.01 );
886 glEnable( GL_BLEND );
887 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
889 glShadeModel( GL_SMOOTH );
890 glEnable( GL_COLOR_MATERIAL );
894 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
895 emis = ( color[0]+color[1]+color[2] ) / 3.0;
899 sgSetVec4( color, emis, emis, emis, 0.0 );
900 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
901 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
902 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
903 sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
904 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
905 sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
906 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
908 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
910 glActiveTexturePtr( GL_TEXTURE0_ARB );
911 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
912 glEnable( GL_TEXTURE_2D );
914 //Bind normalisation cube map to texture unit 1
915 glActiveTexturePtr( GL_TEXTURE1_ARB );
916 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
917 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
918 glActiveTexturePtr( GL_TEXTURE0_ARB );
920 //Set vertex arrays for cloud
921 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
922 glEnableClientState( GL_VERTEX_ARRAY );
924 if ( nb_texture_unit >= 3 ) {
925 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
926 glEnableClientState( GL_COLOR_ARRAY );
929 //Send texture coords for normal map to unit 0
930 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
931 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
933 //Send tangent space light vectors for normalisation to unit 1
934 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
935 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
936 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
938 //Set up texture environment to do (tex0 dot tex1)*color
939 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
940 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
941 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
942 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
943 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
945 glActiveTexturePtr( GL_TEXTURE1_ARB );
947 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
948 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
949 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
950 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
951 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
952 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
954 if ( nb_texture_unit >= 3 ) {
955 glActiveTexturePtr( GL_TEXTURE2_ARB );
956 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
958 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
959 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
960 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
962 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
963 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
964 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
965 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
967 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
968 glActiveTexturePtr( GL_TEXTURE0_ARB );
971 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
972 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
973 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
974 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
976 glDisable( GL_TEXTURE_2D );
977 glActiveTexturePtr( GL_TEXTURE1_ARB );
978 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
979 glActiveTexturePtr( GL_TEXTURE2_ARB );
980 glDisable( GL_TEXTURE_2D );
981 glActiveTexturePtr( GL_TEXTURE0_ARB );
983 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
984 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
985 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
986 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
987 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
988 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
990 glDisableClientState( GL_COLOR_ARRAY );
991 glEnable( GL_LIGHTING );
993 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
996 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
997 glActiveTexturePtr( GL_TEXTURE0_ARB );
1000 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1001 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1002 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1003 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1006 glDisable( GL_TEXTURE_2D );
1008 glActiveTexturePtr( GL_TEXTURE1_ARB );
1009 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
1010 glActiveTexturePtr( GL_TEXTURE0_ARB );
1012 //disable vertex arrays
1013 glDisableClientState( GL_VERTEX_ARRAY );
1015 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1016 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
1017 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1018 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1020 //Return to standard modulate texenv
1021 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1023 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
1024 glDepthFunc(GL_LEQUAL);
1026 glEnable( GL_LIGHTING );
1028 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1029 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1030 average = 0.15 + average/10;
1031 sgVec4 averageColor;
1032 sgSetVec4( averageColor, average, average, average, 1.0f );
1033 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1035 glBlendColorPtr( average, average, average, 1.0f );
1036 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1038 //Perform a second pass to color the torus
1039 //Bind decal texture
1040 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1041 glEnable(GL_TEXTURE_2D);
1043 //Set vertex arrays for torus
1044 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1045 glEnableClientState( GL_VERTEX_ARRAY );
1047 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1048 //glEnableClientState( GL_COLOR_ARRAY );
1050 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1051 glEnableClientState( GL_NORMAL_ARRAY );
1053 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1054 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1057 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1058 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1059 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1060 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1062 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1064 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1068 glDisable( GL_TEXTURE_2D );
1070 glDisableClientState( GL_VERTEX_ARRAY );
1071 glDisableClientState( GL_NORMAL_ARRAY );
1073 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1074 glEnable( GL_CULL_FACE );
1075 glDepthFunc(GL_LESS);
1077 ssgLoadModelviewMatrix( modelview );
1080 state_sel->selectStep( top ? 1 : 0 );
1081 ssgCullAndDraw( layer_root );
1087 // make an ssgSimpleState for a cloud layer given the named texture
1088 ssgSimpleState *sgCloudMakeState( const string &path ) {
1089 ssgSimpleState *state = new ssgSimpleState();
1091 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1093 state->setTexture( (char *)path.c_str() );
1094 state->setShadeModel( GL_SMOOTH );
1095 state->disable( GL_LIGHTING );
1096 state->disable( GL_CULL_FACE );
1097 state->enable( GL_TEXTURE_2D );
1098 state->enable( GL_COLOR_MATERIAL );
1099 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1100 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1101 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1102 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1103 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1104 state->enable( GL_BLEND );
1105 state->enable( GL_ALPHA_TEST );
1106 state->setAlphaClamp( 0.01 );