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 static ssgSimpleState *layer_states[SGCloudLayer::SG_MAX_CLOUD_COVERAGES];
37 static bool state_initialized = false;
41 SGCloudLayer::SGCloudLayer( const string &tex_path ) :
42 layer_root(new ssgRoot),
43 layer_transform(new ssgTransform),
44 texture_path(tex_path),
48 layer_transition(0.0),
49 layer_coverage(SG_CLOUD_CLEAR),
54 cl[0] = cl[1] = cl[2] = cl[3] = NULL;
55 vl[0] = vl[1] = vl[2] = vl[3] = NULL;
56 tl[0] = tl[1] = tl[2] = tl[3] = NULL;
57 layer[0] = layer[1] = layer[2] = layer[3] = NULL;
59 layer_root->addKid(layer_transform);
64 SGCloudLayer::~SGCloudLayer()
66 delete layer_root; // deletes layer_transform and layer as well
70 SGCloudLayer::getSpan_m () const
76 SGCloudLayer::setSpan_m (float span_m)
78 if (span_m != layer_span) {
85 SGCloudLayer::getElevation_m () const
91 SGCloudLayer::setElevation_m (float elevation_m)
93 layer_asl = elevation_m;
97 SGCloudLayer::getThickness_m () const
99 return layer_thickness;
103 SGCloudLayer::setThickness_m (float thickness_m)
105 layer_thickness = thickness_m;
109 SGCloudLayer::getTransition_m () const
111 return layer_transition;
115 SGCloudLayer::setTransition_m (float transition_m)
117 layer_transition = transition_m;
120 SGCloudLayer::Coverage
121 SGCloudLayer::getCoverage () const
123 return layer_coverage;
127 SGCloudLayer::setCoverage (Coverage coverage)
129 if (coverage != layer_coverage) {
130 layer_coverage = coverage;
136 // build the cloud object
138 SGCloudLayer::rebuild()
140 // Initialize states and sizes if necessary.
141 if ( !state_initialized ) {
142 state_initialized = true;
144 cout << "initializing cloud layers" << endl;
148 cloud_path.set(texture_path.str());
149 cloud_path.append("overcast.rgb");
150 layer_states[SG_CLOUD_OVERCAST] = sgCloudMakeState(cloud_path.str());
152 cloud_path.set(texture_path.str());
153 cloud_path.append("broken.rgba");
154 layer_states[SG_CLOUD_BROKEN]
155 = sgCloudMakeState(cloud_path.str());
157 cloud_path.set(texture_path.str());
158 cloud_path.append("scattered.rgba");
159 layer_states[SG_CLOUD_SCATTERED]
160 = sgCloudMakeState(cloud_path.str());
162 cloud_path.set(texture_path.str());
163 cloud_path.append("few.rgba");
164 layer_states[SG_CLOUD_FEW]
165 = sgCloudMakeState(cloud_path.str());
167 cloud_path.set(texture_path.str());
168 cloud_path.append("cirrus.rgba");
169 layer_states[SG_CLOUD_CIRRUS]
170 = sgCloudMakeState(cloud_path.str());
172 layer_states[SG_CLOUD_CLEAR] = 0;
176 last_lon = last_lat = -999.0f;
179 sgSetVec2( base, sg_random(), sg_random() );
181 // build the cloud layer
186 const float layer_scale = layer_span / scale;
187 const float mpi = SG_PI/4;
189 for (int i = 0; i < 4; i++)
191 if ( layer[i] != NULL ) {
192 layer_transform->removeKid(layer[i]); // automatic delete
195 vl[i] = new ssgVertexArray( 10 );
196 cl[i] = new ssgColourArray( 10 );
197 tl[i] = new ssgTexCoordArray( 10 );
200 sgSetVec3( vertex, layer_span*(i-2)/2, -layer_span,
201 500 * (sin(i*mpi) - 2) );
203 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
205 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 0) ? 0.0f : 0.15f );
208 vl[i]->add( vertex );
211 for (int j = 0; j < 4; j++)
213 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span*(j-2)/2,
214 500 * (sin((i+1)*mpi) + sin(j*mpi) - 2) );
216 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
217 base[1] + layer_scale * j/4 );
219 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
220 ( (j == 0) || (i == 3)) ?
221 ( (j == 0) && (i == 3)) ? 0.0f : 0.15f : 1.0f );
224 vl[i]->add( vertex );
228 sgSetVec3( vertex, layer_span*(i-2)/2, layer_span*(j-1)/2,
229 500 * (sin(i*mpi) + sin((j+1)*mpi) - 2) );
231 sgSetVec2( tc, base[0] + layer_scale * i/4,
232 base[1] + layer_scale * (j+1)/4 );
234 sgSetVec4( color, 1.0f, 1.0f, 1.0f,
235 ((j == 3) || (i == 0)) ?
236 ((j == 3) && (i == 0)) ? 0.0f : 0.15f : 1.0f );
238 vl[i]->add( vertex );
242 sgSetVec3( vertex, layer_span*(i-1)/2, layer_span,
243 500 * (sin((i+1)*mpi) - 2) );
245 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
246 base[1] + layer_scale );
248 sgSetVec4( color, 1.0f, 1.0f, 1.0f, (i == 3) ? 0.0f : 0.15f );
251 vl[i]->add( vertex );
254 layer[i] = new ssgVtxTable(GL_TRIANGLE_STRIP, vl[i], NULL, tl[i], cl[i]);
255 layer_transform->addKid( layer[i] );
257 if ( layer_states[layer_coverage] != NULL ) {
258 layer[i]->setState( layer_states[layer_coverage] );
262 // force a repaint of the sky colors with arbitrary defaults
268 // repaint the cloud layer colors
269 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
272 for ( int i = 0; i < 4; i++ )
273 for ( int j = 0; j < 10; ++j ) {
274 color = cl[i]->get( j );
275 sgCopyVec3( color, fog_color );
282 // reposition the cloud layer at the specified origin and orientation
283 // lon specifies a rotation about the Z axis
284 // lat specifies a rotation about the new Y axis
285 // spin specifies a rotation about the new Z axis (and orients the
286 // sunrise/set effects
287 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
293 // combine p and asl (meters) to get translation offset
295 sgCopyVec3( asl_offset, up );
296 sgNormalizeVec3( asl_offset );
297 if ( alt <= layer_asl ) {
298 sgScaleVec3( asl_offset, layer_asl );
300 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
302 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
303 // << "," << asl_offset[2] << endl;
304 sgAddVec3( asl_offset, p );
305 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
306 // << "," << asl_offset[2] << endl;
308 // Translate to zero elevation
309 // Point3D zero_elev = current_view.get_cur_zero_elev();
310 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
311 sgMakeTransMat4( T1, asl_offset );
313 // printf(" Translated to %.2f %.2f %.2f\n",
314 // zero_elev.x, zero_elev.y, zero_elev.z );
316 // Rotate to proper orientation
317 // printf(" lon = %.2f lat = %.2f\n",
318 // lon * SGD_RADIANS_TO_DEGREES,
319 // lat * SGD_RADIANS_TO_DEGREES);
320 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
321 sgSetVec3( axis, 0.0, 0.0, 1.0 );
322 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
324 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
326 sgSetVec3( axis, 0.0, 1.0, 0.0 );
327 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
331 sgCopyMat4( TRANSFORM, T1 );
332 sgPreMultMat4( TRANSFORM, LON );
333 sgPreMultMat4( TRANSFORM, LAT );
336 sgSetCoord( &layerpos, TRANSFORM );
338 layer_transform->setTransform( &layerpos );
340 // now calculate update texture coordinates
341 if ( last_lon < -900 ) {
346 if ( lon != last_lon || lat != last_lat ) {
347 Point3D start( last_lon, last_lat, 0.0 );
348 Point3D dest( lon, lat, 0.0 );
350 calc_gc_course_dist( dest, start, &course, &dist );
351 // cout << "course = " << course << ", dist = " << dist << endl;
353 double xoff = cos( course ) * dist / (2 * scale);
354 double yoff = sin( course ) * dist / (2 * scale);
356 const float layer_scale = layer_span / scale;
358 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
362 base = tl[0]->get( 0 );
365 // the while loops can lead to *long* pauses if base[0] comes
366 // with a bogus value.
367 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
368 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
369 if ( base[0] > -10.0 && base[0] < 10.0 ) {
370 base[0] -= (int)base[0];
373 SG_LOG(SG_ASTRO, SG_DEBUG,
374 "Error: base = " << base[0] << "," << base[1]);
378 // the while loops can lead to *long* pauses if base[0] comes
379 // with a bogus value.
380 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
381 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
382 if ( base[1] > -10.0 && base[1] < 10.0 ) {
383 base[1] -= (int)base[1];
386 SG_LOG(SG_ASTRO, SG_ALERT,
387 "Error: base = " << base[0] << "," << base[1]);
390 // cout << "base = " << base[0] << "," << base[1] << endl;
392 for (int i = 0; i < 4; i++)
394 tc = tl[i]->get( 0 );
395 sgSetVec2( tc, base[0] + layer_scale * i/4, base[1] );
397 for (int j = 0; j < 4; j++)
399 tc = tl[i]->get( j*2+1 );
400 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
401 base[1] + layer_scale * j/4 );
403 tc = tl[i]->get( (j+1)*2 );
404 sgSetVec2( tc, base[0] + layer_scale * i/4,
405 base[1] + layer_scale * (j+1)/4 );
408 tc = tl[i]->get( 9 );
409 sgSetVec2( tc, base[0] + layer_scale * (i+1)/4,
410 base[1] + layer_scale );
421 void SGCloudLayer::draw() {
422 if ( layer_coverage != SG_CLOUD_CLEAR ) {
423 ssgCullAndDraw( layer_root );
428 // make an ssgSimpleState for a cloud layer given the named texture
429 ssgSimpleState *sgCloudMakeState( const string &path ) {
430 ssgSimpleState *state = new ssgSimpleState();
432 cout << " texture = " << path << endl;
434 state->setTexture( (char *)path.c_str() );
435 state->setShadeModel( GL_SMOOTH );
436 state->disable( GL_LIGHTING );
437 state->disable( GL_CULL_FACE );
438 state->enable( GL_TEXTURE_2D );
439 state->enable( GL_COLOR_MATERIAL );
440 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
441 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
442 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
443 state->enable( GL_BLEND );
444 state->enable( GL_ALPHA_TEST );
445 state->setAlphaClamp( 0.01 );
447 // ref() the state so it doesn't get deleted if the last layer of
448 // it's type is deleted.