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>
24 // #if defined (__APPLE__)
25 // // any C++ header file undefines isinf and isnan
26 // // so this should be included before <iostream>
27 // inline int (isinf)(double r) { return isinf(r); }
28 // inline int (isnan)(double r) { return isnan(r); }
31 // #include STL_IOSTREAM
36 #include <simgear/math/point3d.hxx>
37 #include <simgear/math/polar3d.hxx>
38 #include <simgear/math/sg_random.h>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/misc/sg_path.hxx>
45 static ssgStateSelector *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
46 static bool state_initialized = false;
50 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
51 layer_root(new ssgRoot),
52 layer_transform(new ssgTransform),
54 texture_path(tex_path),
58 layer_transition(0.0),
59 layer_coverage(SG_CLOUD_CLEAR),
66 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
67 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
68 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
69 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
71 layer_root->addKid(layer_transform);
76 SGCloudLayer::~SGCloudLayer()
78 delete layer_root; // deletes layer_transform and layer as well
82 SGCloudLayer::getSpan_m () const
88 SGCloudLayer::setSpan_m (float span_m)
90 if (span_m != layer_span) {
97 SGCloudLayer::getElevation_m () const
103 SGCloudLayer::setElevation_m (float elevation_m, bool set_span)
105 layer_asl = elevation_m;
108 if (elevation_m > 4000)
109 setSpan_m( elevation_m * 10 );
116 SGCloudLayer::getThickness_m () const
118 return layer_thickness;
122 SGCloudLayer::setThickness_m (float thickness_m)
124 layer_thickness = thickness_m;
128 SGCloudLayer::getTransition_m () const
130 return layer_transition;
134 SGCloudLayer::setTransition_m (float transition_m)
136 layer_transition = transition_m;
139 SGCloudLayer::Coverage
140 SGCloudLayer::getCoverage () const
142 return layer_coverage;
146 SGCloudLayer::setCoverage (Coverage coverage)
148 if (coverage != layer_coverage) {
149 layer_coverage = coverage;
155 // build the cloud object
157 SGCloudLayer::rebuild()
159 // Initialize states and sizes if necessary.
160 if ( !state_initialized ) {
161 state_initialized = true;
163 SG_LOG(SG_ASTRO, SG_INFO, "initializing cloud layers");
166 ssgStateSelector *state_sel;
167 ssgSimpleState *state;
169 state_sel = new ssgStateSelector( 2 );
170 cloud_path.set(texture_path.str());
171 cloud_path.append("overcast.rgb");
172 state_sel->setStep( 0, sgCloudMakeState(cloud_path.str()) );
173 cloud_path.set(texture_path.str());
174 cloud_path.append("overcast_top.rgb");
175 state_sel->setStep( 1, sgCloudMakeState(cloud_path.str()) );
176 layer_states[SG_CLOUD_OVERCAST] = state_sel;
178 state_sel = new ssgStateSelector( 2 );
179 cloud_path.set(texture_path.str());
180 cloud_path.append("broken.rgba");
181 state = sgCloudMakeState(cloud_path.str());
182 state_sel->setStep( 0, state );
183 state_sel->setStep( 1, state );
184 layer_states[SG_CLOUD_BROKEN] = state_sel;
186 state_sel = new ssgStateSelector( 2 );
187 cloud_path.set(texture_path.str());
188 cloud_path.append("scattered.rgba");
189 state = sgCloudMakeState(cloud_path.str());
190 state_sel->setStep( 0, state );
191 state_sel->setStep( 1, state );
192 layer_states[SG_CLOUD_SCATTERED] = state_sel;
194 state_sel = new ssgStateSelector( 2 );
195 cloud_path.set(texture_path.str());
196 cloud_path.append("few.rgba");
197 state = sgCloudMakeState(cloud_path.str());
198 state_sel->setStep( 0, state );
199 state_sel->setStep( 1, state );
200 layer_states[SG_CLOUD_FEW] = state_sel;
202 state_sel = new ssgStateSelector( 2 );
203 cloud_path.set(texture_path.str());
204 cloud_path.append("cirrus.rgba");
205 state = sgCloudMakeState(cloud_path.str());
206 state_sel->setStep( 0, state );
207 state_sel->setStep( 1, state );
208 layer_states[SG_CLOUD_CIRRUS] = state_sel;
210 layer_states[SG_CLOUD_CLEAR] = 0;
214 last_lon = last_lat = -999.0f;
217 sgSetVec2( base, sg_random(), sg_random() );
219 // build the cloud layer
224 const float layer_scale = layer_span / scale;
225 const float mpi = SG_PI/4;
227 // caclculate the difference between a flat-earth model and
228 // a round earth model given the span and altutude ASL of
229 // the cloud layer. This is the difference in altitude between
230 // the top of the inverted bowl and the edge of the bowl.
231 // const float alt_diff = layer_asl * 0.8;
232 const float layer_to_core = (SG_EARTH_RAD * 1000 + layer_asl);
233 const float layer_angle = acos( 0.5*layer_span / layer_to_core);
234 const float border_to_core = layer_to_core * sin(layer_angle);
235 const float alt_diff = layer_to_core - border_to_core;
237 for (int i = 0; i < 4; i++)
239 if ( layer[i] != NULL ) {
240 layer_transform->removeKid(layer[i]); // automatic delete
243 vl[i] = new ssgVertexArray( 10 );
244 cl[i] = new ssgColourArray( 10 );
245 tl[i] = new ssgTexCoordArray( 10 );
248 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
249 alt_diff * (sin(i*mpi) - 2) );
251 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
253 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
256 vl[i]->add( vertex );
259 for (int j = 0; j < 4; j++)
261 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
262 alt_diff * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
264 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
265 base[1] + layer_scale * j/4 );
267 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
268 ( (j == 0) || (i == 3)) ?
269 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
272 vl[i]->add( vertex );
276 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
277 alt_diff * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
279 sgSetVec2( tc, base[0] + layer_scale * i/4,
280 base[1] + layer_scale * (j+1)/4 );
282 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
283 ((j == 3) || (i == 0)) ?
284 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
286 vl[i]->add( vertex );
290 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
291 alt_diff * (sin((i+1)*mpi) - 2) );
293 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
294 base[1] + layer_scale );
296 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
299 vl[i]->add( vertex );
302 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
303 layer_transform->addKid( layer[i] );
305 if ( layer_states[layer_coverage] != NULL ) {
306 layer[i]->setState( layer_states[layer_coverage] );
308 state_sel = layer_states[layer_coverage];
311 // force a repaint of the sky colors with arbitrary defaults
317 // repaint the cloud layer colors
318 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
321 for ( int i = 0; i < 4; i++ )
322 for ( int j = 0; j < 10; ++j ) {
323 color = cl[i]->get( j );
324 sgCopyVec3( color, fog_color );
331 // reposition the cloud layer at the specified origin and orientation
332 // lon specifies a rotation about the Z axis
333 // lat specifies a rotation about the new Y axis
334 // spin specifies a rotation about the new Z axis (and orients the
335 // sunrise/set effects
336 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
337 double alt, double dt )
342 // combine p and asl (meters) to get translation offset
344 sgCopyVec3( asl_offset, up );
345 sgNormalizeVec3( asl_offset );
346 if ( alt <= layer_asl ) {
347 sgScaleVec3( asl_offset, layer_asl );
349 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
351 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
352 // << "," << asl_offset[2] << endl;
353 sgAddVec3( asl_offset, p );
354 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
355 // << "," << asl_offset[2] << endl;
357 // Translate to zero elevation
358 // Point3D zero_elev = current_view.get_cur_zero_elev();
359 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
360 sgMakeTransMat4( T1, asl_offset );
362 // printf(" Translated to %.2f %.2f %.2f\n",
363 // zero_elev.x, zero_elev.y, zero_elev.z );
365 // Rotate to proper orientation
366 // printf(" lon = %.2f lat = %.2f\n",
367 // lon * SGD_RADIANS_TO_DEGREES,
368 // lat * SGD_RADIANS_TO_DEGREES);
369 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
370 sgSetVec3( axis, 0.0, 0.0, 1.0 );
371 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
373 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
375 sgSetVec3( axis, 0.0, 1.0, 0.0 );
376 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
380 sgCopyMat4( TRANSFORM, T1 );
381 sgPreMultMat4( TRANSFORM, LON );
382 sgPreMultMat4( TRANSFORM, LAT );
385 sgSetCoord( &layerpos, TRANSFORM );
387 layer_transform->setTransform( &layerpos );
389 // now calculate update texture coordinates
390 if ( last_lon < -900 ) {
395 double sp_dist = speed*dt;
397 if ( lon != last_lon || lat != last_lat || sp_dist != 0 ) {
398 Point3D start( last_lon, last_lat, 0.0 );
399 Point3D dest( lon, lat, 0.0 );
400 double course = 0.0, dist = 0.0;
402 calc_gc_course_dist( dest, start, &course, &dist );
403 // cout << "course = " << course << ", dist = " << dist << endl;
405 // if start and dest are too close together,
406 // calc_gc_course_dist() can return a course of "nan". If
407 // this happens, lets just use the last known good course.
408 // This is a hack, and it would probably be better to make
409 // calc_gc_course_dist() more robust.
410 if ( isnan(course) ) {
411 course = last_course;
413 last_course = course;
416 // calculate cloud movement due to external forces
417 double ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0;
420 ax = cos(course) * dist;
421 ay = sin(course) * dist;
425 bx = cos(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
426 by = sin(-direction * SGD_DEGREES_TO_RADIANS) * sp_dist;
430 double xoff = (ax + bx) / (2 * scale);
431 double yoff = (ay + by) / (2 * scale);
433 const float layer_scale = layer_span / scale;
435 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
439 base = tl[0]->get( 0 );
442 // the while loops can lead to *long* pauses if base[0] comes
443 // with a bogus value.
444 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
445 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
446 if ( base[0] > -10.0 && base[0] < 10.0 ) {
447 base[0] -= (int)base[0];
449 SG_LOG(SG_ASTRO, SG_DEBUG,
450 "Error: base = " << base[0] << "," << base[1] <<
451 " course = " << course << " dist = " << dist );
456 // the while loops can lead to *long* pauses if base[0] comes
457 // with a bogus value.
458 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
459 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
460 if ( base[1] > -10.0 && base[1] < 10.0 ) {
461 base[1] -= (int)base[1];
463 SG_LOG(SG_ASTRO, SG_ALERT,
464 "Error: base = " << base[0] << "," << base[1] <<
465 " course = " << course << " dist = " << dist );
469 // cout << "base = " << base[0] << "," << base[1] << endl;
471 for (int i = 0; i < 4; i++)
473 tc = tl[i]->get( 0 );
474 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
476 for (int j = 0; j < 4; j++)
478 tc = tl[i]->get( j*2+1 );
479 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
480 base[1] + layer_scale * j/4 );
482 tc = tl[i]->get( (j+1)*2 );
483 sgSetVec2( tc, base[0] + layer_scale * i/4,
484 base[1] + layer_scale * (j+1)/4 );
487 tc = tl[i]->get( 9 );
488 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
489 base[1] + layer_scale );
500 void SGCloudLayer::draw( bool top ) {
501 if ( layer_coverage != SG_CLOUD_CLEAR ) {
502 state_sel->selectStep( top ? 1 : 0 );
503 ssgCullAndDraw( layer_root );
508 // make an ssgSimpleState for a cloud layer given the named texture
509 ssgSimpleState *sgCloudMakeState( const string &path ) {
510 ssgSimpleState *state = new ssgSimpleState();
512 SG_LOG(SG_ASTRO, SG_INFO, " texture = ");
514 state->setTexture( (char *)path.c_str() );
515 state->setShadeModel( GL_SMOOTH );
516 state->disable( GL_LIGHTING );
517 state->disable( GL_CULL_FACE );
518 state->enable( GL_TEXTURE_2D );
519 state->enable( GL_COLOR_MATERIAL );
520 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
521 state->setMaterial( GL_EMISSION, 0.05, 0.05, 0.05, 0.0 );
522 state->setMaterial( GL_AMBIENT, 0.2, 0.2, 0.2, 0.0 );
523 state->setMaterial( GL_DIFFUSE, 0.5, 0.5, 0.5, 0.0 );
524 state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
525 state->enable( GL_BLEND );
526 state->enable( GL_ALPHA_TEST );
527 state->setAlphaClamp( 0.01 );
529 // ref() the state so it doesn't get deleted if the last layer of
530 // it's type is deleted.