]> git.mxchange.org Git - simgear.git/blob - simgear/ephemeris/star.cxx
01f8c0d673b71e75cdb2cc4eb0cd085d51f16651
[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     
56   FG_LOG( FG_GENERAL, FG_INFO, "Initializing Sun Texture");
57 #ifdef GL_VERSION_1_1
58   xglGenTextures(1, &sun_texid);
59   xglBindTexture(GL_TEXTURE_2D, sun_texid);
60 #elif GL_EXT_texture_object
61   xglGenTexturesEXT(1, &sun_texid);
62   xglBindTextureEXT(GL_TEXTURE_2D, sun_texid);
63 #else
64 #  error port me
65 #endif
66
67   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
68   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
69   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
70   setTexture();
71   glTexImage2D( GL_TEXTURE_2D,
72                 0,
73                 GL_RGBA,
74                 256, 256,
75                 0,
76                 GL_RGBA, GL_UNSIGNED_BYTE,
77                 sun_texbuf);
78      
79   SunObject = gluNewQuadric();
80   if(SunObject == NULL)
81     {
82       printf("gluNewQuadric(SunObject) failed  !\n");
83       exit(0);
84     }
85   
86   //SunList = 0;
87   distance = 0.0;
88 }
89
90 Star::~Star()
91 {
92   //delete SunObject;
93   delete [] sun_texbuf;
94 }
95
96
97
98 static int texWidth = 256;      /* 64x64 is plenty */
99
100 void Star::setTexture()
101 {
102   int texSize;
103   //void *textureBuf;
104   GLubyte *p;
105   int i,j;
106   double radius;
107   
108   texSize = texWidth*texWidth;
109   
110   sun_texbuf = new GLubyte[texSize*4];
111   if (!sun_texbuf) 
112     return;  // Ugly!
113   
114   p = sun_texbuf;
115   
116   radius = (double)(texWidth / 2);
117   
118   for (i=0; i < texWidth; i++) {
119     for (j=0; j < texWidth; j++) {
120       double x, y, d;
121             
122       *p = 0xff;
123       *(p+1) = 0xff;
124       *(p+2) = 0xff;
125
126       x = fabs((double)(i - (texWidth / 2)));
127       y = fabs((double)(j - (texWidth / 2)));
128
129       d = sqrt((x * x) + (y * y));
130       if (d < radius) {
131           double t = 1.0 - (d / radius); // t is 1.0 at center, 0.0 at edge */
132           // inverse square looks nice 
133           *(p+3) = (int)((double) 0xff * (t*t));
134       } else {
135           *(p+3) = 0x00;
136       }
137       p += 4;
138     }
139   }
140   //gluBuild2DMipmaps(GL_TEXTURE_2D, 1, texWidth, texWidth, 
141   //        GL_LUMINANCE,
142   //        GL_UNSIGNED_BYTE, textureBuf);
143   //free(textureBuf);
144 }
145 /*************************************************************************
146  * void Jupiter::updatePosition(FGTime *t, Star *ourSun)
147  * 
148  * calculates the current position of our sun.
149  *************************************************************************/
150 void Star::updatePosition(FGTime *t)
151 {
152   double 
153     actTime, eccAnom, 
154     xv, yv, v, r,
155     xe, ye, ze, ecl;
156
157   updateOrbElements(t);
158   
159   actTime = fgCalcActTime(t);
160   ecl = DEG_TO_RAD * (23.4393 - 3.563E-7 * actTime); // Angle in Radians
161   eccAnom = fgCalcEccAnom(M, e);  // Calculate the eccentric Anomaly (also known as solving Kepler's equation)
162   
163   xv = cos(eccAnom) - e;
164   yv = sqrt (1.0 - e*e) * sin(eccAnom);
165   v = atan2 (yv, xv);                   // the sun's true anomaly
166   distance = r = sqrt (xv*xv + yv*yv);  // and its distance
167
168   lonEcl = v + w; // the sun's true longitude
169   latEcl = 0;
170
171   // convert the sun's true longitude to ecliptic rectangular 
172   // geocentric coordinates (xs, ys)
173   xs = r * cos (lonEcl);
174   ys = r * sin (lonEcl);
175
176   // convert ecliptic coordinates to equatorial rectangular
177   // geocentric coordinates
178
179   xe = xs;
180   ye = ys * cos (ecl);
181   ze = ys * sin (ecl);
182
183   // And finally, calculate right ascension and declination
184   rightAscension = atan2 (ye, xe);
185   declination = atan2 (ze, sqrt (xe*xe + ye*ye));
186 }
187   
188 void Star::newImage(void)
189 {
190   /*static float stars[3];
191   stars[0] = 0.0;
192   stars[1] = 0.0;
193   stars[2] = 1.0;*/
194
195   fgLIGHT *l = &cur_light_params;
196   float sun_angle = l->sun_angle;
197   
198   if( sun_angle*RAD_TO_DEG < 100 ) { // else no need to draw sun
199     
200     
201     double x_2, x_4, x_8, x_10;
202     GLfloat ambient;
203     GLfloat amb[4];
204     int sun_size = 550;
205     
206     // daily variation sun gets larger near horizon
207     /*if(sun_angle*RAD_TO_DEG > 84.0 && sun_angle*RAD_TO_DEG < 95)
208       {
209       double sun_grow = 9*fabs(94-sun_angle*RAD_TO_DEG);
210       sun_size = (int)(sun_size + sun_size * cos(sun_grow*DEG_TO_RAD));
211       }*/
212     x_2 = sun_angle * sun_angle;
213     x_4 = x_2 * x_2;
214     x_8 = x_4 * x_4;
215     x_10 = x_8 * x_2;
216     ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
217     if (ambient < 0.3) ambient = 0.3;
218     if (ambient > 1.0) ambient = 1.0;
219     
220     amb[0] = ((ambient * 6.0)  - 1.0); // minimum value = 0.8
221     amb[1] = ((ambient * 11.0) - 3.0); // minimum value = 0.3
222     amb[2] = ((ambient * 12.0) - 3.6); // minimum value = 0.0
223     amb[3] = 1.00;
224     
225     if (amb[0] > 1.0) amb[0] = 1.0;
226     if (amb[1] > 1.0) amb[1] = 1.0;
227     if (amb[2] > 1.0) amb[2] = 1.0;
228     xglColor3fv(amb);
229     glPushMatrix();
230     {
231       xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
232       xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
233       xglTranslatef(0,60000,0);
234       if (current_options.get_textures())
235         {
236           glEnable(GL_TEXTURE_2D); // TEXTURE ENABLED
237           glEnable(GL_BLEND);   // BLEND ENABLED
238           
239           // glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
240           glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
241           glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  
242           glBindTexture(GL_TEXTURE_2D, sun_texid);
243
244           glBegin(GL_QUADS);
245           glTexCoord2f(0.0f, 0.0f); glVertex3f(-5000, 0.0, -5000);
246           glTexCoord2f(1.0f, 0.0f); glVertex3f( 5000, 0.0, -5000);
247           glTexCoord2f(1.0f, 1.0f); glVertex3f( 5000, 0.0,  5000);
248           glTexCoord2f(0.0f, 1.0f); glVertex3f(-5000, 0.0,  5000);
249           glEnd();
250         }
251       xglDisable(GL_TEXTURE_2D); // TEXTURE DISABLED
252       xglDisable(GL_BLEND);     // BLEND DISABLED
253     }
254
255     glPopMatrix();
256     glDisable(GL_LIGHTING);     // LIGHTING DISABLED
257     glDisable(GL_BLEND);        // BLEND DISABLED
258     glPushMatrix();
259     {     
260       xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
261       xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
262       xglColor4fv(amb);
263       xglTranslatef(0,60000,0);
264       gluSphere( SunObject,  sun_size, 10, 10 );
265       }
266     glPopMatrix();
267     glDisable(GL_TEXTURE_2D);   // TEXTURE DISABLED
268     glDisable(GL_BLEND);        // BLEND DISABLED  
269   }
270 }