cloud.cxx

00001 // cloud.cxx -- model a single cloud layer
00002 //
00003 // Written by Curtis Olson, started June 2000.
00004 //
00005 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
00006 //
00007 // This library is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Library General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 2 of the License, or (at your option) any later version.
00011 //
00012 // This library is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020 //
00021 // $Id: cloud_8cxx-source.html,v 1.15 2007-12-17 15:37:00 curt Exp $
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #  include <simgear_config.h>
00025 #endif
00026 
00027 #include <simgear/compiler.h>
00028 
00029 // #include <stdio.h>
00030 #include <math.h>
00031 
00032 #include <plib/sg.h>
00033 #include <plib/ssg.h>
00034 
00035 #include <simgear/math/point3d.hxx>
00036 #include <simgear/math/polar3d.hxx>
00037 #include <simgear/math/sg_random.h>
00038 #include <simgear/debug/logstream.hxx>
00039 #include <simgear/screen/extensions.hxx>
00040 #include <simgear/screen/texture.hxx>
00041 #include <simgear/structure/ssgSharedPtr.hxx>
00042 
00043 #include "newcloud.hxx"
00044 #include "cloudfield.hxx"
00045 #include "cloud.hxx"
00046 
00047 #if defined(__MINGW32__)
00048 #define isnan(x) _isnan(x)
00049 #endif
00050 
00051 #if defined (__FreeBSD__)
00052 #  if __FreeBSD_version < 500000
00053      extern "C" {
00054        inline int isnan(double r) { return !(r <= 0 || r >= 0); }
00055      }
00056 #  endif
00057 #endif
00058 
00059 #if defined (__CYGWIN__)
00060 #include <ieeefp.h>
00061 #endif
00062 
00063 static ssgSharedPtr<ssgStateSelector> layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
00064 static bool state_initialized = false;
00065 static bool bump_mapping = false;
00066 static GLint nb_texture_unit = 0;
00067 static ssgSharedPtr<ssgTexture> normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
00068 static ssgSharedPtr<ssgTexture> color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2];
00069 static GLuint normalization_cube_map;
00070 
00071 static glActiveTextureProc glActiveTexturePtr = 0;
00072 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
00073 static glBlendColorProc glBlendColorPtr = 0;
00074 
00075 bool SGCloudLayer::enable_bump_mapping = false;
00076 
00077 static void
00078 generateNormalizationCubeMap()
00079 {
00080     unsigned char data[ 32 * 32 * 3 ];
00081     const int size = 32;
00082     const float half_size = 16.0f,
00083                 offset = 0.5f;
00084     sgVec3 zero_normal;
00085     sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
00086     int i, j;
00087 
00088     unsigned char *ptr = data;
00089     for ( j = 0; j < size; j++ ) {
00090         for ( i = 0; i < size; i++ ) {
00091             sgVec3 tmp;
00092             sgSetVec3( tmp, half_size,
00093                             -( j + offset - half_size ),
00094                             -( i + offset - half_size ) );
00095             sgNormalizeVec3( tmp );
00096             sgScaleVec3( tmp, 0.5f );
00097             sgAddVec3( tmp, zero_normal );
00098 
00099             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
00100             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
00101             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
00102         }
00103     }
00104     glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
00105                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
00106 
00107     ptr = data;
00108     for ( j = 0; j < size; j++ ) {
00109         for ( i = 0; i < size; i++ ) {
00110             sgVec3 tmp;
00111             sgSetVec3( tmp, -half_size,
00112                             -( j + offset - half_size ),
00113                             ( i + offset - half_size ) );
00114             sgNormalizeVec3( tmp );
00115             sgScaleVec3( tmp, 0.5f );
00116             sgAddVec3( tmp, zero_normal );
00117 
00118             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
00119             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
00120             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
00121         }
00122     }
00123     glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
00124                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
00125 
00126     ptr = data;
00127     for ( j = 0; j < size; j++ ) {
00128         for ( i = 0; i < size; i++ ) {
00129             sgVec3 tmp;
00130             sgSetVec3( tmp, ( i + offset - half_size ),
00131                             half_size,
00132                             ( j + offset - half_size ) );
00133             sgNormalizeVec3( tmp );
00134             sgScaleVec3( tmp, 0.5f );
00135             sgAddVec3( tmp, zero_normal );
00136 
00137             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
00138             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
00139             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
00140         }
00141     }
00142     glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
00143                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
00144 
00145     ptr = data;
00146     for ( j = 0; j < size; j++ ) {
00147         for ( i = 0; i < size; i++ ) {
00148             sgVec3 tmp;
00149             sgSetVec3( tmp, ( i + offset - half_size ),
00150                             -half_size,
00151                             -( j + offset - half_size ) );
00152             sgNormalizeVec3( tmp );
00153             sgScaleVec3( tmp, 0.5f );
00154             sgAddVec3( tmp, zero_normal );
00155 
00156             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
00157             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
00158             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
00159         }
00160     }
00161     glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
00162                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
00163 
00164     ptr = data;
00165     for ( j = 0; j < size; j++ ) {
00166         for ( i = 0; i < size; i++ ) {
00167             sgVec3 tmp;
00168             sgSetVec3( tmp, ( i + offset - half_size ),
00169                             -( j + offset - half_size ),
00170                             half_size );
00171             sgNormalizeVec3( tmp );
00172             sgScaleVec3( tmp, 0.5f );
00173             sgAddVec3( tmp, zero_normal );
00174 
00175             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
00176             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
00177             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
00178         }
00179     }
00180     glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
00181                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
00182 
00183     ptr = data;
00184     for ( j = 0; j < size; j++ ) {
00185         for ( i = 0; i < size; i++ ) {
00186             sgVec3 tmp;
00187             sgSetVec3( tmp, -( i + offset - half_size ),
00188                             -( j + offset - half_size ),
00189                             -half_size );
00190             sgNormalizeVec3( tmp );
00191             sgScaleVec3( tmp, 0.5f );
00192             sgAddVec3( tmp, zero_normal );
00193 
00194             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
00195             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
00196             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
00197         }
00198     }
00199     glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
00200                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
00201 }
00202 
00203 
00204 // Constructor
00205 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
00206     vertices(0),
00207     indices(0),
00208     layer_root(new ssgRoot),
00209     layer_transform(new ssgTransform),
00210     state_sel(0),
00211     cloud_alpha(1.0),
00212     texture_path(tex_path),
00213     layer_span(0.0),
00214     layer_asl(0.0),
00215     layer_thickness(0.0),
00216     layer_transition(0.0),
00217     layer_coverage(SG_CLOUD_CLEAR),
00218     scale(4000.0),
00219     speed(0.0),
00220     direction(0.0),
00221     last_lon(0.0),
00222     last_lat(0.0)
00223 {
00224     cl[0] = cl[1] = cl[2] = cl[3] = NULL;
00225     vl[0] = vl[1] = vl[2] = vl[3] = NULL;
00226     tl[0] = tl[1] = tl[2] = tl[3] = NULL;
00227     layer[0] = layer[1] = layer[2] = layer[3] = NULL;
00228     sgSetVec2(base, sg_random(), sg_random());
00229 
00230     layer_root->addKid(layer_transform);
00231         layer3D = new SGCloudField;
00232     rebuild();
00233 }
00234 
00235 // Destructor
00236 SGCloudLayer::~SGCloudLayer()
00237 {
00238         delete layer3D;
00239     delete vertices;
00240     delete indices;
00241     delete layer_root;          // deletes layer_transform and layer as well
00242 }
00243 
00244 float
00245 SGCloudLayer::getSpan_m () const
00246 {
00247     return layer_span;
00248 }
00249 
00250 void
00251 SGCloudLayer::setSpan_m (float span_m)
00252 {
00253     if (span_m != layer_span) {
00254         layer_span = span_m;
00255         rebuild();
00256     }
00257 }
00258 
00259 float
00260 SGCloudLayer::getElevation_m () const
00261 {
00262     return layer_asl;
00263 }
00264 
00265 void
00266 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
00267 {
00268     layer_asl = elevation_m;
00269 
00270     if (set_span) {
00271         if (elevation_m > 4000)
00272             setSpan_m(  elevation_m * 10 );
00273         else
00274             setSpan_m( 40000 );
00275     }
00276 }
00277 
00278 float
00279 SGCloudLayer::getThickness_m () const
00280 {
00281     return layer_thickness;
00282 }
00283 
00284 void
00285 SGCloudLayer::setThickness_m (float thickness_m)
00286 {
00287     layer_thickness = thickness_m;
00288 }
00289 
00290 float
00291 SGCloudLayer::getTransition_m () const
00292 {
00293     return layer_transition;
00294 }
00295 
00296 void
00297 SGCloudLayer::setTransition_m (float transition_m)
00298 {
00299     layer_transition = transition_m;
00300 }
00301 
00302 SGCloudLayer::Coverage
00303 SGCloudLayer::getCoverage () const
00304 {
00305     return layer_coverage;
00306 }
00307 
00308 void
00309 SGCloudLayer::setCoverage (Coverage coverage)
00310 {
00311     if (coverage != layer_coverage) {
00312         layer_coverage = coverage;
00313         rebuild();
00314     }
00315 }
00316 
00317 
00318 // build the cloud object
00319 void
00320 SGCloudLayer::rebuild()
00321 {
00322     // Initialize states and sizes if necessary.
00323     if ( !state_initialized ) { 
00324         state_initialized = true;
00325 
00326         SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
00327 
00328         bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
00329                        SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
00330                        SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
00331                        SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") && 
00332                        SGIsOpenGLExtensionSupported("GL_ARB_imaging");
00333 
00334         if ( bump_mapping ) {
00335             glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
00336             if ( nb_texture_unit < 2 ) {
00337                 bump_mapping = false;
00338             }
00339             //nb_texture_unit = 2; // Force the number of units for now
00340         }
00341 
00342         if ( bump_mapping ) {
00343 
00344             // This bump mapping code was inspired by the tutorial available at 
00345             // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
00346             // and a NVidia white paper 
00347             //  http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
00348             // The normal map textures were generated by the normal map Gimp plugin :
00349             //  http://nifelheim.dyndns.org/~cocidius/normalmap/
00350             //
00351             SGPath cloud_path;
00352 
00353             glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
00354             glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
00355             glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
00356 
00357             cloud_path.set(texture_path.str());
00358             cloud_path.append("overcast.rgb");
00359             color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
00360             cloud_path.set(texture_path.str());
00361             cloud_path.append("overcast_n.rgb");
00362             normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
00363 
00364             cloud_path.set(texture_path.str());
00365             cloud_path.append("overcast_top.rgb");
00366             color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
00367             cloud_path.set(texture_path.str());
00368             cloud_path.append("overcast_top_n.rgb");
00369             normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
00370 
00371             cloud_path.set(texture_path.str());
00372             cloud_path.append("broken.rgba");
00373             color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
00374             cloud_path.set(texture_path.str());
00375             cloud_path.append("broken_n.rgb");
00376             normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
00377 
00378             cloud_path.set(texture_path.str());
00379             cloud_path.append("scattered.rgba");
00380             color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
00381             cloud_path.set(texture_path.str());
00382             cloud_path.append("scattered_n.rgb");
00383             normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
00384 
00385             cloud_path.set(texture_path.str());
00386             cloud_path.append("few.rgba");
00387             color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
00388             cloud_path.set(texture_path.str());
00389             cloud_path.append("few_n.rgb");
00390             normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
00391 
00392             cloud_path.set(texture_path.str());
00393             cloud_path.append("cirrus.rgba");
00394             color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
00395             cloud_path.set(texture_path.str());
00396             cloud_path.append("cirrus_n.rgb");
00397             normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
00398 
00399             glGenTextures( 1, &normalization_cube_map );
00400             glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
00401             generateNormalizationCubeMap();
00402             glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00403             glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00404             glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
00405             glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
00406             glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
00407         } /* else */ {
00408             SGPath cloud_path;
00409             ssgStateSelector *state_sel;
00410             ssgSimpleState *state;
00411 
00412             state_sel = new ssgStateSelector( 2 );
00413             cloud_path.set(texture_path.str());
00414             cloud_path.append("overcast.rgb");
00415             state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
00416             cloud_path.set(texture_path.str());
00417             cloud_path.append("overcast_top.rgb");
00418             state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
00419             layer_states[SG_CLOUD_OVERCAST] = state_sel;
00420 
00421             state_sel = new ssgStateSelector( 2 );
00422             cloud_path.set(texture_path.str());
00423             cloud_path.append("broken.rgba");
00424             state = sgCloudMakeState(cloud_path.str());
00425             state_sel->setStep( 0, state );
00426             state_sel->setStep( 1, state );
00427             layer_states[SG_CLOUD_BROKEN] = state_sel;
00428 
00429             state_sel = new ssgStateSelector( 2 );
00430             cloud_path.set(texture_path.str());
00431             cloud_path.append("scattered.rgba");
00432             state = sgCloudMakeState(cloud_path.str());
00433             state_sel->setStep( 0, state );
00434             state_sel->setStep( 1, state );
00435             layer_states[SG_CLOUD_SCATTERED] = state_sel;
00436 
00437             state_sel = new ssgStateSelector( 2 );
00438             cloud_path.set(texture_path.str());
00439             cloud_path.append("few.rgba");
00440             state = sgCloudMakeState(cloud_path.str());
00441             state_sel->setStep( 0, state );
00442             state_sel->setStep( 1, state );
00443             layer_states[SG_CLOUD_FEW] = state_sel;
00444 
00445             state_sel = new ssgStateSelector( 2 );
00446             cloud_path.set(texture_path.str());
00447             cloud_path.append("cirrus.rgba");
00448             state = sgCloudMakeState(cloud_path.str());
00449             state_sel->setStep( 0, state );
00450             state_sel->setStep( 1, state );
00451             layer_states[SG_CLOUD_CIRRUS] = state_sel;
00452 
00453             layer_states[SG_CLOUD_CLEAR] = 0;
00454         }
00455                 SGNewCloud::loadTextures(texture_path.str());
00456                 layer3D->buildTestLayer();
00457     }
00458 
00459     if ( bump_mapping ) {
00460 
00461         if ( !vertices ) {
00462             vertices = new CloudVertex[ 25 ];
00463             indices = new unsigned int[ 40 ];
00464         }
00465 
00466         const float layer_scale = layer_span / scale;
00467         const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
00468         const float half_angle = 0.5 * layer_span / layer_to_core;
00469 
00470         int i;
00471         for ( i = -2; i <= 2; i++ ) {
00472             for ( int j = -2; j <= 2; j++ ) {
00473                 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
00474                 sgSetVec3( v1.position,
00475                            0.5 * i * layer_span,
00476                            0.5 * j * layer_span,
00477                            -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
00478                 sgSetVec2( v1.texCoord,
00479                            base[0] + layer_scale * i * 0.25,
00480                            base[1] + layer_scale * j * 0.25 );
00481                 sgSetVec3( v1.sTangent,
00482                            cos( i * half_angle ),
00483                            0.f,
00484                            -sin( i * half_angle ) );
00485                 sgSetVec3( v1.tTangent,
00486                            0.f,
00487                            cos( j * half_angle ),
00488                            -sin( j * half_angle ) );
00489                 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
00490                 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : cloud_alpha * 0.15f );
00491             }
00492         }
00493         /*
00494          * 0 1 5 6 10 11 15 16 20 21
00495          * 1 2 6 7 11 12 16 17 21 22
00496          * 2 3 7 8 12 13 17 18 22 23
00497          * 3 4 8 9 13 14 18 19 23 24
00498          */
00499         for ( i = 0; i < 4; i++ ) {
00500             for ( int j = 0; j < 5; j++ ) {
00501                 indices[ i*10 + (j*2) ]     =     i + 5 * j;
00502                 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
00503             }
00504         }
00505 
00506     } /* else */ {
00507 
00508         scale = 4000.0;
00509         last_lon = last_lat = -999.0f;
00510 
00511         // build the cloud layer
00512         sgVec4 color;
00513         sgVec3 vertex;
00514         sgVec2 tc;
00515 
00516         const float layer_scale = layer_span / scale;
00517         const float mpi = SG_PI/4;
00518 
00519         // caclculate the difference between a flat-earth model and 
00520         // a round earth model given the span and altutude ASL of
00521         // the cloud layer. This is the difference in altitude between
00522         // the top of the inverted bowl and the edge of the bowl.
00523         // const float alt_diff = layer_asl * 0.8;
00524         const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
00525         const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
00526         const float border_to_core = layer_to_core * cos(layer_angle);
00527         const float alt_diff = layer_to_core - border_to_core;
00528 
00529         for (int i = 0; i < 4; i++)
00530         {
00531             if ( layer[i] != NULL ) {
00532                 layer_transform->removeKid(layer[i]); // automatic delete
00533             }
00534 
00535             vl[i] = new ssgVertexArray( 10 );
00536             cl[i] = new ssgColourArray( 10 );
00537             tl[i] = new ssgTexCoordArray( 10 );
00538 
00539 
00540             sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
00541                             alt_diff * (sin(i*mpi) - 2) );
00542 
00543             sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
00544 
00545             sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
00546 
00547             cl[i]->add( color );
00548             vl[i]->add( vertex );
00549             tl[i]->add( tc );
00550 
00551             for (int j = 0; j < 4; j++)
00552             {
00553                 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
00554                                 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
00555 
00556                 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
00557                             base[1] + layer_scale * j/4 );
00558 
00559                 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
00560                                 ( (j == 0) || (i == 3)) ?  
00561                                 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
00562 
00563                 cl[i]->add( color );
00564                 vl[i]->add( vertex );
00565                 tl[i]->add( tc );
00566 
00567 
00568                 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
00569                                 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
00570 
00571                 sgSetVec2( tc, base[0] + layer_scale * i/4,
00572                             base[1] + layer_scale * (j+1)/4 );
00573 
00574                 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
00575                                 ((j == 3) || (i == 0)) ?
00576                                 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
00577                 cl[i]->add( color );
00578                 vl[i]->add( vertex );
00579                 tl[i]->add( tc );
00580             }
00581 
00582             sgSetVec3( vertex, layer_span*(i-1)/2, layer_span, 
00583                             alt_diff * (sin((i+1)*mpi) - 2) );
00584 
00585             sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
00586                         base[1] + layer_scale );
00587 
00588             sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
00589 
00590             cl[i]->add( color );
00591             vl[i]->add( vertex );
00592             tl[i]->add( tc );
00593 
00594             layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
00595             layer_transform->addKid( layer[i] );
00596 
00597             if ( layer_states[layer_coverage] != NULL ) {
00598                 layer[i]->setState( layer_states[layer_coverage] );
00599             }
00600             state_sel = layer_states[layer_coverage];
00601         }
00602 
00603         // force a repaint of the sky colors with arbitrary defaults
00604         repaint( color );
00605     }
00606 }
00607 
00608 
00609 // repaint the cloud layer colors
00610 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
00611 
00612     if ( bump_mapping && enable_bump_mapping ) {
00613 
00614         for ( int i = 0; i < 25; i++ ) {
00615             sgCopyVec3( vertices[ i ].color, fog_color );
00616         }
00617 
00618     } else {
00619         float *color;
00620 
00621         for ( int i = 0; i < 4; i++ ) {
00622             color = cl[i]->get( 0 );
00623             sgCopyVec3( color, fog_color );
00624             color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
00625 
00626             for ( int j = 0; j < 4; ++j ) {
00627                 color = cl[i]->get( (2*j) + 1 );
00628                 sgCopyVec3( color, fog_color );
00629                 color[3] = 
00630                     ((j == 0) || (i == 3)) ?
00631                     ((j == 0) && (i == 3)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
00632 
00633                 color = cl[i]->get( (2*j) + 2 );
00634                 sgCopyVec3( color, fog_color );
00635                 color[3] = 
00636                     ((j == 3) || (i == 0)) ?
00637                     ((j == 3) && (i == 0)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
00638             }
00639 
00640             color = cl[i]->get( 9 );
00641             sgCopyVec3( color, fog_color );
00642             color[3] = (i == 3) ? 0.0f : cloud_alpha * 0.15f;
00643         }
00644     }
00645 
00646     return true;
00647 }
00648 
00649 // reposition the cloud layer at the specified origin and orientation
00650 // lon specifies a rotation about the Z axis
00651 // lat specifies a rotation about the new Y axis
00652 // spin specifies a rotation about the new Z axis (and orients the
00653 // sunrise/set effects
00654 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
00655                                double alt, double dt )
00656 {
00657     sgMat4 T1, LON, LAT;
00658     sgVec3 axis;
00659 
00660     // combine p and asl (meters) to get translation offset
00661     sgVec3 asl_offset;
00662     sgCopyVec3( asl_offset, up );
00663     sgNormalizeVec3( asl_offset );
00664     if ( alt <= layer_asl ) {
00665         sgScaleVec3( asl_offset, layer_asl );
00666     } else {
00667         sgScaleVec3( asl_offset, layer_asl + layer_thickness );
00668     }
00669     // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
00670     //      << "," << asl_offset[2] << endl;
00671     sgAddVec3( asl_offset, p );
00672     // cout << "  asl_offset = " << asl_offset[0] << "," << asl_offset[1]
00673     //      << "," << asl_offset[2] << endl;
00674 
00675     // Translate to zero elevation
00676     // Point3D zero_elev = current_view.get_cur_zero_elev();
00677     sgMakeTransMat4( T1, asl_offset );
00678 
00679     // printf("  Translated to %.2f %.2f %.2f\n", 
00680     //        zero_elev.x, zero_elev.y, zero_elev.z );
00681 
00682     // Rotate to proper orientation
00683     // printf("  lon = %.2f  lat = %.2f\n", 
00684     //        lon * SGD_RADIANS_TO_DEGREES,
00685     //        lat * SGD_RADIANS_TO_DEGREES);
00686     sgSetVec3( axis, 0.0, 0.0, 1.0 );
00687     sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
00688 
00689     sgSetVec3( axis, 0.0, 1.0, 0.0 );
00690     sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
00691 
00692     sgMat4 TRANSFORM;
00693 
00694     sgCopyMat4( TRANSFORM, T1 );
00695     sgPreMultMat4( TRANSFORM, LON );
00696     sgPreMultMat4( TRANSFORM, LAT );
00697 
00698     sgCoord layerpos;
00699     sgSetCoord( &layerpos, TRANSFORM );
00700 
00701     layer_transform->setTransform( &layerpos );
00702 
00703     // now calculate update texture coordinates
00704     if ( last_lon < -900 ) {
00705         last_lon = lon;
00706         last_lat = lat;
00707     }
00708 
00709     double sp_dist = speed*dt;
00710 
00711     if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
00712         Point3D start( last_lon, last_lat, 0.0 );
00713         Point3D dest( lon, lat, 0.0 );
00714         double course = 0.0, dist = 0.0;
00715 
00716         calc_gc_course_dist( dest, start, &course, &dist );
00717         // cout << "course = " << course << ", dist = " << dist << endl;
00718 
00719         // if start and dest are too close together,
00720         // calc_gc_course_dist() can return a course of "nan".  If
00721         // this happens, lets just use the last known good course.
00722         // This is a hack, and it would probably be better to make
00723         // calc_gc_course_dist() more robust.
00724         if ( isnan(course) ) {
00725             course = last_course;
00726         } else {
00727             last_course = course;
00728         }
00729 
00730         // calculate cloud movement due to external forces
00731         double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
00732 
00733         if (dist > 0.0) {
00734             ax = cos(course) * dist;
00735             ay = sin(course) * dist;
00736         }
00737 
00738         if (sp_dist > 0) {
00739             bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
00740             by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
00741         }
00742 
00743 
00744         double xoff = (ax + bx) / (2 * scale);
00745         double yoff = (ay + by) / (2 * scale);
00746 
00747         const float layer_scale = layer_span / scale;
00748 
00749         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
00750 
00751         float *base;
00752         if ( bump_mapping && enable_bump_mapping ) {
00753             base = vertices[12].texCoord;
00754         } else {
00755             base = tl[0]->get( 0 );
00756         }
00757         base[0] += xoff;
00758 
00759         // the while loops can lead to *long* pauses if base[0] comes
00760         // with a bogus value.
00761         // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
00762         // while ( base[0] < 0.0 ) { base[0] += 1.0; }
00763         if ( base[0] > -10.0 && base[0] < 10.0 ) {
00764             base[0] -= (int)base[0];
00765         } else {
00766             SG_LOG(SG_ASTRO, SG_DEBUG,
00767                 "Error: base = " << base[0] << "," << base[1] <<
00768                 " course = " << course << " dist = " << dist );
00769             base[0] = 0.0;
00770         }
00771 
00772         base[1] += yoff;
00773         // the while loops can lead to *long* pauses if base[0] comes
00774         // with a bogus value.
00775         // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
00776         // while ( base[1] < 0.0 ) { base[1] += 1.0; }
00777         if ( base[1] > -10.0 && base[1] < 10.0 ) {
00778             base[1] -= (int)base[1];
00779         } else {
00780             SG_LOG(SG_ASTRO, SG_DEBUG,
00781                     "Error: base = " << base[0] << "," << base[1] <<
00782                     " course = " << course << " dist = " << dist );
00783             base[1] = 0.0;
00784         }
00785 
00786         if ( bump_mapping && enable_bump_mapping ) {
00787 
00788             for ( int i = -2; i <= 2; i++ ) {
00789                 for ( int j = -2; j <= 2; j++ ) {
00790                     if ( i == 0 && j == 0 )
00791                         continue; // Already done on base
00792                     CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
00793                     sgSetVec2( v1.texCoord,
00794                             base[0] + layer_scale * i * 0.25,
00795                             base[1] + layer_scale * j * 0.25 );
00796                 }
00797             }
00798 
00799         } else {
00800                 // cout << "base = " << base[0] << "," << base[1] << endl;
00801 
00802             float *tc;
00803             for (int i = 0; i < 4; i++) {
00804                 tc = tl[i]->get( 0 );
00805                 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
00806                 
00807                 for (int j = 0; j < 4; j++)
00808                 {
00809                     tc = tl[i]->get( j*2+1 );
00810                     sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
00811                                 base[1] + layer_scale * j/4 );
00812         
00813                     tc = tl[i]->get( (j+1)*2 );
00814                     sgSetVec2( tc, base[0] + layer_scale * i/4,
00815                                 base[1] + layer_scale * (j+1)/4 );
00816                 }
00817         
00818                 tc = tl[i]->get( 9 );
00819                 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
00820                             base[1] + layer_scale );
00821             }
00822         }
00823 
00824         last_lon = lon;
00825         last_lat = lat;
00826     }
00827 
00828         layer3D->reposition( p, up, lon, lat, alt, dt, direction, speed);
00829     return true;
00830 }
00831 
00832 
00833 void SGCloudLayer::draw( bool top, float *sun_color ) {
00834     if ( layer_coverage != SG_CLOUD_CLEAR ) {
00835 
00836                 if ( SGCloudField::enable3D && layer3D->is3D())
00837                         layer3D->Render( sun_color );
00838                 else
00839         if ( bump_mapping && enable_bump_mapping ) {
00840 
00841             sgMat4 modelview,
00842                    tmp,
00843                    transform;
00844             ssgGetModelviewMatrix( modelview );
00845             layer_transform->getTransform( transform );
00846 
00847             sgTransposeNegateMat4( tmp, transform );
00848 
00849             sgPostMultMat4( transform, modelview );
00850             ssgLoadModelviewMatrix( transform );
00851 
00852             sgVec3 lightVec;
00853             ssgGetLight( 0 )->getPosition( lightVec );
00854             sgNegateVec3( lightVec );
00855             sgXformVec3( lightVec, tmp );
00856 
00857             for ( int i = 0; i < 25; i++ ) {
00858                 CloudVertex &v = vertices[ i ];
00859                 sgSetVec3( v.tangentSpLight,
00860                            sgScalarProductVec3( v.sTangent, lightVec ),
00861                            sgScalarProductVec3( v.tTangent, lightVec ),
00862                            sgScalarProductVec3( v.normal, lightVec ) );
00863             }
00864 
00865             ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
00866             if ( top && decal == 0 ) {
00867                 decal = color_map[ layer_coverage ][ 0 ];
00868             }
00869             ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
00870             if ( top && normal == 0 ) {
00871                 normal = normal_map[ layer_coverage ][ 0 ];
00872             }
00873 
00874             glDisable( GL_LIGHTING );
00875             glDisable( GL_CULL_FACE );
00876 //            glDisable( GL_ALPHA_TEST );
00877             if ( layer_coverage == SG_CLOUD_FEW ) {
00878                 glEnable( GL_ALPHA_TEST );
00879                 glAlphaFunc ( GL_GREATER, 0.01 );
00880             }
00881             glEnable( GL_BLEND ); 
00882             glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00883 
00884             glShadeModel( GL_SMOOTH );
00885             glEnable( GL_COLOR_MATERIAL ); 
00886             sgVec4 color;
00887             float emis = 0.05;
00888             if ( 1 ) {
00889                 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
00890                 emis = ( color[0]+color[1]+color[2] ) / 3.0;
00891                 if ( emis < 0.05 )
00892                     emis = 0.05;
00893             }
00894             sgSetVec4( color, emis, emis, emis, 0.0 );
00895             glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
00896             sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
00897             glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
00898             sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
00899             glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
00900             sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
00901             glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
00902 
00903             glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
00904 
00905             glActiveTexturePtr( GL_TEXTURE0_ARB );
00906             glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
00907             glEnable( GL_TEXTURE_2D );
00908 
00909             //Bind normalisation cube map to texture unit 1
00910             glActiveTexturePtr( GL_TEXTURE1_ARB );
00911             glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
00912             glEnable( GL_TEXTURE_CUBE_MAP_ARB );
00913             glActiveTexturePtr( GL_TEXTURE0_ARB );
00914 
00915             //Set vertex arrays for cloud
00916             glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
00917             glEnableClientState( GL_VERTEX_ARRAY );
00918 /*
00919             if ( nb_texture_unit >= 3 ) {
00920                 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
00921                 glEnableClientState( GL_COLOR_ARRAY );
00922             }
00923 */
00924             //Send texture coords for normal map to unit 0
00925             glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
00926             glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00927 
00928             //Send tangent space light vectors for normalisation to unit 1
00929             glClientActiveTexturePtr( GL_TEXTURE1_ARB );
00930             glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
00931             glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00932 
00933             //Set up texture environment to do (tex0 dot tex1)*color
00934             glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
00935             glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
00936             glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
00937             glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
00938             glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
00939 
00940             glActiveTexturePtr( GL_TEXTURE1_ARB );
00941 
00942             glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
00943             glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
00944             glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
00945             glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
00946             glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
00947             glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
00948 
00949             if ( nb_texture_unit >= 3 ) {
00950                 glActiveTexturePtr( GL_TEXTURE2_ARB );
00951                 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
00952 
00953                 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
00954                 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
00955                 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00956 
00957                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
00958                 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
00959                 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
00960                 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
00961 
00962                 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
00963                 glActiveTexturePtr( GL_TEXTURE0_ARB );
00964 
00965                 //Draw cloud layer
00966                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
00967                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
00968                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
00969                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
00970 
00971                 glDisable( GL_TEXTURE_2D );
00972                 glActiveTexturePtr( GL_TEXTURE1_ARB );
00973                 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
00974                 glActiveTexturePtr( GL_TEXTURE2_ARB );
00975                 glDisable( GL_TEXTURE_2D );
00976                 glActiveTexturePtr( GL_TEXTURE0_ARB );
00977 
00978                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
00979                 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
00980                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
00981                 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
00982                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
00983                 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
00984 
00985                 glDisableClientState( GL_COLOR_ARRAY );
00986                 glEnable( GL_LIGHTING );
00987 
00988                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
00989 
00990             } else {
00991                 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
00992                 glActiveTexturePtr( GL_TEXTURE0_ARB );
00993 
00994                 //Draw cloud layer
00995                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
00996                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
00997                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
00998                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
00999 
01000                 //Disable textures
01001                 glDisable( GL_TEXTURE_2D );
01002 
01003                 glActiveTexturePtr( GL_TEXTURE1_ARB );
01004                 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
01005                 glActiveTexturePtr( GL_TEXTURE0_ARB );
01006 
01007                 //disable vertex arrays
01008                 glDisableClientState( GL_VERTEX_ARRAY );
01009 
01010                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
01011                 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
01012                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
01013                 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
01014 
01015                 //Return to standard modulate texenv
01016                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
01017 
01018                 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
01019                     glDepthFunc(GL_LEQUAL);
01020 
01021                     glEnable( GL_LIGHTING );
01022                     sgVec4 color;
01023                     ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
01024                     float average = ( color[0] + color[1] + color[2] ) / 3.0f;
01025                     average = 0.15 + average/10;
01026                     sgVec4 averageColor;
01027                     sgSetVec4( averageColor, average, average, average, 1.0f );
01028                     ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
01029 
01030                     glBlendColorPtr( average, average, average, 1.0f );
01031                     glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
01032 
01033                     //Perform a second pass to color the torus
01034                     //Bind decal texture
01035                     glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
01036                     glEnable(GL_TEXTURE_2D);
01037 
01038                     //Set vertex arrays for torus
01039                     glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
01040                     glEnableClientState( GL_VERTEX_ARRAY );
01041 
01042                     //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
01043                     //glEnableClientState( GL_COLOR_ARRAY );
01044 
01045                     glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
01046                     glEnableClientState( GL_NORMAL_ARRAY );
01047 
01048                     glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
01049                     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
01050 
01051                     //Draw cloud layer
01052                     glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
01053                     glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
01054                     glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
01055                     glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
01056 
01057                     ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
01058 
01059                     glDisableClientState( GL_TEXTURE_COORD_ARRAY );
01060                 }
01061             }
01062             //Disable texture
01063             glDisable( GL_TEXTURE_2D );
01064 
01065             glDisableClientState( GL_VERTEX_ARRAY );
01066             glDisableClientState( GL_NORMAL_ARRAY );
01067 
01068             glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
01069             glEnable( GL_CULL_FACE );
01070             glDepthFunc(GL_LESS);
01071 
01072             ssgLoadModelviewMatrix( modelview );
01073 
01074         } else {
01075             state_sel->selectStep( top ? 1 : 0 );
01076             ssgCullAndDraw( layer_root );
01077         }
01078     }
01079 }
01080 
01081 
01082 // make an ssgSimpleState for a cloud layer given the named texture
01083 ssgSimpleState *sgCloudMakeState( const string &path ) {
01084     ssgSimpleState *state = new ssgSimpleState();
01085 
01086     SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
01087 
01088     state->setTexture( (char *)path.c_str() );
01089     state->setShadeModel( GL_SMOOTH );
01090     state->disable( GL_LIGHTING );
01091     state->disable( GL_CULL_FACE );
01092     state->enable( GL_TEXTURE_2D );
01093     state->enable( GL_COLOR_MATERIAL );
01094     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
01095     state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
01096     state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
01097     state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
01098     state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
01099     state->enable( GL_BLEND );
01100     state->enable( GL_ALPHA_TEST );
01101     state->setAlphaClamp( 0.01 );
01102 
01103     return state;
01104 }

Generated on Mon Dec 17 09:30:54 2007 for SimGear by  doxygen 1.5.1