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( void ) {
57 SGCloudLayer::getSpan_m () const
63 SGCloudLayer::setSpan_m (float span_m)
69 SGCloudLayer::getElevation_m () const
75 SGCloudLayer::setElevation_m (float elevation_m)
77 layer_asl = elevation_m;
81 SGCloudLayer::getThickness_m () const
83 return layer_thickness;
87 SGCloudLayer::setThickness_m (float thickness_m)
89 layer_thickness = thickness_m;
93 SGCloudLayer::getTransition_m () const
95 return layer_transition;
99 SGCloudLayer::setTransition_m (float transition_m)
101 layer_transition = transition_m;
105 SGCloudLayer::getType () const
111 SGCloudLayer::setType (Type type)
118 // build the cloud object
120 SGCloudLayer::rebuild()
122 // Initialize states and sizes if necessary.
123 if (layer_states[0] == 0) {
126 cloud_path.set(texture_path.str());
127 cloud_path.append("overcast.rgb");
128 layer_states[SG_CLOUD_OVERCAST] = SGCloudMakeState(cloud_path.str());
130 cloud_path.set(texture_path.str());
131 cloud_path.append("mostlycloudy.rgba");
132 layer_states[SG_CLOUD_MOSTLY_CLOUDY] =
133 SGCloudMakeState(cloud_path.str());
135 cloud_path.set(texture_path.str());
136 cloud_path.append("mostlysunny.rgba");
137 layer_states[SG_CLOUD_MOSTLY_SUNNY] = SGCloudMakeState(cloud_path.str());
139 cloud_path.set(texture_path.str());
140 cloud_path.append("cirrus.rgba");
141 layer_states[SG_CLOUD_CIRRUS] = SGCloudMakeState(cloud_path.str());
143 layer_states[SG_CLOUD_CLEAR] = 0;
150 last_lon = last_lat = -999.0f;
152 cl = new ssgColourArray( 4 );
153 vl = new ssgVertexArray( 4 );
154 tl = new ssgTexCoordArray( 4 );
156 // build the cloud layer
160 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
162 sgSetVec3( vertex, -layer_span, -layer_span, 0.0f );
164 sgSetVec2( base, sg_random(), sg_random() );
165 sgSetVec2( tc, base[0], base[1] );
170 sgSetVec3( vertex, layer_span, -layer_span, 0.0f );
171 sgSetVec2( tc, base[0] + layer_span / scale, base[1] );
176 sgSetVec3( vertex, -layer_span, layer_span, 0.0f );
177 sgSetVec2( tc, base[0], base[1] + layer_span / scale );
182 sgSetVec3( vertex, layer_span, layer_span, 0.0f );
183 sgSetVec2( tc, base[0] + layer_span / scale, base[1] + layer_span / scale );
189 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
190 if (layer_states[layer_type] != 0)
191 layer->setState( layer_states[layer_type] );
193 // force a repaint of the moon colors with arbitrary defaults
196 // build the ssg scene graph sub tree for the sky and connected
197 // into the provide scene graph branch
198 layer_transform = new ssgTransform;
200 // moon_transform->addKid( halo );
201 layer_transform->addKid( layer );
203 layer_root = new ssgRoot;
204 layer_root->addKid( layer_transform );
208 // repaint the cloud layer colors
209 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
212 for ( int i = 0; i < 4; ++i ) {
213 color = cl->get( i );
214 sgCopyVec4( color, fog_color );
221 // reposition the cloud layer at the specified origin and orientation
222 // lon specifies a rotation about the Z axis
223 // lat specifies a rotation about the new Y axis
224 // spin specifies a rotation about the new Z axis (and orients the
225 // sunrise/set effects
226 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
232 // combine p and asl (meters) to get translation offset
234 sgCopyVec3( asl_offset, up );
235 sgNormalizeVec3( asl_offset );
236 if ( alt <= layer_asl ) {
237 sgScaleVec3( asl_offset, layer_asl );
239 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
241 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
242 // << "," << asl_offset[2] << endl;
243 sgAddVec3( asl_offset, p );
244 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
245 // << "," << asl_offset[2] << endl;
247 // Translate to zero elevation
248 // Point3D zero_elev = current_view.get_cur_zero_elev();
249 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
250 sgMakeTransMat4( T1, asl_offset );
252 // printf(" Translated to %.2f %.2f %.2f\n",
253 // zero_elev.x, zero_elev.y, zero_elev.z );
255 // Rotate to proper orientation
256 // printf(" lon = %.2f lat = %.2f\n",
257 // lon * SGD_RADIANS_TO_DEGREES,
258 // lat * SGD_RADIANS_TO_DEGREES);
259 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
260 sgSetVec3( axis, 0.0, 0.0, 1.0 );
261 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
263 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
265 sgSetVec3( axis, 0.0, 1.0, 0.0 );
266 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
270 sgCopyMat4( TRANSFORM, T1 );
271 sgPreMultMat4( TRANSFORM, LON );
272 sgPreMultMat4( TRANSFORM, LAT );
275 sgSetCoord( &layerpos, TRANSFORM );
277 layer_transform->setTransform( &layerpos );
279 // now calculate update texture coordinates
280 if ( last_lon < -900 ) {
285 if ( lon != last_lon || lat != last_lat ) {
286 Point3D start( last_lon, last_lat, 0.0 );
287 Point3D dest( lon, lat, 0.0 );
289 calc_gc_course_dist( dest, start, &course, &dist );
290 // cout << "course = " << course << ", dist = " << dist << endl;
292 double xoff = cos( course ) * dist / (2 * scale);
293 double yoff = sin( course ) * dist / (2 * scale);
295 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
302 // the while loops can lead to *long* pauses if base[0] comes
303 // with a bogus value.
304 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
305 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
306 if ( base[0] > -10.0 && base[0] < 10.0 ) {
307 base[0] -= (int)base[0];
310 SG_LOG(SG_ASTRO, SG_ALERT,
311 "Error: base = " << base[0] << "," << base[1]);
315 // the while loops can lead to *long* pauses if base[0] comes
316 // with a bogus value.
317 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
318 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
319 if ( base[1] > -10.0 && base[1] < 10.0 ) {
320 base[1] -= (int)base[1];
323 SG_LOG(SG_ASTRO, SG_ALERT,
324 "Error: base = " << base[0] << "," << base[1]);
327 // cout << "base = " << base[0] << "," << base[1] << endl;
330 sgSetVec2( tc, base[0] + layer_span / scale, base[1] );
333 sgSetVec2( tc, base[0], base[1] + layer_span / scale );
336 sgSetVec2( tc, base[0] + layer_span / scale, base[1] + layer_span / scale );
346 void SGCloudLayer::draw() {
347 ssgCullAndDraw( layer_root );
351 // make an ssgSimpleState for a cloud layer given the named texture
352 ssgSimpleState *SGCloudMakeState( const string &path ) {
353 ssgSimpleState *state = new ssgSimpleState();
355 state->setTexture( (char *)path.c_str() );
356 state->setShadeModel( GL_SMOOTH );
357 state->disable( GL_LIGHTING );
358 state->disable( GL_CULL_FACE );
359 state->enable( GL_TEXTURE_2D );
360 state->enable( GL_COLOR_MATERIAL );
361 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
362 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
363 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
364 state->enable( GL_BLEND );
365 state->enable( GL_ALPHA_TEST );
366 state->setAlphaClamp( 0.01 );