]> git.mxchange.org Git - flightgear.git/blob - Astro/sun.c
To version 0.29
[flightgear.git] / Astro / sun.c
1 /**************************************************************************
2  * sun.c
3  *
4  * Written 1997 by Durk Talsma, started October, 1997.  For the flight gear
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * $Id$
22  * (Log is kept at end of this file)
23  **************************************************************************/
24
25 #include <GL/glut.h>
26 #include <XGL/xgl.h>
27
28 #include <Time/fg_time.h>
29 #include <Main/views.h>
30 #include <Astro/orbits.h>
31 #include <Astro/sun.h>
32 #include <Main/fg_debug.h>
33 #include <Include/fg_constants.h>
34
35 GLint sun_obj = 0;
36
37 static struct CelestialCoord sunPos;
38
39 float xSun, ySun, zSun;
40
41 struct SunPos fgCalcSunPos(struct OrbElements params)
42 {
43     double
44         EccAnom, lonSun,
45         xv, yv, v, r;
46     struct SunPos
47         solarPosition;
48
49     /* calculate the eccentric anomaly */
50     EccAnom = fgCalcEccAnom(params.M, params.e);
51
52     /* calculate the Suns distance (r) and its true anomaly (v) */
53          xv = cos(EccAnom) - params.e;
54     yv = sqrt(1.0 - params.e*params.e) * sin(EccAnom);
55     v = atan2(yv, xv);
56     r = sqrt(xv*xv + yv*yv);
57
58     /* calculate the the Sun's true longitude (lonsun) */
59     lonSun = v + params.w;
60
61         /* convert true longitude and distance to ecliptic rectangular geocentric
62       coordinates (xs, ys) */
63     solarPosition.xs = r * cos(lonSun);
64     solarPosition.ys = r * sin(lonSun);
65     solarPosition.dist = r;
66     return solarPosition;
67 }
68
69
70 struct CelestialCoord fgCalculateSun(struct OrbElements params, struct fgTIME t)
71 {
72         struct CelestialCoord
73                 result;
74     struct SunPos
75         SolarPosition;
76     double
77         xe, ye, ze, ecl, actTime;
78
79     /* calculate the angle between ecliptic and equatorial coordinate system */
80     actTime = fgCalcActTime(t);
81     ecl = DEG_TO_RAD * (23.4393 - 3.563E-7 * actTime);                  // Angle now in Rads
82
83     /* calculate the sun's ecliptic position */
84     SolarPosition = fgCalcSunPos(params);
85
86         /* convert ecliptic coordinates to equatorial rectangular geocentric coordinates */
87     xe = SolarPosition.xs;
88     ye = SolarPosition.ys * cos(ecl);
89     ze = SolarPosition.ys * sin(ecl);
90
91     /* and finally... Calulate Right Ascention and Declination */
92     result.RightAscension = atan2( ye, xe);
93     result.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
94     return result;
95 }
96
97
98 /* Initialize the Sun */
99 void fgSunInit( void ) {
100     struct fgLIGHT *l;
101     struct fgTIME *t;
102     struct fgVIEW *v;  
103
104     /* GLfloat color[4] = { 1.00, 1.00, 1.00, 1.00 }; */
105     double x_2, x_4, x_8, x_10;
106     GLfloat ambient;
107     GLfloat amb[4];
108
109     l = &cur_light_params;
110     t = &cur_time_params;
111     v = &current_view;
112
113     fgPrintf( FG_ASTRO, FG_INFO, "  Initializing the Sun\n");
114
115     fgSolarSystemUpdate(&(pltOrbElements[0]), cur_time_params);
116     sunPos = fgCalculateSun(pltOrbElements[0], cur_time_params);
117     fgPrintf( FG_ASTRO, FG_INFO, 
118               "Sun found at %f (ra), %f (dec)\n", 
119               sunPos.RightAscension, sunPos.Declination);
120
121     xSun = 60000.0 * cos(sunPos.RightAscension) * cos(sunPos.Declination);
122     ySun = 60000.0 * sin(sunPos.RightAscension) * cos(sunPos.Declination);
123     zSun = 60000.0 * sin(sunPos.Declination);
124
125
126     if (sun_obj) {
127         xglDeleteLists(sun_obj, 1);
128     }
129
130     /* printf("First time through, creating sun display list\n"); */
131
132     sun_obj = xglGenLists(1);
133     xglNewList(sun_obj, GL_COMPILE );
134
135
136     t = &cur_time_params;
137     v = &current_view;
138     l = &cur_light_params;
139
140     x_2 = l->sun_angle * l->sun_angle;
141     x_4 = x_2 * x_2;
142     x_8 = x_4 * x_4;
143     x_10 = x_8 * x_2;
144
145     ambient = (0.4 * pow(1.1, -x_10 / 30.0));
146     if ( ambient < 0.3 ) ambient = 0.3;
147     if ( ambient > 1.0 ) ambient = 1.0;
148
149     amb[0] = 0.00 + ((ambient * 6.0) - 1.0);     /* minimum val = 0.8 */
150     amb[1] = 0.00 + ((ambient * 11.0) - 3.0);     /* minimum val = 0.3 */ 
151     amb[2] = 0.00 + ((ambient * 12.0) - 3.6);    /* minimum val = 0.0 */ 
152     amb[3] = 1.00;   
153
154     if (amb[0] > 1.0) amb[0] = 1.0;
155     if (amb[1] > 1.0) amb[1] = 1.0;
156     if (amb[2] > 1.0) amb[2] = 1.0;
157
158     fgPrintf( FG_ASTRO, FG_DEBUG, 
159             "Color of the sun: %f, %f, %f\n"
160             "Ambient value   : %f\n"
161             "Sun Angle       : %f\n" , 
162             amb[0], amb[1], amb[2], ambient, l->sun_angle);
163     
164     /* set lighting parameters */
165     /*xglLightfv(GL_LIGHT0, GL_AMBIENT, color );
166       xglLightfv(GL_LIGHT0, GL_DIFFUSE, color );
167       xglMaterialfv(GL_FRONT, GL_AMBIENT, amb);
168       xglMaterialfv(GL_FRONT, GL_DIFFUSE, diff); 
169       xglMaterialfv(GL_FRONT, GL_SHININESS, diff);
170       xglMaterialfv(GL_FRONT, GL_EMISSION, diff);
171       xglMaterialfv(GL_FRONT, GL_SPECULAR, diff); */
172
173     /* xglDisable( GL_LIGHTING ); */
174
175     xglPushMatrix();
176     xglTranslatef(xSun, ySun, zSun);
177     xglScalef(1400, 1400, 1400);
178
179     /*xglColor3f(0.85, 0.65, 0.05);*/
180     xglColor3f(amb[0], amb[1], amb[2]); 
181     glutSolidSphere(1.0, 10, 10);
182
183     xglPopMatrix();
184
185     /* xglEnable( GL_LIGHTING ); */
186
187     xglEndList();
188 }
189
190
191 /* Draw the Sun */
192 void fgSunRender( void ) {
193     xglCallList(sun_obj);
194 }
195
196
197 /* $Log$
198 /* Revision 1.5  1998/02/02 20:53:24  curt
199 /* To version 0.29
200 /*
201  * Revision 1.4  1998/01/27 00:47:50  curt
202  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
203  * system and commandline/config file processing code.
204  *
205  * Revision 1.3  1998/01/19 19:27:00  curt
206  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
207  * This should simplify things tremendously.
208  *
209  * Revision 1.2  1998/01/19 18:40:18  curt
210  * Tons of little changes to clean up the code and to remove fatal errors
211  * when building with the c++ compiler.
212  *
213  * Revision 1.1  1998/01/07 03:16:20  curt
214  * Moved from .../Src/Scenery/ to .../Src/Astro/
215  *
216  * Revision 1.12  1998/01/05 18:44:36  curt
217  * Add an option to advance/decrease time from keyboard.
218  *
219  * Revision 1.11  1997/12/30 23:09:40  curt
220  * Worked on winding problem without luck, so back to calling glFrontFace()
221  * 3 times for each scenery area.
222  *
223  * Revision 1.10  1997/12/30 20:47:54  curt
224  * Integrated new event manager with subsystem initializations.
225  *
226  * Revision 1.9  1997/12/30 16:36:54  curt
227  * Merged in Durk's changes ...
228  *
229  * Revision 1.8  1997/12/19 23:35:00  curt
230  * Lot's of tweaking with sky rendering and lighting.
231  *
232  * Revision 1.7  1997/12/17 23:12:16  curt
233  * Fixed so moon and sun display lists aren't recreate periodically.
234  *
235  * Revision 1.6  1997/12/15 23:55:04  curt
236  * Add xgl wrappers for debugging.
237  * Generate terrain normals on the fly.
238  *
239  * Revision 1.5  1997/12/12 21:41:31  curt
240  * More light/material property tweaking ... still a ways off.
241  *
242  * Revision 1.4  1997/12/10 22:37:53  curt
243  * Prepended "fg" on the name of all global structures that didn't have it yet.
244  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
245  *
246  * Revision 1.3  1997/12/09 05:11:56  curt
247  * Working on tweaking lighting.
248  *
249  * Revision 1.2  1997/11/25 19:25:39  curt
250  * Changes to integrate Durk's moon/sun code updates + clean up.
251  *
252  * Revision 1.1  1997/10/25 03:16:11  curt
253  * Initial revision of code contributed by Durk Talsma.
254  *
255  */
256
257
258
259
260