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