]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloud.cxx
simgear/sky/
[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/2;
186
187     for (int i = -2; i < 2; i++)
188     {
189         int row = i + 2;
190
191         if ( layer[row] != NULL ) {
192             layer_transform->removeKid(layer[row]); // automatic delete
193         }
194
195         vl[row] = new ssgVertexArray( 10 );
196         cl[row] = new ssgColourArray( 10 );
197         tl[row] = new ssgTexCoordArray( 10 );
198
199
200         sgSetVec3( vertex, layer_span*i/2, -layer_span,
201                            -250 * (cos((i+2)*mpi) + 3) );
202
203         sgSetVec2( tc, base[0] + layer_scale * (i+2)/4, base[1] );
204
205         sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == -2) ? 0.0f : 0.15f );
206
207         cl[row]->add( color );
208         vl[row]->add( vertex );
209         tl[row]->add( tc );
210
211         for (int j = -2; j < 2; j++)
212         {
213             sgSetVec3( vertex, layer_span*(i+1)/2, layer_span*j/2,
214                                -250 * (cos((i+3)*mpi) + cos((j+2)*mpi) + 2) );
215
216             sgSetVec2( tc, base[0] + layer_scale * (i+3)/4,
217                            base[1] + layer_scale * (j+2)/4 );
218
219             sgSetVec4( color, 1.0f, 1.0f, 1.0f,
220                               ((j == -2) || (i == 1)) ?  
221                               ((j == -2) && (i == 1)) ? 0.0f : 0.15f : 1.0f );
222
223             cl[row]->add( color );
224             vl[row]->add( vertex );
225             tl[row]->add( tc );
226
227
228             sgSetVec3( vertex, layer_span*i/2, layer_span*(j+1)/2,
229                                -250 * (cos((i+2)*mpi) + cos((j+3)*mpi) + 2) );
230
231             sgSetVec2( tc, base[0] + layer_scale * (i+2)/4,
232                            base[1] + layer_scale * (j+3)/4 );
233
234             sgSetVec4( color, 1.0f, 1.0f, 1.0f,
235                               ((j == 1) || (i == -2)) ?
236                               ((j == 1) && (i == -2)) ? 0.0f : 0.15f : 1.0f );
237             cl[row]->add( color );
238             vl[row]->add( vertex );
239             tl[row]->add( tc );
240         }
241
242         sgSetVec3( vertex, layer_span*(i+1)/2, layer_span, 
243                            -250 * (cos((i+3)*mpi) + 3) );
244
245         sgSetVec2( tc, base[0] + layer_scale * (i+3)/4,
246                        base[1] + layer_scale );
247
248         sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 1) ? 0.0f : 0.15f );
249
250         cl[row]->add( color );
251         vl[row]->add( vertex );
252         tl[row]->add( tc );
253
254         layer[row] = new ssgVtxTable ( GL_TRIANGLE_STRIP, vl[row], NULL, tl[row], cl[row] );
255         layer_transform->addKid( layer[row] );
256
257         if ( layer_states[layer_coverage] != NULL ) {
258             layer[row]->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 row = 0; row < 4; row++ )
273         for ( int i = 0; i < 10; ++i ) {
274             color = cl[row]->get( i );
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 )
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     if ( lon != last_lon || lat != last_lat ) {
347         Point3D start( last_lon, last_lat, 0.0 );
348         Point3D dest( lon, lat, 0.0 );
349         double course, dist;
350         calc_gc_course_dist( dest, start, &course, &dist );
351         // cout << "course = " << course << ", dist = " << dist << endl;
352
353         double xoff = cos( course ) * dist / (2 * scale);
354         double yoff = sin( course ) * dist / (2 * scale);
355
356         const float layer_scale = layer_span / scale;
357
358         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
359
360         float *base, *tc;
361
362         base = tl[0]->get( 0 );
363         base[0] += xoff;
364
365         // the while loops can lead to *long* pauses if base[0] comes
366         // with a bogus value.
367         // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
368         // while ( base[0] < 0.0 ) { base[0] += 1.0; }
369         if ( base[0] > -10.0 && base[0] < 10.0 ) {
370             base[0] -= (int)base[0];
371         } else {
372             base[0] = 0.0;
373             SG_LOG(SG_ASTRO, SG_DEBUG,
374                 "Error: base = " << base[0] << "," << base[1]);
375         }
376
377         base[1] += yoff;
378         // the while loops can lead to *long* pauses if base[0] comes
379         // with a bogus value.
380         // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
381         // while ( base[1] < 0.0 ) { base[1] += 1.0; }
382         if ( base[1] > -10.0 && base[1] < 10.0 ) {
383            base[1] -= (int)base[1];
384         } else {
385            base[1] = 0.0;
386            SG_LOG(SG_ASTRO, SG_ALERT,
387                 "Error: base = " << base[0] << "," << base[1]);
388         }
389
390        // cout << "base = " << base[0] << "," << base[1] << endl;
391
392         for (int i = -2; i < 2; i++)
393         {
394             int row = i + 2;
395
396             tc = tl[row]->get( 0 );
397             sgSetVec2( tc, base[0] + layer_scale * (i+2)/4, base[1] );
398             
399             for (int j = -2; j < 2; j++)
400             {
401                 tc = tl[row]->get( (j+2)*2+1 );
402                 sgSetVec2( tc, base[0] + layer_scale * (i+3)/4,
403                                base[1] + layer_scale * (j+2)/4 );
404  
405                 tc = tl[row]->get( (j+3)*2 );
406                 sgSetVec2( tc, base[0] + layer_scale * (i+2)/4,
407                                base[1] + layer_scale * (j+3)/4 );
408             }
409  
410             tc = tl[row]->get( 9 );
411             sgSetVec2( tc, base[0] + layer_scale * (i+3)/4,
412                            base[1] + layer_scale );
413         }
414  
415         last_lon = lon;
416         last_lat = lat;
417     }
418
419     return true;
420 }
421
422
423 void SGCloudLayer::draw() {
424     if ( layer_coverage != SG_CLOUD_CLEAR ) {
425         ssgCullAndDraw( layer_root );
426     }
427 }
428
429
430 // make an ssgSimpleState for a cloud layer given the named texture
431 ssgSimpleState *SGCloudMakeState( const string &path ) {
432     ssgSimpleState *state = new ssgSimpleState();
433
434     state->setTexture( (char *)path.c_str() );
435     state->setShadeModel( GL_SMOOTH );
436     state->disable( GL_LIGHTING );
437     state->disable( GL_CULL_FACE );
438     state->enable( GL_TEXTURE_2D );
439     state->enable( GL_COLOR_MATERIAL );
440     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
441     state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
442     state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
443     state->enable( GL_BLEND );
444     state->enable( GL_ALPHA_TEST );
445     state->setAlphaClamp( 0.01 );
446
447     // ref() the state so it doesn't get deleted if the last layer of
448     // it's type is deleted.
449     state->ref();
450
451     return state;
452 }