]> git.mxchange.org Git - flightgear.git/blob - Scenery/sun.c
Add xgl wrappers for debugging.
[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 "../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     return solarPosition;
64 }
65
66
67 struct CelestialCoord fgCalculateSun(struct OrbElements params, struct fgTIME t)
68 {
69         struct CelestialCoord
70                 result;
71     struct SunPos
72         SolarPosition;
73     double
74         xe, ye, ze, ecl, actTime;
75
76     /* calculate the angle between ecliptic and equatorial coordinate system */
77     actTime = fgCalcActTime(t);
78     ecl = fgDegToRad(23.4393 - 3.563E-7 * actTime);                     // Angle now in Rads
79
80     /* calculate the sun's ecliptic position */
81     SolarPosition = fgCalcSunPos(params);
82
83         /* convert ecliptic coordinates to equatorial rectangular geocentric coordinates */
84     xe = SolarPosition.xs;
85     ye = SolarPosition.ys * cos(ecl);
86     ze = SolarPosition.ys * sin(ecl);
87
88     /* and finally... Calulate Right Ascention and Declination */
89     result.RightAscension = atan2( ye, xe);
90     result.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
91     return result;
92 }
93
94
95 /* Initialize the Sun */
96 void fgSunInit()
97 {
98 //   int i;
99
100     sun_obj = xglGenLists(1);
101     xglNewList(sun_obj, GL_COMPILE );
102
103 //      xglBegin( GL_POINTS );
104
105     fgSolarSystemUpdate(&(pltOrbElements[0]), cur_time_params);
106     sunPos = fgCalculateSun(pltOrbElements[0], cur_time_params);
107 #ifdef DEBUG
108     printf("Sun found at %f (ra), %f (dec)\n", sunPos.RightAscension, 
109            sunPos.Declination);
110 #endif
111
112     /* give the moon a temporary color, for testing purposes */
113 //   xglColor3f( 0.0, 1.0, 0.0);
114 //   xglVertex3f( 190000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination),
115  //              190000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination),
116 //                 190000.0 * sin(moonPos.Declination) );
117    //xglVertex3f(0.0, 0.0, 0.0);
118 //   xglEnd();
119 //   xglColor3f(1.0, 1.0, 1.0);
120    //xMoon = 190000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination);
121    //yMoon = 190000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination);
122    //zMoon = 190000.0 * sin(moonPos.Declination);
123
124    xSun = 60000.0 * cos(sunPos.RightAscension) * cos(sunPos.Declination);
125    ySun = 60000.0 * sin(sunPos.RightAscension) * cos(sunPos.Declination);
126    zSun = 60000.0 * sin(sunPos.Declination);
127
128 //   xglPushMatrix();
129 //   xglTranslatef(x, y, z);
130 //   xglScalef(16622.8, 16622.8, 16622.8);
131 //     xglBegin(GL_TRIANGLES);
132 //   for (i = 0; i < 20; i++)
133 //      subdivide(&vdata[tindices[i][0]][0],
134 //                &vdata[tindices[i][1]][0],
135 //                &vdata[tindices[i][2]][0], 3);
136 //     glutSolidSphere(1.0, 25, 25);
137
138 //     xglEnd();
139     //xglPopMatrix();
140
141     glutSolidSphere(1.0, 10, 10);
142
143     xglEndList();
144 }
145
146
147 /* Draw the Sun */
148 void fgSunRender() {
149     struct fgVIEW *v;
150     struct fgTIME *t;
151     GLfloat color[4] = { 0.85, 0.65, 0.05, 1.0 };
152     /* double x_2, x_4, x_8, x_10; */
153     /* GLfloat ambient; */
154     /* GLfloat amb[3], diff[3]; */
155
156
157     t = &cur_time_params;
158     v = &current_view;
159
160     /* x_2 = t->sun_angle * t->sun_angle;
161     x_4 = x_2 * x_2;
162     x_8 = x_4 * x_4;
163     x_10 = x_8 * x_2; */
164
165     /* ambient = (0.4 * pow(1.1, -x_10 / 30.0));
166     if ( ambient < 0.3 ) ambient = 0.3;
167     if ( ambient > 1.0 ) ambient = 1.0;
168     amb[0] = 0.50 + ((ambient * 6.66) - 1.6);
169     amb[1] = 0.00 + ((ambient * 6.66) - 1.6);
170     amb[2] = 0.00 + ((ambient * 6.66) - 1.6);
171     amb[3] = 0.00;
172 #ifdef DEBUG
173     printf("Color of the sun: %f, %f, %f\n"
174            "Ambient value   : %f\n"
175            "Sun Angle       : %f\n" , amb[0], amb[1], amb[2], ambient, t->sun_angle);
176 #endif
177     diff[0] = 0.0;
178     diff[1] = 0.0;
179     diff[2] = 0.0;
180     diff[3] = 0.0; */
181
182     /* set lighting parameters */
183     /* xglLightfv(GL_LIGHT0, GL_AMBIENT, color );
184     xglLightfv(GL_LIGHT0, GL_DIFFUSE, color );
185     xglMaterialfv(GL_FRONT, GL_AMBIENT, amb);
186     xglMaterialfv(GL_FRONT, GL_DIFFUSE, diff); */
187
188     xglDisable( GL_LIGHTING );
189
190     xglPushMatrix();
191     xglTranslatef(xSun, ySun, zSun);
192     xglScalef(1400, 1400, 1400);
193
194     xglColor3f(0.85, 0.65, 0.05);
195
196     /* xglColor3fv( color ); */
197     /* xglutSolidSphere(1.0, 25, 25); */
198     xglCallList(sun_obj);
199
200     xglPopMatrix();
201
202     xglEnable( GL_LIGHTING );
203 }
204
205
206 /* $Log$
207 /* Revision 1.6  1997/12/15 23:55:04  curt
208 /* Add xgl wrappers for debugging.
209 /* Generate terrain normals on the fly.
210 /*
211  * Revision 1.5  1997/12/12 21:41:31  curt
212  * More light/material property tweaking ... still a ways off.
213  *
214  * Revision 1.4  1997/12/10 22:37:53  curt
215  * Prepended "fg" on the name of all global structures that didn't have it yet.
216  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
217  *
218  * Revision 1.3  1997/12/09 05:11:56  curt
219  * Working on tweaking lighting.
220  *
221  * Revision 1.2  1997/11/25 19:25:39  curt
222  * Changes to integrate Durk's moon/sun code updates + clean up.
223  *
224  * Revision 1.1  1997/10/25 03:16:11  curt
225  * Initial revision of code contributed by Durk Talsma.
226  *
227  */
228
229
230
231
232