]> git.mxchange.org Git - simgear.git/blob - simgear/sky/cloud.cxx
Updates to cloud code to add different basic cloud types. This isn't the
[simgear.git] / simgear / sky / cloud.cxx
1 // cloud.cxx -- model a single cloud layer
2 //
3 // Written by Curtis Olson, started June 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
15 //
16 // $Id$
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #include <iostream>
25
26 #include <plib/ssg.h>
27
28 #include <simgear/constants.h>
29 #include <simgear/math/fg_random.h>
30 #include <simgear/math/point3d.hxx>
31 #include <simgear/math/polar3d.hxx>
32
33 #include <Objects/matlib.hxx>
34
35 #include "cloud.hxx"
36
37
38 // Constructor
39 SGCloudLayer::SGCloudLayer( void ) {
40 }
41
42
43 // Destructor
44 SGCloudLayer::~SGCloudLayer( void ) {
45 }
46
47
48 // build the moon object
49 void SGCloudLayer::build( FGPath path, double s, double asl, double thickness,
50                           double transition, SGCloudType type )
51 {
52     scale = 4000.0;
53
54     layer_asl = asl;
55     layer_thickness = thickness;
56     layer_transition = transition;
57
58     size = s;
59     last_lon = last_lat = -999.0f;
60
61     // look up the appropriate cloud state
62     FGNewMat *m;
63
64     switch ( type ) {
65     case SG_CLOUD_OVERCAST:
66         m = material_lib.find( "CloudOvercast" );
67         layer_state = m->get_state();
68         break;
69     case SG_CLOUD_MOSTLY_CLOUDY:
70         m = material_lib.find( "CloudMostlyCloudy" );
71         layer_state = m->get_state();
72         break;
73     case SG_CLOUD_MOSTLY_SUNNY:
74         m = material_lib.find( "CloudMostlySunny" );
75         layer_state = m->get_state();
76         break;
77     case SG_CLOUD_CIRRUS:
78         m = material_lib.find( "CloudCirrus" );
79         layer_state = m->get_state();
80         break;
81     }
82
83     cl = new ssgColourArray( 4 );
84     vl = new ssgVertexArray( 4 );
85     tl = new ssgTexCoordArray( 4 );
86
87     // build the cloud layer
88     sgVec4 color;
89     sgVec3 vertex;
90     sgVec2 tc;
91     sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
92
93     sgSetVec3( vertex, -size, -size, 0.0f );
94     sgVec2 base;
95     sgSetVec2( base, fg_random(), fg_random() );
96     sgSetVec2( tc, base[0], base[1] );
97     cl->add( color );
98     vl->add( vertex );
99     tl->add( tc );
100
101     sgSetVec3( vertex, size, -size, 0.0f );
102     sgSetVec2( tc, base[0] + size / scale, base[1] );
103     cl->add( color );
104     vl->add( vertex );
105     tl->add( tc );
106
107     sgSetVec3( vertex, -size, size, 0.0f );
108     sgSetVec2( tc, base[0], base[1] + size / scale );
109     cl->add( color );
110     vl->add( vertex );
111     tl->add( tc );
112
113     sgSetVec3( vertex, size, size, 0.0f );
114     sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
115     cl->add( color );
116     vl->add( vertex );
117     tl->add( tc );
118
119     ssgLeaf *layer = 
120         new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
121     layer->setState( layer_state );
122
123     // force a repaint of the moon colors with arbitrary defaults
124     repaint( color );
125
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;
129
130     // moon_transform->addKid( halo );
131     layer_transform->addKid( layer );
132
133     layer_root = new ssgRoot;
134     layer_root->addKid( layer_transform );
135 }
136
137
138 // repaint the cloud layer colors
139 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
140     float *color;
141
142     for ( int i = 0; i < 4; ++i ) {
143         color = cl->get( i );
144         sgCopyVec4( color, fog_color );
145     }
146
147     return true;
148 }
149
150
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,
157                                double alt )
158 {
159     sgMat4 T1, LON, LAT;
160     sgVec3 axis;
161
162     // combine p and asl (meters) to get translation offset
163     sgVec3 asl_offset;
164     sgCopyVec3( asl_offset, up );
165     sgNormalizeVec3( asl_offset );
166     if ( alt <= layer_asl ) {
167         sgScaleVec3( asl_offset, layer_asl );
168     } else {
169         sgScaleVec3( asl_offset, layer_asl + layer_thickness );
170     }
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;
176
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 );
181
182     // printf("  Translated to %.2f %.2f %.2f\n", 
183     //        zero_elev.x, zero_elev.y, zero_elev.z );
184
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 );
191
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 );
195
196     sgMat4 TRANSFORM;
197
198     sgCopyMat4( TRANSFORM, T1 );
199     sgPreMultMat4( TRANSFORM, LON );
200     sgPreMultMat4( TRANSFORM, LAT );
201
202     sgCoord layerpos;
203     sgSetCoord( &layerpos, TRANSFORM );
204
205     layer_transform->setTransform( &layerpos );
206
207     // now calculate update texture coordinates
208     if ( last_lon < -900 ) {
209         last_lon = lon;
210         last_lat = lat;
211     }
212
213     if ( lon != last_lon || lat != last_lat ) {
214         Point3D start( last_lon, last_lat, 0.0 );
215         Point3D dest( lon, lat, 0.0 );
216         double course, dist;
217         calc_gc_course_dist( dest, start, &course, &dist );
218         // cout << "course = " << course << ", dist = " << dist << endl;
219
220         double xoff = cos( course ) * dist / (2 * scale);
221         double yoff = sin( course ) * dist / (2 * scale);
222
223         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
224
225         float *base, *tc;
226         base = tl->get( 0 );
227
228         base[0] += xoff;
229         while ( base[0] > 1.0 ) { base[0] -= 1.0; }
230         while ( base[0] < 0.0 ) { base[0] += 1.0; }
231
232         base[1] += yoff;
233         while ( base[1] > 1.0 ) { base[1] -= 1.0; }
234         while ( base[1] < 0.0 ) { base[1] += 1.0; }
235
236         // cout << "base = " << base[0] << "," << base[1] << endl;
237
238         tc = tl->get( 1 );
239         sgSetVec2( tc, base[0] + size / scale, base[1] );
240  
241         tc = tl->get( 2 );
242         sgSetVec2( tc, base[0], base[1] + size / scale );
243  
244         tc = tl->get( 3 );
245         sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
246  
247         last_lon = lon;
248         last_lat = lat;
249     }
250
251     return true;
252 }
253
254
255 void SGCloudLayer::draw() {
256     ssgCullAndDraw( layer_root );
257 }
258
259
260 // make an ssgSimpleState for a cloud layer given the named texture
261 ssgSimpleState *SGCloudMakeState( const string &path ) {
262     ssgSimpleState *state = new ssgSimpleState();
263
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 );
276
277     return state;
278 }