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