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