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