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