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/misc/sg_path.hxx>
40 #include <simgear/screen/extensions.hxx>
41 #include <simgear/screen/texture.hxx>
42 #include <simgear/structure/ssgSharedPtr.hxx>
44 #include "newcloud.hxx"
45 #include "cloudfield.hxx"
48 #if defined(__MINGW32__)
49 #define isnan(x) _isnan(x)
52 #if defined (__FreeBSD__)
53 # if __FreeBSD_version < 500000
55 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
61 static ssgSharedPtr<ssgStateSelector> layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
62 static bool state_initialized = false;
63 static bool bump_mapping = false;
64 static GLint nb_texture_unit = 0;
65 static ssgSharedPtr<ssgTexture> normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
66 static ssgSharedPtr<ssgTexture> color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
67 static GLuint normalization_cube_map;
69 static glActiveTextureProc glActiveTexturePtr = 0;
70 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
71 static glBlendColorProc glBlendColorPtr = 0;
73 bool SGCloudLayer::enable_bump_mapping = false;
76 generateNormalizationCubeMap()
78 unsigned char data[ 32 * 32 * 3 ];
80 const float half_size = 16.0f,
83 sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
86 unsigned char *ptr = data;
87 for ( j = 0; j < size; j++ ) {
88 for ( i = 0; i < size; i++ ) {
90 sgSetVec3( tmp, half_size,
91 -( j + offset - half_size ),
92 -( i + offset - half_size ) );
93 sgNormalizeVec3( tmp );
94 sgScaleVec3( tmp, 0.5f );
95 sgAddVec3( tmp, zero_normal );
97 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
98 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
99 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
102 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
103 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
106 for ( j = 0; j < size; j++ ) {
107 for ( i = 0; i < size; i++ ) {
109 sgSetVec3( tmp, -half_size,
110 -( j + offset - half_size ),
111 ( i + offset - half_size ) );
112 sgNormalizeVec3( tmp );
113 sgScaleVec3( tmp, 0.5f );
114 sgAddVec3( tmp, zero_normal );
116 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
117 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
118 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
121 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
122 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
125 for ( j = 0; j < size; j++ ) {
126 for ( i = 0; i < size; i++ ) {
128 sgSetVec3( tmp, ( i + offset - half_size ),
130 ( j + offset - half_size ) );
131 sgNormalizeVec3( tmp );
132 sgScaleVec3( tmp, 0.5f );
133 sgAddVec3( tmp, zero_normal );
135 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
136 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
137 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
140 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
141 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
144 for ( j = 0; j < size; j++ ) {
145 for ( i = 0; i < size; i++ ) {
147 sgSetVec3( tmp, ( i + offset - half_size ),
149 -( j + offset - half_size ) );
150 sgNormalizeVec3( tmp );
151 sgScaleVec3( tmp, 0.5f );
152 sgAddVec3( tmp, zero_normal );
154 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
155 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
156 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
159 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
160 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
163 for ( j = 0; j < size; j++ ) {
164 for ( i = 0; i < size; i++ ) {
166 sgSetVec3( tmp, ( i + offset - half_size ),
167 -( j + offset - half_size ),
169 sgNormalizeVec3( tmp );
170 sgScaleVec3( tmp, 0.5f );
171 sgAddVec3( tmp, zero_normal );
173 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
174 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
175 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
178 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
179 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
182 for ( j = 0; j < size; j++ ) {
183 for ( i = 0; i < size; i++ ) {
185 sgSetVec3( tmp, -( i + offset - half_size ),
186 -( j + offset - half_size ),
188 sgNormalizeVec3( tmp );
189 sgScaleVec3( tmp, 0.5f );
190 sgAddVec3( tmp, zero_normal );
192 *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
193 *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
194 *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
197 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
198 0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
203 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
206 layer_root(new ssgRoot),
207 layer_transform(new ssgTransform),
210 texture_path(tex_path),
213 layer_thickness(0.0),
214 layer_transition(0.0),
215 layer_coverage(SG_CLOUD_CLEAR),
222 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
223 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
224 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
225 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
227 layer_root->addKid(layer_transform);
228 layer3D = new SGCloudField;
233 SGCloudLayer::~SGCloudLayer()
238 delete layer_root; // deletes layer_transform and layer as well
242 SGCloudLayer::getSpan_m () const
248 SGCloudLayer::setSpan_m (float span_m)
250 if (span_m != layer_span) {
257 SGCloudLayer::getElevation_m () const
263 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
265 layer_asl = elevation_m;
268 if (elevation_m > 4000)
269 setSpan_m( elevation_m * 10 );
276 SGCloudLayer::getThickness_m () const
278 return layer_thickness;
282 SGCloudLayer::setThickness_m (float thickness_m)
284 layer_thickness = thickness_m;
288 SGCloudLayer::getTransition_m () const
290 return layer_transition;
294 SGCloudLayer::setTransition_m (float transition_m)
296 layer_transition = transition_m;
299 SGCloudLayer::Coverage
300 SGCloudLayer::getCoverage () const
302 return layer_coverage;
306 SGCloudLayer::setCoverage (Coverage coverage)
308 if (coverage != layer_coverage) {
309 layer_coverage = coverage;
315 // build the cloud object
317 SGCloudLayer::rebuild()
319 // Initialize states and sizes if necessary.
320 if ( !state_initialized ) {
321 state_initialized = true;
323 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
325 bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
326 SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
327 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
328 SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") &&
329 SGIsOpenGLExtensionSupported("GL_ARB_imaging");
331 if ( bump_mapping ) {
332 glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
333 if ( nb_texture_unit < 2 ) {
334 bump_mapping = false;
336 //nb_texture_unit = 2; // Force the number of units for now
339 if ( bump_mapping ) {
341 // This bump mapping code was inspired by the tutorial available at
342 // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
343 // and a NVidia white paper
344 // http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
345 // The normal map textures were generated by the normal map Gimp plugin :
346 // http://nifelheim.dyndns.org/~cocidius/normalmap/
350 glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
351 glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
352 glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
354 cloud_path.set(texture_path.str());
355 cloud_path.append("overcast.rgb");
356 color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
357 cloud_path.set(texture_path.str());
358 cloud_path.append("overcast_n.rgb");
359 normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
361 cloud_path.set(texture_path.str());
362 cloud_path.append("overcast_top.rgb");
363 color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
364 cloud_path.set(texture_path.str());
365 cloud_path.append("overcast_top_n.rgb");
366 normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
368 cloud_path.set(texture_path.str());
369 cloud_path.append("broken.rgba");
370 color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
371 cloud_path.set(texture_path.str());
372 cloud_path.append("broken_n.rgb");
373 normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
375 cloud_path.set(texture_path.str());
376 cloud_path.append("scattered.rgba");
377 color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
378 cloud_path.set(texture_path.str());
379 cloud_path.append("scattered_n.rgb");
380 normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
382 cloud_path.set(texture_path.str());
383 cloud_path.append("few.rgba");
384 color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
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() );
389 cloud_path.set(texture_path.str());
390 cloud_path.append("cirrus.rgba");
391 color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
392 cloud_path.set(texture_path.str());
393 cloud_path.append("cirrus_n.rgb");
394 normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
396 glGenTextures( 1, &normalization_cube_map );
397 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
398 generateNormalizationCubeMap();
399 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
400 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
401 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
402 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
403 glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
406 ssgStateSelector *state_sel;
407 ssgSimpleState *state;
409 state_sel = new ssgStateSelector( 2 );
410 cloud_path.set(texture_path.str());
411 cloud_path.append("overcast.rgb");
412 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
413 cloud_path.set(texture_path.str());
414 cloud_path.append("overcast_top.rgb");
415 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
416 layer_states[SG_CLOUD_OVERCAST] = state_sel;
418 state_sel = new ssgStateSelector( 2 );
419 cloud_path.set(texture_path.str());
420 cloud_path.append("broken.rgba");
421 state = sgCloudMakeState(cloud_path.str());
422 state_sel->setStep( 0, state );
423 state_sel->setStep( 1, state );
424 layer_states[SG_CLOUD_BROKEN] = state_sel;
426 state_sel = new ssgStateSelector( 2 );
427 cloud_path.set(texture_path.str());
428 cloud_path.append("scattered.rgba");
429 state = sgCloudMakeState(cloud_path.str());
430 state_sel->setStep( 0, state );
431 state_sel->setStep( 1, state );
432 layer_states[SG_CLOUD_SCATTERED] = state_sel;
434 state_sel = new ssgStateSelector( 2 );
435 cloud_path.set(texture_path.str());
436 cloud_path.append("few.rgba");
437 state = sgCloudMakeState(cloud_path.str());
438 state_sel->setStep( 0, state );
439 state_sel->setStep( 1, state );
440 layer_states[SG_CLOUD_FEW] = state_sel;
442 state_sel = new ssgStateSelector( 2 );
443 cloud_path.set(texture_path.str());
444 cloud_path.append("cirrus.rgba");
445 state = sgCloudMakeState(cloud_path.str());
446 state_sel->setStep( 0, state );
447 state_sel->setStep( 1, state );
448 layer_states[SG_CLOUD_CIRRUS] = state_sel;
450 layer_states[SG_CLOUD_CLEAR] = 0;
452 SGNewCloud::loadTextures(texture_path.str());
453 layer3D->buildTestLayer();
456 if ( bump_mapping ) {
459 vertices = new CloudVertex[ 25 ];
460 indices = new unsigned int[ 40 ];
464 sgSetVec2( base, sg_random(), sg_random() );
466 const float layer_scale = layer_span / scale;
467 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
468 const float half_angle = 0.5 * layer_span / layer_to_core;
471 for ( i = -2; i <= 2; i++ ) {
472 for ( int j = -2; j <= 2; j++ ) {
473 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
474 sgSetVec3( v1.position,
475 0.5 * i * layer_span,
476 0.5 * j * layer_span,
477 -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
478 sgSetVec2( v1.texCoord,
479 base[0] + layer_scale * i * 0.25,
480 base[1] + layer_scale * j * 0.25 );
481 sgSetVec3( v1.sTangent,
482 cos( i * half_angle ),
484 -sin( i * half_angle ) );
485 sgSetVec3( v1.tTangent,
487 cos( j * half_angle ),
488 -sin( j * half_angle ) );
489 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
490 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : cloud_alpha * 0.15f );
494 * 0 1 5 6 10 11 15 16 20 21
495 * 1 2 6 7 11 12 16 17 21 22
496 * 2 3 7 8 12 13 17 18 22 23
497 * 3 4 8 9 13 14 18 19 23 24
499 for ( i = 0; i < 4; i++ ) {
500 for ( int j = 0; j < 5; j++ ) {
501 indices[ i*10 + (j*2) ] = i + 5 * j;
502 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
509 last_lon = last_lat = -999.0f;
512 sgSetVec2( base, sg_random(), sg_random() );
514 // build the cloud layer
519 const float layer_scale = layer_span / scale;
520 const float mpi = SG_PI/4;
522 // caclculate the difference between a flat-earth model and
523 // a round earth model given the span and altutude ASL of
524 // the cloud layer. This is the difference in altitude between
525 // the top of the inverted bowl and the edge of the bowl.
526 // const float alt_diff = layer_asl * 0.8;
527 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
528 const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
529 const float border_to_core = layer_to_core * cos(layer_angle);
530 const float alt_diff = layer_to_core - border_to_core;
532 for (int i = 0; i < 4; i++)
534 if ( layer[i] != NULL ) {
535 layer_transform->removeKid(layer[i]); // automatic delete
538 vl[i] = new ssgVertexArray( 10 );
539 cl[i] = new ssgColourArray( 10 );
540 tl[i] = new ssgTexCoordArray( 10 );
543 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
544 alt_diff * (sin(i*mpi) - 2) );
546 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
548 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
551 vl[i]->add( vertex );
554 for (int j = 0; j < 4; j++)
556 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
557 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
559 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
560 base[1] + layer_scale * j/4 );
562 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
563 ( (j == 0) || (i == 3)) ?
564 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
567 vl[i]->add( vertex );
571 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
572 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
574 sgSetVec2( tc, base[0] + layer_scale * i/4,
575 base[1] + layer_scale * (j+1)/4 );
577 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
578 ((j == 3) || (i == 0)) ?
579 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
581 vl[i]->add( vertex );
585 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
586 alt_diff * (sin((i+1)*mpi) - 2) );
588 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
589 base[1] + layer_scale );
591 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
594 vl[i]->add( vertex );
597 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
598 layer_transform->addKid( layer[i] );
600 if ( layer_states[layer_coverage] != NULL ) {
601 layer[i]->setState( layer_states[layer_coverage] );
603 state_sel = layer_states[layer_coverage];
606 // force a repaint of the sky colors with arbitrary defaults
612 // repaint the cloud layer colors
613 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
615 if ( bump_mapping && enable_bump_mapping ) {
617 for ( int i = 0; i < 25; i++ ) {
618 sgCopyVec3( vertices[ i ].color, fog_color );
624 for ( int i = 0; i < 4; i++ ) {
625 color = cl[i]->get( 0 );
626 sgCopyVec3( color, fog_color );
627 color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
629 for ( int j = 0; j < 4; ++j ) {
630 color = cl[i]->get( (2*j) + 1 );
631 sgCopyVec3( color, fog_color );
633 ((j == 0) || (i == 3)) ?
634 ((j == 0) && (i == 3)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
636 color = cl[i]->get( (2*j) + 2 );
637 sgCopyVec3( color, fog_color );
639 ((j == 3) || (i == 0)) ?
640 ((j == 3) && (i == 0)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
643 color = cl[i]->get( 9 );
644 sgCopyVec3( color, fog_color );
645 color[3] = (i == 3) ? 0.0f : cloud_alpha * 0.15f;
652 // reposition the cloud layer at the specified origin and orientation
653 // lon specifies a rotation about the Z axis
654 // lat specifies a rotation about the new Y axis
655 // spin specifies a rotation about the new Z axis (and orients the
656 // sunrise/set effects
657 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
658 double alt, double dt )
663 // combine p and asl (meters) to get translation offset
665 sgCopyVec3( asl_offset, up );
666 sgNormalizeVec3( asl_offset );
667 if ( alt <= layer_asl ) {
668 sgScaleVec3( asl_offset, layer_asl );
670 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
672 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
673 // << "," << asl_offset[2] << endl;
674 sgAddVec3( asl_offset, p );
675 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
676 // << "," << asl_offset[2] << endl;
678 // Translate to zero elevation
679 // Point3D zero_elev = current_view.get_cur_zero_elev();
680 sgMakeTransMat4( T1, asl_offset );
682 // printf(" Translated to %.2f %.2f %.2f\n",
683 // zero_elev.x, zero_elev.y, zero_elev.z );
685 // Rotate to proper orientation
686 // printf(" lon = %.2f lat = %.2f\n",
687 // lon * SGD_RADIANS_TO_DEGREES,
688 // lat * SGD_RADIANS_TO_DEGREES);
689 sgSetVec3( axis, 0.0, 0.0, 1.0 );
690 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
692 sgSetVec3( axis, 0.0, 1.0, 0.0 );
693 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
697 sgCopyMat4( TRANSFORM, T1 );
698 sgPreMultMat4( TRANSFORM, LON );
699 sgPreMultMat4( TRANSFORM, LAT );
702 sgSetCoord( &layerpos, TRANSFORM );
704 layer_transform->setTransform( &layerpos );
706 // now calculate update texture coordinates
707 if ( last_lon < -900 ) {
712 double sp_dist = speed*dt;
714 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
715 Point3D start( last_lon, last_lat, 0.0 );
716 Point3D dest( lon, lat, 0.0 );
717 double course = 0.0, dist = 0.0;
719 calc_gc_course_dist( dest, start, &course, &dist );
720 // cout << "course = " << course << ", dist = " << dist << endl;
722 // if start and dest are too close together,
723 // calc_gc_course_dist() can return a course of "nan". If
724 // this happens, lets just use the last known good course.
725 // This is a hack, and it would probably be better to make
726 // calc_gc_course_dist() more robust.
727 if ( isnan(course) ) {
728 course = last_course;
730 last_course = course;
733 // calculate cloud movement due to external forces
734 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
737 ax = cos(course) * dist;
738 ay = sin(course) * dist;
742 bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
743 by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
747 double xoff = (ax + bx) / (2 * scale);
748 double yoff = (ay + by) / (2 * scale);
750 const float layer_scale = layer_span / scale;
752 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
755 if ( bump_mapping && enable_bump_mapping ) {
756 base = vertices[12].texCoord;
758 base = tl[0]->get( 0 );
762 // the while loops can lead to *long* pauses if base[0] comes
763 // with a bogus value.
764 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
765 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
766 if ( base[0] > -10.0 && base[0] < 10.0 ) {
767 base[0] -= (int)base[0];
769 SG_LOG(SG_ASTRO, SG_DEBUG,
770 "Error: base = " << base[0] << "," << base[1] <<
771 " course = " << course << " dist = " << dist );
776 // the while loops can lead to *long* pauses if base[0] comes
777 // with a bogus value.
778 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
779 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
780 if ( base[1] > -10.0 && base[1] < 10.0 ) {
781 base[1] -= (int)base[1];
783 SG_LOG(SG_ASTRO, SG_ALERT,
784 "Error: base = " << base[0] << "," << base[1] <<
785 " course = " << course << " dist = " << dist );
789 if ( bump_mapping && enable_bump_mapping ) {
791 for ( int i = -2; i <= 2; i++ ) {
792 for ( int j = -2; j <= 2; j++ ) {
793 if ( i == 0 && j == 0 )
794 continue; // Already done on base
795 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
796 sgSetVec2( v1.texCoord,
797 base[0] + layer_scale * i * 0.25,
798 base[1] + layer_scale * j * 0.25 );
803 // cout << "base = " << base[0] << "," << base[1] << endl;
806 for (int i = 0; i < 4; i++) {
807 tc = tl[i]->get( 0 );
808 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
810 for (int j = 0; j < 4; j++)
812 tc = tl[i]->get( j*2+1 );
813 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
814 base[1] + layer_scale * j/4 );
816 tc = tl[i]->get( (j+1)*2 );
817 sgSetVec2( tc, base[0] + layer_scale * i/4,
818 base[1] + layer_scale * (j+1)/4 );
821 tc = tl[i]->get( 9 );
822 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
823 base[1] + layer_scale );
831 layer3D->reposition( p, up, lon, lat, alt, dt, direction, speed);
836 void SGCloudLayer::draw( bool top ) {
837 if ( layer_coverage != SG_CLOUD_CLEAR ) {
839 if ( SGCloudField::enable3D && layer3D->is3D())
842 if ( bump_mapping && enable_bump_mapping ) {
847 ssgGetModelviewMatrix( modelview );
848 layer_transform->getTransform( transform );
850 sgTransposeNegateMat4( tmp, transform );
852 sgPostMultMat4( transform, modelview );
853 ssgLoadModelviewMatrix( transform );
856 ssgGetLight( 0 )->getPosition( lightVec );
857 sgNegateVec3( lightVec );
858 sgXformVec3( lightVec, tmp );
860 for ( int i = 0; i < 25; i++ ) {
861 CloudVertex &v = vertices[ i ];
862 sgSetVec3( v.tangentSpLight,
863 sgScalarProductVec3( v.sTangent, lightVec ),
864 sgScalarProductVec3( v.tTangent, lightVec ),
865 sgScalarProductVec3( v.normal, lightVec ) );
868 ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
869 if ( top && decal == 0 ) {
870 decal = color_map[ layer_coverage ][ 0 ];
872 ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
873 if ( top && normal == 0 ) {
874 normal = normal_map[ layer_coverage ][ 0 ];
877 glDisable( GL_LIGHTING );
878 glDisable( GL_CULL_FACE );
879 // glDisable( GL_ALPHA_TEST );
880 if ( layer_coverage == SG_CLOUD_FEW ) {
881 glEnable( GL_ALPHA_TEST );
882 glAlphaFunc ( GL_GREATER, 0.01 );
884 glEnable( GL_BLEND );
885 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
887 glShadeModel( GL_SMOOTH );
888 glEnable( GL_COLOR_MATERIAL );
892 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
893 emis = ( color[0]+color[1]+color[2] ) / 3.0;
897 sgSetVec4( color, emis, emis, emis, 0.0 );
898 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
899 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
900 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
901 sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
902 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
903 sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
904 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
906 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
908 glActiveTexturePtr( GL_TEXTURE0_ARB );
909 glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
910 glEnable( GL_TEXTURE_2D );
912 //Bind normalisation cube map to texture unit 1
913 glActiveTexturePtr( GL_TEXTURE1_ARB );
914 glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
915 glEnable( GL_TEXTURE_CUBE_MAP_ARB );
916 glActiveTexturePtr( GL_TEXTURE0_ARB );
918 //Set vertex arrays for cloud
919 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
920 glEnableClientState( GL_VERTEX_ARRAY );
922 if ( nb_texture_unit >= 3 ) {
923 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
924 glEnableClientState( GL_COLOR_ARRAY );
927 //Send texture coords for normal map to unit 0
928 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
929 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
931 //Send tangent space light vectors for normalisation to unit 1
932 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
933 glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
934 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
936 //Set up texture environment to do (tex0 dot tex1)*color
937 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
938 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
939 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
940 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
941 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
943 glActiveTexturePtr( GL_TEXTURE1_ARB );
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_DOT3_RGB_ARB );
948 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
949 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
950 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
952 if ( nb_texture_unit >= 3 ) {
953 glActiveTexturePtr( GL_TEXTURE2_ARB );
954 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
956 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
957 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
958 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
960 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
961 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
962 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
963 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
965 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
966 glActiveTexturePtr( GL_TEXTURE0_ARB );
969 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
970 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
971 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
972 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
974 glDisable( GL_TEXTURE_2D );
975 glActiveTexturePtr( GL_TEXTURE1_ARB );
976 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
977 glActiveTexturePtr( GL_TEXTURE2_ARB );
978 glDisable( GL_TEXTURE_2D );
979 glActiveTexturePtr( GL_TEXTURE0_ARB );
981 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
982 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
983 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
984 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
985 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
986 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
988 glDisableClientState( GL_COLOR_ARRAY );
989 glEnable( GL_LIGHTING );
991 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
994 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
995 glActiveTexturePtr( GL_TEXTURE0_ARB );
998 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
999 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1000 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1001 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1004 glDisable( GL_TEXTURE_2D );
1006 glActiveTexturePtr( GL_TEXTURE1_ARB );
1007 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
1008 glActiveTexturePtr( GL_TEXTURE0_ARB );
1010 //disable vertex arrays
1011 glDisableClientState( GL_VERTEX_ARRAY );
1013 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1014 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
1015 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1016 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1018 //Return to standard modulate texenv
1019 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1021 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
1022 glDepthFunc(GL_LEQUAL);
1024 glEnable( GL_LIGHTING );
1026 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1027 float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1028 average = 0.15 + average/10;
1029 sgVec4 averageColor;
1030 sgSetVec4( averageColor, average, average, average, 1.0f );
1031 ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1033 glBlendColorPtr( average, average, average, 1.0f );
1034 glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1036 //Perform a second pass to color the torus
1037 //Bind decal texture
1038 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1039 glEnable(GL_TEXTURE_2D);
1041 //Set vertex arrays for torus
1042 glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1043 glEnableClientState( GL_VERTEX_ARRAY );
1045 //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1046 //glEnableClientState( GL_COLOR_ARRAY );
1048 glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1049 glEnableClientState( GL_NORMAL_ARRAY );
1051 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1052 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1055 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1056 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1057 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1058 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1060 ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1062 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1066 glDisable( GL_TEXTURE_2D );
1068 glDisableClientState( GL_VERTEX_ARRAY );
1069 glDisableClientState( GL_NORMAL_ARRAY );
1071 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1072 glEnable( GL_CULL_FACE );
1073 glDepthFunc(GL_LESS);
1075 ssgLoadModelviewMatrix( modelview );
1078 state_sel->selectStep( top ? 1 : 0 );
1079 ssgCullAndDraw( layer_root );
1085 // make an ssgSimpleState for a cloud layer given the named texture
1086 ssgSimpleState *sgCloudMakeState( const string &path ) {
1087 ssgSimpleState *state = new ssgSimpleState();
1089 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1091 state->setTexture( (char *)path.c_str() );
1092 state->setShadeModel( GL_SMOOTH );
1093 state->disable( GL_LIGHTING );
1094 state->disable( GL_CULL_FACE );
1095 state->enable( GL_TEXTURE_2D );
1096 state->enable( GL_COLOR_MATERIAL );
1097 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1098 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1099 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1100 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1101 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1102 state->enable( GL_BLEND );
1103 state->enable( GL_ALPHA_TEST );
1104 state->setAlphaClamp( 0.01 );