]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloud.cxx
Harald JOHSEN: the sky color now fades to black with altitude
[simgear.git] / simgear / scene / sky / cloud.cxx
1 // cloud.cxx -- model a single cloud layer
2 //
3 // Written by Curtis Olson, started June 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is distributed in the hope that it will be useful, but
8 // WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // General Public License for more details.
11 //
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 //
16 // $Id$
17
18
19 #include <simgear/compiler.h>
20
21 // #include <stdio.h>
22 #include <math.h>
23
24 #include <plib/sg.h>
25 #include <plib/ssg.h>
26
27 #include <simgear/math/point3d.hxx>
28 #include <simgear/math/polar3d.hxx>
29 #include <simgear/math/sg_random.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/screen/extensions.hxx>
33 #include <simgear/screen/texture.hxx>
34
35 #include "newcloud.hxx"
36 #include "cloudfield.hxx"
37 #include "cloud.hxx"
38
39 #if defined(__MINGW32__)
40 #define isnan(x) _isnan(x)
41 #endif
42
43 #if defined (__FreeBSD__)
44 #  if __FreeBSD_version < 500000
45      extern "C" {
46        inline int isnan(double r) { return !(r <= 0 || r >= 0); }
47      }
48 #  endif
49 #endif
50
51
52 static ssgStateSelector *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
53 static bool state_initialized = false;
54 static bool bump_mapping = false;
55 static GLint nb_texture_unit = 0;
56 static ssgTexture *normal_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
57 static ssgTexture *color_map[SGCloudLayer::SG_MAX_CLOUD_COVERAGES][2] = { 0 };
58 static GLuint normalization_cube_map;
59
60 static glActiveTextureProc glActiveTexturePtr = 0;
61 static glClientActiveTextureProc glClientActiveTexturePtr = 0;
62 static glBlendColorProc glBlendColorPtr = 0;
63
64 bool SGCloudLayer::enable_bump_mapping = false;
65
66 static void
67 generateNormalizationCubeMap()
68 {
69     unsigned char data[ 32 * 32 * 3 ];
70     const int size = 32;
71     const float half_size = 16.0f,
72                 offset = 0.5f;
73     sgVec3 zero_normal;
74     sgSetVec3( zero_normal, 0.5f, 0.5f, 0.5f );
75     int i, j;
76
77     unsigned char *ptr = data;
78     for ( j = 0; j < size; j++ ) {
79         for ( i = 0; i < size; i++ ) {
80             sgVec3 tmp;
81             sgSetVec3( tmp, half_size,
82                             -( j + offset - half_size ),
83                             -( i + offset - half_size ) );
84             sgNormalizeVec3( tmp );
85             sgScaleVec3( tmp, 0.5f );
86             sgAddVec3( tmp, zero_normal );
87
88             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
89             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
90             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
91         }
92     }
93     glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
94                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
95
96     ptr = data;
97     for ( j = 0; j < size; j++ ) {
98         for ( i = 0; i < size; i++ ) {
99             sgVec3 tmp;
100             sgSetVec3( tmp, -half_size,
101                             -( j + offset - half_size ),
102                             ( i + offset - half_size ) );
103             sgNormalizeVec3( tmp );
104             sgScaleVec3( tmp, 0.5f );
105             sgAddVec3( tmp, zero_normal );
106
107             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
108             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
109             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
110         }
111     }
112     glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
113                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
114
115     ptr = data;
116     for ( j = 0; j < size; j++ ) {
117         for ( i = 0; i < size; i++ ) {
118             sgVec3 tmp;
119             sgSetVec3( tmp, ( i + offset - half_size ),
120                             half_size,
121                             ( j + offset - half_size ) );
122             sgNormalizeVec3( tmp );
123             sgScaleVec3( tmp, 0.5f );
124             sgAddVec3( tmp, zero_normal );
125
126             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
127             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
128             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
129         }
130     }
131     glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
132                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
133
134     ptr = data;
135     for ( j = 0; j < size; j++ ) {
136         for ( i = 0; i < size; i++ ) {
137             sgVec3 tmp;
138             sgSetVec3( tmp, ( i + offset - half_size ),
139                             -half_size,
140                             -( j + offset - half_size ) );
141             sgNormalizeVec3( tmp );
142             sgScaleVec3( tmp, 0.5f );
143             sgAddVec3( tmp, zero_normal );
144
145             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
146             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
147             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
148         }
149     }
150     glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
151                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
152
153     ptr = data;
154     for ( j = 0; j < size; j++ ) {
155         for ( i = 0; i < size; i++ ) {
156             sgVec3 tmp;
157             sgSetVec3( tmp, ( i + offset - half_size ),
158                             -( j + offset - half_size ),
159                             half_size );
160             sgNormalizeVec3( tmp );
161             sgScaleVec3( tmp, 0.5f );
162             sgAddVec3( tmp, zero_normal );
163
164             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
165             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
166             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
167         }
168     }
169     glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
170                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
171
172     ptr = data;
173     for ( j = 0; j < size; j++ ) {
174         for ( i = 0; i < size; i++ ) {
175             sgVec3 tmp;
176             sgSetVec3( tmp, -( i + offset - half_size ),
177                             -( j + offset - half_size ),
178                             -half_size );
179             sgNormalizeVec3( tmp );
180             sgScaleVec3( tmp, 0.5f );
181             sgAddVec3( tmp, zero_normal );
182
183             *ptr++ = (unsigned char)( tmp[ 0 ] * 255 );
184             *ptr++ = (unsigned char)( tmp[ 1 ] * 255 );
185             *ptr++ = (unsigned char)( tmp[ 2 ] * 255 );
186         }
187     }
188     glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
189                   0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
190 }
191
192
193 // Constructor
194 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
195     vertices(0),
196     indices(0),
197     layer_root(new ssgRoot),
198     layer_transform(new ssgTransform),
199     state_sel(0),
200     cloud_alpha(1.0),
201     texture_path(tex_path),
202     layer_span(0.0),
203     layer_asl(0.0),
204     layer_thickness(0.0),
205     layer_transition(0.0),
206     layer_coverage(SG_CLOUD_CLEAR),
207     scale(4000.0),
208     speed(0.0),
209     direction(0.0),
210     last_lon(0.0),
211     last_lat(0.0)
212 {
213     cl[0] = cl[1] = cl[2] = cl[3] = NULL;
214     vl[0] = vl[1] = vl[2] = vl[3] = NULL;
215     tl[0] = tl[1] = tl[2] = tl[3] = NULL;
216     layer[0] = layer[1] = layer[2] = layer[3] = NULL;
217
218     layer_root->addKid(layer_transform);
219         layer3D = new SGCloudField;
220     rebuild();
221 }
222
223 // Destructor
224 SGCloudLayer::~SGCloudLayer()
225 {
226         delete layer3D;
227     delete vertices;
228     delete indices;
229     delete layer_root;          // deletes layer_transform and layer as well
230 }
231
232 float
233 SGCloudLayer::getSpan_m () const
234 {
235     return layer_span;
236 }
237
238 void
239 SGCloudLayer::setSpan_m (float span_m)
240 {
241     if (span_m != layer_span) {
242         layer_span = span_m;
243         rebuild();
244     }
245 }
246
247 float
248 SGCloudLayer::getElevation_m () const
249 {
250     return layer_asl;
251 }
252
253 void
254 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
255 {
256     layer_asl = elevation_m;
257
258     if (set_span) {
259         if (elevation_m > 4000)
260             setSpan_m(  elevation_m * 10 );
261         else
262             setSpan_m( 40000 );
263     }
264 }
265
266 float
267 SGCloudLayer::getThickness_m () const
268 {
269     return layer_thickness;
270 }
271
272 void
273 SGCloudLayer::setThickness_m (float thickness_m)
274 {
275     layer_thickness = thickness_m;
276 }
277
278 float
279 SGCloudLayer::getTransition_m () const
280 {
281     return layer_transition;
282 }
283
284 void
285 SGCloudLayer::setTransition_m (float transition_m)
286 {
287     layer_transition = transition_m;
288 }
289
290 SGCloudLayer::Coverage
291 SGCloudLayer::getCoverage () const
292 {
293     return layer_coverage;
294 }
295
296 void
297 SGCloudLayer::setCoverage (Coverage coverage)
298 {
299     if (coverage != layer_coverage) {
300         layer_coverage = coverage;
301         rebuild();
302     }
303 }
304
305
306 // build the cloud object
307 void
308 SGCloudLayer::rebuild()
309 {
310     // Initialize states and sizes if necessary.
311     if ( !state_initialized ) { 
312         state_initialized = true;
313
314         SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
315
316         bump_mapping = SGIsOpenGLExtensionSupported("GL_ARB_multitexture") &&
317                        SGIsOpenGLExtensionSupported("GL_ARB_texture_cube_map") &&
318                        SGIsOpenGLExtensionSupported("GL_ARB_texture_env_combine") &&
319                        SGIsOpenGLExtensionSupported("GL_ARB_texture_env_dot3") && 
320                        SGIsOpenGLExtensionSupported("GL_ARB_imaging");
321
322         if ( bump_mapping ) {
323             glGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &nb_texture_unit );
324             if ( nb_texture_unit < 2 ) {
325                 bump_mapping = false;
326             }
327             //nb_texture_unit = 2; // Force the number of units for now
328         }
329
330         if ( bump_mapping ) {
331
332             // This bump mapping code was inspired by the tutorial available at 
333             // http://www.paulsprojects.net/tutorials/simplebump/simplebump.html
334             // and a NVidia white paper 
335             //  http://developer.nvidia.com/object/bumpmappingwithregistercombiners.html
336             // The normal map textures were generated by the normal map Gimp plugin :
337             //  http://nifelheim.dyndns.org/~cocidius/normalmap/
338             //
339             SGPath cloud_path;
340
341             glActiveTexturePtr = (glActiveTextureProc)SGLookupFunction("glActiveTextureARB");
342             glClientActiveTexturePtr = (glClientActiveTextureProc)SGLookupFunction("glClientActiveTextureARB");
343             glBlendColorPtr = (glBlendColorProc)SGLookupFunction("glBlendColor");
344
345             cloud_path.set(texture_path.str());
346             cloud_path.append("overcast.rgb");
347             color_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
348             color_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
349             cloud_path.set(texture_path.str());
350             cloud_path.append("overcast_n.rgb");
351             normal_map[ SG_CLOUD_OVERCAST ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
352             normal_map[ SG_CLOUD_OVERCAST ][ 0 ]->ref();
353
354             cloud_path.set(texture_path.str());
355             cloud_path.append("overcast_top.rgb");
356             color_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
357             color_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
358             cloud_path.set(texture_path.str());
359             cloud_path.append("overcast_top_n.rgb");
360             normal_map[ SG_CLOUD_OVERCAST ][ 1 ] = new ssgTexture( cloud_path.str().c_str() );
361             normal_map[ SG_CLOUD_OVERCAST ][ 1 ]->ref();
362
363             cloud_path.set(texture_path.str());
364             cloud_path.append("broken.rgba");
365             color_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
366             color_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
367             cloud_path.set(texture_path.str());
368             cloud_path.append("broken_n.rgb");
369             normal_map[ SG_CLOUD_BROKEN ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
370             normal_map[ SG_CLOUD_BROKEN ][ 0 ]->ref();
371
372             cloud_path.set(texture_path.str());
373             cloud_path.append("scattered.rgba");
374             color_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
375             color_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
376             cloud_path.set(texture_path.str());
377             cloud_path.append("scattered_n.rgb");
378             normal_map[ SG_CLOUD_SCATTERED ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
379             normal_map[ SG_CLOUD_SCATTERED ][ 0 ]->ref();
380
381             cloud_path.set(texture_path.str());
382             cloud_path.append("few.rgba");
383             color_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
384             color_map[ SG_CLOUD_FEW ][ 0 ]->ref();
385             cloud_path.set(texture_path.str());
386             cloud_path.append("few_n.rgb");
387             normal_map[ SG_CLOUD_FEW ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
388             normal_map[ SG_CLOUD_FEW ][ 0 ]->ref();
389
390             cloud_path.set(texture_path.str());
391             cloud_path.append("cirrus.rgba");
392             color_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
393             color_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
394             cloud_path.set(texture_path.str());
395             cloud_path.append("cirrus_n.rgb");
396             normal_map[ SG_CLOUD_CIRRUS ][ 0 ] = new ssgTexture( cloud_path.str().c_str() );
397             normal_map[ SG_CLOUD_CIRRUS ][ 0 ]->ref();
398
399             glGenTextures( 1, &normalization_cube_map );
400             glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
401             generateNormalizationCubeMap();
402             glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
403             glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
404             glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
405             glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
406             glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
407         } /* else */ {
408             SGPath cloud_path;
409             ssgStateSelector *state_sel;
410             ssgSimpleState *state;
411
412             state_sel = new ssgStateSelector( 2 );
413             state_sel->ref();
414             cloud_path.set(texture_path.str());
415             cloud_path.append("overcast.rgb");
416             state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
417             cloud_path.set(texture_path.str());
418             cloud_path.append("overcast_top.rgb");
419             state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
420             layer_states[SG_CLOUD_OVERCAST] = state_sel;
421
422             state_sel = new ssgStateSelector( 2 );
423             state_sel->ref();
424             cloud_path.set(texture_path.str());
425             cloud_path.append("broken.rgba");
426             state = sgCloudMakeState(cloud_path.str());
427             state_sel->setStep( 0, state );
428             state_sel->setStep( 1, state );
429             layer_states[SG_CLOUD_BROKEN] = state_sel;
430
431             state_sel = new ssgStateSelector( 2 );
432             state_sel->ref();
433             cloud_path.set(texture_path.str());
434             cloud_path.append("scattered.rgba");
435             state = sgCloudMakeState(cloud_path.str());
436             state_sel->setStep( 0, state );
437             state_sel->setStep( 1, state );
438             layer_states[SG_CLOUD_SCATTERED] = state_sel;
439
440             state_sel = new ssgStateSelector( 2 );
441             state_sel->ref();
442             cloud_path.set(texture_path.str());
443             cloud_path.append("few.rgba");
444             state = sgCloudMakeState(cloud_path.str());
445             state_sel->setStep( 0, state );
446             state_sel->setStep( 1, state );
447             layer_states[SG_CLOUD_FEW] = state_sel;
448
449             state_sel = new ssgStateSelector( 2 );
450             state_sel->ref();
451             cloud_path.set(texture_path.str());
452             cloud_path.append("cirrus.rgba");
453             state = sgCloudMakeState(cloud_path.str());
454             state_sel->setStep( 0, state );
455             state_sel->setStep( 1, state );
456             layer_states[SG_CLOUD_CIRRUS] = state_sel;
457
458             layer_states[SG_CLOUD_CLEAR] = 0;
459         }
460                 SGNewCloud::loadTextures(texture_path.str());
461                 layer3D->buildTestLayer();
462     }
463
464     if ( bump_mapping ) {
465
466         if ( !vertices ) {
467             vertices = new CloudVertex[ 25 ];
468             indices = new unsigned int[ 40 ];
469         }
470
471         sgVec2 base;
472         sgSetVec2( base, sg_random(), sg_random() );
473
474         const float layer_scale = layer_span / scale;
475         const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
476         const float half_angle = 0.5 * layer_span / layer_to_core;
477
478         int i;
479         for ( i = -2; i <= 2; i++ ) {
480             for ( int j = -2; j <= 2; j++ ) {
481                 CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
482                 sgSetVec3( v1.position,
483                            0.5 * i * layer_span,
484                            0.5 * j * layer_span,
485                            -layer_to_core * ( 1 - cos( i * half_angle ) * cos( j * half_angle ) ) );
486                 sgSetVec2( v1.texCoord,
487                            base[0] + layer_scale * i * 0.25,
488                            base[1] + layer_scale * j * 0.25 );
489                 sgSetVec3( v1.sTangent,
490                            cos( i * half_angle ),
491                            0.f,
492                            -sin( i * half_angle ) );
493                 sgSetVec3( v1.tTangent,
494                            0.f,
495                            cos( j * half_angle ),
496                            -sin( j * half_angle ) );
497                 sgVectorProductVec3( v1.normal, v1.tTangent, v1.sTangent );
498                 sgSetVec4( v1.color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : cloud_alpha * 0.15f );
499             }
500         }
501         /*
502          * 0 1 5 6 10 11 15 16 20 21
503          * 1 2 6 7 11 12 16 17 21 22
504          * 2 3 7 8 12 13 17 18 22 23
505          * 3 4 8 9 13 14 18 19 23 24
506          */
507         for ( i = 0; i < 4; i++ ) {
508             for ( int j = 0; j < 5; j++ ) {
509                 indices[ i*10 + (j*2) ]     =     i + 5 * j;
510                 indices[ i*10 + (j*2) + 1 ] = 1 + i + 5 * j;
511             }
512         }
513
514     } /* else */ {
515
516         scale = 4000.0;
517         last_lon = last_lat = -999.0f;
518
519         sgVec2 base;
520         sgSetVec2( base, sg_random(), sg_random() );
521
522         // build the cloud layer
523         sgVec4 color;
524         sgVec3 vertex;
525         sgVec2 tc;
526
527         const float layer_scale = layer_span / scale;
528         const float mpi = SG_PI/4;
529
530         // caclculate the difference between a flat-earth model and 
531         // a round earth model given the span and altutude ASL of
532         // the cloud layer. This is the difference in altitude between
533         // the top of the inverted bowl and the edge of the bowl.
534         // const float alt_diff = layer_asl * 0.8;
535         const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
536         const float layer_angle = 0.5*layer_span / layer_to_core; // The angle is half the span
537         const float border_to_core = layer_to_core * cos(layer_angle);
538         const float alt_diff = layer_to_core - border_to_core;
539
540         for (int i = 0; i < 4; i++)
541         {
542             if ( layer[i] != NULL ) {
543                 layer_transform->removeKid(layer[i]); // automatic delete
544             }
545
546             vl[i] = new ssgVertexArray( 10 );
547             cl[i] = new ssgColourArray( 10 );
548             tl[i] = new ssgTexCoordArray( 10 );
549
550
551             sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
552                             alt_diff * (sin(i*mpi) - 2) );
553
554             sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
555
556             sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
557
558             cl[i]->add( color );
559             vl[i]->add( vertex );
560             tl[i]->add( tc );
561
562             for (int j = 0; j < 4; j++)
563             {
564                 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
565                                 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
566
567                 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
568                             base[1] + layer_scale * j/4 );
569
570                 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
571                                 ( (j == 0) || (i == 3)) ?  
572                                 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
573
574                 cl[i]->add( color );
575                 vl[i]->add( vertex );
576                 tl[i]->add( tc );
577
578
579                 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
580                                 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
581
582                 sgSetVec2( tc, base[0] + layer_scale * i/4,
583                             base[1] + layer_scale * (j+1)/4 );
584
585                 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
586                                 ((j == 3) || (i == 0)) ?
587                                 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
588                 cl[i]->add( color );
589                 vl[i]->add( vertex );
590                 tl[i]->add( tc );
591             }
592
593             sgSetVec3( vertex, layer_span*(i-1)/2, layer_span, 
594                             alt_diff * (sin((i+1)*mpi) - 2) );
595
596             sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
597                         base[1] + layer_scale );
598
599             sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
600
601             cl[i]->add( color );
602             vl[i]->add( vertex );
603             tl[i]->add( tc );
604
605             layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
606             layer_transform->addKid( layer[i] );
607
608             if ( layer_states[layer_coverage] != NULL ) {
609                 layer[i]->setState( layer_states[layer_coverage] );
610             }
611             state_sel = layer_states[layer_coverage];
612         }
613
614         // force a repaint of the sky colors with arbitrary defaults
615         repaint( color );
616     }
617 }
618
619
620 // repaint the cloud layer colors
621 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
622
623     if ( bump_mapping && enable_bump_mapping ) {
624
625         for ( int i = 0; i < 25; i++ ) {
626             sgCopyVec3( vertices[ i ].color, fog_color );
627         }
628
629     } else {
630         float *color;
631
632         for ( int i = 0; i < 4; i++ ) {
633             color = cl[i]->get( 0 );
634             sgCopyVec3( color, fog_color );
635             color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
636
637             for ( int j = 0; j < 4; ++j ) {
638                 color = cl[i]->get( (2*j) + 1 );
639                 sgCopyVec3( color, fog_color );
640                 color[3] = 
641                     ((j == 0) || (i == 3)) ?
642                     ((j == 0) && (i == 3)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
643
644                 color = cl[i]->get( (2*j) + 2 );
645                 sgCopyVec3( color, fog_color );
646                 color[3] = 
647                     ((j == 3) || (i == 0)) ?
648                     ((j == 3) && (i == 0)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
649             }
650
651             color = cl[i]->get( 9 );
652             sgCopyVec3( color, fog_color );
653             color[3] = (i == 3) ? 0.0f : cloud_alpha * 0.15f;
654         }
655     }
656
657     return true;
658 }
659
660 // reposition the cloud layer at the specified origin and orientation
661 // lon specifies a rotation about the Z axis
662 // lat specifies a rotation about the new Y axis
663 // spin specifies a rotation about the new Z axis (and orients the
664 // sunrise/set effects
665 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
666                                double alt, double dt )
667 {
668     sgMat4 T1, LON, LAT;
669     sgVec3 axis;
670
671     // combine p and asl (meters) to get translation offset
672     sgVec3 asl_offset;
673     sgCopyVec3( asl_offset, up );
674     sgNormalizeVec3( asl_offset );
675     if ( alt <= layer_asl ) {
676         sgScaleVec3( asl_offset, layer_asl );
677     } else {
678         sgScaleVec3( asl_offset, layer_asl + layer_thickness );
679     }
680     // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
681     //      << "," << asl_offset[2] << endl;
682     sgAddVec3( asl_offset, p );
683     // cout << "  asl_offset = " << asl_offset[0] << "," << asl_offset[1]
684     //      << "," << asl_offset[2] << endl;
685
686     // Translate to zero elevation
687     // Point3D zero_elev = current_view.get_cur_zero_elev();
688     sgMakeTransMat4( T1, asl_offset );
689
690     // printf("  Translated to %.2f %.2f %.2f\n", 
691     //        zero_elev.x, zero_elev.y, zero_elev.z );
692
693     // Rotate to proper orientation
694     // printf("  lon = %.2f  lat = %.2f\n", 
695     //        lon * SGD_RADIANS_TO_DEGREES,
696     //        lat * SGD_RADIANS_TO_DEGREES);
697     sgSetVec3( axis, 0.0, 0.0, 1.0 );
698     sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
699
700     sgSetVec3( axis, 0.0, 1.0, 0.0 );
701     sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
702
703     sgMat4 TRANSFORM;
704
705     sgCopyMat4( TRANSFORM, T1 );
706     sgPreMultMat4( TRANSFORM, LON );
707     sgPreMultMat4( TRANSFORM, LAT );
708
709     sgCoord layerpos;
710     sgSetCoord( &layerpos, TRANSFORM );
711
712     layer_transform->setTransform( &layerpos );
713
714     // now calculate update texture coordinates
715     if ( last_lon < -900 ) {
716         last_lon = lon;
717         last_lat = lat;
718     }
719
720     double sp_dist = speed*dt;
721
722     if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
723         Point3D start( last_lon, last_lat, 0.0 );
724         Point3D dest( lon, lat, 0.0 );
725         double course = 0.0, dist = 0.0;
726
727         calc_gc_course_dist( dest, start, &course, &dist );
728         // cout << "course = " << course << ", dist = " << dist << endl;
729
730         // if start and dest are too close together,
731         // calc_gc_course_dist() can return a course of "nan".  If
732         // this happens, lets just use the last known good course.
733         // This is a hack, and it would probably be better to make
734         // calc_gc_course_dist() more robust.
735         if ( isnan(course) ) {
736             course = last_course;
737         } else {
738             last_course = course;
739         }
740
741         // calculate cloud movement due to external forces
742         double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
743
744         if (dist > 0.0) {
745             ax = cos(course) * dist;
746             ay = sin(course) * dist;
747         }
748
749         if (sp_dist > 0) {
750             bx = cos((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
751             by = sin((180.0-direction) * SGD_DEGREES_TO_RADIANS) * sp_dist;
752         }
753
754
755         double xoff = (ax + bx) / (2 * scale);
756         double yoff = (ay + by) / (2 * scale);
757
758         const float layer_scale = layer_span / scale;
759
760         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
761
762         float *base;
763         if ( bump_mapping && enable_bump_mapping ) {
764             base = vertices[12].texCoord;
765         } else {
766             base = tl[0]->get( 0 );
767         }
768         base[0] += xoff;
769
770         // the while loops can lead to *long* pauses if base[0] comes
771         // with a bogus value.
772         // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
773         // while ( base[0] < 0.0 ) { base[0] += 1.0; }
774         if ( base[0] > -10.0 && base[0] < 10.0 ) {
775             base[0] -= (int)base[0];
776         } else {
777             SG_LOG(SG_ASTRO, SG_DEBUG,
778                 "Error: base = " << base[0] << "," << base[1] <<
779                 " course = " << course << " dist = " << dist );
780             base[0] = 0.0;
781         }
782
783         base[1] += yoff;
784         // the while loops can lead to *long* pauses if base[0] comes
785         // with a bogus value.
786         // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
787         // while ( base[1] < 0.0 ) { base[1] += 1.0; }
788         if ( base[1] > -10.0 && base[1] < 10.0 ) {
789             base[1] -= (int)base[1];
790         } else {
791             SG_LOG(SG_ASTRO, SG_ALERT,
792                     "Error: base = " << base[0] << "," << base[1] <<
793                     " course = " << course << " dist = " << dist );
794             base[1] = 0.0;
795         }
796
797         if ( bump_mapping && enable_bump_mapping ) {
798
799             for ( int i = -2; i <= 2; i++ ) {
800                 for ( int j = -2; j <= 2; j++ ) {
801                     if ( i == 0 && j == 0 )
802                         continue; // Already done on base
803                     CloudVertex &v1 = vertices[ (i+2)*5 + (j+2) ];
804                     sgSetVec2( v1.texCoord,
805                             base[0] + layer_scale * i * 0.25,
806                             base[1] + layer_scale * j * 0.25 );
807                 }
808             }
809
810         } else {
811                 // cout << "base = " << base[0] << "," << base[1] << endl;
812
813             float *tc;
814             for (int i = 0; i < 4; i++) {
815                 tc = tl[i]->get( 0 );
816                 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
817                 
818                 for (int j = 0; j < 4; j++)
819                 {
820                     tc = tl[i]->get( j*2+1 );
821                     sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
822                                 base[1] + layer_scale * j/4 );
823         
824                     tc = tl[i]->get( (j+1)*2 );
825                     sgSetVec2( tc, base[0] + layer_scale * i/4,
826                                 base[1] + layer_scale * (j+1)/4 );
827                 }
828         
829                 tc = tl[i]->get( 9 );
830                 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
831                             base[1] + layer_scale );
832             }
833         }
834
835         last_lon = lon;
836         last_lat = lat;
837     }
838
839         layer3D->reposition( p, up, lon, lat, alt, dt, direction, speed);
840     return true;
841 }
842
843
844 void SGCloudLayer::draw( bool top ) {
845     if ( layer_coverage != SG_CLOUD_CLEAR ) {
846
847                 if ( SGCloudField::enable3D && layer3D->is3D())
848                         layer3D->Render();
849                 else
850         if ( bump_mapping && enable_bump_mapping ) {
851
852             sgMat4 modelview,
853                    tmp,
854                    transform;
855             ssgGetModelviewMatrix( modelview );
856             layer_transform->getTransform( transform );
857
858             sgTransposeNegateMat4( tmp, transform );
859
860             sgPostMultMat4( transform, modelview );
861             ssgLoadModelviewMatrix( transform );
862
863             sgVec3 lightVec;
864             ssgGetLight( 0 )->getPosition( lightVec );
865             sgNegateVec3( lightVec );
866             sgXformVec3( lightVec, tmp );
867
868             for ( int i = 0; i < 25; i++ ) {
869                 CloudVertex &v = vertices[ i ];
870                 sgSetVec3( v.tangentSpLight,
871                            sgScalarProductVec3( v.sTangent, lightVec ),
872                            sgScalarProductVec3( v.tTangent, lightVec ),
873                            sgScalarProductVec3( v.normal, lightVec ) );
874             }
875
876             ssgTexture *decal = color_map[ layer_coverage ][ top ? 1 : 0 ];
877             if ( top && decal == 0 ) {
878                 decal = color_map[ layer_coverage ][ 0 ];
879             }
880             ssgTexture *normal = normal_map[ layer_coverage ][ top ? 1 : 0 ];
881             if ( top && normal == 0 ) {
882                 normal = normal_map[ layer_coverage ][ 0 ];
883             }
884
885             glDisable( GL_LIGHTING );
886             glDisable( GL_CULL_FACE );
887 //            glDisable( GL_ALPHA_TEST );
888             if ( layer_coverage == SG_CLOUD_FEW ) {
889                 glEnable( GL_ALPHA_TEST );
890                 glAlphaFunc ( GL_GREATER, 0.01 );
891             }
892             glEnable( GL_BLEND ); 
893             glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
894
895             glShadeModel( GL_SMOOTH );
896             glEnable( GL_COLOR_MATERIAL ); 
897             sgVec4 color;
898             float emis = 0.05;
899             if ( 1 ) {
900                 ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
901                 emis = ( color[0]+color[1]+color[2] ) / 3.0;
902                 if ( emis < 0.05 )
903                     emis = 0.05;
904             }
905             sgSetVec4( color, emis, emis, emis, 0.0 );
906             glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, color );
907             sgSetVec4( color, 1.0f, 1.0f, 1.0f, 0.0 );
908             glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, color );
909             sgSetVec4( color, 1.0, 1.0, 1.0, 0.0 );
910             glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, color );
911             sgSetVec4( color, 0.0, 0.0, 0.0, 0.0 );
912             glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, color );
913
914             glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
915
916             glActiveTexturePtr( GL_TEXTURE0_ARB );
917             glBindTexture( GL_TEXTURE_2D, normal->getHandle() );
918             glEnable( GL_TEXTURE_2D );
919
920             //Bind normalisation cube map to texture unit 1
921             glActiveTexturePtr( GL_TEXTURE1_ARB );
922             glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, normalization_cube_map );
923             glEnable( GL_TEXTURE_CUBE_MAP_ARB );
924             glActiveTexturePtr( GL_TEXTURE0_ARB );
925
926             //Set vertex arrays for cloud
927             glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
928             glEnableClientState( GL_VERTEX_ARRAY );
929 /*
930             if ( nb_texture_unit >= 3 ) {
931                 glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
932                 glEnableClientState( GL_COLOR_ARRAY );
933             }
934 */
935             //Send texture coords for normal map to unit 0
936             glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
937             glEnableClientState( GL_TEXTURE_COORD_ARRAY );
938
939             //Send tangent space light vectors for normalisation to unit 1
940             glClientActiveTexturePtr( GL_TEXTURE1_ARB );
941             glTexCoordPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].tangentSpLight );
942             glEnableClientState( GL_TEXTURE_COORD_ARRAY );
943
944             //Set up texture environment to do (tex0 dot tex1)*color
945             glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
946             glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
947             glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE );
948             glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE );
949             glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
950
951             glActiveTexturePtr( GL_TEXTURE1_ARB );
952
953             glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
954             glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
955             glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB );
956             glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
957             glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS_ARB );
958             glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );
959
960             if ( nb_texture_unit >= 3 ) {
961                 glActiveTexturePtr( GL_TEXTURE2_ARB );
962                 glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
963
964                 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
965                 glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
966                 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
967
968                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
969                 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD );
970                 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE );
971                 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB );
972
973                 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
974                 glActiveTexturePtr( GL_TEXTURE0_ARB );
975
976                 //Draw cloud layer
977                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
978                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
979                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
980                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
981
982                 glDisable( GL_TEXTURE_2D );
983                 glActiveTexturePtr( GL_TEXTURE1_ARB );
984                 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
985                 glActiveTexturePtr( GL_TEXTURE2_ARB );
986                 glDisable( GL_TEXTURE_2D );
987                 glActiveTexturePtr( GL_TEXTURE0_ARB );
988
989                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
990                 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
991                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
992                 glClientActiveTexturePtr( GL_TEXTURE2_ARB );
993                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
994                 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
995
996                 glDisableClientState( GL_COLOR_ARRAY );
997                 glEnable( GL_LIGHTING );
998
999                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1000
1001             } else {
1002                 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1003                 glActiveTexturePtr( GL_TEXTURE0_ARB );
1004
1005                 //Draw cloud layer
1006                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1007                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1008                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1009                 glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1010
1011                 //Disable textures
1012                 glDisable( GL_TEXTURE_2D );
1013
1014                 glActiveTexturePtr( GL_TEXTURE1_ARB );
1015                 glDisable( GL_TEXTURE_CUBE_MAP_ARB );
1016                 glActiveTexturePtr( GL_TEXTURE0_ARB );
1017
1018                 //disable vertex arrays
1019                 glDisableClientState( GL_VERTEX_ARRAY );
1020
1021                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1022                 glClientActiveTexturePtr( GL_TEXTURE1_ARB );
1023                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1024                 glClientActiveTexturePtr( GL_TEXTURE0_ARB );
1025
1026                 //Return to standard modulate texenv
1027                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1028
1029                 if ( layer_coverage == SG_CLOUD_OVERCAST ) {
1030                     glDepthFunc(GL_LEQUAL);
1031
1032                     glEnable( GL_LIGHTING );
1033                     sgVec4 color;
1034                     ssgGetLight( 0 )->getColour( GL_DIFFUSE, color );
1035                     float average = ( color[0] + color[1] + color[2] ) / 3.0f;
1036                     average = 0.15 + average/10;
1037                     sgVec4 averageColor;
1038                     sgSetVec4( averageColor, average, average, average, 1.0f );
1039                     ssgGetLight( 0 )->setColour( GL_DIFFUSE, averageColor );
1040
1041                     glBlendColorPtr( average, average, average, 1.0f );
1042                     glBlendFunc( GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_COLOR );
1043
1044                     //Perform a second pass to color the torus
1045                     //Bind decal texture
1046                     glBindTexture( GL_TEXTURE_2D, decal->getHandle() );
1047                     glEnable(GL_TEXTURE_2D);
1048
1049                     //Set vertex arrays for torus
1050                     glVertexPointer( 3, GL_FLOAT, sizeof(CloudVertex), &vertices[0].position );
1051                     glEnableClientState( GL_VERTEX_ARRAY );
1052
1053                     //glColorPointer( 4, GL_FLOAT, sizeof(CloudVertex), &vertices[0].color );
1054                     //glEnableClientState( GL_COLOR_ARRAY );
1055
1056                     glNormalPointer( GL_FLOAT, sizeof(CloudVertex), &vertices[0].normal );
1057                     glEnableClientState( GL_NORMAL_ARRAY );
1058
1059                     glTexCoordPointer( 2, GL_FLOAT, sizeof(CloudVertex), &vertices[0].texCoord );
1060                     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1061
1062                     //Draw cloud layer
1063                     glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[0] );
1064                     glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[10] );
1065                     glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[20] );
1066                     glDrawElements( GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_INT, &indices[30] );
1067
1068                     ssgGetLight( 0 )->setColour( GL_DIFFUSE, color );
1069
1070                     glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1071                 }
1072             }
1073             //Disable texture
1074             glDisable( GL_TEXTURE_2D );
1075
1076             glDisableClientState( GL_VERTEX_ARRAY );
1077             glDisableClientState( GL_NORMAL_ARRAY );
1078
1079             glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1080             glEnable( GL_CULL_FACE );
1081             glDepthFunc(GL_LESS);
1082
1083             ssgLoadModelviewMatrix( modelview );
1084
1085         } else {
1086             state_sel->selectStep( top ? 1 : 0 );
1087             ssgCullAndDraw( layer_root );
1088         }
1089     }
1090 }
1091
1092
1093 // make an ssgSimpleState for a cloud layer given the named texture
1094 ssgSimpleState *sgCloudMakeState( const string &path ) {
1095     ssgSimpleState *state = new ssgSimpleState();
1096
1097     SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
1098
1099     state->setTexture( (char *)path.c_str() );
1100     state->setShadeModel( GL_SMOOTH );
1101     state->disable( GL_LIGHTING );
1102     state->disable( GL_CULL_FACE );
1103     state->enable( GL_TEXTURE_2D );
1104     state->enable( GL_COLOR_MATERIAL );
1105     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
1106     state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
1107     state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
1108     state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
1109     state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
1110     state->enable( GL_BLEND );
1111     state->enable( GL_ALPHA_TEST );
1112     state->setAlphaClamp( 0.01 );
1113
1114     return state;
1115 }