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