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