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.
29 #include <simgear/math/fg_random.h>
30 #include <simgear/math/point3d.hxx>
31 #include <simgear/math/polar3d.hxx>
37 SGCloudLayer::SGCloudLayer( void ) {
42 SGCloudLayer::~SGCloudLayer( void ) {
46 // build the moon object
47 void SGCloudLayer::build( double s, double asl, double thickness,
48 double transition, ssgSimpleState *state )
53 layer_thickness = thickness;
54 layer_transition = transition;
57 last_lon = last_lat = -999.0f;
61 cl = new ssgColourArray( 4 );
62 vl = new ssgVertexArray( 4 );
63 tl = new ssgTexCoordArray( 4 );
65 // build the cloud layer
69 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
71 sgSetVec3( vertex, -size, -size, 0.0f );
73 sgSetVec2( base, fg_random(), fg_random() );
74 sgSetVec2( tc, base[0], base[1] );
79 sgSetVec3( vertex, size, -size, 0.0f );
80 sgSetVec2( tc, base[0] + size / scale, base[1] );
85 sgSetVec3( vertex, -size, size, 0.0f );
86 sgSetVec2( tc, base[0], base[1] + size / scale );
91 sgSetVec3( vertex, size, size, 0.0f );
92 sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
98 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
99 layer->setState( layer_state );
101 // force a repaint of the moon colors with arbitrary defaults
104 // build the ssg scene graph sub tree for the sky and connected
105 // into the provide scene graph branch
106 layer_transform = new ssgTransform;
108 // moon_transform->addKid( halo );
109 layer_transform->addKid( layer );
111 layer_root = new ssgRoot;
112 layer_root->addKid( layer_transform );
116 // repaint the cloud layer colors
117 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
120 for ( int i = 0; i < 4; ++i ) {
121 color = cl->get( i );
122 sgCopyVec4( color, fog_color );
129 // reposition the cloud layer at the specified origin and orientation
130 // lon specifies a rotation about the Z axis
131 // lat specifies a rotation about the new Y axis
132 // spin specifies a rotation about the new Z axis (and orients the
133 // sunrise/set effects
134 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
140 // combine p and asl (meters) to get translation offset
142 sgCopyVec3( asl_offset, up );
143 sgNormalizeVec3( asl_offset );
144 if ( alt <= layer_asl ) {
145 sgScaleVec3( asl_offset, layer_asl );
147 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
149 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
150 // << "," << asl_offset[2] << endl;
151 sgAddVec3( asl_offset, p );
152 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
153 // << "," << asl_offset[2] << endl;
155 // Translate to zero elevation
156 // Point3D zero_elev = current_view.get_cur_zero_elev();
157 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
158 sgMakeTransMat4( T1, asl_offset );
160 // printf(" Translated to %.2f %.2f %.2f\n",
161 // zero_elev.x, zero_elev.y, zero_elev.z );
163 // Rotate to proper orientation
164 // printf(" lon = %.2f lat = %.2f\n",
165 // FG_Longitude * SGD_RADIANS_TO_DEGREES,
166 // FG_Latitude * SGD_RADIANS_TO_DEGREES);
167 // xglRotatef( f->get_Longitude() * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
168 sgSetVec3( axis, 0.0, 0.0, 1.0 );
169 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
171 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
173 sgSetVec3( axis, 0.0, 1.0, 0.0 );
174 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
178 sgCopyMat4( TRANSFORM, T1 );
179 sgPreMultMat4( TRANSFORM, LON );
180 sgPreMultMat4( TRANSFORM, LAT );
183 sgSetCoord( &layerpos, TRANSFORM );
185 layer_transform->setTransform( &layerpos );
187 // now calculate update texture coordinates
188 if ( last_lon < -900 ) {
193 if ( lon != last_lon || lat != last_lat ) {
194 Point3D start( last_lon, last_lat, 0.0 );
195 Point3D dest( lon, lat, 0.0 );
197 calc_gc_course_dist( dest, start, &course, &dist );
198 // cout << "course = " << course << ", dist = " << dist << endl;
200 double xoff = cos( course ) * dist / (2 * scale);
201 double yoff = sin( course ) * dist / (2 * scale);
203 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
209 while ( base[0] > 1.0 ) { base[0] -= 1.0; }
210 while ( base[0] < 0.0 ) { base[0] += 1.0; }
213 while ( base[1] > 1.0 ) { base[1] -= 1.0; }
214 while ( base[1] < 0.0 ) { base[1] += 1.0; }
216 // cout << "base = " << base[0] << "," << base[1] << endl;
219 sgSetVec2( tc, base[0] + size / scale, base[1] );
222 sgSetVec2( tc, base[0], base[1] + size / scale );
225 sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
235 void SGCloudLayer::draw() {
236 ssgCullAndDraw( layer_root );
240 // make an ssgSimpleState for a cloud layer given the named texture
241 ssgSimpleState *SGCloudMakeState( const string &path ) {
242 ssgSimpleState *state = new ssgSimpleState();
244 state->setTexture( (char *)path.c_str() );
245 state->setShadeModel( GL_SMOOTH );
246 state->disable( GL_LIGHTING );
247 state->disable( GL_CULL_FACE );
248 state->enable( GL_TEXTURE_2D );
249 state->enable( GL_COLOR_MATERIAL );
250 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
251 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
252 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
253 state->enable( GL_BLEND );
254 state->enable( GL_ALPHA_TEST );
255 state->setAlphaClamp( 0.01 );