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