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