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