]> git.mxchange.org Git - simgear.git/blob - Astro/planets.c
Incorporated Durk's Astro/ tweaks. Includes unifying the sun position
[simgear.git] / Astro / planets.c
1 /**************************************************************************
2  * planets.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 WIN32
27 #  include <windows.h>
28 #endif
29
30 #include <GL/glut.h>
31 #include <XGL/xgl.h>
32
33 #include <Time/fg_time.h>
34 #include <Astro/orbits.h>
35 #include <Astro/planets.h>
36 #include <Astro/sun.h>
37 #include <Include/fg_constants.h>
38 #include <Main/fg_debug.h>
39
40 GLint planets = 0;
41
42 struct CelestialCoord fgCalculatePlanet(struct OrbElements planet,
43                                         struct OrbElements theSun,
44                                         struct fgTIME t, int idx)
45 {
46     struct CelestialCoord result;
47
48     fgSUNPOS SolarPosition;
49
50     double eccAnom, r, v, ecl, actTime, R, s, ir, Nr, B, FV, ring_magn,
51         xv, yv, xh, yh, zh, xg, yg, zg, xe, ye, ze;
52
53     actTime = fgCalcActTime(t);
54     /*calculate the angle between ecliptic and equatorial coordinate system */
55     ecl = DEG_TO_RAD * (23.4393 - 3.563E-7 * actTime);
56
57     /* calculate the eccentric anomaly */
58     eccAnom  = fgCalcEccAnom(planet.M, planet.e);
59
60     /* calculate the planets distance (r) and true anomaly (v) */
61     xv = planet.a * (cos(eccAnom) - planet.e);
62     yv = planet.a * (sqrt(1.0 - planet.e*planet.e) * sin(eccAnom));
63     v = atan2(yv, xv);
64     r = sqrt ( xv*xv + yv*yv);
65     
66     /* calculate the planets position in 3-dimensional space */
67     xh = r * (cos (planet.N) * cos (v + planet.w) -
68               sin (planet.N) * sin (v + planet.w) * cos (planet.i));
69     yh = r * (sin (planet.N) * cos (v + planet.w) +
70               cos (planet.N) * sin (v + planet.w) * cos (planet.i));
71     zh = r * ( sin(v+planet.w) * sin(planet.i));
72
73     /* calculate the ecliptic longitude and latitude */
74
75     xg = xh + solarPosition.xs;
76     yg = yh + solarPosition.ys;
77     zg = zh;
78
79     xe = xg;
80     ye = yg * cos(ecl) - zg * sin(ecl);
81     ze = yg * sin(ecl) + zg * cos(ecl);
82
83     result.RightAscension = atan2(ye,xe);
84     result.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
85          
86     /* Let's calculate the brightness of the planet */
87     R = sqrt ( xg*xg + yg*yg + zg*zg);
88     s = SolarPosition.dist;
89     FV = acos(  (r*r + R*R - s*s) / (2*r*R));
90     FV  *= 57.29578;  /* convert radians to degrees */ 
91     switch(idx)
92       {
93       case 2: /* mercury */
94         result.magnitude = -0.36 + 5*log10( r*R ) + 0.027 * FV + 2.2E-13 * pow(FV, 6);
95         break;
96       case 3: /*venus */
97         result.magnitude = -4.34 + 5*log10( r*R ) + 0.013 * FV + 4.2E-07 * pow(FV,3);
98         break;
99       case 4: /* mars */
100         result.magnitude = -1.51 + 5*log10( r*R ) + 0.016 * FV;
101         break;
102       case 5: /* Jupiter */
103         result.magnitude = -9.25 + 5*log10( r*R ) + 0.014 * FV;
104         break;
105       case 6: /* Saturn */
106         
107         ir = 0.4897394;
108         Nr = 2.9585076 + 6.6672E-7*actTime;
109         
110         B = asin (sin (result.Declination) * cos (ir) -
111                   cos (result.Declination) * sin (ir) *
112                   sin (result.RightAscension - Nr));
113         ring_magn = -2.6 * sin (fabs(B)) + 1.2 * pow(sin(B),2);
114         result.magnitude = -9.0 + 5*log10( r*R ) + 0.044 * FV + ring_magn;
115         break;
116       case 7: /* Uranus */
117         result.magnitude = -7.15 + 5*log10( r*R) + 0.001 * FV;
118         break;
119       case 8:  /* Neptune */
120         result.magnitude = -6.90 + 5*log10 (r*R) + 0.001 *FV;
121         break;
122       default:
123         fgPrintf( FG_ASTRO, FG_ALERT, "index %d out of range !!!!\n", idx);
124       }
125     fgPrintf( FG_ASTRO, FG_DEBUG,
126               "    Planet found at %f (ra), %f (dec)\n",
127               result.RightAscension, result.Declination);
128     fgPrintf( FG_ASTRO, FG_DEBUG,
129               "      Geocentric dist     %f\n"
130               "      Heliocentric dist   %f\n"
131               "      Distance to the sun %f\n"
132               "      Phase angle         %f\n"
133               "      Brightness          %f\n", R, r, s, FV, result.magnitude);
134     return result;
135 }
136
137
138 void fgPlanetsInit( void )
139 {
140   struct fgLIGHT *l;
141   int i;
142   struct CelestialCoord pltPos;
143   double magnitude;
144
145   l = &cur_light_params;
146   /* if the display list was already built during a previous init,
147      then recycle it */
148
149   if (planets) {
150       xglDeleteLists(planets, 1);
151   }
152
153   planets = xglGenLists(1);
154   xglNewList( planets, GL_COMPILE );
155   xglBegin( GL_POINTS );
156
157
158   /* Add the planets to all four display lists */
159   for ( i = 2; i < 9; i++ ) {
160     fgSolarSystemUpdate(&(pltOrbElements[i]), cur_time_params);
161     pltPos = fgCalculatePlanet(pltOrbElements[i], 
162                                pltOrbElements[0], cur_time_params, i);
163     
164     magnitude = (0.0 - pltPos.magnitude) / 5.0 + 1.0;
165     
166     /* scale magnitudes again so they look ok */
167     /* magnitude = 
168        magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.1); */
169
170     /* the following statement could be made a little more sopisticated
171        for the moment: Stick to this one */
172     magnitude = magnitude * 0.7 + (3 * 0.1);
173     if ( magnitude > 1.0 ) { magnitude = 1.0; }
174     if ( magnitude < 0.0 ) { magnitude = 0.0; }
175     
176     /* Add planets to the display list, based on sun_angle and current
177        magnitude It's pretty experimental... */
178
179     if ((double) (l->sun_angle - FG_PI_2) > 
180         ((magnitude - 1.0) * -20 * DEG_TO_RAD))
181         {
182             xglColor3f (magnitude, magnitude, magnitude);
183             printf ("Sun Angle to Horizon (in Rads) = %f\n", 
184                     (double) (l->sun_angle - FG_PI_2));
185             printf ("Transformed Magnitude is :%f  %f\n", 
186                     magnitude, (magnitude - 1.0) * -20 * DEG_TO_RAD);
187  
188             xglVertex3f (50000.0 * cos (pltPos.RightAscension) *
189                          cos (pltPos.Declination),
190                          50000.0 * sin (pltPos.RightAscension) *
191                          cos (pltPos.Declination),
192                          50000.0 * sin (pltPos.Declination));
193         }
194   }
195   xglEnd();
196   xglEndList();
197
198 }
199
200
201 void fgPlanetsRender( void ) {
202         xglCallList(planets);
203 }
204
205
206 /* $Log$
207 /* Revision 1.7  1998/02/23 19:07:55  curt
208 /* Incorporated Durk's Astro/ tweaks.  Includes unifying the sun position
209 /* calculation code between sun display, and other FG sections that use this
210 /* for things like lighting.
211 /*
212  * Revision 1.6  1998/02/12 21:59:36  curt
213  * Incorporated code changes contributed by Charlie Hotchkiss
214  * <chotchkiss@namg.us.anritsu.com>
215  *
216  * Revision 1.5  1998/02/03 23:20:12  curt
217  * Lots of little tweaks to fix various consistency problems discovered by
218  * Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
219  * passed arguments along to the real printf().  Also incorporated HUD changes
220  * by Michele America.
221  *
222  * Revision 1.4  1998/02/02 20:53:23  curt
223  * To version 0.29
224  *
225  * Revision 1.3  1998/01/27 00:47:47  curt
226  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
227  * system and commandline/config file processing code.
228  *
229  * Revision 1.2  1998/01/19 19:26:59  curt
230  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
231  * This should simplify things tremendously.
232  *
233  * Revision 1.1  1998/01/07 03:16:18  curt
234  * Moved from .../Src/Scenery/ to .../Src/Astro/
235  *
236  * Revision 1.4  1997/12/30 20:47:52  curt
237  * Integrated new event manager with subsystem initializations.
238  *
239  * Revision 1.3  1997/12/30 16:36:52  curt
240  * Merged in Durk's changes ...
241  *
242  * Revision 1.2  1997/12/12 21:41:29  curt
243  * More light/material property tweaking ... still a ways off.
244  *
245  * Revision 1.1  1997/10/25 03:16:10  curt
246  * Initial revision of code contributed by Durk Talsma.
247  *
248  */
249
250