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