]> git.mxchange.org Git - flightgear.git/blob - Scenery/sun.c
f4ae4e9b014df8a5bb6278a19bf81930f1972fef
[flightgear.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 "../Time/fg_time.h"
27 #include "../Main/views.h"
28 #include "orbits.h"
29 #include "sun.h"
30
31 GLint sun;
32
33 static struct CelestialCoord sunPos;
34
35 float xSun, ySun, zSun;
36
37 struct SunPos fgCalcSunPos(struct OrbElements params)
38 {
39     double
40         EccAnom, lonSun,
41         xv, yv, v, r;
42     struct SunPos
43         solarPosition;
44
45     /* calculate the eccentric anomaly */
46     EccAnom = fgCalcEccAnom(params.M, params.e);
47
48     /* calculate the Suns distance (r) and its true anomaly (v) */
49          xv = cos(EccAnom) - params.e;
50     yv = sqrt(1.0 - params.e*params.e) * sin(EccAnom);
51     v = atan2(yv, xv);
52     r = sqrt(xv*xv + yv*yv);
53
54     /* calculate the the Sun's true longitude (lonsun) */
55     lonSun = v + params.w;
56
57         /* convert true longitude and distance to ecliptic rectangular geocentric
58       coordinates (xs, ys) */
59     solarPosition.xs = r * cos(lonSun);
60     solarPosition.ys = r * sin(lonSun);
61     return solarPosition;
62 }
63
64
65 struct CelestialCoord fgCalculateSun(struct OrbElements params, struct fgTIME t)
66 {
67         struct CelestialCoord
68                 result;
69     struct SunPos
70         SolarPosition;
71     double
72         xe, ye, ze, ecl, actTime;
73
74     /* calculate the angle between ecliptic and equatorial coordinate system */
75     actTime = fgCalcActTime(t);
76     ecl = fgDegToRad(23.4393 - 3.563E-7 * actTime);                     // Angle now in Rads
77
78     /* calculate the sun's ecliptic position */
79     SolarPosition = fgCalcSunPos(params);
80
81         /* convert ecliptic coordinates to equatorial rectangular geocentric coordinates */
82     xe = SolarPosition.xs;
83     ye = SolarPosition.ys * cos(ecl);
84     ze = SolarPosition.ys * sin(ecl);
85
86     /* and finally... Calulate Right Ascention and Declination */
87     result.RightAscension = atan2( ye, xe);
88     result.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
89     return result;
90 }
91
92
93 /* Initialize the Sun */
94 void fgSunInit()
95 {
96 //   int i;
97
98     sun = glGenLists(1);
99     glNewList(sun, GL_COMPILE );
100
101 //      glBegin( GL_POINTS );
102
103     fgSolarSystemUpdate(&(pltOrbElements[0]), cur_time_params);
104     sunPos = fgCalculateSun(pltOrbElements[0], cur_time_params);
105 #ifdef DEBUG
106     printf("Sun found at %f (ra), %f (dec)\n", sunPos.RightAscension, 
107            sunPos.Declination);
108 #endif
109
110     /* give the moon a temporary color, for testing purposes */
111 //   glColor3f( 0.0, 1.0, 0.0);
112 //   glVertex3f( 190000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination),
113  //              190000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination),
114 //                 190000.0 * sin(moonPos.Declination) );
115    //glVertex3f(0.0, 0.0, 0.0);
116 //   glEnd();
117 //   glColor3f(1.0, 1.0, 1.0);
118    //xMoon = 190000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination);
119    //yMoon = 190000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination);
120    //zMoon = 190000.0 * sin(moonPos.Declination);
121
122    xSun = 60000.0 * cos(sunPos.RightAscension) * cos(sunPos.Declination);
123    ySun = 60000.0 * sin(sunPos.RightAscension) * cos(sunPos.Declination);
124    zSun = 60000.0 * sin(sunPos.Declination);
125
126 //   glPushMatrix();
127 //   glTranslatef(x, y, z);
128 //   glScalef(16622.8, 16622.8, 16622.8);
129 //     glBegin(GL_TRIANGLES);
130 //   for (i = 0; i < 20; i++)
131 //      subdivide(&vdata[tindices[i][0]][0],
132 //                &vdata[tindices[i][1]][0],
133 //                &vdata[tindices[i][2]][0], 3);
134 //     glutSolidSphere(1.0, 25, 25);
135
136 //     glEnd();
137     //glPopMatrix();
138
139     glColor3f(0.85, 0.65, 0.05);
140     glutSolidSphere(1.0, 10, 10);
141
142     glEndList();
143 }
144
145
146 /* Draw the Sun */
147 void fgSunRender() {
148     struct VIEW *v;
149     struct fgTIME *t;
150     GLfloat color[4] = { 0.85, 0.65, 0.05, 1.0 };
151     /* double x_2, x_4, x_8, x_10; */
152     /* GLfloat ambient; */
153     /* GLfloat amb[3], diff[3]; */
154
155
156     t = &cur_time_params;
157     v = &current_view;
158
159     /* x_2 = t->sun_angle * t->sun_angle;
160     x_4 = x_2 * x_2;
161     x_8 = x_4 * x_4;
162     x_10 = x_8 * x_2; */
163
164     /* ambient = (0.4 * pow(1.1, -x_10 / 30.0));
165     if ( ambient < 0.3 ) ambient = 0.3;
166     if ( ambient > 1.0 ) ambient = 1.0;
167     amb[0] = 0.50 + ((ambient * 6.66) - 1.6);
168     amb[1] = 0.00 + ((ambient * 6.66) - 1.6);
169     amb[2] = 0.00 + ((ambient * 6.66) - 1.6);
170     amb[3] = 0.00;
171 #ifdef DEBUG
172     printf("Color of the sun: %f, %f, %f\n"
173            "Ambient value   : %f\n"
174            "Sun Angle       : %f\n" , amb[0], amb[1], amb[2], ambient, t->sun_angle);
175 #endif
176     diff[0] = 0.0;
177     diff[1] = 0.0;
178     diff[2] = 0.0;
179     diff[3] = 0.0; */
180
181     /* set lighting parameters */
182     /* glLightfv(GL_LIGHT0, GL_AMBIENT, color );
183     glLightfv(GL_LIGHT0, GL_DIFFUSE, color );
184     glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
185     glMaterialfv(GL_FRONT, GL_DIFFUSE, diff); */
186
187     glDisable( GL_LIGHTING );
188
189     glPushMatrix();
190     glTranslatef(xSun, ySun, zSun);
191     glScalef(1400, 1400, 1400);
192
193     /* glColor3fv( color ); */
194     /* glutSolidSphere(1.0, 25, 25); */
195     glCallList(sun);
196
197     glPopMatrix();
198
199     glEnable( GL_LIGHTING );
200 }
201
202
203 /* $Log$
204 /* Revision 1.3  1997/12/09 05:11:56  curt
205 /* Working on tweaking lighting.
206 /*
207  * Revision 1.2  1997/11/25 19:25:39  curt
208  * Changes to integrate Durk's moon/sun code updates + clean up.
209  *
210  * Revision 1.1  1997/10/25 03:16:11  curt
211  * Initial revision of code contributed by Durk Talsma.
212  *
213  */
214
215
216
217
218