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