]> git.mxchange.org Git - simgear.git/blob - simgear/sky/cloud.cxx
Cloud texcoord fixes to tie apparent cloud position to earth even though
[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/point3d.hxx>
30 #include <simgear/math/polar3d.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 moon object
46 ssgBranch * SGCloudLayer::build( FGPath path, double s, double asl ) {
47
48     layer_asl = asl;
49     size = s;
50     last_lon = last_lat = -999.0f;
51
52     // set up the cloud state
53     path.append( "cloud.rgba" );
54     layer_state = new ssgSimpleState();
55     layer_state->setTexture( (char *)path.c_str() );
56     layer_state->setShadeModel( GL_SMOOTH );
57     layer_state->disable( GL_LIGHTING );
58     layer_state->disable( GL_CULL_FACE );
59     layer_state->enable( GL_TEXTURE_2D );
60     layer_state->enable( GL_COLOR_MATERIAL );
61     layer_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
62     layer_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
63     layer_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
64     layer_state->enable( GL_BLEND );
65     layer_state->enable( GL_ALPHA_TEST );
66     layer_state->setAlphaClamp( 0.01 );
67
68     cl = new ssgColourArray( 4 );
69     vl = new ssgVertexArray( 4 );
70     tl = new ssgTexCoordArray( 4 );
71
72     // build the cloud layer
73     sgVec4 color;
74     sgVec3 vertex;
75     sgVec2 tc;
76     sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
77
78     sgSetVec3( vertex, -size, -size, 0.0f );
79     sgSetVec2( tc, 0.0f, 0.0f );
80     cl->add( color );
81     vl->add( vertex );
82     tl->add( tc );
83
84     sgSetVec3( vertex, size, -size, 0.0f );
85     sgSetVec2( tc, size / 1000.0f, 0.0f );
86     cl->add( color );
87     vl->add( vertex );
88     tl->add( tc );
89
90     sgSetVec3( vertex, -size, size, 0.0f );
91     sgSetVec2( tc, 0.0f, size / 1000.0f );
92     cl->add( color );
93     vl->add( vertex );
94     tl->add( tc );
95
96     sgSetVec3( vertex, size, size, 0.0f );
97     sgSetVec2( tc, size / 1000.0f, size / 1000.0f );
98     cl->add( color );
99     vl->add( vertex );
100     tl->add( tc );
101
102     ssgLeaf *layer = 
103         new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
104     layer->setState( layer_state );
105
106     // force a repaint of the moon colors with arbitrary defaults
107     repaint( color );
108
109     // build the ssg scene graph sub tree for the sky and connected
110     // into the provide scene graph branch
111     layer_transform = new ssgTransform;
112
113     // moon_transform->addKid( halo );
114     layer_transform->addKid( layer );
115
116     return layer_transform;
117 }
118
119
120 // repaint the cloud layer colors
121 bool SGCloudLayer::repaint( sgVec3 fog_color ) {
122     float *color;
123
124     for ( int i = 0; i < 4; ++i ) {
125         color = cl->get( i );
126         sgCopyVec4( color, fog_color );
127     }
128
129     return true;
130 }
131
132
133 // reposition the cloud layer at the specified origin and orientation
134 // lon specifies a rotation about the Z axis
135 // lat specifies a rotation about the new Y axis
136 // spin specifies a rotation about the new Z axis (and orients the
137 // sunrise/set effects
138 bool SGCloudLayer::reposition( sgVec3 p, sgVec3 up, double lon, double lat ) {
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     sgScaleVec3( asl_offset, layer_asl );
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", FG_Longitude * RAD_TO_DEG,
163     //        FG_Latitude * RAD_TO_DEG);
164     // xglRotatef( f->get_Longitude() * RAD_TO_DEG, 0.0, 0.0, 1.0 );
165     sgSetVec3( axis, 0.0, 0.0, 1.0 );
166     sgMakeRotMat4( LON, lon * RAD_TO_DEG, axis );
167
168     // xglRotatef( 90.0 - f->get_Latitude() * RAD_TO_DEG, 0.0, 1.0, 0.0 );
169     sgSetVec3( axis, 0.0, 1.0, 0.0 );
170     sgMakeRotMat4( LAT, 90.0 - lat * RAD_TO_DEG, axis );
171
172     sgMat4 TRANSFORM;
173
174     sgCopyMat4( TRANSFORM, T1 );
175     sgPreMultMat4( TRANSFORM, LON );
176     sgPreMultMat4( TRANSFORM, LAT );
177
178     sgCoord layerpos;
179     sgSetCoord( &layerpos, TRANSFORM );
180
181     layer_transform->setTransform( &layerpos );
182
183     // now calculate update texture coordinates
184     if ( last_lon < -900 ) {
185         last_lon = lon;
186         last_lat = lat;
187     }
188
189     if ( lon != last_lon || lat != last_lat ) {
190         Point3D start( last_lon, last_lat, 0.0 );
191         Point3D dest( lon, lat, 0.0 );
192         double course, dist;
193         calc_gc_course_dist( dest, start, &course, &dist );
194         // cout << "course = " << course << ", dist = " << dist << endl;
195
196         double xoff = cos( course ) * dist / 500.0;
197         double yoff = sin( course ) * dist / 500.0;
198
199         // cout << "xoff = " << xoff << ", yoff = " << yoff << endl;
200
201         float *base, *tc;
202         base = tl->get( 0 );
203
204         base[0] += xoff;
205         while ( base[0] > 1.0 ) { base[0] -= 1.0; }
206         while ( base[0] < 0.0 ) { base[0] += 1.0; }
207
208         base[1] += yoff;
209         while ( base[1] > 1.0 ) { base[1] -= 1.0; }
210         while ( base[1] < 0.0 ) { base[1] += 1.0; }
211
212         // cout << "base = " << base[0] << "," << base[1] << endl;
213
214         tc = tl->get( 1 );
215         sgSetVec2( tc, base[0] + size / 1000.0f, base[1] );
216  
217         tc = tl->get( 2 );
218         sgSetVec2( tc, base[0], base[1] + size / 1000.0f );
219  
220         tc = tl->get( 3 );
221         sgSetVec2( tc, base[0] + size / 1000.0f, base[1] + size / 1000.0f );
222  
223         last_lon = lon;
224         last_lat = lat;
225     }
226
227     return true;
228 }