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