1 // cloud.cxx -- model a single cloud layer
3 // Written by Curtis Olson, started June 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
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.
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.
19 #include <simgear/compiler.h>
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>
36 SGCloudLayer::layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
40 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
41 layer_root(new ssgRoot),
42 layer_transform(new ssgTransform),
43 texture_path(tex_path),
47 layer_transition(0.0),
48 layer_coverage(SG_CLOUD_CLEAR),
53 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
54 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
55 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
56 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
58 for ( int i = 0; i < SG_MAX_CLOUD_COVERAGES; ++i ) {
59 layer_states[i] = NULL;
61 layer_root->addKid(layer_transform);
66 SGCloudLayer::~SGCloudLayer()
68 delete layer_root; // deletes layer_transform and layer as well
72 SGCloudLayer::getSpan_m () const
78 SGCloudLayer::setSpan_m (float span_m)
80 if (span_m != layer_span) {
87 SGCloudLayer::getElevation_m () const
93 SGCloudLayer::setElevation_m (float elevation_m)
95 layer_asl = elevation_m;
99 SGCloudLayer::getThickness_m () const
101 return layer_thickness;
105 SGCloudLayer::setThickness_m (float thickness_m)
107 layer_thickness = thickness_m;
111 SGCloudLayer::getTransition_m () const
113 return layer_transition;
117 SGCloudLayer::setTransition_m (float transition_m)
119 layer_transition = transition_m;
122 SGCloudLayer::Coverage
123 SGCloudLayer::getCoverage () const
125 return layer_coverage;
129 SGCloudLayer::setCoverage (Coverage coverage)
131 if (coverage != layer_coverage) {
132 layer_coverage = coverage;
138 // build the cloud object
140 SGCloudLayer::rebuild()
142 // Initialize states and sizes if necessary.
143 if ( layer_states[0] == NULL ) {
146 cloud_path.set(texture_path.str());
147 cloud_path.append("overcast.rgb");
148 layer_states[SG_CLOUD_OVERCAST] = SGCloudMakeState(cloud_path.str());
150 cloud_path.set(texture_path.str());
151 cloud_path.append("broken.rgba");
152 layer_states[SG_CLOUD_BROKEN]
153 = SGCloudMakeState(cloud_path.str());
155 cloud_path.set(texture_path.str());
156 cloud_path.append("scattered.rgba");
157 layer_states[SG_CLOUD_SCATTERED]
158 = SGCloudMakeState(cloud_path.str());
160 cloud_path.set(texture_path.str());
161 cloud_path.append("few.rgba");
162 layer_states[SG_CLOUD_FEW]
163 = SGCloudMakeState(cloud_path.str());
165 cloud_path.set(texture_path.str());
166 cloud_path.append("cirrus.rgba");
167 layer_states[SG_CLOUD_CIRRUS]
168 = SGCloudMakeState(cloud_path.str());
170 layer_states[SG_CLOUD_CLEAR] = 0;
174 last_lon = last_lat = -999.0f;
177 sgSetVec2( base, sg_random(), sg_random() );
179 // build the cloud layer
184 const float layer_scale = layer_span / scale;
185 const float mpi = SG_PI/4;
187 for (int i = 0; i < 4; i++)
189 if ( layer[i] != NULL ) {
190 layer_transform->removeKid(layer[i]); // automatic delete
193 vl[i] = new ssgVertexArray( 10 );
194 cl[i] = new ssgColourArray( 10 );
195 tl[i] = new ssgTexCoordArray( 10 );
198 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
199 500 * (sin(i*mpi) - 2) );
201 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
203 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
206 vl[i]->add( vertex );
209 for (int j = 0; j < 4; j++)
211 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
212 500 * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
214 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
215 base[1] + layer_scale * j/4 );
217 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
218 ( (j == 0) || (i == 3)) ?
219 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
222 vl[i]->add( vertex );
226 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
227 500 * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
229 sgSetVec2( tc, base[0] + layer_scale * i/4,
230 base[1] + layer_scale * (j+1)/4 );
232 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
233 ((j == 3) || (i == 0)) ?
234 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
236 vl[i]->add( vertex );
240 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
241 500 * (sin((i+1)*mpi) - 2) );
243 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
244 base[1] + layer_scale );
246 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
249 vl[i]->add( vertex );
252 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
253 layer_transform->addKid( layer[i] );
255 if ( layer_states[layer_coverage] != NULL ) {
256 layer[i]->setState( layer_states[layer_coverage] );
260 // force a repaint of the sky colors with arbitrary defaults
266 // repaint the cloud layer colors
267 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
270 for ( int i = 0; i < 4; i++ )
271 for ( int j = 0; j < 10; ++j ) {
272 color = cl[i]->get( j );
273 sgCopyVec3( color, fog_color );
280 // reposition the cloud layer at the specified origin and orientation
281 // lon specifies a rotation about the Z axis
282 // lat specifies a rotation about the new Y axis
283 // spin specifies a rotation about the new Z axis (and orients the
284 // sunrise/set effects
285 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
291 // combine p and asl (meters) to get translation offset
293 sgCopyVec3( asl_offset, up );
294 sgNormalizeVec3( asl_offset );
295 if ( alt <= layer_asl ) {
296 sgScaleVec3( asl_offset, layer_asl );
298 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
300 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
301 // << "," << asl_offset[2] << endl;
302 sgAddVec3( asl_offset, p );
303 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
304 // << "," << asl_offset[2] << endl;
306 // Translate to zero elevation
307 // Point3D zero_elev = current_view.get_cur_zero_elev();
308 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
309 sgMakeTransMat4( T1, asl_offset );
311 // printf(" Translated to %.2f %.2f %.2f\n",
312 // zero_elev.x, zero_elev.y, zero_elev.z );
314 // Rotate to proper orientation
315 // printf(" lon = %.2f lat = %.2f\n",
316 // lon * SGD_RADIANS_TO_DEGREES,
317 // lat * SGD_RADIANS_TO_DEGREES);
318 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
319 sgSetVec3( axis, 0.0, 0.0, 1.0 );
320 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
322 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
324 sgSetVec3( axis, 0.0, 1.0, 0.0 );
325 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
329 sgCopyMat4( TRANSFORM, T1 );
330 sgPreMultMat4( TRANSFORM, LON );
331 sgPreMultMat4( TRANSFORM, LAT );
334 sgSetCoord( &layerpos, TRANSFORM );
336 layer_transform->setTransform( &layerpos );
338 // now calculate update texture coordinates
339 if ( last_lon < -900 ) {
344 if ( lon != last_lon || lat != last_lat ) {
345 Point3D start( last_lon, last_lat, 0.0 );
346 Point3D dest( lon, lat, 0.0 );
348 calc_gc_course_dist( dest, start, &course, &dist );
349 // cout << "course = " << course << ", dist = " << dist << endl;
351 double xoff = cos( course ) * dist / (2 * scale);
352 double yoff = sin( course ) * dist / (2 * scale);
354 const float layer_scale = layer_span / scale;
356 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
360 base = tl[0]->get( 0 );
363 // the while loops can lead to *long* pauses if base[0] comes
364 // with a bogus value.
365 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
366 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
367 if ( base[0] > -10.0 && base[0] < 10.0 ) {
368 base[0] -= (int)base[0];
371 SG_LOG(SG_ASTRO, SG_DEBUG,
372 "Error: base = " << base[0] << "," << base[1]);
376 // the while loops can lead to *long* pauses if base[0] comes
377 // with a bogus value.
378 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
379 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
380 if ( base[1] > -10.0 && base[1] < 10.0 ) {
381 base[1] -= (int)base[1];
384 SG_LOG(SG_ASTRO, SG_ALERT,
385 "Error: base = " << base[0] << "," << base[1]);
388 // cout << "base = " << base[0] << "," << base[1] << endl;
390 for (int i = 0; i < 4; i++)
392 tc = tl[i]->get( 0 );
393 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
395 for (int j = 0; j < 4; j++)
397 tc = tl[i]->get( j*2+1 );
398 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
399 base[1] + layer_scale * j/4 );
401 tc = tl[i]->get( (j+1)*2 );
402 sgSetVec2( tc, base[0] + layer_scale * i/4,
403 base[1] + layer_scale * (j+1)/4 );
406 tc = tl[i]->get( 9 );
407 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
408 base[1] + layer_scale );
419 void SGCloudLayer::draw() {
420 if ( layer_coverage != SG_CLOUD_CLEAR ) {
421 ssgCullAndDraw( layer_root );
426 // make an ssgSimpleState for a cloud layer given the named texture
427 ssgSimpleState *SGCloudMakeState( const string &path ) {
428 ssgSimpleState *state = new ssgSimpleState();
430 state->setTexture( (char *)path.c_str() );
431 state->setShadeModel( GL_SMOOTH );
432 state->disable( GL_LIGHTING );
433 state->disable( GL_CULL_FACE );
434 state->enable( GL_TEXTURE_2D );
435 state->enable( GL_COLOR_MATERIAL );
436 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
437 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
438 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
439 state->enable( GL_BLEND );
440 state->enable( GL_ALPHA_TEST );
441 state->setAlphaClamp( 0.01 );
443 // ref() the state so it doesn't get deleted if the last layer of
444 // it's type is deleted.