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