]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/moon.cxx
Fix an initialization problem
[simgear.git] / simgear / scene / sky / moon.cxx
1 // moon.hxx -- model earth's moon
2 //
3 // Written by Durk Talsma. Originally started October 1997, for distribution  
4 // with the FlightGear project. Version 2 was written in August and 
5 // September 1998. This code is based upon algorithms and data kindly 
6 // provided by Mr. Paul Schlyter. (pausch@saaf.se). 
7 //
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
9 // March 2000
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA  02111-1307, USA.
24 //
25 // $Id$
26
27
28 #include <simgear/compiler.h>
29
30 #include <stdio.h>
31 #include STL_IOSTREAM
32
33 #include <plib/sg.h>
34 #include <plib/ssg.h>
35
36 #include <simgear/constants.h>
37 #include <simgear/screen/colors.hxx>
38
39 #include "sphere.hxx"
40 #include "moon.hxx"
41
42
43 // Set up moon rendering call backs
44 static int sgMoonOrbPreDraw( ssgEntity *e ) {
45     /* cout << endl << "Moon orb pre draw" << endl << "----------------" 
46          << endl << endl; */
47
48     ssgLeaf *f = (ssgLeaf *)e;
49     if ( f -> hasState () ) f->getState()->apply() ;
50
51     glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT );
52     // cout << "push error = " << glGetError() << endl;
53
54     glDisable( GL_DEPTH_TEST );
55     glDisable( GL_FOG );
56     glBlendFunc ( GL_SRC_ALPHA, GL_ONE ) ;
57
58     return true;
59 }
60
61
62 static int sgMoonOrbPostDraw( ssgEntity *e ) {
63     /* cout << endl << "Moon orb post draw" << endl << "----------------" 
64          << endl << endl; */
65     // glEnable( GL_DEPTH_TEST );
66     // glEnable( GL_FOG );
67
68     // Some drivers don't properly reset glBendFunc with a
69     // glPopAttrib() so we reset it to the 'default' here.
70     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
71
72     glPopAttrib();
73     // cout << "pop error = " << glGetError() << endl;
74     
75     /* test
76     glDisable( GL_LIGHTING );
77     glDisable( GL_CULL_FACE );
78     glEnable( GL_COLOR_MATERIAL );
79     glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
80     glEnable( GL_CULL_FACE );
81     glEnable( GL_LIGHTING ); */
82
83     return true;
84 }
85
86
87 #if 0
88 static int sgMoonHaloPreDraw( ssgEntity *e ) {
89     /* cout << endl << "Moon halo pre draw" << endl << "----------------" 
90          << endl << endl; */
91
92     ssgLeaf *f = (ssgLeaf *)e;
93     if ( f -> hasState () ) f->getState()->apply() ;
94
95     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT | GL_COLOR_BUFFER_BIT);
96     // cout << "push error = " << glGetError() << endl;
97
98     glDisable( GL_DEPTH_TEST );
99     glDisable( GL_FOG );
100     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
101
102     return true;
103 }
104
105 static int sgMoonHaloPostDraw( ssgEntity *e ) {
106     /* cout << endl << "Moon halo post draw" << endl << "----------------" 
107          << endl << endl; */
108     // glEnable( GL_DEPTH_TEST );
109     // glEnable( GL_FOG );
110     // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
111     
112     glPopAttrib();
113     // cout << "pop error = " << glGetError() << endl;
114
115     return true;
116 }
117 #endif
118
119
120 // Constructor
121 SGMoon::SGMoon( void ) :
122     prev_moon_angle(-1)
123 {
124 }
125
126
127 // Destructor
128 SGMoon::~SGMoon( void ) {
129 }
130
131
132 // build the moon object
133 ssgBranch * SGMoon::build( SGPath path, double moon_size ) {
134
135     // set up the orb state
136     path.append( "moon.rgba" );
137     orb_state = new ssgSimpleState();
138     orb_state->setTexture( (char *)path.c_str() );
139     orb_state->setShadeModel( GL_SMOOTH );
140     orb_state->enable( GL_LIGHTING );
141     orb_state->enable( GL_CULL_FACE );
142     orb_state->enable( GL_TEXTURE_2D );
143     orb_state->enable( GL_COLOR_MATERIAL );
144     orb_state->setColourMaterial( GL_DIFFUSE );
145     orb_state->setMaterial( GL_AMBIENT, 0, 0, 0, 1.0 );
146     orb_state->setMaterial( GL_EMISSION, 0.0, 0.0, 0.0, 1 );
147     orb_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
148     orb_state->enable( GL_BLEND );
149     orb_state->enable( GL_ALPHA_TEST );
150     orb_state->setAlphaClamp( 0.01 );
151
152     cl = new ssgColourArray( 1 );
153     sgVec4 color;
154     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
155     cl->add( color );
156
157     ssgBranch *orb = ssgMakeSphere( orb_state, cl, moon_size, 15, 15,
158                                     sgMoonOrbPreDraw, sgMoonOrbPostDraw );
159
160     // force a repaint of the moon colors with arbitrary defaults
161     repaint( 0.0 );
162
163     // build the halo
164     // moon_texbuf = new GLubyte[64*64*3];
165     // moon_texid = makeHalo( moon_texbuf, 64 );
166     // my_glWritePPMFile("moonhalo.ppm", moon_texbuf, 64, 64, RGB);
167
168 #if 0
169     // set up the halo state
170     halo_state = new ssgSimpleState();
171     halo_state->setTexture( "halo.rgb" );
172     // halo_state->setTexture( moon_texid );
173     halo_state->enable( GL_TEXTURE_2D );
174     halo_state->disable( GL_LIGHTING );
175     halo_state->setShadeModel( GL_SMOOTH );
176     halo_state->disable( GL_CULL_FACE );
177
178     halo_state->disable( GL_COLOR_MATERIAL );
179     halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
180     halo_state->setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
181     halo_state->setMaterial ( GL_EMISSION, 0, 0, 0, 1 ) ;
182     halo_state->setMaterial ( GL_SPECULAR, 0, 0, 0, 1 ) ;
183     // halo_state -> setShininess ( 0 ) ;
184     halo_state->enable( GL_ALPHA_TEST );
185     halo_state->setAlphaClamp(0.01);
186     halo_state->enable ( GL_BLEND ) ;
187
188
189     // Build ssg structure
190     double size = moon_size * 10.0;
191     sgVec3 v3;
192     halo_vl = new ssgVertexArray;
193     sgSetVec3( v3, -size, 0.0, -size );
194     halo_vl->add( v3 );
195     sgSetVec3( v3, size, 0.0, -size );
196     halo_vl->add( v3 );
197     sgSetVec3( v3, -size, 0.0,  size );
198     halo_vl->add( v3 );
199     sgSetVec3( v3, size, 0.0,  size );
200     halo_vl->add( v3 );
201
202     sgVec2 v2;
203     halo_tl = new ssgTexCoordArray;
204     sgSetVec2( v2, 0.0f, 0.0f );
205     halo_tl->add( v2 );
206     sgSetVec2( v2, 1.0, 0.0 );
207     halo_tl->add( v2 );
208     sgSetVec2( v2, 0.0, 1.0 );
209     halo_tl->add( v2 );
210     sgSetVec2( v2, 1.0, 1.0 );
211     halo_tl->add( v2 );
212
213     ssgLeaf *halo = 
214         new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
215     halo->setState( halo_state );
216 #endif
217
218     // build the ssg scene graph sub tree for the sky and connected
219     // into the provide scene graph branch
220     moon_transform = new ssgTransform;
221
222     // moon_transform->addKid( halo );
223     moon_transform->addKid( orb );
224
225     return moon_transform;
226 }
227
228
229 // repaint the moon colors based on current value of moon_angle in
230 // degrees relative to verticle
231 // 0 degrees = high noon
232 // 90 degrees = moon rise/set
233 // 180 degrees = darkest midnight
234 bool SGMoon::repaint( double moon_angle ) {
235
236     if (prev_moon_angle != moon_angle) {
237         prev_moon_angle = moon_angle;
238
239         float moon_factor = 4*cos(moon_angle);
240
241         if (moon_factor > 1) moon_factor = 1.0;
242         if (moon_factor < -1) moon_factor = -1.0;
243         moon_factor = moon_factor/2 + 0.5;
244
245         sgVec4 color;
246         color[1] = sqrt(moon_factor);
247         color[0] = sqrt(color[1]);
248         color[2] = moon_factor * moon_factor;
249         color[2] *= color[2];
250         color[3] = 1.0;
251
252         gamma_correct_rgb( color );
253
254         // cout << "color = " << color[0] << " " << color[1] << " "
255         //      << color[2] << endl;
256
257         float *ptr;
258         ptr = cl->get( 0 );
259         sgCopyVec4( ptr, color );
260     }
261
262     return true;
263 }
264
265
266 // reposition the moon at the specified right ascension and
267 // declination, offset by our current position (p) so that it appears
268 // fixed at a great distance from the viewer.  Also add in an optional
269 // rotation (i.e. for the current time of day.)
270 bool SGMoon::reposition( sgVec3 p, double angle,
271                          double rightAscension, double declination,
272                          double moon_dist )
273 {
274     sgMat4 T1, T2, GST, RA, DEC;
275     sgVec3 axis;
276     sgVec3 v;
277
278     sgMakeTransMat4( T1, p );
279
280     sgSetVec3( axis, 0.0, 0.0, -1.0 );
281     sgMakeRotMat4( GST, angle, axis );
282
283     // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
284     //             0.0, 0.0, 1.0);
285     sgSetVec3( axis, 0.0, 0.0, 1.0 );
286     sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis );
287
288     // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
289     sgSetVec3( axis, 1.0, 0.0, 0.0 );
290     sgMakeRotMat4( DEC, declination * SGD_RADIANS_TO_DEGREES, axis );
291
292     // xglTranslatef(0,moon_dist);
293     sgSetVec3( v, 0.0, moon_dist, 0.0 );
294     sgMakeTransMat4( T2, v );
295
296     sgMat4 TRANSFORM;
297     sgCopyMat4( TRANSFORM, T1 );
298     sgPreMultMat4( TRANSFORM, GST );
299     sgPreMultMat4( TRANSFORM, RA );
300     sgPreMultMat4( TRANSFORM, DEC );
301     sgPreMultMat4( TRANSFORM, T2 );
302
303     sgCoord skypos;
304     sgSetCoord( &skypos, TRANSFORM );
305
306     moon_transform->setTransform( &skypos );
307
308     return true;
309 }
310