1 // moon.hxx -- model earth's moon
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).
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
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.
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.
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.
38 #include <simgear/constants.h>
44 // Set up moon rendering call backs
45 static int sgMoonOrbPreDraw( ssgEntity *e ) {
46 /* cout << endl << "Moon orb pre draw" << endl << "----------------"
49 ssgLeaf *f = (ssgLeaf *)e;
50 if ( f -> hasState () ) f->getState()->apply() ;
52 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT );
53 // cout << "push error = " << glGetError() << endl;
55 glDisable( GL_DEPTH_TEST );
57 glBlendFunc ( GL_SRC_ALPHA, GL_ONE ) ;
62 static int sgMoonOrbPostDraw( ssgEntity *e ) {
63 /* cout << endl << "Moon orb post draw" << endl << "----------------"
65 // glEnable( GL_DEPTH_TEST );
66 // glEnable( GL_FOG );
67 // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
70 // cout << "pop error = " << glGetError() << endl;
73 glDisable( GL_LIGHTING );
74 glDisable( GL_CULL_FACE );
75 glEnable( GL_COLOR_MATERIAL );
76 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
77 glEnable( GL_CULL_FACE );
78 glEnable( GL_LIGHTING ); */
83 static int sgMoonHaloPreDraw( ssgEntity *e ) {
84 /* cout << endl << "Moon halo pre draw" << endl << "----------------"
87 ssgLeaf *f = (ssgLeaf *)e;
88 if ( f -> hasState () ) f->getState()->apply() ;
90 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT | GL_COLOR_BUFFER_BIT);
91 cout << "push error = " << glGetError() << endl;
93 glDisable( GL_DEPTH_TEST );
95 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
100 static int sgMoonHaloPostDraw( ssgEntity *e ) {
101 /* cout << endl << "Moon halo post draw" << endl << "----------------"
103 // glEnable( GL_DEPTH_TEST );
104 // glEnable( GL_FOG );
105 // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
108 cout << "pop error = " << glGetError() << endl;
115 SGMoon::SGMoon( void ) {
120 SGMoon::~SGMoon( void ) {
124 // build the moon object
125 ssgBranch * SGMoon::build( FGPath path, double moon_size ) {
127 // set up the orb state
128 path.append( "moon.rgba" );
129 orb_state = new ssgSimpleState();
130 orb_state->setTexture( (char *)path.c_str() );
131 orb_state->setShadeModel( GL_SMOOTH );
132 orb_state->enable( GL_LIGHTING );
133 orb_state->enable( GL_CULL_FACE );
134 orb_state->enable( GL_TEXTURE_2D );
135 orb_state->enable( GL_COLOR_MATERIAL );
136 orb_state->setColourMaterial( GL_DIFFUSE );
137 orb_state->setMaterial( GL_AMBIENT, 0, 0, 0, 1.0 );
138 orb_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
139 orb_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
140 orb_state->enable( GL_BLEND );
141 orb_state->enable( GL_ALPHA_TEST );
142 orb_state->setAlphaClamp( 0.01 );
144 cl = new ssgColourArray( 1 );
146 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
149 ssgBranch *orb = ssgMakeSphere( orb_state, cl, moon_size, 15, 15,
150 sgMoonOrbPreDraw, sgMoonOrbPostDraw );
152 // force a repaint of the moon colors with arbitrary defaults
156 // moon_texbuf = new GLubyte[64*64*3];
157 // moon_texid = makeHalo( moon_texbuf, 64 );
158 // my_glWritePPMFile("moonhalo.ppm", moon_texbuf, 64, 64, RGB);
161 // set up the halo state
162 halo_state = new ssgSimpleState();
163 halo_state->setTexture( "halo.rgb" );
164 // halo_state->setTexture( moon_texid );
165 halo_state->enable( GL_TEXTURE_2D );
166 halo_state->disable( GL_LIGHTING );
167 halo_state->setShadeModel( GL_SMOOTH );
168 halo_state->disable( GL_CULL_FACE );
170 halo_state->disable( GL_COLOR_MATERIAL );
171 halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
172 halo_state->setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
173 halo_state->setMaterial ( GL_EMISSION, 0, 0, 0, 1 ) ;
174 halo_state->setMaterial ( GL_SPECULAR, 0, 0, 0, 1 ) ;
175 // halo_state -> setShininess ( 0 ) ;
176 halo_state->enable( GL_ALPHA_TEST );
177 halo_state->setAlphaClamp(0.01);
178 halo_state->enable ( GL_BLEND ) ;
181 // Build ssg structure
182 double size = moon_size * 10.0;
184 halo_vl = new ssgVertexArray;
185 sgSetVec3( v3, -size, 0.0, -size );
187 sgSetVec3( v3, size, 0.0, -size );
189 sgSetVec3( v3, -size, 0.0, size );
191 sgSetVec3( v3, size, 0.0, size );
195 halo_tl = new ssgTexCoordArray;
196 sgSetVec2( v2, 0.0f, 0.0f );
198 sgSetVec2( v2, 1.0, 0.0 );
200 sgSetVec2( v2, 0.0, 1.0 );
202 sgSetVec2( v2, 1.0, 1.0 );
206 new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
207 halo->setState( halo_state );
210 // build the ssg scene graph sub tree for the sky and connected
211 // into the provide scene graph branch
212 moon_transform = new ssgTransform;
214 // moon_transform->addKid( halo );
215 moon_transform->addKid( orb );
217 return moon_transform;
221 // repaint the moon colors based on current value of moon_angle in
222 // degrees relative to verticle
223 // 0 degrees = high noon
224 // 90 degrees = moon rise/set
225 // 180 degrees = darkest midnight
226 bool SGMoon::repaint( double moon_angle ) {
227 if ( moon_angle * SGD_RADIANS_TO_DEGREES < 100 ) {
228 // else moon is well below horizon (so no point in repainting it)
230 // x_10 = moon_angle^10
231 double x_10 = moon_angle * moon_angle * moon_angle * moon_angle
232 * moon_angle * moon_angle * moon_angle * moon_angle * moon_angle
235 float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
236 if (ambient < 0.3) { ambient = 0.3; }
237 if (ambient > 1.0) { ambient = 1.0; }
241 (ambient * 6.0) - 1.0, // minimum value = 0.8
242 (ambient * 11.0) - 3.0, // minimum value = 0.3
243 (ambient * 12.0) - 3.6, // minimum value = 0.0
246 // temp test, forces the color to always be white
247 // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
249 if (color[0] > 1.0) color[0] = 1.0;
250 if (color[1] > 1.0) color[1] = 1.0;
251 if (color[2] > 1.0) color[2] = 1.0;
253 // cout << "color = " << color[0] << " " << color[1] << " "
254 // << color[2] << endl;
258 sgCopyVec4( ptr, color );
265 // reposition the moon at the specified right ascension and
266 // declination, offset by our current position (p) so that it appears
267 // fixed at a great distance from the viewer. Also add in an optional
268 // rotation (i.e. for the current time of day.)
269 bool SGMoon::reposition( sgVec3 p, double angle,
270 double rightAscension, double declination,
273 sgMat4 T1, T2, GST, RA, DEC;
277 sgMakeTransMat4( T1, p );
279 sgSetVec3( axis, 0.0, 0.0, -1.0 );
280 sgMakeRotMat4( GST, angle, axis );
282 // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
284 sgSetVec3( axis, 0.0, 0.0, 1.0 );
285 sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis );
287 // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
288 sgSetVec3( axis, 1.0, 0.0, 0.0 );
289 sgMakeRotMat4( DEC, declination * SGD_RADIANS_TO_DEGREES, axis );
291 // xglTranslatef(0,moon_dist);
292 sgSetVec3( v, 0.0, moon_dist, 0.0 );
293 sgMakeTransMat4( T2, v );
296 sgCopyMat4( TRANSFORM, T1 );
297 sgPreMultMat4( TRANSFORM, GST );
298 sgPreMultMat4( TRANSFORM, RA );
299 sgPreMultMat4( TRANSFORM, DEC );
300 sgPreMultMat4( TRANSFORM, T2 );
303 sgSetCoord( &skypos, TRANSFORM );
305 moon_transform->setTransform( &skypos );