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