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