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