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