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