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