From 3a5804a3f9f4e48325c6c2666286092a9eb943f8 Mon Sep 17 00:00:00 2001 From: curt Date: Wed, 7 Jan 1998 03:16:15 +0000 Subject: [PATCH] Moved from .../Src/Scenery/ to .../Src/Astro/ --- Astro/Makefile | 165 +++++++++++++++++++++ Astro/depend | 56 ++++++++ Astro/moon.c | 338 +++++++++++++++++++++++++++++++++++++++++++ Astro/moon.h | 70 +++++++++ Astro/orbits.c | 199 ++++++++++++++++++++++++++ Astro/orbits.h | 93 ++++++++++++ Astro/planets.c | 153 ++++++++++++++++++++ Astro/planets.h | 50 +++++++ Astro/sky.c | 370 ++++++++++++++++++++++++++++++++++++++++++++++++ Astro/sky.h | 48 +++++++ Astro/stars.c | 357 ++++++++++++++++++++++++++++++++++++++++++++++ Astro/stars.h | 69 +++++++++ Astro/sun.c | 240 +++++++++++++++++++++++++++++++ Astro/sun.h | 57 ++++++++ 14 files changed, 2265 insertions(+) create mode 100644 Astro/Makefile create mode 100644 Astro/depend create mode 100644 Astro/moon.c create mode 100644 Astro/moon.h create mode 100644 Astro/orbits.c create mode 100644 Astro/orbits.h create mode 100644 Astro/planets.c create mode 100644 Astro/planets.h create mode 100644 Astro/sky.c create mode 100644 Astro/sky.h create mode 100644 Astro/stars.c create mode 100644 Astro/stars.h create mode 100644 Astro/sun.c create mode 100644 Astro/sun.h diff --git a/Astro/Makefile b/Astro/Makefile new file mode 100644 index 000000000..24e85c651 --- /dev/null +++ b/Astro/Makefile @@ -0,0 +1,165 @@ +#--------------------------------------------------------------------------- +# Makefile +# +# Written by Curtis Olson, started May 1997. +# +# Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com +# +# 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) +#--------------------------------------------------------------------------- + + +TARGET = libAstro.a + +CFILES = moon.c orbits.c planets.c sky.c stars.c sun.c + +OFILES = $(CFILES:.c=.o) + + +include ../make.inc + + +CFLAGS = $(FG_CFLAGS) + + +#--------------------------------------------------------------------------- +# Primary Targets +#--------------------------------------------------------------------------- + +$(TARGET): $(OFILES) $(HFILES) + $(AR) rv $(TARGET) $(OFILES) + $(RANLIB) $(TARGET) + +all: $(TARGET) + +clean: + rm -f *.o $(TARGET) lib*.a *.os2 *~ core + +realclean: clean + + +#--------------------------------------------------------------------------- +# Secondary Targets +#--------------------------------------------------------------------------- + +include depend + +moon.o: + $(CC) $(CFLAGS) -c moon.c -o $@ + +orbits.o: + $(CC) $(CFLAGS) -c orbits.c -o $@ + +planets.o: + $(CC) $(CFLAGS) -c planets.c -o $@ + +sky.c: + $(CC) $(CFLAGS) -c sky.c -o $@ + +stars.c: + $(CC) $(CFLAGS) -c stars.c -o $@ + +sun.o: + $(CC) $(CFLAGS) -c sun.c -o $@ + + + +#--------------------------------------------------------------------------- +# $Log$ +# Revision 1.1 1998/01/07 03:16:15 curt +# Moved from .../Src/Scenery/ to .../Src/Astro/ +# +# Revision 1.26 1997/12/19 16:45:01 curt +# Working on scene rendering order and options. +# +# Revision 1.25 1997/12/17 23:13:45 curt +# Began working on rendering the sky. +# +# Revision 1.24 1997/11/25 19:25:33 curt +# Changes to integrate Durk's moon/sun code updates + clean up. +# +# Revision 1.23 1997/10/28 21:00:20 curt +# Changing to new terrain format. +# +# Revision 1.22 1997/10/25 03:30:07 curt +# Misc. tweaks. +# +# Revision 1.21 1997/10/25 03:18:26 curt +# Incorporated sun, moon, and planet position and rendering code contributed +# by Durk Talsma. +# +# Revision 1.20 1997/09/22 14:44:21 curt +# Continuing to try to align stars correctly. +# +# Revision 1.19 1997/08/27 03:30:23 curt +# Changed naming scheme of basic shared structures. +# +# Revision 1.18 1997/08/02 19:10:12 curt +# Incorporated mesh2GL.c into mesh.c +# +# Revision 1.17 1997/07/23 21:52:23 curt +# Put comments around the text after an #endif for increased portability. +# +# Revision 1.16 1997/07/20 02:19:11 curt +# First stab at a system to generate os2 makefiles automatically. +# +# Revision 1.15 1997/07/12 02:24:47 curt +# Added ranlib. +# +# Revision 1.14 1997/06/29 21:16:47 curt +# More twiddling with the Scenery Management system. +# +# Revision 1.13 1997/06/27 21:38:10 curt +# Working on Makefile structure. +# +# Revision 1.12 1997/06/27 20:03:37 curt +# Working on Makefile structure. +# +# Revision 1.11 1997/06/26 22:14:57 curt +# Beginning work on a scenery management system. +# +# Revision 1.10 1997/06/26 19:08:34 curt +# Restructuring make, adding automatic "make dep" support. +# +# Revision 1.9 1997/06/25 15:39:48 curt +# Minor changes to compile with rsxnt/win32. +# +# Revision 1.8 1997/06/21 17:58:07 curt +# directory shuffling ... +# +# Revision 1.1 1997/06/21 17:39:28 curt +# Moved to the ParseScn subdirectory. +# +# Revision 1.6 1997/06/21 17:12:55 curt +# Capitalized subdirectory names. +# +# Revision 1.5 1997/05/31 19:16:29 curt +# Elevator trim added. +# +# Revision 1.4 1997/05/27 17:48:50 curt +# Added -f flag to flex to generate a "fast" scanner. +# +# Revision 1.3 1997/05/23 15:40:40 curt +# Added GNU copyright headers. +# +# Revision 1.2 1997/05/17 00:17:01 curt +# Cosmetic changes. +# +# Revision 1.1 1997/05/16 16:07:02 curt +# Initial revision. +# diff --git a/Astro/depend b/Astro/depend new file mode 100644 index 000000000..a948633f1 --- /dev/null +++ b/Astro/depend @@ -0,0 +1,56 @@ +astro.o: astro.c ../XGL/xgl.h astro.h stars.h moon.h orbits.h \ + ../Time/fg_time.h ../Time/../Include/types.h \ + ../Time/../Flight/flight.h ../Time/../Flight/Slew/slew.h \ + ../Time/../Flight/LaRCsim/ls_interface.h \ + ../Time/../Flight/LaRCsim/../flight.h planets.h sun.h \ + ../Include/constants.h ../Include/general.h ../Main/views.h \ + ../Main/../Include/types.h ../Main/../Flight/flight.h \ + ../Main/../Math/mat3.h ../Main/../Time/fg_time.h \ + ../Aircraft/aircraft.h ../Aircraft/../Flight/flight.h \ + ../Aircraft/../Controls/controls.h \ + ../Aircraft/../Controls/../Include/limits.h +moon.o: moon.c ../XGL/xgl.h orbits.h ../Time/fg_time.h \ + ../Time/../Include/types.h ../Time/../Flight/flight.h \ + ../Time/../Flight/Slew/slew.h \ + ../Time/../Flight/LaRCsim/ls_interface.h \ + ../Time/../Flight/LaRCsim/../flight.h moon.h ../Aircraft/aircraft.h \ + ../Aircraft/../Flight/flight.h ../Aircraft/../Controls/controls.h \ + ../Aircraft/../Controls/../Include/limits.h ../Include/constants.h \ + ../Include/general.h ../Main/views.h ../Main/../Include/types.h \ + ../Main/../Flight/flight.h ../Main/../Math/mat3.h \ + ../Main/../Time/fg_time.h +orbits.o: orbits.c orbits.h ../Time/fg_time.h \ + ../Time/../Include/types.h ../Time/../Flight/flight.h \ + ../Time/../Flight/Slew/slew.h \ + ../Time/../Flight/LaRCsim/ls_interface.h \ + ../Time/../Flight/LaRCsim/../flight.h ../Include/general.h +planets.o: planets.c ../Time/fg_time.h ../Time/../Include/types.h \ + ../Time/../Flight/flight.h ../Time/../Flight/Slew/slew.h \ + ../Time/../Flight/LaRCsim/ls_interface.h \ + ../Time/../Flight/LaRCsim/../flight.h orbits.h planets.h sun.h +sky.o: sky.c ../XGL/xgl.h sky.h ../Time/event.h ../Time/fg_time.h \ + ../Time/../Include/types.h ../Time/../Flight/flight.h \ + ../Time/../Flight/Slew/slew.h \ + ../Time/../Flight/LaRCsim/ls_interface.h \ + ../Time/../Flight/LaRCsim/../flight.h ../Aircraft/aircraft.h \ + ../Aircraft/../Flight/flight.h ../Aircraft/../Controls/controls.h \ + ../Aircraft/../Controls/../Include/limits.h ../Flight/flight.h \ + ../Include/constants.h ../Main/views.h ../Main/../Include/types.h \ + ../Main/../Flight/flight.h ../Main/../Math/mat3.h \ + ../Main/../Time/fg_time.h ../Math/fg_random.h +stars.o: stars.c ../XGL/xgl.h orbits.h ../Time/fg_time.h \ + ../Time/../Include/types.h ../Time/../Flight/flight.h \ + ../Time/../Flight/Slew/slew.h \ + ../Time/../Flight/LaRCsim/ls_interface.h \ + ../Time/../Flight/LaRCsim/../flight.h planets.h stars.h \ + ../Include/constants.h ../Include/general.h ../Aircraft/aircraft.h \ + ../Aircraft/../Flight/flight.h ../Aircraft/../Controls/controls.h \ + ../Aircraft/../Controls/../Include/limits.h ../Main/views.h \ + ../Main/../Include/types.h ../Main/../Flight/flight.h \ + ../Main/../Math/mat3.h ../Main/../Time/fg_time.h +sun.o: sun.c ../XGL/xgl.h ../Time/fg_time.h ../Time/../Include/types.h \ + ../Time/../Flight/flight.h ../Time/../Flight/Slew/slew.h \ + ../Time/../Flight/LaRCsim/ls_interface.h \ + ../Time/../Flight/LaRCsim/../flight.h ../Main/views.h \ + ../Main/../Include/types.h ../Main/../Flight/flight.h \ + ../Main/../Math/mat3.h ../Main/../Time/fg_time.h orbits.h sun.h diff --git a/Astro/moon.c b/Astro/moon.c new file mode 100644 index 000000000..aa99c92dd --- /dev/null +++ b/Astro/moon.c @@ -0,0 +1,338 @@ +/************************************************************************** + * moon.c + * Written 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) + **************************************************************************/ + + +#include +#include +#include "../XGL/xgl.h" + +#include "orbits.h" +#include "moon.h" + +#include "../Aircraft/aircraft.h" +#include "../Include/constants.h" +#include "../Include/general.h" +#include "../Main/views.h" +#include "../Time/fg_time.h" + +struct CelestialCoord moonPos; + +static float xMoon, yMoon, zMoon; +static GLint moon; + +/* +static GLfloat vdata[12][3] = +{ + {-X, 0.0, Z }, { X, 0.0, Z }, {-X, 0.0, -Z}, {X, 0.0, -Z }, + { 0.0, Z, X }, { 0.0, Z, -X}, {0.0, -Z, -X}, {0.0, -Z, -X}, + { Z, X, 0.0 }, { -Z, X, 0.0}, {Z, -X, 0.0 }, {-Z, -X, 0.0} +}; + +static GLuint tindices[20][3] = +{ + {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1}, + {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3}, + {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6}, + {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} +};*/ + +/* ------------------------------------------------------------- + This section contains the code that generates a yellow + Icosahedron. It's under development... (of Course) +______________________________________________________________*/ +/* +void NormalizeVector(float v[3]) +{ + GLfloat d = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); + if (d == 0.0) + { + printf("zero length vector\n"); + return; + } + v[0] /= d; + v[1] /= d; + v[2] /= d; +} + +void drawTriangle(float *v1, float *v2, float *v3) +{ + xglBegin(GL_POLYGON); + //xglBegin(GL_POINTS); + xglNormal3fv(v1); + xglVertex3fv(v1); + xglNormal3fv(v2); + xglVertex3fv(v2); + xglNormal3fv(v3); + xglVertex3fv(v3); + xglEnd(); +} + +void subdivide(float *v1, float *v2, float *v3, long depth) +{ + GLfloat v12[3], v23[3], v31[3]; + GLint i; + + if (!depth) + { + drawTriangle(v1, v2, v3); + return; + } + for (i = 0; i < 3; i++) + { + v12[i] = (v1[i] + v2[i]); + v23[i] = (v2[i] + v3[i]); + v31[i] = (v3[i] + v1[i]); + } + NormalizeVector(v12); + NormalizeVector(v23); + NormalizeVector(v31); + subdivide(v1, v12, v31, depth - 1); + subdivide(v2, v23, v12, depth - 1); + subdivide(v3, v31, v23, depth - 1); + subdivide(v12, v23, v31,depth - 1); + +} */ +/* +void display(void) +{ + int i; + xglClear(GL_COLOR_BUFFER_BIT); + xglPushMatrix(); + xglRotatef(spin, 0.0, 0.0, 0.0); + xglColor3f(1.0, 1.0, 0.0); +// xglBegin(GL_LINE_LOOP); + for (i = 0; i < 20; i++) + { + + //xglVertex3fv(&vdata[tindices[i][0]][0]); + //xglVertex3fv(&vdata[tindices[i][1]][0]); + //xglVertex3fv(&vdata[tindices[i][2]][0]); + + subdivide(&vdata[tindices[i][0]][0], + &vdata[tindices[i][1]][0], + &vdata[tindices[i][2]][0], 3); + + + } +// xglEnd(); + // xglFlush(); + xglPopMatrix(); + glutSwapBuffers(); +} */ + +/* -------------------------------------------------------------- + + This section contains the code that calculates the actual + position of the moon in the night sky. + +----------------------------------------------------------------*/ + +struct CelestialCoord fgCalculateMoon(struct OrbElements params, + struct OrbElements sunParams, + struct fgTIME t) +{ + struct CelestialCoord + geocCoord, topocCoord; + + + double + eccAnom, ecl, lonecl, latecl, actTime, + xv, yv, v, r, xh, yh, zh, xg, yg, zg, xe, ye, ze, + Ls, Lm, D, F, mpar, gclat, rho, HA, g; + + struct fgAIRCRAFT *a; + struct fgFLIGHT *f; + + a = ¤t_aircraft; + f = &a->flight; + +/* calculate the angle between ecliptic and equatorial coordinate system */ + actTime = fgCalcActTime(t); + ecl = fgDegToRad(23.4393 - 3.563E-7 * actTime); // in radians of course + + /* calculate the eccentric anomaly */ + eccAnom = fgCalcEccAnom(params.M, params.e); + + /* calculate the moon's distance (d) and true anomaly (v) */ + xv = params.a * ( cos(eccAnom) - params.e); + yv = params.a * ( sqrt(1.0 - params.e*params.e) * sin(eccAnom)); + v =atan2(yv, xv); + r = sqrt(xv*xv + yv*yv); + + /* estimate the geocentric rectangular coordinates here */ + xh = r * (cos(params.N) * cos(v + params.w) - sin(params.N) * sin(v + params.w) * cos(params.i)); + yh = r * (sin(params.N) * cos(v + params.w) + cos(params.N) * sin(v + params.w) * cos(params.i)); + zh = r * (sin(v + params.w) * sin(params.i)); + + /* calculate the ecliptic latitude and longitude here */ + lonecl = atan2( yh, xh); + latecl = atan2( zh, sqrt( xh*xh + yh*yh)); + + /* calculate a number of perturbations */ + Ls = sunParams.M + sunParams.w; + Lm = params.M + params.w + params.N; + D = Lm - Ls; + F = Lm - params.N; + + lonecl += fgDegToRad( + - 1.274 * sin (params.M - 2*D) // the Evection + + 0.658 * sin (2 * D) // the Variation + - 0.186 * sin (sunParams.M) // the yearly variation + - 0.059 * sin (2*params.M - 2*D) + - 0.057 * sin (params.M - 2*D + sunParams.M) + + 0.053 * sin (params.M + 2*D) + + 0.046 * sin (2*D - sunParams.M) + + 0.041 * sin (params.M - sunParams.M) + - 0.035 * sin (D) // the Parallactic Equation + - 0.031 * sin (params.M + sunParams.M) + - 0.015 * sin (2*F - 2*D) + + 0.011 * sin (params.M - 4*D) + ); /* Pheeuuwwww */ + latecl += fgDegToRad( + - 0.173 * sin (F - 2*D) + - 0.055 * sin (params.M - F - 2*D) + - 0.046 * sin (params.M + F - 2*D) + + 0.033 * sin (F + 2*D) + + 0.017 * sin (2 * params.M + F) + ); /* Yep */ + + r += ( + - 0.58 * cos(params.M - 2*D) + - 0.46 * cos(2*D) + ); /* Ok! */ + + xg = r * cos(lonecl) * cos(latecl); + yg = r * sin(lonecl) * cos(latecl); + zg = r * sin(latecl); + + xe = xg; + ye = yg * cos(ecl) - zg * sin(ecl); + ze = yg * sin(ecl) + zg * cos(ecl); + + + + + geocCoord.RightAscension = atan2(ye, xe); + geocCoord.Declination = atan2(ze, sqrt(xe*xe + ye*ye)); + + /* New since 25 december 1997 */ + /* Calculate the moon's topocentric position instead of it's geocentric! */ + + mpar = asin( 1 / r); /* calculate the moon's parrallax, i.e. the apparent size of the + (equatorial) radius of the Earth, as seen from the moon */ + gclat = FG_Latitude - 0.083358 * sin (2 * fgDegToRad( FG_Latitude)); + rho = 0.99883 + 0.00167 * cos(2 * fgDegToRad(FG_Latitude)); + + if (geocCoord.RightAscension < 0) + geocCoord.RightAscension += (2*FG_PI); + + HA = t.lst - (3.8197186 * geocCoord.RightAscension); + + g = atan (tan(gclat) / cos( (HA / 3.8197186))); + + + + topocCoord.RightAscension = geocCoord.RightAscension - mpar * rho * cos(gclat) * sin(HA) / cos(geocCoord.Declination); + topocCoord.Declination = geocCoord.Declination - mpar * rho * sin(gclat) * sin(g - geocCoord.Declination) / sin(g); + return topocCoord; +} + + +void fgMoonInit() { + struct fgLIGHT *l; + static int dl_exists = 0; + + printf("Initializing the Moon\n"); + + l = &cur_light_params; + + /* position the moon */ + fgSolarSystemUpdate(&(pltOrbElements[1]), cur_time_params); + moonPos = fgCalculateMoon(pltOrbElements[1], pltOrbElements[0], + cur_time_params); +#ifdef DEBUG + printf("Moon found at %f (ra), %f (dec)\n", moonPos.RightAscension, + moonPos.Declination); +#endif + + xMoon = 60000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination); + yMoon = 60000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination); + zMoon = 60000.0 * sin(moonPos.Declination); + + if ( !dl_exists ) { + dl_exists = 1; + + /* printf("First time through, creating moon display list\n"); */ + + moon = xglGenLists(1); + xglNewList(moon, GL_COMPILE ); + + /* xglMaterialfv(GL_FRONT, GL_AMBIENT, l->scene_clear); + xglMaterialfv(GL_FRONT, GL_DIFFUSE, moon_color); */ + + + glutSolidSphere(1.0, 10, 10); + + xglEndList(); + } +} + + +/* Draw the moon */ +void fgMoonRender() { + struct fgLIGHT *l; + GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 }; + + /* printf("Rendering moon\n"); */ + + l = &cur_light_params; + + xglMaterialfv(GL_FRONT, GL_AMBIENT, l->sky_color ); + xglMaterialfv(GL_FRONT, GL_DIFFUSE, white); + + xglPushMatrix(); + xglTranslatef(xMoon, yMoon, zMoon); + xglScalef(1400, 1400, 1400); + + xglCallList(moon); + + xglPopMatrix(); +} + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:16 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.16 1998/01/06 01:20:24 curt + * Tweaks to help building with MSVC++ + * + * Revision 1.15 1998/01/05 18:44:35 curt + * Add an option to advance/decrease time from keyboard. + * + * Revision 1.14 1997/12/30 20:47:50 curt + * Integrated new event manager with subsystem initializations. + * + * Revision 1.13 1997/12/30 16:41:00 curt + * Added log at end of file. + * + */ diff --git a/Astro/moon.h b/Astro/moon.h new file mode 100644 index 000000000..c4d21ed20 --- /dev/null +++ b/Astro/moon.h @@ -0,0 +1,70 @@ +/************************************************************************** + * moon.h + * + * Written 1997 by Durk Talsma, started October, 1997. + * + * 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) + **************************************************************************/ + + +#ifndef _MOON_H_ +#define _MOON_H_ + + +#include "orbits.h" + +#include "../Time/fg_time.h" +#include + +#define X .525731112119133606 +#define Z .850650808352039932 + + + /* Initialize the Moon Display management Subsystem */ +void fgMoonInit(); + +/* Draw the Stars */ +void fgMoonRender(); + +struct CelestialCoord fgCalculateMoon(struct OrbElements Params, + struct OrbElements sunParams, + struct fgTIME t); + +extern struct OrbElements pltOrbElements[9]; + +#endif /* _MOON_H_ */ + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:16 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.4 1997/12/11 04:43:56 curt + * Fixed sun vector and lighting problems. I thing the moon is now lit + * correctly. + * + * Revision 1.3 1997/11/25 19:25:35 curt + * Changes to integrate Durk's moon/sun code updates + clean up. + * + * Revision 1.2 1997/10/25 03:24:23 curt + * Incorporated sun, moon, and star positioning code contributed by Durk Talsma. + * + * Revision 1.1 1997/10/25 03:16:09 curt + * Initial revision of code contributed by Durk Talsma. + * + */ diff --git a/Astro/orbits.c b/Astro/orbits.c new file mode 100644 index 000000000..50a283668 --- /dev/null +++ b/Astro/orbits.c @@ -0,0 +1,199 @@ +/************************************************************************** + * orbits.c - calculates the orbital elements of the sun, moon and planets. + * For inclusion in flight gear + * + * Written 1997 by Durk Talsma, started October 19, 1997. + * + * 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) + **************************************************************************/ + + +#include + +#include "orbits.h" + +#include "../Include/general.h" +#include "../Time/fg_time.h" + + +struct OrbElements pltOrbElements[9]; + + +double fgCalcActTime(struct fgTIME t) +{ + double + actTime, UT; + int year; + + /* a hack. This one introduces the 2000 problem into the program */ + year = t.gmt->tm_year + 1900; + + /* calculate the actual time, rember to add 1 to tm_mon! */ + actTime = 367 * year - 7 * + (year + ((t.gmt->tm_mon+1) + 9) / 12) / 4 + 275 * + (t.gmt->tm_mon+1) / 9 + t.gmt->tm_mday - 730530; + + UT = t.gmt->tm_hour + ((double) t.gmt->tm_min / 60); + /*printf("UT = %f\n", UT); */ + actTime += (UT / 24.0); + #define DEBUG 1 + #ifdef DEBUG + /* printf(" Actual Time:\n"); */ + /* printf(" current day = %f\t", actTime); */ + /* printf(" GMT = %d, %d, %d, %d, %d, %d\n", + year, t.gmt->tm_mon, t.gmt->tm_mday, + t.gmt->tm_hour, t.gmt->tm_min, t.gmt->tm_sec); */ + #endif + return actTime; +} + +/* convert degrees to radians */ +double fgDegToRad(double angle) +{ + return (angle * PIOVER180); +} + +double fgCalcEccAnom(double M, double e) +{ + double + eccAnom, E0, E1, diff; + + eccAnom = M + e * sin(M) * (1.0 + e * cos(M)); + /* iterate to achieve a greater precision for larger eccentricities */ + if (e > 0.05) + { + E0 = eccAnom; + do + { + E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0)); + diff = abs(E0 - E1); + E0 = E1; + } + while (diff > fgDegToRad(0.001)); + return E0; + } + return eccAnom; +} + + + +void fgReadOrbElements(struct OrbElements *dest, FILE *src) +{ + char line[256]; + int i,j; + j = 0; + do + { + fgets(line, 256,src); + for (i = 0; i < 256; i++) + { + if (line[i] == '#') + line[i] = 0; + } + /*printf("Reading line %d\n", j++); */ + } + while (!(strlen(line))); + sscanf(line, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n", + &dest->NFirst, &dest->NSec, + &dest->iFirst, &dest->iSec, + &dest->wFirst, &dest->wSec, + &dest->aFirst, &dest->aSec, + &dest->eFirst, &dest->eSec, + &dest->MFirst, &dest->MSec); +} + + +void fgSolarSystemInit(struct fgTIME t) +{ + struct fgGENERAL *g; + char path[80]; + int i; + FILE *data; + + printf("Initializing solar system\n"); + + /* build the full path name to the orbital elements database file */ + g = &general; + path[0] = '\0'; + strcat(path, g->root_dir); + strcat(path, "/Scenery/"); + strcat(path, "Planets.dat"); + + if ( (data = fopen(path, "r")) == NULL ) + { + printf("Cannot open data file: '%s'\n", path); + return; + } + #ifdef DEBUG + printf(" reading datafile %s\n", path); + #endif + + /* for all the objects... */ + for (i = 0; i < 9; i ++) + { + /* ...read from the data file ... */ + fgReadOrbElements(&pltOrbElements[i], data); + /* ...and calculate the actual values */ + fgSolarSystemUpdate(&pltOrbElements[i], t); + } + +} + + +void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t) +{ + double + actTime; + + actTime = fgCalcActTime(t); + + /* calculate the actual orbital elements */ + planet->M = fgDegToRad(planet->MFirst + (planet->MSec * actTime)); // angle in radians + planet->w = fgDegToRad(planet->wFirst + (planet->wSec * actTime)); // angle in radians + planet->N = fgDegToRad(planet->NFirst + (planet->NSec * actTime)); // angle in radians + planet->i = fgDegToRad(planet->iFirst + (planet->iSec * actTime)); // angle in radians + planet->e = planet->eFirst + (planet->eSec * actTime); + planet->a = planet->aFirst + (planet->aSec * actTime); +} + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:17 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.6 1997/12/30 20:47:52 curt + * Integrated new event manager with subsystem initializations. + * + * Revision 1.5 1997/12/15 23:55:02 curt + * Add xgl wrappers for debugging. + * Generate terrain normals on the fly. + * + * Revision 1.4 1997/12/10 22:37:51 curt + * Prepended "fg" on the name of all global structures that didn't have it yet. + * i.e. "struct WEATHER {}" became "struct fgWEATHER {}" + * + * Revision 1.3 1997/11/25 23:20:44 curt + * Changed planets.dat Planets.dat + * + * Revision 1.2 1997/11/25 19:25:36 curt + * Changes to integrate Durk's moon/sun code updates + clean up. + * + * Revision 1.1 1997/10/25 03:16:10 curt + * Initial revision of code contributed by Durk Talsma. + * + */ diff --git a/Astro/orbits.h b/Astro/orbits.h new file mode 100644 index 000000000..9077cd4aa --- /dev/null +++ b/Astro/orbits.h @@ -0,0 +1,93 @@ +/************************************************************************** + * orbits.h - calculates the orbital elements of the sun, moon and planets. + * For inclusion in flight gear + * + * Written 1997 by Durk Talsma, started October 19, 1997. + * + * 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) + **************************************************************************/ + + +#ifndef ORBITS_H +#define ORBITS_H + + +#include +#include + +#include "../Time/fg_time.h" + + + +#define STANDARDEPOCH 2000 +#define PIOVER180 1.74532925199433E-002 + +struct SunPos { + double xs; + double ys; + double dist; +}; + +struct OrbElements { + double NFirst; /* longitude of the ascending node first part */ + double NSec; /* longitude of the ascending node second part */ + double iFirst; /* inclination to the ecliptic first part */ + double iSec; /* inclination to the ecliptic second part */ + double wFirst; /* first part of argument of perihelion */ + double wSec; /* second part of argument of perihelion */ + double aFirst; /* semimayor axis first part*/ + double aSec; /* semimayor axis second part */ + double eFirst; /* eccentricity first part */ + double eSec; /* eccentricity second part */ + double MFirst; /* Mean anomaly first part */ + double MSec; /* Mean anomaly second part */ + + double N, i, w, a, e, M; /* the resultant orbital elements, obtained from the former */ +}; + +struct CelestialCoord { + double RightAscension; + double Declination; + double distance; + double magnitude; +}; + + +double fgDegToRad(double angle); +double fgCalcEccAnom(double M, double e); +double fgCalcActTime(struct fgTIME t); + +void fgReadOrbElements(struct OrbElements *dest, FILE *src); +void fgSolarSystemInit(struct fgTIME t); +void fgSolarSystemUpdate(struct OrbElements *planets, struct fgTIME t); + + +#endif /* ORBITS_H */ + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:17 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.2 1997/12/30 16:36:52 curt + * Merged in Durk's changes ... + * + * Revision 1.1 1997/10/25 03:16:10 curt + * Initial revision of code contributed by Durk Talsma. + * + */ diff --git a/Astro/planets.c b/Astro/planets.c new file mode 100644 index 000000000..0453cb334 --- /dev/null +++ b/Astro/planets.c @@ -0,0 +1,153 @@ +/************************************************************************** + * planets.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) + **************************************************************************/ + + +#include "../Time/fg_time.h" +#include "orbits.h" +#include "planets.h" +#include "sun.h" + + +struct CelestialCoord fgCalculatePlanet(struct OrbElements planet, + struct OrbElements theSun, + struct fgTIME t, int idx) +{ + struct CelestialCoord + result; + + struct SunPos + SolarPosition; + + double + eccAnom, r, v, ecl, actTime, R, s, ir, Nr, B, FV, ring_magn, + xv, yv, xh, yh, zh, xg, yg, zg, xe, ye, ze; + + actTime = fgCalcActTime(t); + /* calculate the angle between ecliptic and equatorial coordinate system */ + ecl = fgDegToRad(23.4393 - 3.563E-7 * actTime); + + + /* calculate the eccentric anomaly */ + eccAnom = fgCalcEccAnom(planet.M, planet.e); + + /* calculate the planets distance (r) and true anomaly (v) */ + xv = planet.a * (cos(eccAnom) - planet.e); + yv = planet.a * (sqrt(1.0 - planet.e*planet.e) * sin(eccAnom)); + v = atan2(yv, xv); + r = sqrt ( xv*xv + yv*yv); + + /* calculate the planets position in 3-dimensional space */ + xh = r * ( cos(planet.N) * cos(v+planet.w) - sin(planet.N) * sin(v+planet.w) * cos(planet.i)); + yh = r * ( sin(planet.N) * cos(v+planet.w) + cos(planet.N) * sin(v+planet.w) * cos(planet.i)); + zh = r * ( sin(v+planet.w) * sin(planet.i)); + + /* calculate the ecleptic longitude and latitude */ + + /* + lonecl = atan2(yh, xh); + latecl = atan2(zh, sqrt ( xh*xh + yh*yh)); + */ + /* calculate the solar position */ + + SolarPosition = fgCalcSunPos(theSun); + xg = xh + SolarPosition.xs; + yg = yh + SolarPosition.ys; + zg = zh; + + xe = xg; + ye = yg * cos(ecl) - zg * sin(ecl); + ze = yg * sin(ecl) + zg * cos(ecl); + + result.RightAscension = atan2(ye,xe); + result.Declination = atan2(ze, sqrt(xe*xe + ye*ye)); + + + /* Let's calculate the brightness of the planet */ + R = sqrt ( xg*xg + yg*yg + zg*zg); + s = SolarPosition.dist; + FV = acos( (r*r + R*R - s*s) / (2*r*R)); + FV *= 57.29578; /* convert radians to degrees */ + switch(idx) + { + case 2: /* mercury */ + result.magnitude = -0.36 + 5*log10( r*R ) + 0.027 * FV + 2.2E-13 * pow(FV, 6); + break; + case 3: /*venus */ + result.magnitude = -4.34 + 5*log10( r*R ) + 0.013 * FV + 4.2E-07 * pow(FV,3); + break; + case 4: /* mars */ + result.magnitude = -1.51 + 5*log10( r*R ) + 0.016 * FV; + break; + case 5: /* Jupiter */ + result.magnitude = -9.25 + 5*log10( r*R ) + 0.014 * FV; + break; + case 6: /* Saturn */ + + ir = 0.4897394; + Nr = 2.9585076 + 6.6672E-7*actTime; + + B = asin ( sin (result.Declination) * cos(ir) - cos(result.Declination) * sin (ir) * sin (result.RightAscension - Nr)); + ring_magn = -2.6 * sin (abs(B)) + 1.2 * pow(sin(B),2); + result.magnitude = -9.0 + 5*log10( r*R ) + 0.044 * FV + ring_magn; + break; + case 7: /* Uranus */ + result.magnitude = -7.15 + 5*log10( r*R) + 0.001 * FV; + break; + case 8: /* Neptune */ + result.magnitude = -6.90 + 5*log10 (r*R) + 0.001 *FV; + break; + default: + printf("index %d out of range !!!!\n", idx); + } + printf(" Planet found at %f (ra), %f (dec)\n", + result.RightAscension, result.Declination); + printf(" Geocentric dist %f\n" + " Heliocentric dist %f\n" + " Distance to the sun %f\n" + " Phase angle %f\n" + " Brightness %f\n", R, r, s, FV, result.magnitude); + return result; +} + + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:18 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.4 1997/12/30 20:47:52 curt + * Integrated new event manager with subsystem initializations. + * + * Revision 1.3 1997/12/30 16:36:52 curt + * Merged in Durk's changes ... + * + * Revision 1.2 1997/12/12 21:41:29 curt + * More light/material property tweaking ... still a ways off. + * + * Revision 1.1 1997/10/25 03:16:10 curt + * Initial revision of code contributed by Durk Talsma. + * + */ + + diff --git a/Astro/planets.h b/Astro/planets.h new file mode 100644 index 000000000..6cf874896 --- /dev/null +++ b/Astro/planets.h @@ -0,0 +1,50 @@ +/************************************************************************** + * planets.h + * + * 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) + **************************************************************************/ + + +#ifndef PLANETS_H +#define PLANETS_H + + +struct CelestialCoord fgCalculatePlanet(struct OrbElements planet, + struct OrbElements theSun, + struct fgTIME t, int idx); + +#endif /* PLANETS_H */ + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:18 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.3 1997/12/30 16:36:53 curt + * Merged in Durk's changes ... + * + * Revision 1.2 1997/12/12 21:41:30 curt + * More light/material property tweaking ... still a ways off. + * + * Revision 1.1 1997/10/25 03:16:11 curt + * Initial revision of code contributed by Durk Talsma. + * + */ diff --git a/Astro/sky.c b/Astro/sky.c new file mode 100644 index 000000000..4de37c736 --- /dev/null +++ b/Astro/sky.c @@ -0,0 +1,370 @@ +/************************************************************************** + * sky.c -- model sky with an upside down "bowl" + * + * Written by Curtis Olson, started December 1997. + * + * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com + * + * 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) + **************************************************************************/ + + +#ifdef WIN32 +# include +#endif + +#include +/* +#include +#include +#include +*/ + +#include +#include "../XGL/xgl.h" + +#include "sky.h" + +#include "../Time/event.h" +#include "../Time/fg_time.h" + +#include "../Aircraft/aircraft.h" +#include "../Flight/flight.h" +#include "../Include/constants.h" +#include "../Main/views.h" +#include "../Math/fg_random.h" +/* +#include "../Include/general.h" +*/ + +/* in meters of course */ +#define CENTER_ELEV 25000.0 +#define INNER_RADIUS 50000.0 +#define INNER_ELEV 20000.0 +#define MIDDLE_RADIUS 70000.0 +#define MIDDLE_ELEV 8000.0 +#define OUTER_RADIUS 80000.0 +#define OUTER_ELEV 0.0 + + +static float inner_vertex[12][3]; +static float middle_vertex[12][3]; +static float outer_vertex[12][3]; + +static float inner_color[12][4]; +static float middle_color[12][4]; +static float outer_color[12][4]; + + +/* Calculate the sky structure vertices */ +void fgSkyVerticesInit() { + float theta; + int i; + + printf(" Generating the sky dome vertices.\n"); + + for ( i = 0; i < 12; i++ ) { + theta = (i * 30.0) * DEG_TO_RAD; + + inner_vertex[i][0] = cos(theta) * INNER_RADIUS; + inner_vertex[i][1] = sin(theta) * INNER_RADIUS; + inner_vertex[i][2] = INNER_ELEV; + + /* printf(" %.2f %.2f\n", cos(theta) * INNER_RADIUS, + sin(theta) * INNER_RADIUS); */ + + middle_vertex[i][0] = cos((double)theta) * MIDDLE_RADIUS; + middle_vertex[i][1] = sin((double)theta) * MIDDLE_RADIUS; + middle_vertex[i][2] = MIDDLE_ELEV; + + outer_vertex[i][0] = cos((double)theta) * OUTER_RADIUS; + outer_vertex[i][1] = sin((double)theta) * OUTER_RADIUS; + outer_vertex[i][2] = OUTER_ELEV; + + } +} + + +/* (Re)calculate the sky colors at each vertex */ +void fgSkyColorsInit() { + struct fgLIGHT *l; + float sun_angle, diff; + float outer_param[3], outer_amt[3], outer_diff[3]; + float middle_param[3], middle_amt[3], middle_diff[3]; + int i, j; + + l = &cur_light_params; + + printf(" Generating the sky colors for each vertex.\n"); + + /* setup for the possibility of sunset effects */ + sun_angle = l->sun_angle * RAD_TO_DEG; + printf(" Sun angle in degrees = %.2f\n", sun_angle); + + if ( (sun_angle > 80.0) && (sun_angle < 100.0) ) { + /* 0.0 - 0.4 */ + outer_param[0] = (10.0 - fabs(90.0 - sun_angle)) / 25.0; + outer_param[1] = (10.0 - fabs(90.0 - sun_angle)) / 45.0; + outer_param[2] = 0.0; + + middle_param[0] = (10.0 - fabs(90.0 - sun_angle)) / 40.0; + middle_param[1] = (10.0 - fabs(90.0 - sun_angle)) / 60.0; + middle_param[2] = 0.0; + + outer_diff[0] = outer_param[0] / 6.0; + outer_diff[1] = outer_param[1] / 6.0; + outer_diff[2] = outer_param[2] / 6.0; + + middle_diff[0] = middle_param[0] / 6.0; + middle_diff[1] = middle_param[1] / 6.0; + middle_diff[2] = middle_param[2] / 6.0; + } else { + outer_param[0] = outer_param[1] = outer_param[2] = 0.0; + middle_param[0] = middle_param[1] = middle_param[2] = 0.0; + + outer_diff[0] = outer_diff[1] = outer_diff[2] = 0.0; + middle_diff[0] = middle_diff[1] = middle_diff[2] = 0.0; + } + /* printf(" outer_red_param = %.2f outer_red_diff = %.2f\n", + outer_red_param, outer_red_diff); */ + + /* calculate transition colors between sky and fog */ + for ( j = 0; j < 3; j++ ) { + outer_amt[j] = outer_param[j]; + middle_amt[j] = middle_param[j]; + } + + for ( i = 0; i < 6; i++ ) { + for ( j = 0; j < 3; j++ ) { + diff = l->sky_color[j] - l->fog_color[j]; + + inner_color[i][j] = l->sky_color[j] - diff * 0.3; + middle_color[i][j] = l->sky_color[j] - diff * 0.9 + middle_amt[j]; + outer_color[i][j] = l->fog_color[j] + outer_amt[j]; + + if ( middle_color[i][j] > 1.00 ) { middle_color[i][j] = 1.00; } + if ( middle_color[i][j] < 0.10 ) { middle_color[i][j] = 0.10; } + if ( outer_color[i][j] > 1.00 ) { outer_color[i][j] = 1.00; } + if ( outer_color[i][j] < 0.10 ) { outer_color[i][j] = 0.10; } + } + inner_color[i][3] = middle_color[i][3] = outer_color[i][3] = + l->sky_color[3]; + + for ( j = 0; j < 3; j++ ) { + outer_amt[j] -= outer_diff[j]; + middle_amt[j] -= middle_diff[j]; + } + + /* + printf("inner_color[%d] = %.2f %.2f %.2f %.2f\n", i, inner_color[i][0], + inner_color[i][1], inner_color[i][2], inner_color[i][3]); + printf("middle_color[%d] = %.2f %.2f %.2f %.2f\n", i, + middle_color[i][0], middle_color[i][1], middle_color[i][2], + middle_color[i][3]); + printf("outer_color[%d] = %.2f %.2f %.2f %.2f\n", i, + outer_color[i][0], outer_color[i][1], outer_color[i][2], + outer_color[i][3]); + */ + } + + for ( j = 0; j < 3; j++ ) { + outer_amt[j] = 0.0; + middle_amt[j] = 0.0; + } + + for ( i = 6; i < 12; i++ ) { + + for ( j = 0; j < 3; j++ ) { + diff = l->sky_color[j] - l->fog_color[j]; + + inner_color[i][j] = l->sky_color[j] - diff * 0.3; + middle_color[i][j] = l->sky_color[j] - diff * 0.9 + middle_amt[j]; + outer_color[i][j] = l->fog_color[j] + outer_amt[j]; + + if ( middle_color[i][j] > 1.00 ) { middle_color[i][j] = 1.00; } + if ( middle_color[i][j] < 0.10 ) { middle_color[i][j] = 0.10; } + if ( outer_color[i][j] > 1.00 ) { outer_color[i][j] = 1.00; } + if ( outer_color[i][j] < 0.15 ) { outer_color[i][j] = 0.15; } + } + inner_color[i][3] = middle_color[i][3] = outer_color[i][3] = + l->sky_color[3]; + + for ( j = 0; j < 3; j++ ) { + outer_amt[j] += outer_diff[j]; + middle_amt[j] += middle_diff[j]; + } + + /* + printf("inner_color[%d] = %.2f %.2f %.2f %.2f\n", i, inner_color[i][0], + inner_color[i][1], inner_color[i][2], inner_color[i][3]); + printf("middle_color[%d] = %.2f %.2f %.2f %.2f\n", i, + middle_color[i][0], middle_color[i][1], middle_color[i][2], + middle_color[i][3]); + printf("outer_color[%d] = %.2f %.2f %.2f %.2f\n", i, + outer_color[i][0], outer_color[i][1], outer_color[i][2], + outer_color[i][3]); + */ + } +} + + +/* Initialize the sky structure and colors */ +void fgSkyInit() { + printf("Initializing the sky\n"); + + fgSkyVerticesInit(); + + /* regester fgSkyColorsInit() as an event to be run periodically */ + fgEventRegister("fgSkyColorsInit()", fgSkyColorsInit, + FG_EVENT_READY, 30000); +} + + +/* Draw the Sky */ +void fgSkyRender() { + struct fgFLIGHT *f; + struct fgLIGHT *l; + struct fgVIEW *v; + float /* inner_color[4], middle_color[4], diff, */ east_dot, dot, angle; + int i; + + f = ¤t_aircraft.flight; + l = &cur_light_params; + v = ¤t_view; + + /* printf("Rendering the sky.\n"); */ + + xglPushMatrix(); + + /* calculate the angle between v->surface_to_sun and + * v->surface_east. We do this so we can sort out the acos() + * ambiguity. I wish I could think of a more efficient way ... :-( */ + east_dot = MAT3_DOT_PRODUCT(v->surface_to_sun, v->surface_east); + /* printf(" East dot product = %.2f\n", east_dot); */ + + /* calculate the angle between v->surface_to_sun and + * v->surface_south. this is how much we have to rotate the sky + * for it to align with the sun */ + dot = MAT3_DOT_PRODUCT(v->surface_to_sun, v->surface_south); + /* printf(" Dot product = %.2f\n", dot); */ + if ( east_dot >= 0 ) { + angle = acos(dot); + } else { + angle = -acos(dot); + } + /*printf(" Sky needs to rotate = %.3f rads = %.1f degrees.\n", + angle, angle * RAD_TO_DEG); */ + + /* Translate to view position */ + xglTranslatef( v->cur_zero_elev.x, v->cur_zero_elev.y, v->cur_zero_elev.z ); + /* printf(" Translated to %.2f %.2f %.2f\n", + v->cur_zero_elev.x, v->cur_zero_elev.y, v->cur_zero_elev.z ); */ + + /* Rotate to proper orientation */ + /* printf(" lon = %.2f lat = %.2f\n", FG_Longitude * RAD_TO_DEG, + FG_Latitude * RAD_TO_DEG); */ + xglRotatef( FG_Longitude * RAD_TO_DEG, 0.0, 0.0, 1.0 ); + xglRotatef( 90.0 - FG_Latitude * RAD_TO_DEG, 0.0, 1.0, 0.0 ); + xglRotatef( angle * RAD_TO_DEG, 0.0, 0.0, 1.0 ); + + /* Draw inner/center section of sky*/ + xglBegin( GL_TRIANGLE_FAN ); + xglColor4fv(l->sky_color); + xglVertex3f(0.0, 0.0, CENTER_ELEV); + for ( i = 0; i < 12; i++ ) { + xglColor4fv( inner_color[i] ); + xglVertex3fv( inner_vertex[i] ); + } + xglColor4fv( inner_color[0] ); + xglVertex3fv( inner_vertex[0] ); + xglEnd(); + + /* Draw the middle ring */ + xglBegin( GL_TRIANGLE_STRIP ); + for ( i = 0; i < 12; i++ ) { + xglColor4fv( middle_color[i] ); + xglVertex3fv( middle_vertex[i] ); + xglColor4fv( inner_color[i] ); + xglVertex3fv( inner_vertex[i] ); + } + xglColor4fv( middle_color[0] ); + /* xglColor4f(1.0, 0.0, 0.0, 1.0); */ + xglVertex3fv( middle_vertex[0] ); + xglColor4fv( inner_color[0] ); + /* xglColor4f(1.0, 0.0, 0.0, 1.0); */ + xglVertex3fv( inner_vertex[0] ); + xglEnd(); + + /* Draw the outer ring */ + xglBegin( GL_TRIANGLE_STRIP ); + for ( i = 0; i < 12; i++ ) { + xglColor4fv( outer_color[i] ); + xglVertex3fv( outer_vertex[i] ); + xglColor4fv( middle_color[i] ); + xglVertex3fv( middle_vertex[i] ); + } + xglColor4fv( outer_color[0] ); + xglVertex3fv( outer_vertex[0] ); + xglColor4fv( middle_color[0] ); + xglVertex3fv( middle_vertex[0] ); + xglEnd(); + + xglPopMatrix(); +} + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:19 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.11 1997/12/30 22:22:38 curt + * Further integration of event manager. + * + * Revision 1.10 1997/12/30 20:47:53 curt + * Integrated new event manager with subsystem initializations. + * + * Revision 1.9 1997/12/30 13:06:57 curt + * A couple lighting tweaks ... + * + * Revision 1.8 1997/12/23 04:58:38 curt + * Tweaked the sky coloring a bit to build in structures to allow finer rgb + * control. + * + * Revision 1.7 1997/12/22 23:45:48 curt + * First stab at sunset/sunrise sky glow effects. + * + * Revision 1.6 1997/12/22 04:14:34 curt + * Aligned sky with sun so dusk/dawn effects can be correct relative to the sun. + * + * Revision 1.5 1997/12/19 23:34:59 curt + * Lot's of tweaking with sky rendering and lighting. + * + * Revision 1.4 1997/12/19 16:45:02 curt + * Working on scene rendering order and options. + * + * Revision 1.3 1997/12/18 23:32:36 curt + * First stab at sky dome actually starting to look reasonable. :-) + * + * Revision 1.2 1997/12/18 04:07:03 curt + * Worked on properly translating and positioning the sky dome. + * + * Revision 1.1 1997/12/17 23:14:30 curt + * Initial revision. + * Begin work on rendering the sky. (Rather than just using a clear screen.) + * + */ diff --git a/Astro/sky.h b/Astro/sky.h new file mode 100644 index 000000000..7ba4863e3 --- /dev/null +++ b/Astro/sky.h @@ -0,0 +1,48 @@ +/************************************************************************** + * sky.h -- model sky with an upside down "bowl" + * + * Written by Curtis Olson, started December 1997. + * + * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com + * + * 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) + **************************************************************************/ + + +/* (Re)generate the display list */ +void fgSkyInit(); + +/* (Re)calculate the sky colors at each vertex */ +void fgSkyColorsInit(); + +/* Draw the Sky */ +void fgSkyRender(); + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:19 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.2 1997/12/22 23:45:49 curt + * First stab at sunset/sunrise sky glow effects. + * + * Revision 1.1 1997/12/17 23:14:31 curt + * Initial revision. + * Begin work on rendering the sky. (Rather than just using a clear screen.) + * + */ diff --git a/Astro/stars.c b/Astro/stars.c new file mode 100644 index 000000000..6d3d4a53e --- /dev/null +++ b/Astro/stars.c @@ -0,0 +1,357 @@ +/************************************************************************** + * stars.c -- data structures and routines for managing and rendering stars. + * + * Written by Curtis Olson, started August 1997. + * + * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com + * + * 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) + **************************************************************************/ + + +#ifdef WIN32 +# include +#endif + +#include +#include +#include +#include + +#include +#include "../XGL/xgl.h" + +#include "orbits.h" +#include "planets.h" +#include "stars.h" + +#include "../Include/constants.h" +#include "../Include/general.h" +#include "../Aircraft/aircraft.h" +#include "../Main/views.h" +#include "../Time/fg_time.h" + + +#define EpochStart (631065600) +#define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600))) + + +/* Define four structures, each with varying amounts of stars */ +/* static */ GLint stars[FG_STAR_LEVELS]; + + +/* Initialize the Star Management Subsystem */ +void fgStarsInit() { + FILE *fd; + struct fgGENERAL *g; + struct CelestialCoord pltPos; + char path[1024]; + char line[256], name[256]; + char *front, *end; + double right_ascension, declination, magnitude; + double ra_save, decl_save; + double ra_save1, decl_save1; + int count, i, j, max_stars; + + printf("Initializing stars\n"); + + g = &general; + + /* build the full path name to the stars data base file */ + path[0] = '\0'; + strcat(path, g->root_dir); + strcat(path, "/Scenery/"); + strcat(path, "Stars.dat"); + + max_stars = FG_MAX_STARS; + + for ( i = 0; i < FG_STAR_LEVELS; i++ ) { + printf(" Loading %d Stars: %s\n", max_stars, path); + + if ( (fd = fopen(path, "r")) == NULL ) { + printf("Cannot open star file: '%s'\n", path); + return; + } + + stars[i] = xglGenLists(1); + xglNewList( stars[i], GL_COMPILE ); + xglBegin( GL_POINTS ); + + /* read in each line of the file */ + count = 0; + while ( (fgets(line, 256, fd) != NULL) && (count < max_stars) ) { + front = line; + + /* printf(" Read line = %s", front); */ + + /* advance to first non-whitespace character */ + while ( (front[0] == ' ') || (front[0] == '\t') ) { + front++; + } + + /* printf(" Line length (after trimming) = %d\n", strlen(front));*/ + + if ( front[0] == '#' ) { + /* comment */ + } else if ( strlen(front) <= 1 ) { + /* blank line */ + } else { + /* star data line */ + + /* get name */ + end = front; + while ( end[0] != ',' ) { + end++; + } + end[0] = '\0'; + strcpy(name, front); + front = end; + front++; + + sscanf(front, "%lf,%lf,%lf\n", + &right_ascension, &declination, &magnitude); + + /* + if ( strcmp(name, "Betelgeuse") == 0 ) { + printf(" *** Marking %s\n", name); + ra_save = right_ascension; + decl_save = declination; + } + */ + + /* + if ( strcmp(name, "Alnilam") == 0 ) { + printf(" *** Marking %s\n", name); + ra_save1 = right_ascension; + decl_save1 = declination; + } + */ + + /* scale magnitudes to (0.0 - 1.0) */ + magnitude = (0.0 - magnitude) / 5.0 + 1.0; + + /* scale magnitudes again so they look ok */ + if ( magnitude > 1.0 ) { magnitude = 1.0; } + if ( magnitude < 0.0 ) { magnitude = 0.0; } + magnitude = + magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.1); + /* printf(" Found star: %d %s, %.3f %.3f %.3f\n", count, + name, right_ascension, declination, magnitude); */ + + xglColor3f( magnitude, magnitude, magnitude ); + /*xglColor3f(0,0,0);*/ + xglVertex3f( 50000.0 * cos(right_ascension) * cos(declination), + 50000.0 * sin(right_ascension) * cos(declination), + 50000.0 * sin(declination) ); + + count++; + } /* if valid line */ + + } /* while */ + + fclose(fd); + + /* Add the planets to all four display lists */ + for ( j = 2; j < 9; j++ ) { + pltPos = fgCalculatePlanet(pltOrbElements[j], + pltOrbElements[0], cur_time_params, j); + /* give the planets a temporary color, for testing purposes */ + /* xglColor3f( 1.0, 0.0, 0.0); */ + + /* scale magnitudes to (0.0 - 1.0) */ + magnitude = (0.0 - pltPos.magnitude) / 5.0 + 1.0; + + /* scale magnitudes again so they look ok */ + if ( magnitude > 1.0 ) { magnitude = 1.0; } + if ( magnitude < 0.0 ) { magnitude = 0.0; } + magnitude = + magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.1); + + + xglColor3f(magnitude, magnitude, magnitude); + + xglVertex3f( 50000.0 * cos(pltPos.RightAscension) * + cos(pltPos.Declination), + 50000.0 * sin(pltPos.RightAscension) * + cos(pltPos.Declination), + 50000.0 * sin(pltPos.Declination) ); + } + xglEnd(); + + /* + xglBegin(GL_LINE_LOOP); + xglColor3f(1.0, 0.0, 0.0); + xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save-0.2), + 50000.0 * sin(ra_save-0.2) * cos(decl_save-0.2), + 50000.0 * sin(decl_save-0.2) ); + xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save-0.2), + 50000.0 * sin(ra_save+0.2) * cos(decl_save-0.2), + 50000.0 * sin(decl_save-0.2) ); + xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save+0.2), + 50000.0 * sin(ra_save+0.2) * cos(decl_save+0.2), + 50000.0 * sin(decl_save+0.2) ); + xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save+0.2), + 50000.0 * sin(ra_save-0.2) * cos(decl_save+0.2), + 50000.0 * sin(decl_save+0.2) ); + xglEnd(); + */ + + /* + xglBegin(GL_LINE_LOOP); + xglColor3f(0.0, 1.0, 0.0); + xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2), + 50000.0 * sin(ra_save1-0.2) * cos(decl_save1-0.2), + 50000.0 * sin(decl_save1-0.2) ); + xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2), + 50000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2), + 50000.0 * sin(decl_save1-0.2) ); + xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1+0.2), + 50000.0 * sin(ra_save1+0.2) * cos(decl_save1+0.2), + 50000.0 * sin(decl_save1+0.2) ); + xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1+0.2), + 50000.0 * sin(ra_save1-0.2) * cos(decl_save1+0.2), + 50000.0 * sin(decl_save1+0.2) ); + xglEnd(); + */ + + xglEndList(); + + max_stars /= 2; + } +} + + +/* Draw the Stars */ +void fgStarsRender() { + struct fgFLIGHT *f; + struct fgVIEW *v; + struct fgLIGHT *l; + struct fgTIME *t; + int i; + + f = ¤t_aircraft.flight; + l = &cur_light_params; + t = &cur_time_params; + v = ¤t_view; + + /* FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise */ + + /* t->sun_angle = 3.0; */ /* to force stars to be drawn (for testing) */ + + /* render the stars */ + if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) { + /* determine which star structure to draw */ + if ( l->sun_angle > (FG_PI_2 + 7.25 * DEG_TO_RAD ) ) { + i = 0; + } else if ( l->sun_angle > (FG_PI_2 + 6.50 * DEG_TO_RAD ) ) { + i = 1; + } else if ( l->sun_angle > (FG_PI_2 + 5.75 * DEG_TO_RAD ) ) { + i = 2; + } else { + i = 3; + } + + /* printf("RENDERING STARS = %d (night)\n", i); */ + + xglCallList(stars[i]); + } else { + /* printf("not RENDERING STARS (day)\n"); */ + } +} + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:20 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.24 1997/12/30 22:22:39 curt + * Further integration of event manager. + * + * Revision 1.23 1997/12/30 20:47:53 curt + * Integrated new event manager with subsystem initializations. + * + * Revision 1.22 1997/12/30 16:36:53 curt + * Merged in Durk's changes ... + * + * Revision 1.21 1997/12/19 23:35:00 curt + * Lot's of tweaking with sky rendering and lighting. + * + * Revision 1.20 1997/12/15 23:55:03 curt + * Add xgl wrappers for debugging. + * Generate terrain normals on the fly. + * + * Revision 1.19 1997/12/12 19:53:00 curt + * Working on lightling and material properties. + * + * Revision 1.18 1997/12/10 22:37:52 curt + * Prepended "fg" on the name of all global structures that didn't have it yet. + * i.e. "struct WEATHER {}" became "struct fgWEATHER {}" + * + * Revision 1.17 1997/12/09 04:25:33 curt + * Working on adding a global lighting params structure. + * + * Revision 1.16 1997/11/25 19:25:38 curt + * Changes to integrate Durk's moon/sun code updates + clean up. + * + * Revision 1.15 1997/10/30 12:38:45 curt + * Working on new scenery subsystem. + * + * Revision 1.14 1997/10/28 21:00:22 curt + * Changing to new terrain format. + * + * Revision 1.13 1997/10/25 03:18:28 curt + * Incorporated sun, moon, and planet position and rendering code contributed + * by Durk Talsma. + * + * Revision 1.12 1997/09/23 00:29:43 curt + * Tweaks to get things to compile with gcc-win32. + * + * Revision 1.11 1997/09/22 14:44:21 curt + * Continuing to try to align stars correctly. + * + * Revision 1.10 1997/09/20 03:34:32 curt + * Still trying to get those durned stars aligned properly. + * + * Revision 1.9 1997/09/18 16:20:09 curt + * At dusk/dawn add/remove stars in stages. + * + * Revision 1.8 1997/09/16 22:14:52 curt + * Tweaked time of day lighting equations. Don't draw stars during the day. + * + * Revision 1.7 1997/09/16 15:50:31 curt + * Working on star alignment and time issues. + * + * Revision 1.6 1997/09/05 14:17:31 curt + * More tweaking with stars. + * + * Revision 1.5 1997/09/05 01:35:59 curt + * Working on getting stars right. + * + * Revision 1.4 1997/09/04 02:17:38 curt + * Shufflin' stuff. + * + * Revision 1.3 1997/08/29 17:55:28 curt + * Worked on properly aligning the stars. + * + * Revision 1.2 1997/08/27 21:32:30 curt + * Restructured view calculation code. Added stars. + * + * Revision 1.1 1997/08/27 03:34:48 curt + * Initial revision. + * + */ diff --git a/Astro/stars.h b/Astro/stars.h new file mode 100644 index 000000000..19524e120 --- /dev/null +++ b/Astro/stars.h @@ -0,0 +1,69 @@ +/************************************************************************** + * stars.h -- data structures and routines for managing and rendering stars. + * + * Written by Curtis Olson, started August 1997. + * + * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com + * + * 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) + **************************************************************************/ + + +#ifndef STARS_H +#define STARS_H + + +#define FG_MAX_STARS 500 +#define FG_STAR_LEVELS 4 /* how many star transitions */ +#define FG_MIN_STAR_MAG 0.738750 /* magnitude of weakest star we'll display */ + +/* Initialize the Star Management Subsystem */ +void fgStarsInit(); + +/* Draw the Stars */ +void fgStarsRender(); +extern struct OrbElements pltOrbElements[9]; +extern struct fgTIME cur_time_params; + +#endif /* STARS_H */ + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:20 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.6 1997/10/25 03:18:29 curt + * Incorporated sun, moon, and planet position and rendering code contributed + * by Durk Talsma. + * + * Revision 1.5 1997/09/18 16:20:09 curt + * At dusk/dawn add/remove stars in stages. + * + * Revision 1.4 1997/09/05 01:36:00 curt + * Working on getting stars right. + * + * Revision 1.3 1997/08/29 17:55:28 curt + * Worked on properly aligning the stars. + * + * Revision 1.2 1997/08/27 21:32:30 curt + * Restructured view calculation code. Added stars. + * + * Revision 1.1 1997/08/27 03:34:50 curt + * Initial revision. + * + */ diff --git a/Astro/sun.c b/Astro/sun.c new file mode 100644 index 000000000..43e2c353e --- /dev/null +++ b/Astro/sun.c @@ -0,0 +1,240 @@ +/************************************************************************** + * 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) + **************************************************************************/ + +#include +#include "../XGL/xgl.h" + +#include "../Time/fg_time.h" +#include "../Main/views.h" +#include "orbits.h" +#include "sun.h" + +GLint sun_obj; + +static struct CelestialCoord sunPos; + +float xSun, ySun, zSun; + +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) */ + xv = cos(EccAnom) - params.e; + yv = sqrt(1.0 - params.e*params.e) * sin(EccAnom); + v = atan2(yv, xv); + r = sqrt(xv*xv + yv*yv); + + /* calculate the the Sun's true longitude (lonsun) */ + lonSun = v + params.w; + + /* convert true longitude and distance to ecliptic rectangular geocentric + coordinates (xs, ys) */ + solarPosition.xs = r * cos(lonSun); + solarPosition.ys = r * sin(lonSun); + solarPosition.dist = r; + return solarPosition; +} + + +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 */ +void fgSunInit() { + static int dl_exists = 0; + + printf(" Initializing the Sun\n"); + + fgSolarSystemUpdate(&(pltOrbElements[0]), cur_time_params); + sunPos = fgCalculateSun(pltOrbElements[0], cur_time_params); +#ifdef DEBUG + printf("Sun found at %f (ra), %f (dec)\n", sunPos.RightAscension, + sunPos.Declination); +#endif + + xSun = 60000.0 * cos(sunPos.RightAscension) * cos(sunPos.Declination); + ySun = 60000.0 * sin(sunPos.RightAscension) * cos(sunPos.Declination); + zSun = 60000.0 * sin(sunPos.Declination); + + 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(); + } +} + + +/* Draw the Sun */ +void fgSunRender() { + struct fgVIEW *v; + struct fgTIME *t; + 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]; + + + t = &cur_time_params; + v = ¤t_view; + l = &cur_light_params; + + x_2 = l->sun_angle * l->sun_angle; + x_4 = x_2 * x_2; + x_8 = x_4 * x_4; + x_10 = x_8 * x_2; + + ambient = (0.4 * pow(1.1, -x_10 / 30.0)); + if ( ambient < 0.3 ) ambient = 0.3; + if ( ambient > 1.0 ) ambient = 1.0; + + 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 + printf("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); +#endif + diff[0] = 0.0; + diff[1] = 0.0; + diff[2] = 0.0; + diff[3] = 1.0; + + /* set lighting parameters */ + xglLightfv(GL_LIGHT0, GL_AMBIENT, color ); + xglLightfv(GL_LIGHT0, GL_DIFFUSE, color ); + xglMaterialfv(GL_FRONT, GL_AMBIENT, amb); + xglMaterialfv(GL_FRONT, GL_DIFFUSE, diff); + xglMaterialfv(GL_FRONT, GL_SHININESS, diff); + xglMaterialfv(GL_FRONT, GL_EMISSION, diff); + xglMaterialfv(GL_FRONT, GL_SPECULAR, diff); + + /* xglDisable( GL_LIGHTING ); */ + + xglPushMatrix(); + xglTranslatef(xSun, ySun, zSun); + xglScalef(1400, 1400, 1400); + + xglColor3f(0.85, 0.65, 0.05); + + xglCallList(sun_obj); + + xglPopMatrix(); + + /* xglEnable( GL_LIGHTING ); */ +} + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:20 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.12 1998/01/05 18:44:36 curt + * Add an option to advance/decrease time from keyboard. + * + * 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. + * + * Revision 1.10 1997/12/30 20:47:54 curt + * Integrated new event manager with subsystem initializations. + * + * Revision 1.9 1997/12/30 16:36:54 curt + * Merged in Durk's changes ... + * + * Revision 1.8 1997/12/19 23:35:00 curt + * Lot's of tweaking with sky rendering and lighting. + * + * Revision 1.7 1997/12/17 23:12:16 curt + * Fixed so moon and sun display lists aren't recreate periodically. + * + * Revision 1.6 1997/12/15 23:55:04 curt + * Add xgl wrappers for debugging. + * Generate terrain normals on the fly. + * + * Revision 1.5 1997/12/12 21:41:31 curt + * More light/material property tweaking ... still a ways off. + * + * 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 {}" + * + * Revision 1.3 1997/12/09 05:11:56 curt + * Working on tweaking lighting. + * + * Revision 1.2 1997/11/25 19:25:39 curt + * Changes to integrate Durk's moon/sun code updates + clean up. + * + * Revision 1.1 1997/10/25 03:16:11 curt + * Initial revision of code contributed by Durk Talsma. + * + */ + + + + + diff --git a/Astro/sun.h b/Astro/sun.h new file mode 100644 index 000000000..260101f76 --- /dev/null +++ b/Astro/sun.h @@ -0,0 +1,57 @@ +/************************************************************************** + * sun.h + * + * 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) + **************************************************************************/ + + +#ifndef SUN_H +#define SUN_H + + +struct SunPos fgCalcSunPos(struct OrbElements sunParams); +extern struct OrbElements pltOrbElements[9]; + +/* Initialize the Sun */ +void fgSunInit(); + +/* Draw the Sun */ +void fgSunRender(); + + +#endif /* SUN_H */ + + +/* $Log$ +/* Revision 1.1 1998/01/07 03:16:21 curt +/* Moved from .../Src/Scenery/ to .../Src/Astro/ +/* + * Revision 1.3 1997/12/11 04:43:56 curt + * Fixed sun vector and lighting problems. I thing the moon is now lit + * correctly. + * + * Revision 1.2 1997/11/25 19:25:39 curt + * Changes to integrate Durk's moon/sun code updates + clean up. + * + * Revision 1.1 1997/10/25 03:16:12 curt + * Initial revision of code contributed by Durk Talsma. + * + */ -- 2.39.2