]> git.mxchange.org Git - flightgear.git/blob - Scenery/stars.c
Working on getting stars right.
[flightgear.git] / Scenery / stars.c
1 /**************************************************************************
2  * stars.c -- data structures and routines for managing and rendering stars.
3  *
4  * Written by Curtis Olson, started August 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
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  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #ifdef WIN32
28 #  include <windows.h>
29 #endif
30
31 #include <math.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35
36 #include <GL/glut.h>
37
38 #include "stars.h"
39
40 #include "../constants.h"
41 #include "../general.h"
42
43 #include "../GLUT/views.h"
44 #include "../Aircraft/aircraft.h"
45
46
47 #define EpochStart           (631065600)
48 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
49
50
51 static GLint stars;
52
53
54 /* Initialize the Star Management Subsystem */
55 void fgStarsInit() {
56     FILE *fd;
57     struct GENERAL *g;
58     char path[1024];
59     char line[256], name[256];
60     char *front, *end;
61     double right_ascension, declination, magnitude;
62     double ra_save, decl_save;
63     double ra_save1, decl_save1;
64     double ra_save2, decl_save2;
65     GLfloat mag[4] = {0.0, 0.0, 0.0, 1.0};
66     int count;
67
68     g = &general;
69
70     /* build the full path name to the stars data base file */
71     path[0] = '\0';
72     strcat(path, g->root_dir);
73     strcat(path, "/Scenery/");
74     strcat(path, "Stars.dat");
75
76     printf("Loading Stars: %s\n", path);
77
78     if ( (fd = fopen(path, "r")) == NULL ) {
79         printf("Cannot open star file: '%s'\n", path);
80         return;
81     }
82
83     stars = glGenLists(1);
84     glNewList( stars, GL_COMPILE );
85     glBegin( GL_POINTS );
86
87     /* read in each line of the file */
88     count = 0;
89     while ( (fgets(line, 256, fd) != NULL) && (count < FG_MAX_STARS) ) {
90         front = line;
91
92         /* printf("Read line = %s", front); */
93
94         /* advance to first non-whitespace character */
95         while ( (front[0] == ' ') || (front[0] == '\t') ) {
96             front++;
97         }
98
99         /* printf("Line length (after trimming) = %d\n", strlen(front)); */
100
101         if ( front[0] == '#' ) {
102             /* comment */
103         } else if ( strlen(front) <= 1 ) {
104             /* blank line */
105         } else {
106             /* star data line */
107
108             /* get name */
109             end = front;
110             while ( end[0] != ',' ) {
111                 end++;
112             }
113             end[0] = '\0';
114             strcpy(name, front);
115             front = end;
116             front++;
117
118             sscanf(front, "%lf,%lf,%lf\n", 
119                    &right_ascension, &declination, &magnitude);
120
121             if ( strcmp(name, "Deneb") == 0 ) {
122                 printf("\n*** Marking %s\n\n", name);
123                 ra_save = right_ascension;
124                 decl_save = declination;
125             }
126
127             if ( strcmp(name, "Alderamin") == 0 ) {
128                 printf("\n*** Marking %s\n\n", name);
129                 ra_save1 = right_ascension;
130                 decl_save1 = declination;
131             }
132
133             /* scale magnitudes to (0.0 - 1.0) */
134             magnitude = (-1.46 - magnitude) / 10.0 + 1.0;
135
136             /* scale magnitudes again so they look ok */
137             magnitude = magnitude * 0.8 + 0.2;
138             mag[0] = mag[1] = mag[2] = magnitude;
139
140             printf("Found star: %d %s, %.3f %.3f %.3f\n", count,
141                name, right_ascension, declination, magnitude);
142
143             glColor3f( mag[0], mag[1], mag[2] );
144             glVertex3f( 190000.0 * sin(right_ascension) * cos(declination),
145                         190000.0 * cos(right_ascension) * cos(declination),
146                         190000.0 * sin(declination) );
147
148             count++;
149         } /* if valid line */
150
151     } /* while */
152
153     fclose(fd);
154
155     glEnd();
156
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) );
171     glEnd();
172
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) );
187     glEnd();
188        
189     glEndList();
190 }
191
192
193 /* Draw the Stars */
194 void fgStarsRender() {
195     struct FLIGHT *f;
196     struct VIEW *v;
197     double angle;
198
199     f = &current_aircraft.flight;
200     v = &current_view;
201
202     printf("RENDERING STARS\n");
203
204     glDisable( GL_FOG );
205     glDisable( GL_LIGHTING );
206     glPushMatrix();
207
208     glTranslatef( v->view_pos.x, v->view_pos.y, v->view_pos.z );
209
210     angle = FG_2PI * fmod(DaysSinceEpoch(time(NULL)), 1.0);
211     glRotatef( -angle * RAD_TO_DEG, 0.0, 0.0, 1.0 );
212     printf("Rotating stars by %.2f\n", -angle * RAD_TO_DEG);
213
214     glCallList(stars);
215
216     glPopMatrix();
217     glEnable( GL_LIGHTING );
218     glEnable( GL_FOG );
219 }
220
221
222 /* $Log$
223 /* Revision 1.5  1997/09/05 01:35:59  curt
224 /* Working on getting stars right.
225 /*
226  * Revision 1.4  1997/09/04 02:17:38  curt
227  * Shufflin' stuff.
228  *
229  * Revision 1.3  1997/08/29 17:55:28  curt
230  * Worked on properly aligning the stars.
231  *
232  * Revision 1.2  1997/08/27 21:32:30  curt
233  * Restructured view calculation code.  Added stars.
234  *
235  * Revision 1.1  1997/08/27 03:34:48  curt
236  * Initial revision.
237  *
238  */