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