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