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/point3d.hxx>
30 #include <simgear/math/polar3d.hxx>
36 SGCloudLayer::SGCloudLayer( void ) {
41 SGCloudLayer::~SGCloudLayer( void ) {
45 // build the moon object
46 ssgBranch * SGCloudLayer::build( FGPath path, double s, double asl ) {
50 last_lon = last_lat = -999.0f;
52 // set up the cloud state
53 path.append( "cloud.rgba" );
54 layer_state = new ssgSimpleState();
55 layer_state->setTexture( (char *)path.c_str() );
56 layer_state->setShadeModel( GL_SMOOTH );
57 layer_state->disable( GL_LIGHTING );
58 layer_state->disable( GL_CULL_FACE );
59 layer_state->enable( GL_TEXTURE_2D );
60 layer_state->enable( GL_COLOR_MATERIAL );
61 layer_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
62 layer_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
63 layer_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
64 layer_state->enable( GL_BLEND );
65 layer_state->enable( GL_ALPHA_TEST );
66 layer_state->setAlphaClamp( 0.01 );
68 cl = new ssgColourArray( 4 );
69 vl = new ssgVertexArray( 4 );
70 tl = new ssgTexCoordArray( 4 );
72 // build the cloud layer
76 sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
78 sgSetVec3( vertex, -size, -size, 0.0f );
79 sgSetVec2( tc, 0.0f, 0.0f );
84 sgSetVec3( vertex, size, -size, 0.0f );
85 sgSetVec2( tc, size / 1000.0f, 0.0f );
90 sgSetVec3( vertex, -size, size, 0.0f );
91 sgSetVec2( tc, 0.0f, size / 1000.0f );
96 sgSetVec3( vertex, size, size, 0.0f );
97 sgSetVec2( tc, size / 1000.0f, size / 1000.0f );
103 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
104 layer->setState( layer_state );
106 // force a repaint of the moon colors with arbitrary defaults
109 // build the ssg scene graph sub tree for the sky and connected
110 // into the provide scene graph branch
111 layer_transform = new ssgTransform;
113 // moon_transform->addKid( halo );
114 layer_transform->addKid( layer );
116 return layer_transform;
120 // repaint the cloud layer colors
121 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
124 for ( int i = 0; i < 4; ++i ) {
125 color = cl->get( i );
126 sgCopyVec4( color, fog_color );
133 // reposition the cloud layer at the specified origin and orientation
134 // lon specifies a rotation about the Z axis
135 // lat specifies a rotation about the new Y axis
136 // spin specifies a rotation about the new Z axis (and orients the
137 // sunrise/set effects
138 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 sgScaleVec3( asl_offset, layer_asl );
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", FG_Longitude * RAD_TO_DEG,
163 // FG_Latitude * RAD_TO_DEG);
164 // xglRotatef( f->get_Longitude() * RAD_TO_DEG, 0.0, 0.0, 1.0 );
165 sgSetVec3( axis, 0.0, 0.0, 1.0 );
166 sgMakeRotMat4( LON, lon * RAD_TO_DEG, axis );
168 // xglRotatef( 90.0 - f->get_Latitude() * RAD_TO_DEG, 0.0, 1.0, 0.0 );
169 sgSetVec3( axis, 0.0, 1.0, 0.0 );
170 sgMakeRotMat4( LAT, 90.0 - lat * RAD_TO_DEG, axis );
174 sgCopyMat4( TRANSFORM, T1 );
175 sgPreMultMat4( TRANSFORM, LON );
176 sgPreMultMat4( TRANSFORM, LAT );
179 sgSetCoord( &layerpos, TRANSFORM );
181 layer_transform->setTransform( &layerpos );
183 // now calculate update texture coordinates
184 if ( last_lon < -900 ) {
189 if ( lon != last_lon || lat != last_lat ) {
190 Point3D start( last_lon, last_lat, 0.0 );
191 Point3D dest( lon, lat, 0.0 );
193 calc_gc_course_dist( dest, start, &course, &dist );
194 // cout << "course = " << course << ", dist = " << dist << endl;
196 double xoff = cos( course ) * dist / 500.0;
197 double yoff = sin( course ) * dist / 500.0;
199 // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
205 while ( base[0] > 1.0 ) { base[0] -= 1.0; }
206 while ( base[0] < 0.0 ) { base[0] += 1.0; }
209 while ( base[1] > 1.0 ) { base[1] -= 1.0; }
210 while ( base[1] < 0.0 ) { base[1] += 1.0; }
212 // cout << "base = " << base[0] << "," << base[1] << endl;
215 sgSetVec2( tc, base[0] + size / 1000.0f, base[1] );
218 sgSetVec2( tc, base[0], base[1] + size / 1000.0f );
221 sgSetVec2( tc, base[0] + size / 1000.0f, base[1] + size / 1000.0f );