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>
36 SGCloudLayer::SGCloudLayer( void ) {
41 SGCloudLayer::~SGCloudLayer( void ) {
45 // build the cloud object
46 void SGCloudLayer::build( double s, double asl, double thickness,
47 double transition, ssgSimpleState *state )
52 layer_thickness = thickness;
53 layer_transition = transition;
56 last_lon = last_lat = -999.0f;
60 cl = new ssgColourArray( 4 );
61 vl = new ssgVertexArray( 4 );
62 tl = new ssgTexCoordArray( 4 );
64 // build the cloud layer
68 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
70 sgSetVec3( vertex, -size, -size, 0.0f );
72 sgSetVec2( base, sg_random(), sg_random() );
73 sgSetVec2( tc, base[0], base[1] );
78 sgSetVec3( vertex, size, -size, 0.0f );
79 sgSetVec2( tc, base[0] + size / scale, base[1] );
84 sgSetVec3( vertex, -size, size, 0.0f );
85 sgSetVec2( tc, base[0], base[1] + size / scale );
90 sgSetVec3( vertex, size, size, 0.0f );
91 sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
97 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
98 layer->setState( layer_state );
100 // force a repaint of the moon colors with arbitrary defaults
103 // build the ssg scene graph sub tree for the sky and connected
104 // into the provide scene graph branch
105 layer_transform = new ssgTransform;
107 // moon_transform->addKid( halo );
108 layer_transform->addKid( layer );
110 layer_root = new ssgRoot;
111 layer_root->addKid( layer_transform );
115 // repaint the cloud layer colors
116 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
119 for ( int i = 0; i < 4; ++i ) {
120 color = cl->get( i );
121 sgCopyVec4( color, fog_color );
128 // reposition the cloud layer at the specified origin and orientation
129 // lon specifies a rotation about the Z axis
130 // lat specifies a rotation about the new Y axis
131 // spin specifies a rotation about the new Z axis (and orients the
132 // sunrise/set effects
133 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
139 // combine p and asl (meters) to get translation offset
141 sgCopyVec3( asl_offset, up );
142 sgNormalizeVec3( asl_offset );
143 if ( alt <= layer_asl ) {
144 sgScaleVec3( asl_offset, layer_asl );
146 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
148 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
149 // << "," << asl_offset[2] << endl;
150 sgAddVec3( asl_offset, p );
151 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
152 // << "," << asl_offset[2] << endl;
154 // Translate to zero elevation
155 // Point3D zero_elev = current_view.get_cur_zero_elev();
156 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
157 sgMakeTransMat4( T1, asl_offset );
159 // printf(" Translated to %.2f %.2f %.2f\n",
160 // zero_elev.x, zero_elev.y, zero_elev.z );
162 // Rotate to proper orientation
163 // printf(" lon = %.2f lat = %.2f\n",
164 // lon * SGD_RADIANS_TO_DEGREES,
165 // lat * SGD_RADIANS_TO_DEGREES);
166 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
167 sgSetVec3( axis, 0.0, 0.0, 1.0 );
168 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
170 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
172 sgSetVec3( axis, 0.0, 1.0, 0.0 );
173 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
177 sgCopyMat4( TRANSFORM, T1 );
178 sgPreMultMat4( TRANSFORM, LON );
179 sgPreMultMat4( TRANSFORM, LAT );
182 sgSetCoord( &layerpos, TRANSFORM );
184 layer_transform->setTransform( &layerpos );
186 // now calculate update texture coordinates
187 if ( last_lon < -900 ) {
192 if ( lon != last_lon || lat != last_lat ) {
193 Point3D start( last_lon, last_lat, 0.0 );
194 Point3D dest( lon, lat, 0.0 );
196 calc_gc_course_dist( dest, start, &course, &dist );
197 // cout << "course = " << course << ", dist = " << dist << endl;
199 double xoff = cos( course ) * dist / (2 * scale);
200 double yoff = sin( course ) * dist / (2 * scale);
202 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
209 // the while loops can lead to *long* pauses if base[0] comes
210 // with a bogus value.
211 // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
212 // while ( base[0] < 0.0 ) { base[0] += 1.0; }
213 if ( base[0] > -10.0 && base[0] < 10.0 ) {
214 base[0] -= (int)base[0];
217 SG_LOG(SG_ASTRO, SG_ALERT,
218 "Error: base = " << base[0] << "," << base[1]);
222 // the while loops can lead to *long* pauses if base[0] comes
223 // with a bogus value.
224 // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
225 // while ( base[1] < 0.0 ) { base[1] += 1.0; }
226 if ( base[1] > -10.0 && base[1] < 10.0 ) {
227 base[1] -= (int)base[1];
230 SG_LOG(SG_ASTRO, SG_ALERT,
231 "Error: base = " << base[0] << "," << base[1]);
234 // cout << "base = " << base[0] << "," << base[1] << endl;
237 sgSetVec2( tc, base[0] + size / scale, base[1] );
240 sgSetVec2( tc, base[0], base[1] + size / scale );
243 sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
253 void SGCloudLayer::draw() {
254 ssgCullAndDraw( layer_root );
258 // make an ssgSimpleState for a cloud layer given the named texture
259 ssgSimpleState *SGCloudMakeState( const string &path ) {
260 ssgSimpleState *state = new ssgSimpleState();
262 state->setTexture( (char *)path.c_str() );
263 state->setShadeModel( GL_SMOOTH );
264 state->disable( GL_LIGHTING );
265 state->disable( GL_CULL_FACE );
266 state->enable( GL_TEXTURE_2D );
267 state->enable( GL_COLOR_MATERIAL );
268 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
269 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
270 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
271 state->enable( GL_BLEND );
272 state->enable( GL_ALPHA_TEST );
273 state->setAlphaClamp( 0.01 );