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