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