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