]> git.mxchange.org Git - simgear.git/blob - simgear/sky/moon.cxx
Complete overhaul of the sky/sun/moon/stars/planets. It is now an ssg
[simgear.git] / simgear / 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 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.
14 //
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #include <stdio.h>
28 #include <iostream>
29
30 #include <plib/ssg.h>
31
32 #include <simgear/constants.h>
33
34 #include "sphere.hxx"
35 #include "moon.hxx"
36
37
38 // Set up moon rendering call backs
39 static int sgMoonOrbPreDraw( ssgEntity *e ) {
40     /* cout << endl << "Moon orb pre draw" << endl << "----------------" 
41          << endl << endl; */
42     glDisable( GL_DEPTH_TEST );
43     glDisable( GL_FOG );
44     glBlendFunc ( GL_SRC_ALPHA, GL_ONE ) ;
45     // sgVec4 color;
46     // sgSetVec4( color, 0.0, 0.0, 0.0, 1.0 );
47     // glMaterialfv ( GL_FRONT_AND_BACK, GL_AMBIENT, color ) ;
48
49     return true;
50 }
51
52 static int sgMoonOrbPostDraw( ssgEntity *e ) {
53     /* cout << endl << "Moon orb post draw" << endl << "----------------" 
54          << endl << endl; */
55     glEnable( GL_DEPTH_TEST );
56     glEnable( GL_FOG );
57     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
58
59     return true;
60 }
61
62 static int sgMoonHaloPreDraw( ssgEntity *e ) {
63     /* cout << endl << "Moon halo pre draw" << endl << "----------------" 
64          << endl << endl; */
65     glDisable( GL_DEPTH_TEST );
66     glDisable( GL_FOG );
67     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
68
69     return true;
70 }
71
72 static int sgMoonHaloPostDraw( ssgEntity *e ) {
73     /* cout << endl << "Moon halo post draw" << endl << "----------------" 
74          << endl << endl; */
75     glEnable( GL_DEPTH_TEST );
76     glEnable( GL_FOG );
77     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
78
79     return true;
80 }
81
82
83 // Constructor
84 SGMoon::SGMoon( void ) {
85 }
86
87
88 // Destructor
89 SGMoon::~SGMoon( void ) {
90 }
91
92
93 // build the moon object
94 ssgBranch * SGMoon::build( FGPath path, double moon_size ) {
95
96     // set up the orb state
97     path.append( "moon.rgba" );
98     orb_state = new ssgSimpleState();
99     orb_state->setTexture( (char *)path.c_str() );
100     orb_state->setShadeModel( GL_SMOOTH );
101     orb_state->enable( GL_LIGHTING );
102     orb_state->enable( GL_CULL_FACE );
103     orb_state->enable( GL_TEXTURE_2D );
104     orb_state->enable( GL_COLOR_MATERIAL );
105     orb_state->setColourMaterial( GL_DIFFUSE );
106     orb_state->setMaterial( GL_AMBIENT, 0.0, 0.0, 0.0, 1.0 );
107     orb_state->setMaterial( GL_SPECULAR, 0.0, 0.0, 0.0, 1.0 );
108     orb_state->enable( GL_BLEND );
109     orb_state->enable( GL_ALPHA_TEST );
110     orb_state->setAlphaClamp( 0.01 );
111
112     cl = new ssgColourArray( 1 );
113     sgVec4 color;
114     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
115     cl->add( color );
116
117     ssgBranch *orb = ssgMakeSphere( orb_state, cl, moon_size, 15, 15,
118                                     sgMoonOrbPreDraw, sgMoonOrbPostDraw );
119
120     // force a repaint of the moon colors with arbitrary defaults
121     repaint( 0.0 );
122
123     // build the halo
124     // moon_texbuf = new GLubyte[64*64*3];
125     // moon_texid = makeHalo( moon_texbuf, 64 );
126     // my_glWritePPMFile("moonhalo.ppm", moon_texbuf, 64, 64, RGB);
127
128 #if 0
129     // set up the halo state
130     halo_state = new ssgSimpleState();
131     halo_state->setTexture( "halo.rgb" );
132     // halo_state->setTexture( moon_texid );
133     halo_state->enable( GL_TEXTURE_2D );
134     halo_state->disable( GL_LIGHTING );
135     halo_state->setShadeModel( GL_SMOOTH );
136     halo_state->disable( GL_CULL_FACE );
137
138     halo_state->disable( GL_COLOR_MATERIAL );
139     halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
140     halo_state->setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
141     halo_state -> setMaterial ( GL_EMISSION, 0, 0, 0, 1 ) ;
142     halo_state -> setMaterial ( GL_SPECULAR, 0, 0, 0, 1 ) ;
143     // halo_state -> setShininess ( 0 ) ;
144     halo_state->enable( GL_ALPHA_TEST );
145     halo_state->setAlphaClamp(0.01);
146     halo_state->enable ( GL_BLEND ) ;
147
148
149     // Build ssg structure
150     double size = moon_size * 10.0;
151     sgVec3 v3;
152     halo_vl = new ssgVertexArray;
153     sgSetVec3( v3, -size, 0.0, -size );
154     halo_vl->add( v3 );
155     sgSetVec3( v3, size, 0.0, -size );
156     halo_vl->add( v3 );
157     sgSetVec3( v3, -size, 0.0,  size );
158     halo_vl->add( v3 );
159     sgSetVec3( v3, size, 0.0,  size );
160     halo_vl->add( v3 );
161
162     sgVec2 v2;
163     halo_tl = new ssgTexCoordArray;
164     sgSetVec2( v2, 0.0f, 0.0f );
165     halo_tl->add( v2 );
166     sgSetVec2( v2, 1.0, 0.0 );
167     halo_tl->add( v2 );
168     sgSetVec2( v2, 0.0, 1.0 );
169     halo_tl->add( v2 );
170     sgSetVec2( v2, 1.0, 1.0 );
171     halo_tl->add( v2 );
172
173     ssgLeaf *halo = 
174         new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
175     halo->setState( halo_state );
176 #endif
177
178     // build the ssg scene graph sub tree for the sky and connected
179     // into the provide scene graph branch
180     moon_transform = new ssgTransform;
181
182     // moon_transform->addKid( halo );
183     moon_transform->addKid( orb );
184
185     return moon_transform;
186 }
187
188
189 // repaint the moon colors based on current value of moon_angle in
190 // degrees relative to verticle
191 // 0 degrees = high noon
192 // 90 degrees = moon rise/set
193 // 180 degrees = darkest midnight
194 bool SGMoon::repaint( double moon_angle ) {
195     if ( moon_angle * RAD_TO_DEG < 100 ) {
196         // else moon is well below horizon (so no point in repainting it)
197     
198         // x_10 = moon_angle^10
199         double x_10 = moon_angle * moon_angle * moon_angle * moon_angle
200             * moon_angle * moon_angle * moon_angle * moon_angle * moon_angle
201             * moon_angle;
202
203         float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
204         if (ambient < 0.3) { ambient = 0.3; }
205         if (ambient > 1.0) { ambient = 1.0; }
206
207         sgVec4 color;
208         sgSetVec4( color,
209                    (ambient * 6.0)  - 1.0, // minimum value = 0.8
210                    (ambient * 11.0) - 3.0, // minimum value = 0.3
211                    (ambient * 12.0) - 3.6, // minimum value = 0.0
212                    0.5 );
213     
214         if (color[0] > 1.0) color[0] = 1.0;
215         if (color[1] > 1.0) color[1] = 1.0;
216         if (color[2] > 1.0) color[2] = 1.0;
217
218         // cout << "color = " << color[0] << " " << color[1] << " " 
219         //      << color[2] << endl;
220
221         float *ptr;
222         ptr = cl->get( 0 );
223         sgCopyVec4( ptr, color );
224     }
225
226     return true;
227 }
228
229
230 // reposition the moon at the specified right ascension and
231 // declination, offset by our current position (p) so that it appears
232 // fixed at a great distance from the viewer.  Also add in an optional
233 // rotation (i.e. for the current time of day.)
234 bool SGMoon::reposition( sgVec3 p, double angle,
235                          double rightAscension, double declination,
236                          double moon_dist )
237 {
238     sgMat4 T1, T2, GST, RA, DEC;
239     sgVec3 axis;
240     sgVec3 v;
241
242     sgMakeTransMat4( T1, p );
243
244     sgSetVec3( axis, 0.0, 0.0, -1.0 );
245     sgMakeRotMat4( GST, angle, axis );
246
247     // xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
248     sgSetVec3( axis, 0.0, 0.0, 1.0 );
249     sgMakeRotMat4( RA, (rightAscension * RAD_TO_DEG) - 90.0, axis );
250
251     // xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
252     sgSetVec3( axis, 1.0, 0.0, 0.0 );
253     sgMakeRotMat4( DEC, declination * RAD_TO_DEG, axis );
254
255     // xglTranslatef(0,60000,0);
256     sgSetVec3( v, 0.0, 60000.0, 0.0 );
257     sgMakeTransMat4( T2, v );
258
259     sgMat4 TRANSFORM;
260     sgCopyMat4( TRANSFORM, T1 );
261     sgPreMultMat4( TRANSFORM, GST );
262     sgPreMultMat4( TRANSFORM, RA );
263     sgPreMultMat4( TRANSFORM, DEC );
264     sgPreMultMat4( TRANSFORM, T2 );
265
266     sgCoord skypos;
267     sgSetCoord( &skypos, TRANSFORM );
268
269     moon_transform->setTransform( &skypos );
270
271     return true;
272 }