]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloud.cxx
309572314c4b4d0717277766b10d39bb347f32cc
[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
207     // caclculate the difference between a flat-earth model and 
208     // a round earth model given the span and altutude ASL of
209     // the cloud layer. This is the difference in altitude between
210     // the top of the inverted bowl and the edge of the bowl.
211     // const float alt_diff = layer_asl * 0.8;
212     const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
213     const float layer_angle = acos( 0.5*layer_span / layer_to_core);
214     const float border_to_core = layer_to_core * sin(layer_angle);
215     const float alt_diff = layer_to_core - border_to_core;
216
217     for (int i = 0; i < 4; i++)
218     {
219         if ( layer[i] != NULL ) {
220             layer_transform->removeKid(layer[i]); // automatic delete
221         }
222
223         vl[i] = new ssgVertexArray( 10 );
224         cl[i] = new ssgColourArray( 10 );
225         tl[i] = new ssgTexCoordArray( 10 );
226
227
228         sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
229                            alt_diff * (sin(i*mpi) - 2) );
230
231         sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
232
233         sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
234
235         cl[i]->add( color );
236         vl[i]->add( vertex );
237         tl[i]->add( tc );
238
239         for (int j = 0; j < 4; j++)
240         {
241             sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
242                                alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
243
244             sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
245                            base[1] + layer_scale * j/4 );
246
247             sgSetVec4( color, 1.0f, 1.0f, 1.0f,
248                               ( (j == 0) || (i == 3)) ?  
249                               ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
250
251             cl[i]->add( color );
252             vl[i]->add( vertex );
253             tl[i]->add( tc );
254
255
256             sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
257                                alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
258
259             sgSetVec2( tc, base[0] + layer_scale * i/4,
260                            base[1] + layer_scale * (j+1)/4 );
261
262             sgSetVec4( color, 1.0f, 1.0f, 1.0f,
263                               ((j == 3) || (i == 0)) ?
264                               ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
265             cl[i]->add( color );
266             vl[i]->add( vertex );
267             tl[i]->add( tc );
268         }
269
270         sgSetVec3( vertex, layer_span*(i-1)/2, layer_span, 
271                            alt_diff * (sin((i+1)*mpi) - 2) );
272
273         sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
274                        base[1] + layer_scale );
275
276         sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
277
278         cl[i]->add( color );
279         vl[i]->add( vertex );
280         tl[i]->add( tc );
281
282         layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
283         layer_transform->addKid( layer[i] );
284
285         if ( layer_states[layer_coverage] != NULL ) {
286             layer[i]->setState( layer_states[layer_coverage] );
287         }
288     }
289
290     // force a repaint of the sky colors with arbitrary defaults
291     repaint( color );
292
293 }
294
295
296 // repaint the cloud layer colors
297 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
298     float *color;
299
300     for ( int i = 0; i < 4; i++ )
301         for ( int j = 0; j < 10; ++j ) {
302             color = cl[i]->get( j );
303             sgCopyVec3( color, fog_color );
304         }
305
306     return true;
307 }
308
309
310 // reposition the cloud layer at the specified origin and orientation
311 // lon specifies a rotation about the Z axis
312 // lat specifies a rotation about the new Y axis
313 // spin specifies a rotation about the new Z axis (and orients the
314 // sunrise/set effects
315 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
316                                double alt, double dt )
317 {
318     sgMat4 T1, LON, LAT;
319     sgVec3 axis;
320
321     // combine p and asl (meters) to get translation offset
322     sgVec3 asl_offset;
323     sgCopyVec3( asl_offset, up );
324     sgNormalizeVec3( asl_offset );
325     if ( alt <= layer_asl ) {
326         sgScaleVec3( asl_offset, layer_asl );
327     } else {
328         sgScaleVec3( asl_offset, layer_asl + layer_thickness );
329     }
330     // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
331     //      << "," << asl_offset[2] << endl;
332     sgAddVec3( asl_offset, p );
333     // cout << "  asl_offset = " << asl_offset[0] << "," << asl_offset[1]
334     //      << "," << asl_offset[2] << endl;
335
336     // Translate to zero elevation
337     // Point3D zero_elev = current_view.get_cur_zero_elev();
338     // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
339     sgMakeTransMat4( T1, asl_offset );
340
341     // printf("  Translated to %.2f %.2f %.2f\n", 
342     //        zero_elev.x, zero_elev.y, zero_elev.z );
343
344     // Rotate to proper orientation
345     // printf("  lon = %.2f  lat = %.2f\n", 
346     //        lon * SGD_RADIANS_TO_DEGREES,
347     //        lat * SGD_RADIANS_TO_DEGREES);
348     // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
349     sgSetVec3( axis, 0.0, 0.0, 1.0 );
350     sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
351
352     // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
353     //             0.0, 1.0, 0.0 );
354     sgSetVec3( axis, 0.0, 1.0, 0.0 );
355     sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
356
357     sgMat4 TRANSFORM;
358
359     sgCopyMat4( TRANSFORM, T1 );
360     sgPreMultMat4( TRANSFORM, LON );
361     sgPreMultMat4( TRANSFORM, LAT );
362
363     sgCoord layerpos;
364     sgSetCoord( &layerpos, TRANSFORM );
365
366     layer_transform->setTransform( &layerpos );
367
368     // now calculate update texture coordinates
369     if ( last_lon < -900 ) {
370         last_lon = lon;
371         last_lat = lat;
372     }
373
374     double sp_dist = speed*dt;
375
376     if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
377         Point3D start( last_lon, last_lat, 0.0 );
378         Point3D dest( lon, lat, 0.0 );
379         double course = 0.0, dist = 0.0;
380
381         calc_gc_course_dist( dest, start, &course, &dist );
382         // cout << "course = " << course << ", dist = " << dist << endl;
383
384         // if start and dest are too close together,
385         // calc_gc_course_dist() can return a course of "nan".  If
386         // this happens, lets just use the last known good course.
387         // This is a hack, and it would probably be better to make
388         // calc_gc_course_dist() more robust.
389         if ( isnan(course) ) {
390             course = last_course;
391         } else {
392             last_course = course;
393         }
394
395         // calculate cloud movement due to external forces
396         double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
397
398         if (dist > 0.0) {
399             ax = cos(course) * dist;
400             ay = sin(course) * dist;
401         }
402
403         if (sp_dist > 0) {
404             bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
405             by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
406         }
407
408
409         double xoff = (ax + bx) / (2 * scale);
410         double yoff = (ay + by) / (2 * scale);
411
412         const float layer_scale = layer_span / scale;
413
414         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
415
416         float *base, *tc;
417
418         base = tl[0]->get( 0 );
419         base[0] += xoff;
420
421         // the while loops can lead to *long* pauses if base[0] comes
422         // with a bogus value.
423         // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
424         // while ( base[0] < 0.0 ) { base[0] += 1.0; }
425         if ( base[0] > -10.0 && base[0] < 10.0 ) {
426             base[0] -= (int)base[0];
427         } else {
428             SG_LOG(SG_ASTRO, SG_DEBUG,
429                    "Error: base = " << base[0] << "," << base[1] <<
430                    " course = " << course << " dist = " << dist );
431             base[0] = 0.0;
432         }
433
434         base[1] += yoff;
435         // the while loops can lead to *long* pauses if base[0] comes
436         // with a bogus value.
437         // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
438         // while ( base[1] < 0.0 ) { base[1] += 1.0; }
439         if ( base[1] > -10.0 && base[1] < 10.0 ) {
440            base[1] -= (int)base[1];
441         } else {
442            SG_LOG(SG_ASTRO, SG_ALERT,
443                   "Error: base = " << base[0] << "," << base[1] <<
444                   " course = " << course << " dist = " << dist );
445            base[1] = 0.0;
446         }
447
448         // cout << "base = " << base[0] << "," << base[1] << endl;
449
450         for (int i = 0; i < 4; i++)
451         {
452             tc = tl[i]->get( 0 );
453             sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
454             
455             for (int j = 0; j < 4; j++)
456             {
457                 tc = tl[i]->get( j*2+1 );
458                 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
459                                base[1] + layer_scale * j/4 );
460  
461                 tc = tl[i]->get( (j+1)*2 );
462                 sgSetVec2( tc, base[0] + layer_scale * i/4,
463                                base[1] + layer_scale * (j+1)/4 );
464             }
465  
466             tc = tl[i]->get( 9 );
467             sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
468                            base[1] + layer_scale );
469         }
470  
471         last_lon = lon;
472         last_lat = lat;
473     }
474
475     return true;
476 }
477
478
479 void SGCloudLayer::draw() {
480     if ( layer_coverage != SG_CLOUD_CLEAR ) {
481         ssgCullAndDraw( layer_root );
482     }
483 }
484
485
486 // make an ssgSimpleState for a cloud layer given the named texture
487 ssgSimpleState *sgCloudMakeState( const string &path ) {
488     ssgSimpleState *state = new ssgSimpleState();
489
490     SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
491
492     state->setTexture( (char *)path.c_str() );
493     state->setShadeModel( GL_SMOOTH );
494     state->disable( GL_LIGHTING );
495     state->disable( GL_CULL_FACE );
496     state->enable( GL_TEXTURE_2D );
497     state->enable( GL_COLOR_MATERIAL );
498     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
499     state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
500     state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
501     state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
502     state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
503     state->enable( GL_BLEND );
504     state->enable( GL_ALPHA_TEST );
505     state->setAlphaClamp( 0.01 );
506
507     // ref() the state so it doesn't get deleted if the last layer of
508     // it's type is deleted.
509     state->ref();
510
511     return state;
512 }