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