]> git.mxchange.org Git - simgear.git/blob - simgear/ephemeris/star.cxx
c036521a76928dbd7124b0bb02ad62a3595a9383
[simgear.git] / simgear / ephemeris / star.cxx
1 /**************************************************************************
2  * star.cxx
3  * Written by Durk Talsma. Originally started October 1997, for distribution  
4  * with the FlightGear project. Version 2 was written in August and 
5  * September 1998. This code is based upon algorithms and data kindly 
6  * provided by Mr. Paul Schlyter. (pausch@saaf.se). 
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  **************************************************************************/
24
25 #ifdef __BORLANDC__
26 #  define exception c_exception
27 #endif
28 #include <math.h>
29
30 #include <simgear/debug/logstream.hxx>
31
32 #include <Time/sunpos.hxx>
33 #include <Time/light.hxx>
34 #include <Main/options.hxx>
35
36 #include "star.hxx"
37
38 /*************************************************************************
39  * Star::Star(FGTime *t)
40  * Public constructor for class Star
41  * Argument: The current time.
42  * the hard coded orbital elements our sun are passed to 
43  * CelestialBody::CelestialBody();
44  * note that the word sun is avoided, in order to prevent some compilation
45  * problems on sun systems 
46  ************************************************************************/
47 Star::Star(FGTime *t) :
48     CelestialBody (0.000000,  0.0000000000,
49                    0.0000,    0.00000,
50                    282.9404,  4.7093500E-5,     
51                    1.0000000, 0.000000, 
52                    0.016709,  -1.151E-9,
53                    356.0470,  0.98560025850, t)
54 {
55     distance = 0.0;
56 }
57
58 Star::Star() :
59     CelestialBody (0.000000,  0.0000000000,
60                    0.0000,    0.00000,
61                    282.9404,  4.7093500E-5,     
62                    1.0000000, 0.000000, 
63                    0.016709,  -1.151E-9,
64                    356.0470,  0.98560025850)
65 {
66     distance = 0.0;
67 }
68
69 Star::~Star()
70 {
71 #if 0
72   //delete SunObject;
73   delete [] sun_texbuf;
74 #endif
75 }
76
77
78
79 #if 0
80 static int texWidth = 256;      /* 64x64 is plenty */
81
82 void Star::setTexture()
83 {
84   int texSize;
85   //void *textureBuf;
86   GLubyte *p;
87   int i,j;
88   double radius;
89   
90   texSize = texWidth*texWidth;
91   
92   sun_texbuf = new GLubyte[texSize*4];
93   if (!sun_texbuf) 
94     return;  // Ugly!
95   
96   p = sun_texbuf;
97   
98   radius = (double)(texWidth / 2);
99   
100   for (i=0; i < texWidth; i++) {
101     for (j=0; j < texWidth; j++) {
102       double x, y, d;
103             
104       *p = 0xff;
105       *(p+1) = 0xff;
106       *(p+2) = 0xff;
107
108       x = fabs((double)(i - (texWidth / 2)));
109       y = fabs((double)(j - (texWidth / 2)));
110
111       d = sqrt((x * x) + (y * y));
112       if (d < radius) {
113           double t = 1.0 - (d / radius); // t is 1.0 at center, 0.0 at edge */
114           // inverse square looks nice 
115           *(p+3) = (int)((double) 0xff * (t*t));
116       } else {
117           *(p+3) = 0x00;
118       }
119       p += 4;
120     }
121   }
122   //gluBuild2DMipmaps(GL_TEXTURE_2D, 1, texWidth, texWidth, 
123   //        GL_LUMINANCE,
124   //        GL_UNSIGNED_BYTE, textureBuf);
125   //free(textureBuf);
126 }
127 #endif
128
129
130 /*************************************************************************
131  * void Jupiter::updatePosition(FGTime *t, Star *ourSun)
132  * 
133  * calculates the current position of our sun.
134  *************************************************************************/
135 void Star::updatePosition(FGTime *t)
136 {
137   double 
138     actTime, eccAnom, 
139     xv, yv, v, r,
140     xe, ye, ze, ecl;
141
142   updateOrbElements(t);
143   
144   actTime = fgCalcActTime(t);
145   ecl = DEG_TO_RAD * (23.4393 - 3.563E-7 * actTime); // Angle in Radians
146   eccAnom = fgCalcEccAnom(M, e);  // Calculate the eccentric Anomaly (also known as solving Kepler's equation)
147   
148   xv = cos(eccAnom) - e;
149   yv = sqrt (1.0 - e*e) * sin(eccAnom);
150   v = atan2 (yv, xv);                   // the sun's true anomaly
151   distance = r = sqrt (xv*xv + yv*yv);  // and its distance
152
153   lonEcl = v + w; // the sun's true longitude
154   latEcl = 0;
155
156   // convert the sun's true longitude to ecliptic rectangular 
157   // geocentric coordinates (xs, ys)
158   xs = r * cos (lonEcl);
159   ys = r * sin (lonEcl);
160
161   // convert ecliptic coordinates to equatorial rectangular
162   // geocentric coordinates
163
164   xe = xs;
165   ye = ys * cos (ecl);
166   ze = ys * sin (ecl);
167
168   // And finally, calculate right ascension and declination
169   rightAscension = atan2 (ye, xe);
170   declination = atan2 (ze, sqrt (xe*xe + ye*ye));
171 }
172
173
174 #if 0  
175 void Star::newImage(void)
176 {
177   /*static float stars[3];
178   stars[0] = 0.0;
179   stars[1] = 0.0;
180   stars[2] = 1.0;*/
181
182   fgLIGHT *l = &cur_light_params;
183   float sun_angle = l->sun_angle;
184   
185   if( sun_angle*RAD_TO_DEG < 100 ) { // else no need to draw sun
186     
187     
188     double x_2, x_4, x_8, x_10;
189     GLfloat ambient;
190     GLfloat amb[4];
191     int sun_size = 550;
192     
193     // daily variation sun gets larger near horizon
194     /*if(sun_angle*RAD_TO_DEG > 84.0 && sun_angle*RAD_TO_DEG < 95)
195       {
196       double sun_grow = 9*fabs(94-sun_angle*RAD_TO_DEG);
197       sun_size = (int)(sun_size + sun_size * cos(sun_grow*DEG_TO_RAD));
198       }*/
199     x_2 = sun_angle * sun_angle;
200     x_4 = x_2 * x_2;
201     x_8 = x_4 * x_4;
202     x_10 = x_8 * x_2;
203     ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
204     if (ambient < 0.3) ambient = 0.3;
205     if (ambient > 1.0) ambient = 1.0;
206     
207     amb[0] = ((ambient * 6.0)  - 1.0); // minimum value = 0.8
208     amb[1] = ((ambient * 11.0) - 3.0); // minimum value = 0.3
209     amb[2] = ((ambient * 12.0) - 3.6); // minimum value = 0.0
210     amb[3] = 1.00;
211     
212     if (amb[0] > 1.0) amb[0] = 1.0;
213     if (amb[1] > 1.0) amb[1] = 1.0;
214     if (amb[2] > 1.0) amb[2] = 1.0;
215     xglColor3fv(amb);
216     glPushMatrix();
217     {
218       xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
219       xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
220       xglTranslatef(0,60000,0);
221       if (current_options.get_textures())
222         {
223           glEnable(GL_TEXTURE_2D); // TEXTURE ENABLED
224           glEnable(GL_BLEND);   // BLEND ENABLED
225           
226           // glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
227           glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
228           glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  
229           glBindTexture(GL_TEXTURE_2D, sun_texid);
230
231           glBegin(GL_QUADS);
232           glTexCoord2f(0.0f, 0.0f); glVertex3f(-5000, 0.0, -5000);
233           glTexCoord2f(1.0f, 0.0f); glVertex3f( 5000, 0.0, -5000);
234           glTexCoord2f(1.0f, 1.0f); glVertex3f( 5000, 0.0,  5000);
235           glTexCoord2f(0.0f, 1.0f); glVertex3f(-5000, 0.0,  5000);
236           glEnd();
237         }
238       xglDisable(GL_TEXTURE_2D); // TEXTURE DISABLED
239       xglDisable(GL_BLEND);     // BLEND DISABLED
240     }
241
242     glPopMatrix();
243     glDisable(GL_LIGHTING);     // LIGHTING DISABLED
244     glDisable(GL_BLEND);        // BLEND DISABLED
245     glPushMatrix();
246     {     
247       xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
248       xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
249       xglColor4fv(amb);
250       xglTranslatef(0,60000,0);
251       gluSphere( SunObject,  sun_size, 10, 10 );
252       }
253     glPopMatrix();
254     glDisable(GL_TEXTURE_2D);   // TEXTURE DISABLED
255     glDisable(GL_BLEND);        // BLEND DISABLED  
256   }
257 }
258 #endif