]> git.mxchange.org Git - simgear.git/blob - simgear/sky/moon.cxx
Patch from Melchior Franz:
[simgear.git] / simgear / 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
38 #include "sphere.hxx"
39 #include "moon.hxx"
40
41
42 // Set up moon rendering call backs
43 static int sgMoonOrbPreDraw( ssgEntity *e ) {
44     /* cout << endl << "Moon orb pre draw" << endl << "----------------" 
45          << endl << endl; */
46
47     ssgLeaf *f = (ssgLeaf *)e;
48     if ( f -> hasState () ) f->getState()->apply() ;
49
50     glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT );
51     // cout << "push error = " << glGetError() << endl;
52
53     glDisable( GL_DEPTH_TEST );
54     glDisable( GL_FOG );
55     glBlendFunc ( GL_SRC_ALPHA, GL_ONE ) ;
56
57     return true;
58 }
59
60
61 static int sgMoonOrbPostDraw( ssgEntity *e ) {
62     /* cout << endl << "Moon orb post draw" << endl << "----------------" 
63          << endl << endl; */
64     // glEnable( GL_DEPTH_TEST );
65     // glEnable( GL_FOG );
66
67     // Some drivers don't properly reset glBendFunc with a
68     // glPopAttrib() so we reset it to the 'default' here.
69     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
70
71     glPopAttrib();
72     // cout << "pop error = " << glGetError() << endl;
73     
74     /* test
75     glDisable( GL_LIGHTING );
76     glDisable( GL_CULL_FACE );
77     glEnable( GL_COLOR_MATERIAL );
78     glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
79     glEnable( GL_CULL_FACE );
80     glEnable( GL_LIGHTING ); */
81
82     return true;
83 }
84
85
86 #if 0
87 static int sgMoonHaloPreDraw( ssgEntity *e ) {
88     /* cout << endl << "Moon halo pre draw" << endl << "----------------" 
89          << endl << endl; */
90
91     ssgLeaf *f = (ssgLeaf *)e;
92     if ( f -> hasState () ) f->getState()->apply() ;
93
94     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT | GL_COLOR_BUFFER_BIT);
95     // cout << "push error = " << glGetError() << endl;
96
97     glDisable( GL_DEPTH_TEST );
98     glDisable( GL_FOG );
99     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
100
101     return true;
102 }
103
104 static int sgMoonHaloPostDraw( ssgEntity *e ) {
105     /* cout << endl << "Moon halo post draw" << endl << "----------------" 
106          << endl << endl; */
107     // glEnable( GL_DEPTH_TEST );
108     // glEnable( GL_FOG );
109     // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
110     
111     glPopAttrib();
112     // cout << "pop error = " << glGetError() << endl;
113
114     return true;
115 }
116 #endif
117
118
119 // Constructor
120 SGMoon::SGMoon( void ) {
121 }
122
123
124 // Destructor
125 SGMoon::~SGMoon( void ) {
126 }
127
128
129 // build the moon object
130 ssgBranch * SGMoon::build( SGPath path, double moon_size ) {
131
132     // set up the orb state
133     path.append( "moon.rgba" );
134     orb_state = new ssgSimpleState();
135     orb_state->setTexture( (char *)path.c_str() );
136     orb_state->setShadeModel( GL_SMOOTH );
137     orb_state->enable( GL_LIGHTING );
138     orb_state->enable( GL_CULL_FACE );
139     orb_state->enable( GL_TEXTURE_2D );
140     orb_state->enable( GL_COLOR_MATERIAL );
141     orb_state->setColourMaterial( GL_DIFFUSE );
142     orb_state->setMaterial( GL_AMBIENT, 0, 0, 0, 1.0 );
143     orb_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
144     orb_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
145     orb_state->enable( GL_BLEND );
146     orb_state->enable( GL_ALPHA_TEST );
147     orb_state->setAlphaClamp( 0.01 );
148
149     cl = new ssgColourArray( 1 );
150     sgVec4 color;
151     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
152     cl->add( color );
153
154     ssgBranch *orb = ssgMakeSphere( orb_state, cl, moon_size, 15, 15,
155                                     sgMoonOrbPreDraw, sgMoonOrbPostDraw );
156
157     // force a repaint of the moon colors with arbitrary defaults
158     repaint( 0.0 );
159
160     // build the halo
161     // moon_texbuf = new GLubyte[64*64*3];
162     // moon_texid = makeHalo( moon_texbuf, 64 );
163     // my_glWritePPMFile("moonhalo.ppm", moon_texbuf, 64, 64, RGB);
164
165 #if 0
166     // set up the halo state
167     halo_state = new ssgSimpleState();
168     halo_state->setTexture( "halo.rgb" );
169     // halo_state->setTexture( moon_texid );
170     halo_state->enable( GL_TEXTURE_2D );
171     halo_state->disable( GL_LIGHTING );
172     halo_state->setShadeModel( GL_SMOOTH );
173     halo_state->disable( GL_CULL_FACE );
174
175     halo_state->disable( GL_COLOR_MATERIAL );
176     halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
177     halo_state->setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
178     halo_state->setMaterial ( GL_EMISSION, 0, 0, 0, 1 ) ;
179     halo_state->setMaterial ( GL_SPECULAR, 0, 0, 0, 1 ) ;
180     // halo_state -> setShininess ( 0 ) ;
181     halo_state->enable( GL_ALPHA_TEST );
182     halo_state->setAlphaClamp(0.01);
183     halo_state->enable ( GL_BLEND ) ;
184
185
186     // Build ssg structure
187     double size = moon_size * 10.0;
188     sgVec3 v3;
189     halo_vl = new ssgVertexArray;
190     sgSetVec3( v3, -size, 0.0, -size );
191     halo_vl->add( v3 );
192     sgSetVec3( v3, size, 0.0, -size );
193     halo_vl->add( v3 );
194     sgSetVec3( v3, -size, 0.0,  size );
195     halo_vl->add( v3 );
196     sgSetVec3( v3, size, 0.0,  size );
197     halo_vl->add( v3 );
198
199     sgVec2 v2;
200     halo_tl = new ssgTexCoordArray;
201     sgSetVec2( v2, 0.0f, 0.0f );
202     halo_tl->add( v2 );
203     sgSetVec2( v2, 1.0, 0.0 );
204     halo_tl->add( v2 );
205     sgSetVec2( v2, 0.0, 1.0 );
206     halo_tl->add( v2 );
207     sgSetVec2( v2, 1.0, 1.0 );
208     halo_tl->add( v2 );
209
210     ssgLeaf *halo = 
211         new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
212     halo->setState( halo_state );
213 #endif
214
215     // build the ssg scene graph sub tree for the sky and connected
216     // into the provide scene graph branch
217     moon_transform = new ssgTransform;
218
219     // moon_transform->addKid( halo );
220     moon_transform->addKid( orb );
221
222     return moon_transform;
223 }
224
225
226 // repaint the moon colors based on current value of moon_angle in
227 // degrees relative to verticle
228 // 0 degrees = high noon
229 // 90 degrees = moon rise/set
230 // 180 degrees = darkest midnight
231 bool SGMoon::repaint( double moon_angle ) {
232     if ( moon_angle * SGD_RADIANS_TO_DEGREES < 100 ) {
233         // else moon is well below horizon (so no point in repainting it)
234     
235         // x_10 = moon_angle^10
236         double x_10 = moon_angle * moon_angle * moon_angle * moon_angle
237             * moon_angle * moon_angle * moon_angle * moon_angle * moon_angle
238             * moon_angle;
239
240         float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
241         if (ambient < 0.3) { ambient = 0.3; }
242         if (ambient > 1.0) { ambient = 1.0; }
243
244         sgVec4 color;
245         sgSetVec4( color,
246                    (ambient * 6.0)  - 1.0, // minimum value = 0.8
247                    (ambient * 11.0) - 3.0, // minimum value = 0.3
248                    (ambient * 12.0) - 3.6, // minimum value = 0.0
249                    0.5 );
250
251         // temp test, forces the color to always be white
252         // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
253
254         if (color[0] > 1.0) color[0] = 1.0;
255         if (color[1] > 1.0) color[1] = 1.0;
256         if (color[2] > 1.0) color[2] = 1.0;
257
258         // cout << "color = " << color[0] << " " << color[1] << " " 
259         //      << color[2] << endl;
260
261         float *ptr;
262         ptr = cl->get( 0 );
263         sgCopyVec4( ptr, color );
264     }
265
266     return true;
267 }
268
269
270 // reposition the moon at the specified right ascension and
271 // declination, offset by our current position (p) so that it appears
272 // fixed at a great distance from the viewer.  Also add in an optional
273 // rotation (i.e. for the current time of day.)
274 bool SGMoon::reposition( sgVec3 p, double angle,
275                          double rightAscension, double declination,
276                          double moon_dist )
277 {
278     sgMat4 T1, T2, GST, RA, DEC;
279     sgVec3 axis;
280     sgVec3 v;
281
282     sgMakeTransMat4( T1, p );
283
284     sgSetVec3( axis, 0.0, 0.0, -1.0 );
285     sgMakeRotMat4( GST, angle, axis );
286
287     // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
288     //             0.0, 0.0, 1.0);
289     sgSetVec3( axis, 0.0, 0.0, 1.0 );
290     sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis );
291
292     // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
293     sgSetVec3( axis, 1.0, 0.0, 0.0 );
294     sgMakeRotMat4( DEC, declination * SGD_RADIANS_TO_DEGREES, axis );
295
296     // xglTranslatef(0,moon_dist);
297     sgSetVec3( v, 0.0, moon_dist, 0.0 );
298     sgMakeTransMat4( T2, v );
299
300     sgMat4 TRANSFORM;
301     sgCopyMat4( TRANSFORM, T1 );
302     sgPreMultMat4( TRANSFORM, GST );
303     sgPreMultMat4( TRANSFORM, RA );
304     sgPreMultMat4( TRANSFORM, DEC );
305     sgPreMultMat4( TRANSFORM, T2 );
306
307     sgCoord skypos;
308     sgSetCoord( &skypos, TRANSFORM );
309
310     moon_transform->setTransform( &skypos );
311
312     return true;
313 }
314