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