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