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