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