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