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