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