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