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