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_TYPES];
40 SGCloudLayer::SGCloudLayer( const string &tex_path )
43 texture_path(tex_path),
47 layer_type(SG_CLOUD_CLEAR)
53 SGCloudLayer::~SGCloudLayer()
59 SGCloudLayer::getSpan_m () const
65 SGCloudLayer::setSpan_m (float span_m)
67 if (span_m != layer_span) {
74 SGCloudLayer::getElevation_m () const
80 SGCloudLayer::setElevation_m (float elevation_m)
82 layer_asl = elevation_m;
86 SGCloudLayer::getThickness_m () const
88 return layer_thickness;
92 SGCloudLayer::setThickness_m (float thickness_m)
94 layer_thickness = thickness_m;
98 SGCloudLayer::getTransition_m () const
100 return layer_transition;
104 SGCloudLayer::setTransition_m (float transition_m)
106 layer_transition = transition_m;
110 SGCloudLayer::getType () const
116 SGCloudLayer::setType (Type type)
118 if (type != layer_type) {
125 // build the cloud object
127 SGCloudLayer::rebuild()
129 // Initialize states and sizes if necessary.
130 if (layer_states[0] == 0) {
133 cloud_path.set(texture_path.str());
134 cloud_path.append("overcast.rgb");
135 layer_states[SG_CLOUD_OVERCAST] = SGCloudMakeState(cloud_path.str());
137 cloud_path.set(texture_path.str());
138 cloud_path.append("mostlycloudy.rgba");
139 layer_states[SG_CLOUD_MOSTLY_CLOUDY] =
140 SGCloudMakeState(cloud_path.str());
142 cloud_path.set(texture_path.str());
143 cloud_path.append("mostlysunny.rgba");
144 layer_states[SG_CLOUD_MOSTLY_SUNNY] = SGCloudMakeState(cloud_path.str());
146 cloud_path.set(texture_path.str());
147 cloud_path.append("cirrus.rgba");
148 layer_states[SG_CLOUD_CIRRUS] = SGCloudMakeState(cloud_path.str());
150 layer_states[SG_CLOUD_CLEAR] = 0;
153 // This should automatically delete
154 // layer_transform as well.
161 last_lon = last_lat = -999.0f;
163 cl = new ssgColourArray( 4 );
164 vl = new ssgVertexArray( 4 );
165 tl = new ssgTexCoordArray( 4 );
167 // build the cloud layer
171 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
173 sgSetVec3( vertex, -layer_span, -layer_span, 0.0f );
175 sgSetVec2( base, sg_random(), sg_random() );
176 sgSetVec2( tc, base[0], base[1] );
181 sgSetVec3( vertex, layer_span, -layer_span, 0.0f );
182 sgSetVec2( tc, base[0] + layer_span / scale, base[1] );
187 sgSetVec3( vertex, -layer_span, layer_span, 0.0f );
188 sgSetVec2( tc, base[0], base[1] + layer_span / scale );
193 sgSetVec3( vertex, layer_span, layer_span, 0.0f );
194 sgSetVec2( tc, base[0] + layer_span / scale, base[1] + layer_span / scale );
200 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
201 if (layer_states[layer_type] != 0)
202 layer->setState( layer_states[layer_type] );
204 // force a repaint of the moon colors with arbitrary defaults
207 // build the ssg scene graph sub tree for the sky and connected
208 // into the provide scene graph branch
209 layer_transform = new ssgTransform;
211 // moon_transform->addKid( halo );
212 layer_transform->addKid( layer );
214 layer_root = new ssgRoot;
215 layer_root->addKid( layer_transform );
219 // repaint the cloud layer colors
220 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
223 for ( int i = 0; i < 4; ++i ) {
224 color = cl->get( i );
225 sgCopyVec4( color, fog_color );
232 // reposition the cloud layer at the specified origin and orientation
233 // lon specifies a rotation about the Z axis
234 // lat specifies a rotation about the new Y axis
235 // spin specifies a rotation about the new Z axis (and orients the
236 // sunrise/set effects
237 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
243 // combine p and asl (meters) to get translation offset
245 sgCopyVec3( asl_offset, up );
246 sgNormalizeVec3( asl_offset );
247 if ( alt <= layer_asl ) {
248 sgScaleVec3( asl_offset, layer_asl );
250 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
252 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
253 // << "," << asl_offset[2] << endl;
254 sgAddVec3( asl_offset, p );
255 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
256 // << "," << asl_offset[2] << endl;
258 // Translate to zero elevation
259 // Point3D zero_elev = current_view.get_cur_zero_elev();
260 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
261 sgMakeTransMat4( T1, asl_offset );
263 // printf(" Translated to %.2f %.2f %.2f\n",
264 // zero_elev.x, zero_elev.y, zero_elev.z );
266 // Rotate to proper orientation
267 // printf(" lon = %.2f lat = %.2f\n",
268 // lon * SGD_RADIANS_TO_DEGREES,
269 // lat * SGD_RADIANS_TO_DEGREES);
270 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
271 sgSetVec3( axis, 0.0, 0.0, 1.0 );
272 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
274 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
276 sgSetVec3( axis, 0.0, 1.0, 0.0 );
277 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
281 sgCopyMat4( TRANSFORM, T1 );
282 sgPreMultMat4( TRANSFORM, LON );
283 sgPreMultMat4( TRANSFORM, LAT );
286 sgSetCoord( &layerpos, TRANSFORM );
288 layer_transform->setTransform( &layerpos );
290 // now calculate update texture coordinates
291 if ( last_lon < -900 ) {
296 if ( lon != last_lon || lat != last_lat ) {
297 Point3D start( last_lon, last_lat, 0.0 );
298 Point3D dest( lon, lat, 0.0 );
300 calc_gc_course_dist( dest, start, &course, &dist );
301 // cout << "course = " << course << ", dist = " << dist << endl;
303 double xoff = cos( course ) * dist / (2 * scale);
304 double yoff = sin( course ) * dist / (2 * scale);
306 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
313 // the while loops can lead to *long* pauses if base[0] comes
314 // with a bogus value.
315 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
316 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
317 if ( base[0] > -10.0 && base[0] < 10.0 ) {
318 base[0] -= (int)base[0];
321 SG_LOG(SG_ASTRO, SG_ALERT,
322 "Error: base = " << base[0] << "," << base[1]);
326 // the while loops can lead to *long* pauses if base[0] comes
327 // with a bogus value.
328 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
329 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
330 if ( base[1] > -10.0 && base[1] < 10.0 ) {
331 base[1] -= (int)base[1];
334 SG_LOG(SG_ASTRO, SG_ALERT,
335 "Error: base = " << base[0] << "," << base[1]);
338 // cout << "base = " << base[0] << "," << base[1] << endl;
341 sgSetVec2( tc, base[0] + layer_span / scale, base[1] );
344 sgSetVec2( tc, base[0], base[1] + layer_span / scale );
347 sgSetVec2( tc, base[0] + layer_span / scale, base[1] + layer_span / scale );
357 void SGCloudLayer::draw() {
358 if (layer_type != SG_CLOUD_CLEAR)
359 ssgCullAndDraw( layer_root );
363 // make an ssgSimpleState for a cloud layer given the named texture
364 ssgSimpleState *SGCloudMakeState( const string &path ) {
365 ssgSimpleState *state = new ssgSimpleState();
367 state->setTexture( (char *)path.c_str() );
368 state->setShadeModel( GL_SMOOTH );
369 state->disable( GL_LIGHTING );
370 state->disable( GL_CULL_FACE );
371 state->enable( GL_TEXTURE_2D );
372 state->enable( GL_COLOR_MATERIAL );
373 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
374 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
375 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
376 state->enable( GL_BLEND );
377 state->enable( GL_ALPHA_TEST );
378 state->setAlphaClamp( 0.01 );