]> git.mxchange.org Git - simgear.git/blob - simgear/sky/cloud.cxx
147f80788a2f87dd88cef678e24102f0e15769fe
[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
30 #include "cloud.hxx"
31
32
33 // Constructor
34 SGCloudLayer::SGCloudLayer( void ) {
35 }
36
37
38 // Destructor
39 SGCloudLayer::~SGCloudLayer( void ) {
40 }
41
42
43 // build the moon object
44 ssgBranch * SGCloudLayer::build( FGPath path, double size, double asl ) {
45
46     layer_asl = asl;
47
48     // set up the cloud state
49     path.append( "cloud.rgba" );
50     layer_state = new ssgSimpleState();
51     layer_state->setTexture( (char *)path.c_str() );
52     layer_state->setShadeModel( GL_SMOOTH );
53     layer_state->disable( GL_LIGHTING );
54     layer_state->disable( GL_CULL_FACE );
55     layer_state->enable( GL_TEXTURE_2D );
56     layer_state->enable( GL_COLOR_MATERIAL );
57     layer_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
58     layer_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
59     layer_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
60     layer_state->enable( GL_BLEND );
61     layer_state->enable( GL_ALPHA_TEST );
62     layer_state->setAlphaClamp( 0.01 );
63
64     cl = new ssgColourArray( 4 );
65     vl = new ssgVertexArray( 4 );
66     tl = new ssgTexCoordArray( 4 );
67
68     // build the cloud layer
69     sgVec4 color;
70     sgVec3 vertex;
71     sgVec2 tc;
72     sgSetVec4( color, 1.0f, 1.0f, 1.0f, 1.0f );
73
74     sgSetVec3( vertex, -size, -size, 0.0f );
75     sgSetVec2( tc, 0.0f, 0.0f );
76     cl->add( color );
77     vl->add( vertex );
78     tl->add( tc );
79
80     sgSetVec3( vertex, size, -size, 0.0f );
81     sgSetVec2( tc, size / 1000.0f, 0.0f );
82     cl->add( color );
83     vl->add( vertex );
84     tl->add( tc );
85
86     sgSetVec3( vertex, -size, size, 0.0f );
87     sgSetVec2( tc, 0.0f, size / 1000.0f );
88     cl->add( color );
89     vl->add( vertex );
90     tl->add( tc );
91
92     sgSetVec3( vertex, size, size, 0.0f );
93     sgSetVec2( tc, size / 1000.0f, size / 1000.0f );
94     cl->add( color );
95     vl->add( vertex );
96     tl->add( tc );
97
98     ssgLeaf *layer = 
99         new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, NULL, tl, cl );
100     layer->setState( layer_state );
101
102     // force a repaint of the moon colors with arbitrary defaults
103     repaint( color );
104
105     // build the ssg scene graph sub tree for the sky and connected
106     // into the provide scene graph branch
107     layer_transform = new ssgTransform;
108
109     // moon_transform->addKid( halo );
110     layer_transform->addKid( layer );
111
112     return 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     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     sgScaleVec3( asl_offset, layer_asl );
143     // cout << "asl_offset = " << asl_offset[0] << "," << asl_offset[1]
144     //      << "," << asl_offset[2] << endl;
145     sgAddVec3( asl_offset, p );
146     // cout << "  asl_offset = " << asl_offset[0] << "," << asl_offset[1]
147     //      << "," << asl_offset[2] << endl;
148
149     // Translate to zero elevation
150     // Point3D zero_elev = current_view.get_cur_zero_elev();
151     // xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
152     sgMakeTransMat4( T1, asl_offset );
153
154     // printf("  Translated to %.2f %.2f %.2f\n", 
155     //        zero_elev.x, zero_elev.y, zero_elev.z );
156
157     // Rotate to proper orientation
158     // printf("  lon = %.2f  lat = %.2f\n", FG_Longitude * RAD_TO_DEG,
159     //        FG_Latitude * RAD_TO_DEG);
160     // xglRotatef( f->get_Longitude() * RAD_TO_DEG, 0.0, 0.0, 1.0 );
161     sgSetVec3( axis, 0.0, 0.0, 1.0 );
162     sgMakeRotMat4( LON, lon * RAD_TO_DEG, axis );
163
164     // xglRotatef( 90.0 - f->get_Latitude() * RAD_TO_DEG, 0.0, 1.0, 0.0 );
165     sgSetVec3( axis, 0.0, 1.0, 0.0 );
166     sgMakeRotMat4( LAT, 90.0 - lat * RAD_TO_DEG, axis );
167
168     sgMat4 TRANSFORM;
169
170     sgCopyMat4( TRANSFORM, T1 );
171     sgPreMultMat4( TRANSFORM, LON );
172     sgPreMultMat4( TRANSFORM, LAT );
173
174     sgCoord layerpos;
175     sgSetCoord( &layerpos, TRANSFORM );
176
177     layer_transform->setTransform( &layerpos );
178
179     return true;
180 }