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