]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloud.cxx
Remove 3d clouds from the default build. These can still be built manually
[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_TYPES];
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_type(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_TYPES; ++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::Type
122 SGCloudLayer::getType () const
123 {
124     return layer_type;
125 }
126
127 void
128 SGCloudLayer::setType (Type type)
129 {
130     if (type != layer_type) {
131         layer_type = type;
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("mostlycloudy.rgba");
151         layer_states[SG_CLOUD_MOSTLY_CLOUDY]
152             = SGCloudMakeState(cloud_path.str());
153
154         cloud_path.set(texture_path.str());
155         cloud_path.append("mostlysunny.rgba");
156         layer_states[SG_CLOUD_MOSTLY_SUNNY]
157             = SGCloudMakeState(cloud_path.str());
158
159         cloud_path.set(texture_path.str());
160         cloud_path.append("cirrus.rgba");
161         layer_states[SG_CLOUD_CIRRUS] = SGCloudMakeState(cloud_path.str());
162
163         layer_states[SG_CLOUD_CLEAR] = 0;
164     }
165
166     scale = 4000.0;
167
168     last_lon = last_lat = -999.0f;
169
170     if ( layer != NULL ) {
171         layer_transform->removeKid(layer); // automatic delete
172     }
173
174     cl = new ssgColourArray( 4 );
175     vl = new ssgVertexArray( 4 );
176     tl = new ssgTexCoordArray( 4 );
177
178     // build the cloud layer
179     sgVec4 color;
180     sgVec3 vertex;
181     sgVec2 tc;
182     sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
183
184     sgSetVec3( vertex, -layer_span, -layer_span, 0.0f );
185     sgVec2 base;
186     sgSetVec2( base, sg_random(), sg_random() );
187     sgSetVec2( tc, base[0], base[1] );
188     cl->add( color );
189     vl->add( vertex );
190     tl->add( tc );
191
192     sgSetVec3( vertex, layer_span, -layer_span, 0.0f );
193     sgSetVec2( tc, base[0] + layer_span / scale, 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], base[1] + layer_span / scale );
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] + layer_span / scale, base[1] + layer_span / scale );
206     cl->add( color );
207     vl->add( vertex );
208     tl->add( tc );
209
210     layer = new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
211     layer_transform->addKid( layer );
212     if ( layer_states[layer_type] != NULL ) {
213         layer->setState( layer_states[layer_type] );
214     }
215
216     // force a repaint of the sky colors with arbitrary defaults
217     repaint( color );
218
219 }
220
221
222 // repaint the cloud layer colors
223 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
224     float *color;
225
226     for ( int i = 0; i < 4; ++i ) {
227         color = cl->get( i );
228         sgCopyVec4( color, fog_color );
229     }
230
231     return true;
232 }
233
234
235 // reposition the cloud layer at the specified origin and orientation
236 // lon specifies a rotation about the Z axis
237 // lat specifies a rotation about the new Y axis
238 // spin specifies a rotation about the new Z axis (and orients the
239 // sunrise/set effects
240 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
241                                double alt )
242 {
243     sgMat4 T1, LON, LAT;
244     sgVec3 axis;
245
246     // combine p and asl (meters) to get translation offset
247     sgVec3 asl_offset;
248     sgCopyVec3( asl_offset, up );
249     sgNormalizeVec3( asl_offset );
250     if ( alt <= layer_asl ) {
251         sgScaleVec3( asl_offset, layer_asl );
252     } else {
253         sgScaleVec3( asl_offset, layer_asl + layer_thickness );
254     }
255     // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
256     //      << "," << asl_offset[2] << endl;
257     sgAddVec3( asl_offset, p );
258     // cout << "  asl_offset = " << asl_offset[0] << "," << asl_offset[1]
259     //      << "," << asl_offset[2] << endl;
260
261     // Translate to zero elevation
262     // Point3D zero_elev = current_view.get_cur_zero_elev();
263     // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
264     sgMakeTransMat4( T1, asl_offset );
265
266     // printf("  Translated to %.2f %.2f %.2f\n", 
267     //        zero_elev.x, zero_elev.y, zero_elev.z );
268
269     // Rotate to proper orientation
270     // printf("  lon = %.2f  lat = %.2f\n", 
271     //        lon * SGD_RADIANS_TO_DEGREES,
272     //        lat * SGD_RADIANS_TO_DEGREES);
273     // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
274     sgSetVec3( axis, 0.0, 0.0, 1.0 );
275     sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
276
277     // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
278     //             0.0, 1.0, 0.0 );
279     sgSetVec3( axis, 0.0, 1.0, 0.0 );
280     sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
281
282     sgMat4 TRANSFORM;
283
284     sgCopyMat4( TRANSFORM, T1 );
285     sgPreMultMat4( TRANSFORM, LON );
286     sgPreMultMat4( TRANSFORM, LAT );
287
288     sgCoord layerpos;
289     sgSetCoord( &layerpos, TRANSFORM );
290
291     layer_transform->setTransform( &layerpos );
292
293     // now calculate update texture coordinates
294     if ( last_lon < -900 ) {
295         last_lon = lon;
296         last_lat = lat;
297     }
298
299     if ( lon != last_lon || lat != last_lat ) {
300         Point3D start( last_lon, last_lat, 0.0 );
301         Point3D dest( lon, lat, 0.0 );
302         double course, dist;
303         calc_gc_course_dist( dest, start, &course, &dist );
304         // cout << "course = " << course << ", dist = " << dist << endl;
305
306         double xoff = cos( course ) * dist / (2 * scale);
307         double yoff = sin( course ) * dist / (2 * scale);
308
309         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
310
311         float *base, *tc;
312         base = tl->get( 0 );
313
314         base[0] += xoff;
315
316         // the while loops can lead to *long* pauses if base[0] comes
317         // with a bogus value.
318         // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
319         // while ( base[0] < 0.0 ) { base[0] += 1.0; }
320         if ( base[0] > -10.0 && base[0] < 10.0 ) {
321             base[0] -= (int)base[0];
322         } else {
323             base[0] = 0.0;
324             SG_LOG(SG_ASTRO, SG_DEBUG,
325                    "Error: base = " << base[0] << "," << base[1]);
326         }
327
328         base[1] += yoff;
329         // the while loops can lead to *long* pauses if base[0] comes
330         // with a bogus value.
331         // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
332         // while ( base[1] < 0.0 ) { base[1] += 1.0; }
333         if ( base[1] > -10.0 && base[1] < 10.0 ) {
334             base[1] -= (int)base[1];
335         } else {
336             base[1] = 0.0;
337             SG_LOG(SG_ASTRO, SG_ALERT,
338                    "Error: base = " << base[0] << "," << base[1]);
339         }
340
341         // cout << "base = " << base[0] << "," << base[1] << endl;
342
343         tc = tl->get( 1 );
344         sgSetVec2( tc, base[0] + layer_span / scale, base[1] );
345  
346         tc = tl->get( 2 );
347         sgSetVec2( tc, base[0], base[1] + layer_span / scale );
348  
349         tc = tl->get( 3 );
350         sgSetVec2( tc, base[0] + layer_span / scale, base[1] + layer_span / scale );
351  
352         last_lon = lon;
353         last_lat = lat;
354     }
355
356     return true;
357 }
358
359
360 void SGCloudLayer::draw() {
361     if ( layer_type != SG_CLOUD_CLEAR ) {
362         ssgCullAndDraw( layer_root );
363     }
364 }
365
366
367 // make an ssgSimpleState for a cloud layer given the named texture
368 ssgSimpleState *SGCloudMakeState( const string &path ) {
369     ssgSimpleState *state = new ssgSimpleState();
370
371     state->setTexture( (char *)path.c_str() );
372     state->setShadeModel( GL_SMOOTH );
373     state->disable( GL_LIGHTING );
374     state->disable( GL_CULL_FACE );
375     state->enable( GL_TEXTURE_2D );
376     state->enable( GL_COLOR_MATERIAL );
377     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
378     state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
379     state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
380     state->enable( GL_BLEND );
381     state->enable( GL_ALPHA_TEST );
382     state->setAlphaClamp( 0.01 );
383
384     // ref() the state so it doesn't get deleted if the last layer of
385     // it's type is deleted.
386     state->ref();
387
388     return state;
389 }