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