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