]> git.mxchange.org Git - simgear.git/blob - simgear/sky/cloud.cxx
47be63501ec27b9b818b8f9d43a27519d6f78a9b
[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 #include <simgear/compiler.h>
20
21 #include <stdio.h>
22 #include STL_IOSTREAM
23
24 #include <plib/sg.h>
25 #include <plib/ssg.h>
26
27 #include <simgear/math/point3d.hxx>
28 #include <simgear/math/polar3d.hxx>
29 #include <simgear/math/sg_random.h>
30
31 #include "cloud.hxx"
32
33
34 // Constructor
35 SGCloudLayer::SGCloudLayer( void ) {
36 }
37
38
39 // Destructor
40 SGCloudLayer::~SGCloudLayer( void ) {
41 }
42
43
44 // build the cloud object
45 void SGCloudLayer::build( double s, double asl, double thickness,
46                           double transition, ssgSimpleState *state )
47 {
48     scale = 4000.0;
49
50     layer_asl = asl;
51     layer_thickness = thickness;
52     layer_transition = transition;
53
54     size = s;
55     last_lon = last_lat = -999.0f;
56
57     layer_state = state;
58
59     cl = new ssgColourArray( 4 );
60     vl = new ssgVertexArray( 4 );
61     tl = new ssgTexCoordArray( 4 );
62
63     // build the cloud layer
64     sgVec4 color;
65     sgVec3 vertex;
66     sgVec2 tc;
67     sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
68
69     sgSetVec3( vertex, -size, -size, 0.0f );
70     sgVec2 base;
71     sgSetVec2( base, sg_random(), sg_random() );
72     sgSetVec2( tc, base[0], base[1] );
73     cl->add( color );
74     vl->add( vertex );
75     tl->add( tc );
76
77     sgSetVec3( vertex, size, -size, 0.0f );
78     sgSetVec2( tc, base[0] + size / scale, base[1] );
79     cl->add( color );
80     vl->add( vertex );
81     tl->add( tc );
82
83     sgSetVec3( vertex, -size, size, 0.0f );
84     sgSetVec2( tc, base[0], base[1] + size / scale );
85     cl->add( color );
86     vl->add( vertex );
87     tl->add( tc );
88
89     sgSetVec3( vertex, size, size, 0.0f );
90     sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
91     cl->add( color );
92     vl->add( vertex );
93     tl->add( tc );
94
95     ssgLeaf *layer = 
96         new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
97     layer->setState( layer_state );
98
99     // force a repaint of the moon colors with arbitrary defaults
100     repaint( color );
101
102     // build the ssg scene graph sub tree for the sky and connected
103     // into the provide scene graph branch
104     layer_transform = new ssgTransform;
105
106     // moon_transform->addKid( halo );
107     layer_transform->addKid( layer );
108
109     layer_root = new ssgRoot;
110     layer_root->addKid( layer_transform );
111 }
112
113
114 // repaint the cloud layer colors
115 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
116     float *color;
117
118     for ( int i = 0; i < 4; ++i ) {
119         color = cl->get( i );
120         sgCopyVec4( color, fog_color );
121     }
122
123     return true;
124 }
125
126
127 // reposition the cloud layer at the specified origin and orientation
128 // lon specifies a rotation about the Z axis
129 // lat specifies a rotation about the new Y axis
130 // spin specifies a rotation about the new Z axis (and orients the
131 // sunrise/set effects
132 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat,
133                                double alt )
134 {
135     sgMat4 T1, LON, LAT;
136     sgVec3 axis;
137
138     // combine p and asl (meters) to get translation offset
139     sgVec3 asl_offset;
140     sgCopyVec3( asl_offset, up );
141     sgNormalizeVec3( asl_offset );
142     if ( alt <= layer_asl ) {
143         sgScaleVec3( asl_offset, layer_asl );
144     } else {
145         sgScaleVec3( asl_offset, layer_asl + layer_thickness );
146     }
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;
152
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 );
157
158     // printf("  Translated to %.2f %.2f %.2f\n", 
159     //        zero_elev.x, zero_elev.y, zero_elev.z );
160
161     // Rotate to proper orientation
162     // printf("  lon = %.2f  lat = %.2f\n", 
163     //        lon * SGD_RADIANS_TO_DEGREES,
164     //        lat * SGD_RADIANS_TO_DEGREES);
165     // xglRotatef( lon * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0 );
166     sgSetVec3( axis, 0.0, 0.0, 1.0 );
167     sgMakeRotMat4( LON, lon * SGD_RADIANS_TO_DEGREES, axis );
168
169     // xglRotatef( 90.0 - f->get_Latitude() * SGD_RADIANS_TO_DEGREES,
170     //             0.0, 1.0, 0.0 );
171     sgSetVec3( axis, 0.0, 1.0, 0.0 );
172     sgMakeRotMat4( LAT, 90.0 - lat * SGD_RADIANS_TO_DEGREES, axis );
173
174     sgMat4 TRANSFORM;
175
176     sgCopyMat4( TRANSFORM, T1 );
177     sgPreMultMat4( TRANSFORM, LON );
178     sgPreMultMat4( TRANSFORM, LAT );
179
180     sgCoord layerpos;
181     sgSetCoord( &layerpos, TRANSFORM );
182
183     layer_transform->setTransform( &layerpos );
184
185     // now calculate update texture coordinates
186     if ( last_lon < -900 ) {
187         last_lon = lon;
188         last_lat = lat;
189     }
190
191     if ( lon != last_lon || lat != last_lat ) {
192         Point3D start( last_lon, last_lat, 0.0 );
193         Point3D dest( lon, lat, 0.0 );
194         double course, dist;
195         calc_gc_course_dist( dest, start, &course, &dist );
196         // cout << "course = " << course << ", dist = " << dist << endl;
197
198         double xoff = cos( course ) * dist / (2 * scale);
199         double yoff = sin( course ) * dist / (2 * scale);
200
201         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
202
203         float *base, *tc;
204         base = tl->get( 0 );
205
206         base[0] += xoff;
207
208         // the while loops can lead to *long* pauses if base[0] comes
209         // with a bogus value.
210         // while ( base[0] > 1.0 ) { base[0] -= 1.0; }
211         // while ( base[0] < 0.0 ) { base[0] += 1.0; }
212         if ( base[0] > -10.0 && base[0] < 10.0 ) {
213             base[0] -= (int)base[0];
214         } else {
215             base[0] = 0.0;
216             cout << "Error: base = " << base[0] << "," << base[1] << endl;
217         }
218
219         base[1] += yoff;
220         // the while loops can lead to *long* pauses if base[0] comes
221         // with a bogus value.
222         // while ( base[1] > 1.0 ) { base[1] -= 1.0; }
223         // while ( base[1] < 0.0 ) { base[1] += 1.0; }
224         if ( base[1] > -10.0 && base[1] < 10.0 ) {
225             base[1] -= (int)base[1];
226         } else {
227             base[1] = 0.0;
228             cout << "Error: base = " << base[0] << "," << base[1] << endl;
229         }
230
231         // cout << "base = " << base[0] << "," << base[1] << endl;
232
233         tc = tl->get( 1 );
234         sgSetVec2( tc, base[0] + size / scale, base[1] );
235  
236         tc = tl->get( 2 );
237         sgSetVec2( tc, base[0], base[1] + size / scale );
238  
239         tc = tl->get( 3 );
240         sgSetVec2( tc, base[0] + size / scale, base[1] + size / scale );
241  
242         last_lon = lon;
243         last_lat = lat;
244     }
245
246     return true;
247 }
248
249
250 void SGCloudLayer::draw() {
251     ssgCullAndDraw( layer_root );
252 }
253
254
255 // make an ssgSimpleState for a cloud layer given the named texture
256 ssgSimpleState *SGCloudMakeState( const string &path ) {
257     ssgSimpleState *state = new ssgSimpleState();
258
259     state->setTexture( (char *)path.c_str() );
260     state->setShadeModel( GL_SMOOTH );
261     state->disable( GL_LIGHTING );
262     state->disable( GL_CULL_FACE );
263     state->enable( GL_TEXTURE_2D );
264     state->enable( GL_COLOR_MATERIAL );
265     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
266     state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
267     state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
268     state->enable( GL_BLEND );
269     state->enable( GL_ALPHA_TEST );
270     state->setAlphaClamp( 0.01 );
271
272     return state;
273 }