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