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