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.
23 #include <simgear/compiler.h>
31 #include <simgear/math/point3d.hxx>
32 #include <simgear/math/polar3d.hxx>
33 #include <simgear/math/sg_random.h>
39 SGCloudLayer::SGCloudLayer( void ) {
44 SGCloudLayer::~SGCloudLayer( void ) {
48 // build the moon object
49 void SGCloudLayer::build( double s, double asl, double thickness,
50 double transition, ssgSimpleState *state )
55 layer_thickness = thickness;
56 layer_transition = transition;
59 last_lon = last_lat = -999.0f;
63 cl = new ssgColourArray( 4 );
64 vl = new ssgVertexArray( 4 );
65 tl = new ssgTexCoordArray( 4 );
67 // build the cloud layer
71 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
73 sgSetVec3( vertex, -size, -size, 0.0f );
75 sgSetVec2( base, sg_random(), sg_random() );
76 sgSetVec2( tc, base[0], base[1] );
81 sgSetVec3( vertex, size, -size, 0.0f );
82 sgSetVec2( tc, base[0] + size / scale, base[1] );
87 sgSetVec3( vertex, -size, size, 0.0f );
88 sgSetVec2( tc, base[0], base[1] + size / scale );
93 sgSetVec3( vertex, size, size, 0.0f );
94 sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
100 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
101 layer->setState( layer_state );
103 // force a repaint of the moon colors with arbitrary defaults
106 // build the ssg scene graph sub tree for the sky and connected
107 // into the provide scene graph branch
108 layer_transform = new ssgTransform;
110 // moon_transform->addKid( halo );
111 layer_transform->addKid( layer );
113 layer_root = new ssgRoot;
114 layer_root->addKid( layer_transform );
118 // repaint the cloud layer colors
119 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
122 for ( int i = 0; i < 4; ++i ) {
123 color = cl->get( i );
124 sgCopyVec4( color, fog_color );
131 // reposition the cloud layer at the specified origin and orientation
132 // lon specifies a rotation about the Z axis
133 // lat specifies a rotation about the new Y axis
134 // spin specifies a rotation about the new Z axis (and orients the
135 // sunrise/set effects
136 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
142 // combine p and asl (meters) to get translation offset
144 sgCopyVec3( asl_offset, up );
145 sgNormalizeVec3( asl_offset );
146 if ( alt <= layer_asl ) {
147 sgScaleVec3( asl_offset, layer_asl );
149 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
151 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
152 // << "," << asl_offset[2] << endl;
153 sgAddVec3( asl_offset, p );
154 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
155 // << "," << asl_offset[2] << endl;
157 // Translate to zero elevation
158 // Point3D zero_elev = current_view.get_cur_zero_elev();
159 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
160 sgMakeTransMat4( T1, asl_offset );
162 // printf(" Translated to %.2f %.2f %.2f\n",
163 // zero_elev.x, zero_elev.y, zero_elev.z );
165 // Rotate to proper orientation
166 // printf(" lon = %.2f lat = %.2f\n",
167 // lon * SGD_RADIANS_TO_DEGREES,
168 // lat * SGD_RADIANS_TO_DEGREES);
169 // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
170 sgSetVec3( axis, 0.0, 0.0, 1.0 );
171 sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
173 // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
175 sgSetVec3( axis, 0.0, 1.0, 0.0 );
176 sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
180 sgCopyMat4( TRANSFORM, T1 );
181 sgPreMultMat4( TRANSFORM, LON );
182 sgPreMultMat4( TRANSFORM, LAT );
185 sgSetCoord( &layerpos, TRANSFORM );
187 layer_transform->setTransform( &layerpos );
189 // now calculate update texture coordinates
190 if ( last_lon < -900 ) {
195 if ( lon != last_lon || lat != last_lat ) {
196 Point3D start( last_lon, last_lat, 0.0 );
197 Point3D dest( lon, lat, 0.0 );
199 calc_gc_course_dist( dest, start, &course, &dist );
200 // cout << "course = " << course << ", dist = " << dist << endl;
202 double xoff = cos( course ) * dist / (2 * scale);
203 double yoff = sin( course ) * dist / (2 * scale);
205 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
211 while ( base[0] > 1.0 ) { base[0] -= 1.0; }
212 while ( base[0] < 0.0 ) { base[0] += 1.0; }
215 while ( base[1] > 1.0 ) { base[1] -= 1.0; }
216 while ( base[1] < 0.0 ) { base[1] += 1.0; }
218 // cout << "base = " << base[0] << "," << base[1] << endl;
221 sgSetVec2( tc, base[0] + size / scale, base[1] );
224 sgSetVec2( tc, base[0], base[1] + size / scale );
227 sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
237 void SGCloudLayer::draw() {
238 ssgCullAndDraw( layer_root );
242 // make an ssgSimpleState for a cloud layer given the named texture
243 ssgSimpleState *SGCloudMakeState( const string &path ) {
244 ssgSimpleState *state = new ssgSimpleState();
246 state->setTexture( (char *)path.c_str() );
247 state->setShadeModel( GL_SMOOTH );
248 state->disable( GL_LIGHTING );
249 state->disable( GL_CULL_FACE );
250 state->enable( GL_TEXTURE_2D );
251 state->enable( GL_COLOR_MATERIAL );
252 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
253 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
254 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
255 state->enable( GL_BLEND );
256 state->enable( GL_ALPHA_TEST );
257 state->setAlphaClamp( 0.01 );