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