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