1 /**************************************************************************
2 * stars.c -- data structures and routines for managing and rendering stars.
4 * Written by Curtis Olson, started August 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
40 #include "../constants.h"
41 #include "../general.h"
43 #include "../GLUT/views.h"
44 #include "../Aircraft/aircraft.h"
47 #define EpochStart (631065600)
48 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
54 /* Initialize the Star Management Subsystem */
59 char line[256], name[256];
61 double right_ascension, declination, magnitude;
62 double ra_save, decl_save;
63 double ra_save1, decl_save1;
64 double ra_save2, decl_save2;
69 /* build the full path name to the stars data base file */
71 strcat(path, g->root_dir);
72 strcat(path, "/Scenery/");
73 strcat(path, "Stars.dat");
75 printf("Loading Stars: %s\n", path);
77 if ( (fd = fopen(path, "r")) == NULL ) {
78 printf("Cannot open star file: '%s'\n", path);
82 stars = glGenLists(1);
83 glNewList( stars, GL_COMPILE );
86 /* read in each line of the file */
88 while ( (fgets(line, 256, fd) != NULL) && (count < FG_MAX_STARS) ) {
91 /* printf("Read line = %s", front); */
93 /* advance to first non-whitespace character */
94 while ( (front[0] == ' ') || (front[0] == '\t') ) {
98 /* printf("Line length (after trimming) = %d\n", strlen(front)); */
100 if ( front[0] == '#' ) {
102 } else if ( strlen(front) <= 1 ) {
109 while ( end[0] != ',' ) {
117 sscanf(front, "%lf,%lf,%lf\n",
118 &right_ascension, &declination, &magnitude);
120 if ( strcmp(name, "Merak") == 0 ) {
121 printf("\n*** Marking %s\n\n", name);
122 ra_save = right_ascension;
123 decl_save = declination;
126 if ( strcmp(name, "Rastaban") == 0 ) {
127 printf("\n*** Marking %s\n\n", name);
128 ra_save1 = right_ascension;
129 decl_save1 = declination;
132 /* scale magnitudes to (0.0 - 1.0) */
133 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
135 /* scale magnitudes again so they look ok */
136 if ( magnitude > 1.0 ) { magnitude = 1.0; }
137 if ( magnitude < 0.0 ) { magnitude = 0.0; }
138 magnitude = magnitude * 0.8 + 0.2;
140 printf("Found star: %d %s, %.3f %.3f %.3f\n", count,
141 name, right_ascension, declination, magnitude);
143 glColor3f( magnitude, magnitude, magnitude );
144 glVertex3f( 190000.0 * sin(right_ascension) * cos(declination),
145 -190000.0 * cos(right_ascension) * cos(declination),
146 190000.0 * sin(declination) );
149 } /* if valid line */
157 glBegin(GL_LINE_LOOP);
158 glColor3f(1.0, 0.0, 0.0);
159 glVertex3f( 190000.0 * sin(ra_save-0.2) * cos(decl_save-0.2),
160 -190000.0 * cos(ra_save-0.2) * cos(decl_save-0.2),
161 190000.0 * sin(decl_save-0.2) );
162 glVertex3f( 190000.0 * sin(ra_save+0.2) * cos(decl_save-0.2),
163 -190000.0 * cos(ra_save+0.2) * cos(decl_save-0.2),
164 190000.0 * sin(decl_save-0.2) );
165 glVertex3f( 190000.0 * sin(ra_save+0.2) * cos(decl_save+0.2),
166 -190000.0 * cos(ra_save+0.2) * cos(decl_save+0.2),
167 190000.0 * sin(decl_save+0.2) );
168 glVertex3f( 190000.0 * sin(ra_save-0.2) * cos(decl_save+0.2),
169 -190000.0 * cos(ra_save-0.2) * cos(decl_save+0.2),
170 190000.0 * sin(decl_save+0.2) );
173 glBegin(GL_LINE_LOOP);
174 glColor3f(0.0, 1.0, 0.0);
175 glVertex3f( 190000.0 * sin(ra_save1-0.2) * cos(decl_save1-0.2),
176 -190000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2),
177 190000.0 * sin(decl_save1-0.2) );
178 glVertex3f( 190000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2),
179 -190000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2),
180 190000.0 * sin(decl_save1-0.2) );
181 glVertex3f( 190000.0 * sin(ra_save1+0.2) * cos(decl_save1+0.2),
182 -190000.0 * cos(ra_save1+0.2) * cos(decl_save1+0.2),
183 190000.0 * sin(decl_save1+0.2) );
184 glVertex3f( 190000.0 * sin(ra_save1-0.2) * cos(decl_save1+0.2),
185 -190000.0 * cos(ra_save1-0.2) * cos(decl_save1+0.2),
186 190000.0 * sin(decl_save1+0.2) );
194 void fgStarsRender() {
198 static double warp = 0;
200 f = ¤t_aircraft.flight;
203 printf("RENDERING STARS\n");
206 glDisable( GL_LIGHTING );
209 glTranslatef( v->view_pos.x, v->view_pos.y, v->view_pos.z );
211 angle = FG_2PI * fmod(DaysSinceEpoch(time(NULL)), 1.0);
212 warp += 0.5 * DEG_TO_RAD;
213 warp = 240.0 * DEG_TO_RAD;
214 glRotatef( -(angle+warp) * RAD_TO_DEG, 0.0, 0.0, 1.0 );
215 printf("Rotating stars by %.2f + %.2f\n", -angle * RAD_TO_DEG,
221 glEnable( GL_LIGHTING );
227 /* Revision 1.6 1997/09/05 14:17:31 curt
228 /* More tweaking with stars.
230 * Revision 1.5 1997/09/05 01:35:59 curt
231 * Working on getting stars right.
233 * Revision 1.4 1997/09/04 02:17:38 curt
236 * Revision 1.3 1997/08/29 17:55:28 curt
237 * Worked on properly aligning the stars.
239 * Revision 1.2 1997/08/27 21:32:30 curt
240 * Restructured view calculation code. Added stars.
242 * Revision 1.1 1997/08/27 03:34:48 curt