]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloud.cxx
These test should not be needed when properly using OpnGL colors.
[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
42 #include "cloud.hxx"
43
44
45 static ssgSimpleState *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
46 static bool state_initialized = false;
47
48
49 // Constructor
50 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
51     layer_root(new ssgRoot),
52     layer_transform(new ssgTransform),
53     texture_path(tex_path),
54     layer_span(0.0),
55     layer_asl(0.0),
56     layer_thickness(0.0),
57     layer_transition(0.0),
58     layer_coverage(SG_CLOUD_CLEAR),
59     scale(4000.0),
60     speed(0.0),
61     direction(0.0),
62     last_lon(0.0),
63     last_lat(0.0)
64 {
65     cl[0] = cl[1] = cl[2] = cl[3] = NULL;
66     vl[0] = vl[1] = vl[2] = vl[3] = NULL;
67     tl[0] = tl[1] = tl[2] = tl[3] = NULL;
68     layer[0] = layer[1] = layer[2] = layer[3] = NULL;
69
70     layer_root->addKid(layer_transform);
71     rebuild();
72 }
73
74 // Destructor
75 SGCloudLayer::~SGCloudLayer()
76 {
77     delete layer_root;          // deletes layer_transform and layer as well
78 }
79
80 float
81 SGCloudLayer::getSpan_m () const
82 {
83     return layer_span;
84 }
85
86 void
87 SGCloudLayer::setSpan_m (float span_m)
88 {
89     if (span_m != layer_span) {
90         layer_span = span_m;
91         rebuild();
92     }
93 }
94
95 float
96 SGCloudLayer::getElevation_m () const
97 {
98     return layer_asl;
99 }
100
101 void
102 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
103 {
104     layer_asl = elevation_m;
105
106     if (set_span) {
107         if (elevation_m > 4000)
108             setSpan_m(  elevation_m * 10 );
109         else
110             setSpan_m( 40000 );
111     }
112 }
113
114 float
115 SGCloudLayer::getThickness_m () const
116 {
117     return layer_thickness;
118 }
119
120 void
121 SGCloudLayer::setThickness_m (float thickness_m)
122 {
123     layer_thickness = thickness_m;
124 }
125
126 float
127 SGCloudLayer::getTransition_m () const
128 {
129     return layer_transition;
130 }
131
132 void
133 SGCloudLayer::setTransition_m (float transition_m)
134 {
135     layer_transition = transition_m;
136 }
137
138 SGCloudLayer::Coverage
139 SGCloudLayer::getCoverage () const
140 {
141     return layer_coverage;
142 }
143
144 void
145 SGCloudLayer::setCoverage (Coverage coverage)
146 {
147     if (coverage != layer_coverage) {
148         layer_coverage = coverage;
149         rebuild();
150     }
151 }
152
153
154 // build the cloud object
155 void
156 SGCloudLayer::rebuild()
157 {
158     // Initialize states and sizes if necessary.
159     if ( !state_initialized ) { 
160         state_initialized = true;
161
162         SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
163
164         SGPath cloud_path;
165
166         cloud_path.set(texture_path.str());
167         cloud_path.append("overcast.rgb");
168         layer_states[SG_CLOUD_OVERCAST] = sgCloudMakeState(cloud_path.str());
169
170         cloud_path.set(texture_path.str());
171         cloud_path.append("broken.rgba");
172         layer_states[SG_CLOUD_BROKEN]
173             = sgCloudMakeState(cloud_path.str());
174
175         cloud_path.set(texture_path.str());
176         cloud_path.append("scattered.rgba");
177         layer_states[SG_CLOUD_SCATTERED]
178             = sgCloudMakeState(cloud_path.str());
179
180         cloud_path.set(texture_path.str());
181         cloud_path.append("few.rgba");
182         layer_states[SG_CLOUD_FEW]
183             = sgCloudMakeState(cloud_path.str());
184
185         cloud_path.set(texture_path.str());
186         cloud_path.append("cirrus.rgba");
187         layer_states[SG_CLOUD_CIRRUS]
188             = sgCloudMakeState(cloud_path.str());
189
190         layer_states[SG_CLOUD_CLEAR] = 0;
191     }
192
193     scale = 4000.0;
194     last_lon = last_lat = -999.0f;
195
196     sgVec2 base;
197     sgSetVec2( base, sg_random(), sg_random() );
198
199     // build the cloud layer
200     sgVec4 color;
201     sgVec3 vertex;
202     sgVec2 tc;
203
204     const float layer_scale = layer_span / scale;
205     const float mpi = SG_PI/4;
206     const float alt_diff = layer_asl * 0.8;
207
208     for (int i = 0; i < 4; i++)
209     {
210         if ( layer[i] != NULL ) {
211             layer_transform->removeKid(layer[i]); // automatic delete
212         }
213
214         vl[i] = new ssgVertexArray( 10 );
215         cl[i] = new ssgColourArray( 10 );
216         tl[i] = new ssgTexCoordArray( 10 );
217
218
219         sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
220                            alt_diff * (sin(i*mpi) - 2) );
221
222         sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
223
224         sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
225
226         cl[i]->add( color );
227         vl[i]->add( vertex );
228         tl[i]->add( tc );
229
230         for (int j = 0; j < 4; j++)
231         {
232             sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
233                                alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
234
235             sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
236                            base[1] + layer_scale * j/4 );
237
238             sgSetVec4( color, 1.0f, 1.0f, 1.0f,
239                               ( (j == 0) || (i == 3)) ?  
240                               ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
241
242             cl[i]->add( color );
243             vl[i]->add( vertex );
244             tl[i]->add( tc );
245
246
247             sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
248                                alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
249
250             sgSetVec2( tc, base[0] + layer_scale * i/4,
251                            base[1] + layer_scale * (j+1)/4 );
252
253             sgSetVec4( color, 1.0f, 1.0f, 1.0f,
254                               ((j == 3) || (i == 0)) ?
255                               ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
256             cl[i]->add( color );
257             vl[i]->add( vertex );
258             tl[i]->add( tc );
259         }
260
261         sgSetVec3( vertex, layer_span*(i-1)/2, layer_span, 
262                            alt_diff * (sin((i+1)*mpi) - 2) );
263
264         sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
265                        base[1] + layer_scale );
266
267         sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
268
269         cl[i]->add( color );
270         vl[i]->add( vertex );
271         tl[i]->add( tc );
272
273         layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
274         layer_transform->addKid( layer[i] );
275
276         if ( layer_states[layer_coverage] != NULL ) {
277             layer[i]->setState( layer_states[layer_coverage] );
278         }
279     }
280
281     // force a repaint of the sky colors with arbitrary defaults
282     repaint( color );
283
284 }
285
286
287 // repaint the cloud layer colors
288 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
289     float *color;
290
291     for ( int i = 0; i < 4; i++ )
292         for ( int j = 0; j < 10; ++j ) {
293             color = cl[i]->get( j );
294             sgCopyVec3( color, fog_color );
295         }
296
297     return true;
298 }
299
300
301 // reposition the cloud layer at the specified origin and orientation
302 // lon specifies a rotation about the Z axis
303 // lat specifies a rotation about the new Y axis
304 // spin specifies a rotation about the new Z axis (and orients the
305 // sunrise/set effects
306 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
307                                double alt, double dt )
308 {
309     sgMat4 T1, LON, LAT;
310     sgVec3 axis;
311
312     // combine p and asl (meters) to get translation offset
313     sgVec3 asl_offset;
314     sgCopyVec3( asl_offset, up );
315     sgNormalizeVec3( asl_offset );
316     if ( alt <= layer_asl ) {
317         sgScaleVec3( asl_offset, layer_asl );
318     } else {
319         sgScaleVec3( asl_offset, layer_asl + layer_thickness );
320     }
321     // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
322     //      << "," << asl_offset[2] << endl;
323     sgAddVec3( asl_offset, p );
324     // cout << "  asl_offset = " << asl_offset[0] << "," << asl_offset[1]
325     //      << "," << asl_offset[2] << endl;
326
327     // Translate to zero elevation
328     // Point3D zero_elev = current_view.get_cur_zero_elev();
329     // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
330     sgMakeTransMat4( T1, asl_offset );
331
332     // printf("  Translated to %.2f %.2f %.2f\n", 
333     //        zero_elev.x, zero_elev.y, zero_elev.z );
334
335     // Rotate to proper orientation
336     // printf("  lon = %.2f  lat = %.2f\n", 
337     //        lon * SGD_RADIANS_TO_DEGREES,
338     //        lat * SGD_RADIANS_TO_DEGREES);
339     // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
340     sgSetVec3( axis, 0.0, 0.0, 1.0 );
341     sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
342
343     // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
344     //             0.0, 1.0, 0.0 );
345     sgSetVec3( axis, 0.0, 1.0, 0.0 );
346     sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
347
348     sgMat4 TRANSFORM;
349
350     sgCopyMat4( TRANSFORM, T1 );
351     sgPreMultMat4( TRANSFORM, LON );
352     sgPreMultMat4( TRANSFORM, LAT );
353
354     sgCoord layerpos;
355     sgSetCoord( &layerpos, TRANSFORM );
356
357     layer_transform->setTransform( &layerpos );
358
359     // now calculate update texture coordinates
360     if ( last_lon < -900 ) {
361         last_lon = lon;
362         last_lat = lat;
363     }
364
365     double sp_dist = speed*dt;
366
367     if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
368         Point3D start( last_lon, last_lat, 0.0 );
369         Point3D dest( lon, lat, 0.0 );
370         double course = 0.0, dist = 0.0;
371
372         calc_gc_course_dist( dest, start, &course, &dist );
373         // cout << "course = " << course << ", dist = " << dist << endl;
374
375         // if start and dest are too close together,
376         // calc_gc_course_dist() can return a course of "nan".  If
377         // this happens, lets just use the last known good course.
378         // This is a hack, and it would probably be better to make
379         // calc_gc_course_dist() more robust.
380         if ( isnan(course) ) {
381             course = last_course;
382         } else {
383             last_course = course;
384         }
385
386         // calculate cloud movement due to external forces
387         double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
388
389         if (dist > 0.0) {
390             ax = cos(course) * dist;
391             ay = sin(course) * dist;
392         }
393
394         if (sp_dist > 0) {
395             bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
396             by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
397         }
398
399
400         double xoff = (ax + bx) / (2 * scale);
401         double yoff = (ay + by) / (2 * scale);
402
403         const float layer_scale = layer_span / scale;
404
405         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
406
407         float *base, *tc;
408
409         base = tl[0]->get( 0 );
410         base[0] += xoff;
411
412         // the while loops can lead to *long* pauses if base[0] comes
413         // with a bogus value.
414         // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
415         // while ( base[0] < 0.0 ) { base[0] += 1.0; }
416         if ( base[0] > -10.0 && base[0] < 10.0 ) {
417             base[0] -= (int)base[0];
418         } else {
419             SG_LOG(SG_ASTRO, SG_DEBUG,
420                    "Error: base = " << base[0] << "," << base[1] <<
421                    " course = " << course << " dist = " << dist );
422             base[0] = 0.0;
423         }
424
425         base[1] += yoff;
426         // the while loops can lead to *long* pauses if base[0] comes
427         // with a bogus value.
428         // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
429         // while ( base[1] < 0.0 ) { base[1] += 1.0; }
430         if ( base[1] > -10.0 && base[1] < 10.0 ) {
431            base[1] -= (int)base[1];
432         } else {
433            SG_LOG(SG_ASTRO, SG_ALERT,
434                   "Error: base = " << base[0] << "," << base[1] <<
435                   " course = " << course << " dist = " << dist );
436            base[1] = 0.0;
437         }
438
439         // cout << "base = " << base[0] << "," << base[1] << endl;
440
441         for (int i = 0; i < 4; i++)
442         {
443             tc = tl[i]->get( 0 );
444             sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
445             
446             for (int j = 0; j < 4; j++)
447             {
448                 tc = tl[i]->get( j*2+1 );
449                 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
450                                base[1] + layer_scale * j/4 );
451  
452                 tc = tl[i]->get( (j+1)*2 );
453                 sgSetVec2( tc, base[0] + layer_scale * i/4,
454                                base[1] + layer_scale * (j+1)/4 );
455             }
456  
457             tc = tl[i]->get( 9 );
458             sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
459                            base[1] + layer_scale );
460         }
461  
462         last_lon = lon;
463         last_lat = lat;
464     }
465
466     return true;
467 }
468
469
470 void SGCloudLayer::draw() {
471     if ( layer_coverage != SG_CLOUD_CLEAR ) {
472         ssgCullAndDraw( layer_root );
473     }
474 }
475
476
477 // make an ssgSimpleState for a cloud layer given the named texture
478 ssgSimpleState *sgCloudMakeState( const string &path ) {
479     ssgSimpleState *state = new ssgSimpleState();
480
481     SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
482
483     state->setTexture( (char *)path.c_str() );
484     state->setShadeModel( GL_SMOOTH );
485     state->disable( GL_LIGHTING );
486     state->disable( GL_CULL_FACE );
487     state->enable( GL_TEXTURE_2D );
488     state->enable( GL_COLOR_MATERIAL );
489     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
490     state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
491     state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
492     state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
493     state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
494     state->enable( GL_BLEND );
495     state->enable( GL_ALPHA_TEST );
496     state->setAlphaClamp( 0.01 );
497
498     // ref() the state so it doesn't get deleted if the last layer of
499     // it's type is deleted.
500     state->ref();
501
502     return state;
503 }