1997-10-25 11:16:08 +08:00
|
|
|
/**************************************************************************
|
|
|
|
* sun.c
|
|
|
|
*
|
|
|
|
* Written 1997 by Durk Talsma, started October, 1997. For the flight gear
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
* (Log is kept at end of this file)
|
|
|
|
**************************************************************************/
|
|
|
|
|
1997-11-26 03:25:27 +08:00
|
|
|
#include <GL/glut.h>
|
1998-01-20 03:26:51 +08:00
|
|
|
#include <XGL/xgl.h>
|
1997-12-16 07:54:25 +08:00
|
|
|
|
1998-01-20 03:26:51 +08:00
|
|
|
#include <Time/fg_time.h>
|
|
|
|
#include <Main/views.h>
|
|
|
|
#include <Astro/orbits.h>
|
|
|
|
#include <Astro/sun.h>
|
1998-01-27 08:47:41 +08:00
|
|
|
#include <Main/fg_debug.h>
|
1997-11-26 03:25:27 +08:00
|
|
|
|
1997-12-13 05:41:24 +08:00
|
|
|
GLint sun_obj;
|
1997-11-26 03:25:27 +08:00
|
|
|
|
1997-12-09 13:11:54 +08:00
|
|
|
static struct CelestialCoord sunPos;
|
1997-11-26 03:25:27 +08:00
|
|
|
|
|
|
|
float xSun, ySun, zSun;
|
1997-10-25 11:16:08 +08:00
|
|
|
|
|
|
|
struct SunPos fgCalcSunPos(struct OrbElements params)
|
|
|
|
{
|
|
|
|
double
|
|
|
|
EccAnom, lonSun,
|
|
|
|
xv, yv, v, r;
|
|
|
|
struct SunPos
|
|
|
|
solarPosition;
|
|
|
|
|
|
|
|
/* calculate the eccentric anomaly */
|
|
|
|
EccAnom = fgCalcEccAnom(params.M, params.e);
|
|
|
|
|
|
|
|
/* calculate the Suns distance (r) and its true anomaly (v) */
|
1997-11-26 03:25:27 +08:00
|
|
|
xv = cos(EccAnom) - params.e;
|
1997-10-25 11:16:08 +08:00
|
|
|
yv = sqrt(1.0 - params.e*params.e) * sin(EccAnom);
|
|
|
|
v = atan2(yv, xv);
|
|
|
|
r = sqrt(xv*xv + yv*yv);
|
|
|
|
|
1997-11-26 03:25:27 +08:00
|
|
|
/* calculate the the Sun's true longitude (lonsun) */
|
1997-10-25 11:16:08 +08:00
|
|
|
lonSun = v + params.w;
|
|
|
|
|
|
|
|
/* convert true longitude and distance to ecliptic rectangular geocentric
|
1997-11-26 03:25:27 +08:00
|
|
|
coordinates (xs, ys) */
|
1997-10-25 11:16:08 +08:00
|
|
|
solarPosition.xs = r * cos(lonSun);
|
|
|
|
solarPosition.ys = r * sin(lonSun);
|
1997-12-31 00:36:40 +08:00
|
|
|
solarPosition.dist = r;
|
1997-10-25 11:16:08 +08:00
|
|
|
return solarPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-26 03:25:27 +08:00
|
|
|
struct CelestialCoord fgCalculateSun(struct OrbElements params, struct fgTIME t)
|
|
|
|
{
|
|
|
|
struct CelestialCoord
|
|
|
|
result;
|
|
|
|
struct SunPos
|
|
|
|
SolarPosition;
|
|
|
|
double
|
|
|
|
xe, ye, ze, ecl, actTime;
|
|
|
|
|
|
|
|
/* calculate the angle between ecliptic and equatorial coordinate system */
|
|
|
|
actTime = fgCalcActTime(t);
|
|
|
|
ecl = fgDegToRad(23.4393 - 3.563E-7 * actTime); // Angle now in Rads
|
|
|
|
|
|
|
|
/* calculate the sun's ecliptic position */
|
|
|
|
SolarPosition = fgCalcSunPos(params);
|
|
|
|
|
|
|
|
/* convert ecliptic coordinates to equatorial rectangular geocentric coordinates */
|
|
|
|
xe = SolarPosition.xs;
|
|
|
|
ye = SolarPosition.ys * cos(ecl);
|
|
|
|
ze = SolarPosition.ys * sin(ecl);
|
|
|
|
|
|
|
|
/* and finally... Calulate Right Ascention and Declination */
|
|
|
|
result.RightAscension = atan2( ye, xe);
|
|
|
|
result.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize the Sun */
|
1998-01-20 02:40:15 +08:00
|
|
|
void fgSunInit( void ) {
|
1997-12-18 07:12:14 +08:00
|
|
|
static int dl_exists = 0;
|
1997-12-09 13:11:54 +08:00
|
|
|
|
1998-01-27 08:47:41 +08:00
|
|
|
fgPrintf( FG_ASTRO, FG_INFO, " Initializing the Sun\n");
|
1997-12-31 04:47:34 +08:00
|
|
|
|
1997-12-09 13:11:54 +08:00
|
|
|
fgSolarSystemUpdate(&(pltOrbElements[0]), cur_time_params);
|
|
|
|
sunPos = fgCalculateSun(pltOrbElements[0], cur_time_params);
|
|
|
|
#ifdef DEBUG
|
1998-01-27 08:47:41 +08:00
|
|
|
fgPrintf( FG_ASTRO, FG_INFO,
|
|
|
|
"Sun found at %f (ra), %f (dec)\n",
|
|
|
|
sunPos.RightAscension, sunPos.Declination);
|
1997-12-09 13:11:54 +08:00
|
|
|
#endif
|
|
|
|
|
1998-01-06 02:44:33 +08:00
|
|
|
xSun = 60000.0 * cos(sunPos.RightAscension) * cos(sunPos.Declination);
|
|
|
|
ySun = 60000.0 * sin(sunPos.RightAscension) * cos(sunPos.Declination);
|
|
|
|
zSun = 60000.0 * sin(sunPos.Declination);
|
|
|
|
|
1997-12-18 07:12:14 +08:00
|
|
|
if ( !dl_exists ) {
|
|
|
|
dl_exists = 1;
|
|
|
|
|
|
|
|
/* printf("First time through, creating sun display list\n"); */
|
|
|
|
|
|
|
|
sun_obj = xglGenLists(1);
|
|
|
|
xglNewList(sun_obj, GL_COMPILE );
|
|
|
|
|
|
|
|
glutSolidSphere(1.0, 10, 10);
|
|
|
|
|
|
|
|
xglEndList();
|
|
|
|
}
|
1997-11-26 03:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Draw the Sun */
|
1998-01-20 02:40:15 +08:00
|
|
|
void fgSunRender( void ) {
|
1997-12-11 06:37:34 +08:00
|
|
|
struct fgVIEW *v;
|
1997-11-26 03:25:27 +08:00
|
|
|
struct fgTIME *t;
|
1997-12-31 00:36:40 +08:00
|
|
|
struct fgLIGHT *l;
|
|
|
|
/* GLfloat color[4] = { 0.85, 0.65, 0.05, 1.0 }; */
|
|
|
|
GLfloat color[4] = { 1.00, 1.00, 1.00, 1.00 };
|
|
|
|
double x_2, x_4, x_8, x_10;
|
|
|
|
GLfloat ambient;
|
|
|
|
GLfloat amb[3], diff[3];
|
1997-11-26 03:25:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
t = &cur_time_params;
|
|
|
|
v = ¤t_view;
|
1997-12-31 00:36:40 +08:00
|
|
|
l = &cur_light_params;
|
1997-11-26 03:25:27 +08:00
|
|
|
|
1997-12-31 00:36:40 +08:00
|
|
|
x_2 = l->sun_angle * l->sun_angle;
|
1997-11-26 03:25:27 +08:00
|
|
|
x_4 = x_2 * x_2;
|
|
|
|
x_8 = x_4 * x_4;
|
1997-12-31 00:36:40 +08:00
|
|
|
x_10 = x_8 * x_2;
|
1997-11-26 03:25:27 +08:00
|
|
|
|
1997-12-31 00:36:40 +08:00
|
|
|
ambient = (0.4 * pow(1.1, -x_10 / 30.0));
|
1997-11-26 03:25:27 +08:00
|
|
|
if ( ambient < 0.3 ) ambient = 0.3;
|
|
|
|
if ( ambient > 1.0 ) ambient = 1.0;
|
1997-12-31 00:36:40 +08:00
|
|
|
|
1997-11-26 03:25:27 +08:00
|
|
|
amb[0] = 0.50 + ((ambient * 6.66) - 1.6);
|
|
|
|
amb[1] = 0.00 + ((ambient * 6.66) - 1.6);
|
|
|
|
amb[2] = 0.00 + ((ambient * 6.66) - 1.6);
|
|
|
|
amb[3] = 0.00;
|
|
|
|
#ifdef DEBUG
|
1998-01-27 08:47:41 +08:00
|
|
|
fgPrintf( FG_ASTRO, FG_INFO,
|
|
|
|
"Color of the sun: %f, %f, %f\n"
|
|
|
|
"Ambient value : %f\n"
|
|
|
|
"Sun Angle : %f\n" , amb[0], amb[1], amb[2], ambient, t->sun_angle);
|
1997-11-26 03:25:27 +08:00
|
|
|
#endif
|
|
|
|
diff[0] = 0.0;
|
|
|
|
diff[1] = 0.0;
|
|
|
|
diff[2] = 0.0;
|
1997-12-31 00:36:40 +08:00
|
|
|
diff[3] = 1.0;
|
1997-11-26 03:25:27 +08:00
|
|
|
|
|
|
|
/* set lighting parameters */
|
1997-12-31 00:36:40 +08:00
|
|
|
xglLightfv(GL_LIGHT0, GL_AMBIENT, color );
|
1997-12-16 07:54:25 +08:00
|
|
|
xglLightfv(GL_LIGHT0, GL_DIFFUSE, color );
|
|
|
|
xglMaterialfv(GL_FRONT, GL_AMBIENT, amb);
|
1997-12-31 00:36:40 +08:00
|
|
|
xglMaterialfv(GL_FRONT, GL_DIFFUSE, diff);
|
|
|
|
xglMaterialfv(GL_FRONT, GL_SHININESS, diff);
|
|
|
|
xglMaterialfv(GL_FRONT, GL_EMISSION, diff);
|
|
|
|
xglMaterialfv(GL_FRONT, GL_SPECULAR, diff);
|
1997-11-26 03:25:27 +08:00
|
|
|
|
1997-12-31 00:36:40 +08:00
|
|
|
/* xglDisable( GL_LIGHTING ); */
|
1997-12-09 13:11:54 +08:00
|
|
|
|
1997-12-16 07:54:25 +08:00
|
|
|
xglPushMatrix();
|
|
|
|
xglTranslatef(xSun, ySun, zSun);
|
|
|
|
xglScalef(1400, 1400, 1400);
|
1997-12-09 13:11:54 +08:00
|
|
|
|
1997-12-31 00:36:40 +08:00
|
|
|
xglColor3f(0.85, 0.65, 0.05);
|
1997-12-13 05:41:24 +08:00
|
|
|
|
1997-12-16 07:54:25 +08:00
|
|
|
xglCallList(sun_obj);
|
1997-12-09 13:11:54 +08:00
|
|
|
|
1997-12-16 07:54:25 +08:00
|
|
|
xglPopMatrix();
|
1997-12-09 13:11:54 +08:00
|
|
|
|
1997-12-31 00:36:40 +08:00
|
|
|
/* xglEnable( GL_LIGHTING ); */
|
1997-11-26 03:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-10-25 11:16:08 +08:00
|
|
|
/* $Log$
|
1998-01-27 08:47:41 +08:00
|
|
|
/* Revision 1.4 1998/01/27 00:47:50 curt
|
|
|
|
/* Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
|
|
|
|
/* system and commandline/config file processing code.
|
1997-10-25 11:16:08 +08:00
|
|
|
/*
|
1998-01-27 08:47:41 +08:00
|
|
|
* Revision 1.3 1998/01/19 19:27:00 curt
|
|
|
|
* Merged in make system changes from Bob Kuehne <rpk@sgi.com>
|
|
|
|
* This should simplify things tremendously.
|
|
|
|
*
|
1998-01-20 03:26:51 +08:00
|
|
|
* Revision 1.2 1998/01/19 18:40:18 curt
|
|
|
|
* Tons of little changes to clean up the code and to remove fatal errors
|
|
|
|
* when building with the c++ compiler.
|
|
|
|
*
|
1998-01-20 02:40:15 +08:00
|
|
|
* Revision 1.1 1998/01/07 03:16:20 curt
|
|
|
|
* Moved from .../Src/Scenery/ to .../Src/Astro/
|
|
|
|
*
|
1998-01-07 11:06:12 +08:00
|
|
|
* Revision 1.12 1998/01/05 18:44:36 curt
|
|
|
|
* Add an option to advance/decrease time from keyboard.
|
|
|
|
*
|
1998-01-06 02:44:33 +08:00
|
|
|
* Revision 1.11 1997/12/30 23:09:40 curt
|
|
|
|
* Worked on winding problem without luck, so back to calling glFrontFace()
|
|
|
|
* 3 times for each scenery area.
|
|
|
|
*
|
1997-12-31 07:09:40 +08:00
|
|
|
* Revision 1.10 1997/12/30 20:47:54 curt
|
|
|
|
* Integrated new event manager with subsystem initializations.
|
|
|
|
*
|
1997-12-31 04:47:34 +08:00
|
|
|
* Revision 1.9 1997/12/30 16:36:54 curt
|
|
|
|
* Merged in Durk's changes ...
|
|
|
|
*
|
1997-12-31 00:36:40 +08:00
|
|
|
* Revision 1.8 1997/12/19 23:35:00 curt
|
|
|
|
* Lot's of tweaking with sky rendering and lighting.
|
|
|
|
*
|
1997-12-20 07:34:03 +08:00
|
|
|
* Revision 1.7 1997/12/17 23:12:16 curt
|
|
|
|
* Fixed so moon and sun display lists aren't recreate periodically.
|
|
|
|
*
|
1997-12-18 07:12:14 +08:00
|
|
|
* Revision 1.6 1997/12/15 23:55:04 curt
|
|
|
|
* Add xgl wrappers for debugging.
|
|
|
|
* Generate terrain normals on the fly.
|
|
|
|
*
|
1997-12-16 07:54:25 +08:00
|
|
|
* Revision 1.5 1997/12/12 21:41:31 curt
|
|
|
|
* More light/material property tweaking ... still a ways off.
|
|
|
|
*
|
1997-12-13 05:41:24 +08:00
|
|
|
* Revision 1.4 1997/12/10 22:37:53 curt
|
|
|
|
* Prepended "fg" on the name of all global structures that didn't have it yet.
|
|
|
|
* i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
|
|
|
|
*
|
1997-12-11 06:37:34 +08:00
|
|
|
* Revision 1.3 1997/12/09 05:11:56 curt
|
|
|
|
* Working on tweaking lighting.
|
|
|
|
*
|
1997-12-09 13:11:54 +08:00
|
|
|
* Revision 1.2 1997/11/25 19:25:39 curt
|
|
|
|
* Changes to integrate Durk's moon/sun code updates + clean up.
|
|
|
|
*
|
1997-11-26 03:25:27 +08:00
|
|
|
* Revision 1.1 1997/10/25 03:16:11 curt
|
|
|
|
* Initial revision of code contributed by Durk Talsma.
|
|
|
|
*
|
1997-10-25 11:16:08 +08:00
|
|
|
*/
|
1997-11-26 03:25:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|