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>
35 SGCloudLayer::SGCloudLayer( void ) {
40 SGCloudLayer::~SGCloudLayer( void ) {
44 // build the cloud object
45 void SGCloudLayer::build( double s, double asl, double thickness,
46 double transition, ssgSimpleState *state )
51 layer_thickness = thickness;
52 layer_transition = transition;
55 last_lon = last_lat = -999.0f;
59 cl = new ssgColourArray( 4 );
60 vl = new ssgVertexArray( 4 );
61 tl = new ssgTexCoordArray( 4 );
63 // build the cloud layer
67 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
69 sgSetVec3( vertex, -size, -size, 0.0f );
71 sgSetVec2( base, sg_random(), sg_random() );
72 sgSetVec2( tc, base[0], base[1] );
77 sgSetVec3( vertex, size, -size, 0.0f );
78 sgSetVec2( tc, base[0] + size / scale, base[1] );
83 sgSetVec3( vertex, -size, size, 0.0f );
84 sgSetVec2( tc, base[0], base[1] + size / scale );
89 sgSetVec3( vertex, size, size, 0.0f );
90 sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
96 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
97 layer->setState( layer_state );
99 // force a repaint of the moon colors with arbitrary defaults
102 // build the ssg scene graph sub tree for the sky and connected
103 // into the provide scene graph branch
104 layer_transform = new ssgTransform;
106 // moon_transform->addKid( halo );
107 layer_transform->addKid( layer );
109 layer_root = new ssgRoot;
110 layer_root->addKid( layer_transform );
114 // repaint the cloud layer colors
115 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
118 for ( int i = 0; i < 4; ++i ) {
119 color = cl->get( i );
120 sgCopyVec4( color, fog_color );
127 // reposition the cloud layer at the specified origin and orientation
128 // lon specifies a rotation about the Z axis
129 // lat specifies a rotation about the new Y axis
130 // spin specifies a rotation about the new Z axis (and orients the
131 // sunrise/set effects
132 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
138 // combine p and asl (meters) to get translation offset
140 sgCopyVec3( asl_offset, up );
141 sgNormalizeVec3( asl_offset );
142 if ( alt <= layer_asl ) {
143 sgScaleVec3( asl_offset, layer_asl );
145 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
147 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
148 // << "," << asl_offset[2] << endl;
149 sgAddVec3( asl_offset, p );
150 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
151 // << "," << asl_offset[2] << endl;
153 // Translate to zero elevation
154 // Point3D zero_elev = current_view.get_cur_zero_elev();
155 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
156 sgMakeTransMat4( T1, asl_offset );
158 // printf(" Translated to %.2f %.2f %.2f\n",
159 // zero_elev.x, zero_elev.y, zero_elev.z );
161 // Rotate to proper orientation
162 // printf(" lon = %.2f lat = %.2f\n",
163 // lon * SGD_RADIANS_TO_DEGREES,
164 // lat * SGD_RADIANS_TO_DEGREES);
165 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
166 sgSetVec3( axis, 0.0, 0.0, 1.0 );
167 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
169 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
171 sgSetVec3( axis, 0.0, 1.0, 0.0 );
172 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
176 sgCopyMat4( TRANSFORM, T1 );
177 sgPreMultMat4( TRANSFORM, LON );
178 sgPreMultMat4( TRANSFORM, LAT );
181 sgSetCoord( &layerpos, TRANSFORM );
183 layer_transform->setTransform( &layerpos );
185 // now calculate update texture coordinates
186 if ( last_lon < -900 ) {
191 if ( lon != last_lon || lat != last_lat ) {
192 Point3D start( last_lon, last_lat, 0.0 );
193 Point3D dest( lon, lat, 0.0 );
195 calc_gc_course_dist( dest, start, &course, &dist );
196 // cout << "course = " << course << ", dist = " << dist << endl;
198 double xoff = cos( course ) * dist / (2 * scale);
199 double yoff = sin( course ) * dist / (2 * scale);
201 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
207 while ( base[0] > 1.0 ) { base[0] -= 1.0; }
208 while ( base[0] < 0.0 ) { base[0] += 1.0; }
211 while ( base[1] > 1.0 ) { base[1] -= 1.0; }
212 while ( base[1] < 0.0 ) { base[1] += 1.0; }
214 // cout << "base = " << base[0] << "," << base[1] << endl;
217 sgSetVec2( tc, base[0] + size / scale, base[1] );
220 sgSetVec2( tc, base[0], base[1] + size / scale );
223 sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
233 void SGCloudLayer::draw() {
234 ssgCullAndDraw( layer_root );
238 // make an ssgSimpleState for a cloud layer given the named texture
239 ssgSimpleState *SGCloudMakeState( const string &path ) {
240 ssgSimpleState *state = new ssgSimpleState();
242 state->setTexture( (char *)path.c_str() );
243 state->setShadeModel( GL_SMOOTH );
244 state->disable( GL_LIGHTING );
245 state->disable( GL_CULL_FACE );
246 state->enable( GL_TEXTURE_2D );
247 state->enable( GL_COLOR_MATERIAL );
248 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
249 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
250 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
251 state->enable( GL_BLEND );
252 state->enable( GL_ALPHA_TEST );
253 state->setAlphaClamp( 0.01 );