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 program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include <simgear/constants.h>
42 // Set up moon rendering call backs
43 static int sgMoonOrbPreDraw( ssgEntity *e ) {
44 /* cout << endl << "Moon orb pre draw" << endl << "----------------"
46 glDisable( GL_DEPTH_TEST );
48 glBlendFunc ( GL_SRC_ALPHA, GL_ONE ) ;
50 // sgSetVec4( color, 0.0, 0.0, 0.0, 1.0 );
51 // glMaterialfv ( GL_FRONT_AND_BACK, GL_AMBIENT, color ) ;
56 static int sgMoonOrbPostDraw( ssgEntity *e ) {
57 /* cout << endl << "Moon orb post draw" << endl << "----------------"
59 glEnable( GL_DEPTH_TEST );
61 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
66 static int sgMoonHaloPreDraw( ssgEntity *e ) {
67 /* cout << endl << "Moon halo pre draw" << endl << "----------------"
69 glDisable( GL_DEPTH_TEST );
71 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
76 static int sgMoonHaloPostDraw( ssgEntity *e ) {
77 /* cout << endl << "Moon halo post draw" << endl << "----------------"
79 glEnable( GL_DEPTH_TEST );
81 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
88 SGMoon::SGMoon( void ) {
93 SGMoon::~SGMoon( void ) {
97 // build the moon object
98 ssgBranch * SGMoon::build( FGPath path, double moon_size ) {
100 // set up the orb state
101 path.append( "moon.rgba" );
102 orb_state = new ssgSimpleState();
103 orb_state->setTexture( (char *)path.c_str() );
104 orb_state->setShadeModel( GL_SMOOTH );
105 orb_state->enable( GL_LIGHTING );
106 orb_state->enable( GL_CULL_FACE );
107 orb_state->enable( GL_TEXTURE_2D );
108 orb_state->enable( GL_COLOR_MATERIAL );
109 orb_state->setColourMaterial( GL_DIFFUSE );
110 orb_state->setMaterial( GL_AMBIENT, 0.0, 0.0, 0.0, 1.0 );
111 orb_state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 1.0 );
112 orb_state->enable( GL_BLEND );
113 orb_state->enable( GL_ALPHA_TEST );
114 orb_state->setAlphaClamp( 0.01 );
116 cl = new ssgColourArray( 1 );
118 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
121 ssgBranch *orb = ssgMakeSphere( orb_state, cl, moon_size, 15, 15,
122 sgMoonOrbPreDraw, sgMoonOrbPostDraw );
124 // force a repaint of the moon colors with arbitrary defaults
128 // moon_texbuf = new GLubyte[64*64*3];
129 // moon_texid = makeHalo( moon_texbuf, 64 );
130 // my_glWritePPMFile("moonhalo.ppm", moon_texbuf, 64, 64, RGB);
133 // set up the halo state
134 halo_state = new ssgSimpleState();
135 halo_state->setTexture( "halo.rgb" );
136 // halo_state->setTexture( moon_texid );
137 halo_state->enable( GL_TEXTURE_2D );
138 halo_state->disable( GL_LIGHTING );
139 halo_state->setShadeModel( GL_SMOOTH );
140 halo_state->disable( GL_CULL_FACE );
142 halo_state->disable( GL_COLOR_MATERIAL );
143 halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
144 halo_state->setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
145 halo_state -> setMaterial ( GL_EMISSION, 0, 0, 0, 1 ) ;
146 halo_state -> setMaterial ( GL_SPECULAR, 0, 0, 0, 1 ) ;
147 // halo_state -> setShininess ( 0 ) ;
148 halo_state->enable( GL_ALPHA_TEST );
149 halo_state->setAlphaClamp(0.01);
150 halo_state->enable ( GL_BLEND ) ;
153 // Build ssg structure
154 double size = moon_size * 10.0;
156 halo_vl = new ssgVertexArray;
157 sgSetVec3( v3, -size, 0.0, -size );
159 sgSetVec3( v3, size, 0.0, -size );
161 sgSetVec3( v3, -size, 0.0, size );
163 sgSetVec3( v3, size, 0.0, size );
167 halo_tl = new ssgTexCoordArray;
168 sgSetVec2( v2, 0.0f, 0.0f );
170 sgSetVec2( v2, 1.0, 0.0 );
172 sgSetVec2( v2, 0.0, 1.0 );
174 sgSetVec2( v2, 1.0, 1.0 );
178 new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
179 halo->setState( halo_state );
182 // build the ssg scene graph sub tree for the sky and connected
183 // into the provide scene graph branch
184 moon_transform = new ssgTransform;
186 // moon_transform->addKid( halo );
187 moon_transform->addKid( orb );
189 return moon_transform;
193 // repaint the moon colors based on current value of moon_angle in
194 // degrees relative to verticle
195 // 0 degrees = high noon
196 // 90 degrees = moon rise/set
197 // 180 degrees = darkest midnight
198 bool SGMoon::repaint( double moon_angle ) {
199 if ( moon_angle * RAD_TO_DEG < 100 ) {
200 // else moon is well below horizon (so no point in repainting it)
202 // x_10 = moon_angle^10
203 double x_10 = moon_angle * moon_angle * moon_angle * moon_angle
204 * moon_angle * moon_angle * moon_angle * moon_angle * moon_angle
207 float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
208 if (ambient < 0.3) { ambient = 0.3; }
209 if (ambient > 1.0) { ambient = 1.0; }
213 (ambient * 6.0) - 1.0, // minimum value = 0.8
214 (ambient * 11.0) - 3.0, // minimum value = 0.3
215 (ambient * 12.0) - 3.6, // minimum value = 0.0
218 if (color[0] > 1.0) color[0] = 1.0;
219 if (color[1] > 1.0) color[1] = 1.0;
220 if (color[2] > 1.0) color[2] = 1.0;
222 // cout << "color = " << color[0] << " " << color[1] << " "
223 // << color[2] << endl;
227 sgCopyVec4( ptr, color );
234 // reposition the moon at the specified right ascension and
235 // declination, offset by our current position (p) so that it appears
236 // fixed at a great distance from the viewer. Also add in an optional
237 // rotation (i.e. for the current time of day.)
238 bool SGMoon::reposition( sgVec3 p, double angle,
239 double rightAscension, double declination,
242 sgMat4 T1, T2, GST, RA, DEC;
246 sgMakeTransMat4( T1, p );
248 sgSetVec3( axis, 0.0, 0.0, -1.0 );
249 sgMakeRotMat4( GST, angle, axis );
251 // xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
252 sgSetVec3( axis, 0.0, 0.0, 1.0 );
253 sgMakeRotMat4( RA, (rightAscension * RAD_TO_DEG) - 90.0, axis );
255 // xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
256 sgSetVec3( axis, 1.0, 0.0, 0.0 );
257 sgMakeRotMat4( DEC, declination * RAD_TO_DEG, axis );
259 // xglTranslatef(0,60000,0);
260 sgSetVec3( v, 0.0, 60000.0, 0.0 );
261 sgMakeTransMat4( T2, v );
264 sgCopyMat4( TRANSFORM, T1 );
265 sgPreMultMat4( TRANSFORM, GST );
266 sgPreMultMat4( TRANSFORM, RA );
267 sgPreMultMat4( TRANSFORM, DEC );
268 sgPreMultMat4( TRANSFORM, T2 );
271 sgSetCoord( &skypos, TRANSFORM );
273 moon_transform->setTransform( &skypos );