]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloud.cxx
Oops, make the cloud layer look more like an inverted bowl than like a sombrero
[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 ssgSimpleState *
36 SGCloudLayer::layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
37
38
39 // Constructor
40 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
41     layer_root(new ssgRoot),
42     layer_transform(new ssgTransform),
43     texture_path(tex_path),
44     layer_span(0.0),
45     layer_asl(0.0),
46     layer_thickness(0.0),
47     layer_transition(0.0),
48     layer_coverage(SG_CLOUD_CLEAR),
49     scale(4000.0),
50     last_lon(0.0),
51     last_lat(0.0)
52 {
53     cl[0] = cl[1] = cl[2] = cl[3] = NULL;
54     vl[0] = vl[1] = vl[2] = vl[3] = NULL;
55     tl[0] = tl[1] = tl[2] = tl[3] = NULL;
56     layer[0] = layer[1] = layer[2] = layer[3] = NULL;
57
58     for ( int i = 0; i < SG_MAX_CLOUD_COVERAGES; ++i ) {
59         layer_states[i] = 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 ( layer_states[0] == NULL ) {
144         SGPath cloud_path;
145
146         cloud_path.set(texture_path.str());
147         cloud_path.append("overcast.rgb");
148         layer_states[SG_CLOUD_OVERCAST] = SGCloudMakeState(cloud_path.str());
149
150         cloud_path.set(texture_path.str());
151         cloud_path.append("broken.rgba");
152         layer_states[SG_CLOUD_BROKEN]
153             = SGCloudMakeState(cloud_path.str());
154
155         cloud_path.set(texture_path.str());
156         cloud_path.append("scattered.rgba");
157         layer_states[SG_CLOUD_SCATTERED]
158             = SGCloudMakeState(cloud_path.str());
159
160         cloud_path.set(texture_path.str());
161         cloud_path.append("few.rgba");
162         layer_states[SG_CLOUD_FEW]
163             = SGCloudMakeState(cloud_path.str());
164
165         cloud_path.set(texture_path.str());
166         cloud_path.append("cirrus.rgba");
167         layer_states[SG_CLOUD_CIRRUS]
168             = SGCloudMakeState(cloud_path.str());
169
170         layer_states[SG_CLOUD_CLEAR] = 0;
171     }
172
173     scale = 4000.0;
174     last_lon = last_lat = -999.0f;
175
176     sgVec2 base;
177     sgSetVec2( base, sg_random(), sg_random() );
178
179     // build the cloud layer
180     sgVec4 color;
181     sgVec3 vertex;
182     sgVec2 tc;
183
184     const float layer_scale = layer_span / scale;
185     const float mpi = SG_PI/4;
186
187     for (int i = -2; i < 2; i++)
188     {
189         if ( layer[i+2] != NULL ) {
190             layer_transform->removeKid(layer[i+2]); // automatic delete
191         }
192
193         vl[i+2] = new ssgVertexArray( 10 );
194         cl[i+2] = new ssgColourArray( 10 );
195         tl[i+2] = new ssgTexCoordArray( 10 );
196
197
198         sgSetVec3( vertex, layer_span*i/2, -layer_span,
199                            500 * (sin((i+2)*mpi) - 2) );
200
201         sgSetVec2( tc, base[0] + layer_scale * (i+2)/4, base[1] );
202
203         sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == -2) ? 0.0f : 0.15f );
204
205         cl[i+2]->add( color );
206         vl[i+2]->add( vertex );
207         tl[i+2]->add( tc );
208
209         for (int j = -2; j < 2; j++)
210         {
211             sgSetVec3( vertex, layer_span*(i+1)/2, layer_span*j/2,
212                                500 * (sin((i+3)*mpi) + sin((j+2)*mpi) - 2) );
213
214             sgSetVec2( tc, base[0] + layer_scale * (i+3)/4,
215                            base[1] + layer_scale * (j+2)/4 );
216
217             sgSetVec4( color, 1.0f, 1.0f, 1.0f,
218                               ((j == -2) || (i == 1)) ?  
219                               ((j == -2) && (i == 1)) ? 0.0f : 0.15f : 1.0f );
220
221             cl[i+2]->add( color );
222             vl[i+2]->add( vertex );
223             tl[i+2]->add( tc );
224
225
226             sgSetVec3( vertex, layer_span*i/2, layer_span*(j+1)/2,
227                                500 * (sin((i+2)*mpi) + sin((j+3)*mpi) - 2) );
228
229             sgSetVec2( tc, base[0] + layer_scale * (i+2)/4,
230                            base[1] + layer_scale * (j+3)/4 );
231
232             sgSetVec4( color, 1.0f, 1.0f, 1.0f,
233                               ((j == 1) || (i == -2)) ?
234                               ((j == 1) && (i == -2)) ? 0.0f : 0.15f : 1.0f );
235             cl[i+2]->add( color );
236             vl[i+2]->add( vertex );
237             tl[i+2]->add( tc );
238         }
239
240         sgSetVec3( vertex, layer_span*(i+1)/2, layer_span, 
241                            500 * (sin((i+3)*mpi) - 2) );
242
243         sgSetVec2( tc, base[0] + layer_scale * (i+3)/4,
244                        base[1] + layer_scale );
245
246         sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 1) ? 0.0f : 0.15f );
247
248         cl[i+2]->add( color );
249         vl[i+2]->add( vertex );
250         tl[i+2]->add( tc );
251
252         layer[i+2] = new ssgVtxTable ( GL_TRIANGLE_STRIP, vl[i+2], NULL, tl[i+2], cl[i+2] );
253         layer_transform->addKid( layer[i+2] );
254
255         if ( layer_states[layer_coverage] != NULL ) {
256             layer[i+2]->setState( layer_states[layer_coverage] );
257         }
258     }
259
260     // force a repaint of the sky colors with arbitrary defaults
261     repaint( color );
262
263 }
264
265
266 // repaint the cloud layer colors
267 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
268     float *color;
269
270     for ( int i = 0; i < 4; i++ )
271         for ( int j = 0; j < 10; ++j ) {
272             color = cl[i]->get( j );
273             sgCopyVec3( color, fog_color );
274         }
275
276     return true;
277 }
278
279
280 // reposition the cloud layer at the specified origin and orientation
281 // lon specifies a rotation about the Z axis
282 // lat specifies a rotation about the new Y axis
283 // spin specifies a rotation about the new Z axis (and orients the
284 // sunrise/set effects
285 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
286                                double alt )
287 {
288     sgMat4 T1, LON, LAT;
289     sgVec3 axis;
290
291     // combine p and asl (meters) to get translation offset
292     sgVec3 asl_offset;
293     sgCopyVec3( asl_offset, up );
294     sgNormalizeVec3( asl_offset );
295     if ( alt <= layer_asl ) {
296         sgScaleVec3( asl_offset, layer_asl );
297     } else {
298         sgScaleVec3( asl_offset, layer_asl + layer_thickness );
299     }
300     // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
301     //      << "," << asl_offset[2] << endl;
302     sgAddVec3( asl_offset, p );
303     // cout << "  asl_offset = " << asl_offset[0] << "," << asl_offset[1]
304     //      << "," << asl_offset[2] << endl;
305
306     // Translate to zero elevation
307     // Point3D zero_elev = current_view.get_cur_zero_elev();
308     // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
309     sgMakeTransMat4( T1, asl_offset );
310
311     // printf("  Translated to %.2f %.2f %.2f\n", 
312     //        zero_elev.x, zero_elev.y, zero_elev.z );
313
314     // Rotate to proper orientation
315     // printf("  lon = %.2f  lat = %.2f\n", 
316     //        lon * SGD_RADIANS_TO_DEGREES,
317     //        lat * SGD_RADIANS_TO_DEGREES);
318     // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
319     sgSetVec3( axis, 0.0, 0.0, 1.0 );
320     sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
321
322     // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
323     //             0.0, 1.0, 0.0 );
324     sgSetVec3( axis, 0.0, 1.0, 0.0 );
325     sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
326
327     sgMat4 TRANSFORM;
328
329     sgCopyMat4( TRANSFORM, T1 );
330     sgPreMultMat4( TRANSFORM, LON );
331     sgPreMultMat4( TRANSFORM, LAT );
332
333     sgCoord layerpos;
334     sgSetCoord( &layerpos, TRANSFORM );
335
336     layer_transform->setTransform( &layerpos );
337
338     // now calculate update texture coordinates
339     if ( last_lon < -900 ) {
340         last_lon = lon;
341         last_lat = lat;
342     }
343
344     if ( lon != last_lon || lat != last_lat ) {
345         Point3D start( last_lon, last_lat, 0.0 );
346         Point3D dest( lon, lat, 0.0 );
347         double course, dist;
348         calc_gc_course_dist( dest, start, &course, &dist );
349         // cout << "course = " << course << ", dist = " << dist << endl;
350
351         double xoff = cos( course ) * dist / (2 * scale);
352         double yoff = sin( course ) * dist / (2 * scale);
353
354         const float layer_scale = layer_span / scale;
355
356         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
357
358         float *base, *tc;
359
360         base = tl[0]->get( 0 );
361         base[0] += xoff;
362
363         // the while loops can lead to *long* pauses if base[0] comes
364         // with a bogus value.
365         // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
366         // while ( base[0] < 0.0 ) { base[0] += 1.0; }
367         if ( base[0] > -10.0 && base[0] < 10.0 ) {
368             base[0] -= (int)base[0];
369         } else {
370             base[0] = 0.0;
371             SG_LOG(SG_ASTRO, SG_DEBUG,
372                 "Error: base = " << base[0] << "," << base[1]);
373         }
374
375         base[1] += yoff;
376         // the while loops can lead to *long* pauses if base[0] comes
377         // with a bogus value.
378         // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
379         // while ( base[1] < 0.0 ) { base[1] += 1.0; }
380         if ( base[1] > -10.0 && base[1] < 10.0 ) {
381            base[1] -= (int)base[1];
382         } else {
383            base[1] = 0.0;
384            SG_LOG(SG_ASTRO, SG_ALERT,
385                 "Error: base = " << base[0] << "," << base[1]);
386         }
387
388        // cout << "base = " << base[0] << "," << base[1] << endl;
389
390         for (int i = -2; i < 2; i++)
391         {
392             tc = tl[i+2]->get( 0 );
393             sgSetVec2( tc, base[0] + layer_scale * (i+2)/4, base[1] );
394             
395             for (int j = -2; j < 2; j++)
396             {
397                 tc = tl[i+2]->get( (j+2)*2+1 );
398                 sgSetVec2( tc, base[0] + layer_scale * (i+3)/4,
399                                base[1] + layer_scale * (j+2)/4 );
400  
401                 tc = tl[i+2]->get( (j+3)*2 );
402                 sgSetVec2( tc, base[0] + layer_scale * (i+2)/4,
403                                base[1] + layer_scale * (j+3)/4 );
404             }
405  
406             tc = tl[i+2]->get( 9 );
407             sgSetVec2( tc, base[0] + layer_scale * (i+3)/4,
408                            base[1] + layer_scale );
409         }
410  
411         last_lon = lon;
412         last_lat = lat;
413     }
414
415     return true;
416 }
417
418
419 void SGCloudLayer::draw() {
420     if ( layer_coverage != SG_CLOUD_CLEAR ) {
421         ssgCullAndDraw( layer_root );
422     }
423 }
424
425
426 // make an ssgSimpleState for a cloud layer given the named texture
427 ssgSimpleState *SGCloudMakeState( const string &path ) {
428     ssgSimpleState *state = new ssgSimpleState();
429
430     state->setTexture( (char *)path.c_str() );
431     state->setShadeModel( GL_SMOOTH );
432     state->disable( GL_LIGHTING );
433     state->disable( GL_CULL_FACE );
434     state->enable( GL_TEXTURE_2D );
435     state->enable( GL_COLOR_MATERIAL );
436     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
437     state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
438     state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
439     state->enable( GL_BLEND );
440     state->enable( GL_ALPHA_TEST );
441     state->setAlphaClamp( 0.01 );
442
443     // ref() the state so it doesn't get deleted if the last layer of
444     // it's type is deleted.
445     state->ref();
446
447     return state;
448 }