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.
28 #include <simgear/constants.h>
29 #include <simgear/math/fg_random.h>
30 #include <simgear/math/point3d.hxx>
31 #include <simgear/math/polar3d.hxx>
33 #include <Objects/matlib.hxx>
39 SGCloudLayer::SGCloudLayer( void ) {
44 SGCloudLayer::~SGCloudLayer( void ) {
48 // build the moon object
49 void SGCloudLayer::build( FGPath path, double s, double asl, double thickness,
50 double transition, SGCloudType type )
55 layer_thickness = thickness;
56 layer_transition = transition;
59 last_lon = last_lat = -999.0f;
61 // look up the appropriate cloud state
65 case SG_CLOUD_OVERCAST:
66 m = material_lib.find( "CloudOvercast" );
67 layer_state = m->get_state();
69 case SG_CLOUD_MOSTLY_CLOUDY:
70 m = material_lib.find( "CloudMostlyCloudy" );
71 layer_state = m->get_state();
73 case SG_CLOUD_MOSTLY_SUNNY:
74 m = material_lib.find( "CloudMostlySunny" );
75 layer_state = m->get_state();
78 m = material_lib.find( "CloudCirrus" );
79 layer_state = m->get_state();
83 cl = new ssgColourArray( 4 );
84 vl = new ssgVertexArray( 4 );
85 tl = new ssgTexCoordArray( 4 );
87 // build the cloud layer
91 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
93 sgSetVec3( vertex, -size, -size, 0.0f );
95 sgSetVec2( base, fg_random(), fg_random() );
96 sgSetVec2( tc, base[0], base[1] );
101 sgSetVec3( vertex, size, -size, 0.0f );
102 sgSetVec2( tc, base[0] + size / scale, base[1] );
107 sgSetVec3( vertex, -size, size, 0.0f );
108 sgSetVec2( tc, base[0], base[1] + size / scale );
113 sgSetVec3( vertex, size, size, 0.0f );
114 sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
120 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
121 layer->setState( layer_state );
123 // force a repaint of the moon colors with arbitrary defaults
126 // build the ssg scene graph sub tree for the sky and connected
127 // into the provide scene graph branch
128 layer_transform = new ssgTransform;
130 // moon_transform->addKid( halo );
131 layer_transform->addKid( layer );
133 layer_root = new ssgRoot;
134 layer_root->addKid( layer_transform );
138 // repaint the cloud layer colors
139 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
142 for ( int i = 0; i < 4; ++i ) {
143 color = cl->get( i );
144 sgCopyVec4( color, fog_color );
151 // reposition the cloud layer at the specified origin and orientation
152 // lon specifies a rotation about the Z axis
153 // lat specifies a rotation about the new Y axis
154 // spin specifies a rotation about the new Z axis (and orients the
155 // sunrise/set effects
156 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
162 // combine p and asl (meters) to get translation offset
164 sgCopyVec3( asl_offset, up );
165 sgNormalizeVec3( asl_offset );
166 if ( alt <= layer_asl ) {
167 sgScaleVec3( asl_offset, layer_asl );
169 sgScaleVec3( asl_offset, layer_asl + layer_thickness );
171 // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
172 // << "," << asl_offset[2] << endl;
173 sgAddVec3( asl_offset, p );
174 // cout << " asl_offset = " << asl_offset[0] << "," << asl_offset[1]
175 // << "," << asl_offset[2] << endl;
177 // Translate to zero elevation
178 // Point3D zero_elev = current_view.get_cur_zero_elev();
179 // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
180 sgMakeTransMat4( T1, asl_offset );
182 // printf(" Translated to %.2f %.2f %.2f\n",
183 // zero_elev.x, zero_elev.y, zero_elev.z );
185 // Rotate to proper orientation
186 // printf(" lon = %.2f lat = %.2f\n", FG_Longitude * RAD_TO_DEG,
187 // FG_Latitude * RAD_TO_DEG);
188 // xglRotatef( f->get_Longitude() * RAD_TO_DEG, 0.0, 0.0, 1.0 );
189 sgSetVec3( axis, 0.0, 0.0, 1.0 );
190 sgMakeRotMat4( LON, lon * RAD_TO_DEG, axis );
192 // xglRotatef( 90.0 - f->get_Latitude() * RAD_TO_DEG, 0.0, 1.0, 0.0 );
193 sgSetVec3( axis, 0.0, 1.0, 0.0 );
194 sgMakeRotMat4( LAT, 90.0 - lat * RAD_TO_DEG, axis );
198 sgCopyMat4( TRANSFORM, T1 );
199 sgPreMultMat4( TRANSFORM, LON );
200 sgPreMultMat4( TRANSFORM, LAT );
203 sgSetCoord( &layerpos, TRANSFORM );
205 layer_transform->setTransform( &layerpos );
207 // now calculate update texture coordinates
208 if ( last_lon < -900 ) {
213 if ( lon != last_lon || lat != last_lat ) {
214 Point3D start( last_lon, last_lat, 0.0 );
215 Point3D dest( lon, lat, 0.0 );
217 calc_gc_course_dist( dest, start, &course, &dist );
218 // cout << "course = " << course << ", dist = " << dist << endl;
220 double xoff = cos( course ) * dist / (2 * scale);
221 double yoff = sin( course ) * dist / (2 * scale);
223 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
229 while ( base[0] > 1.0 ) { base[0] -= 1.0; }
230 while ( base[0] < 0.0 ) { base[0] += 1.0; }
233 while ( base[1] > 1.0 ) { base[1] -= 1.0; }
234 while ( base[1] < 0.0 ) { base[1] += 1.0; }
236 // cout << "base = " << base[0] << "," << base[1] << endl;
239 sgSetVec2( tc, base[0] + size / scale, base[1] );
242 sgSetVec2( tc, base[0], base[1] + size / scale );
245 sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
255 void SGCloudLayer::draw() {
256 ssgCullAndDraw( layer_root );
260 // make an ssgSimpleState for a cloud layer given the named texture
261 ssgSimpleState *SGCloudMakeState( const string &path ) {
262 ssgSimpleState *state = new ssgSimpleState();
264 state->setTexture( (char *)path.c_str() );
265 state->setShadeModel( GL_SMOOTH );
266 state->disable( GL_LIGHTING );
267 state->disable( GL_CULL_FACE );
268 state->enable( GL_TEXTURE_2D );
269 state->enable( GL_COLOR_MATERIAL );
270 state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
271 state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
272 state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
273 state->enable( GL_BLEND );
274 state->enable( GL_ALPHA_TEST );
275 state->setAlphaClamp( 0.01 );