]> git.mxchange.org Git - flightgear.git/blob - Scenery/stars.c
More tweaking with stars.
[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     int count;
66
67     g = &general;
68
69     /* build the full path name to the stars data base file */
70     path[0] = '\0';
71     strcat(path, g->root_dir);
72     strcat(path, "/Scenery/");
73     strcat(path, "Stars.dat");
74
75     printf("Loading Stars: %s\n", path);
76
77     if ( (fd = fopen(path, "r")) == NULL ) {
78         printf("Cannot open star file: '%s'\n", path);
79         return;
80     }
81
82     stars = glGenLists(1);
83     glNewList( stars, GL_COMPILE );
84     glBegin( GL_POINTS );
85
86     /* read in each line of the file */
87     count = 0;
88     while ( (fgets(line, 256, fd) != NULL) && (count < FG_MAX_STARS) ) {
89         front = line;
90
91         /* printf("Read line = %s", front); */
92
93         /* advance to first non-whitespace character */
94         while ( (front[0] == ' ') || (front[0] == '\t') ) {
95             front++;
96         }
97
98         /* printf("Line length (after trimming) = %d\n", strlen(front)); */
99
100         if ( front[0] == '#' ) {
101             /* comment */
102         } else if ( strlen(front) <= 1 ) {
103             /* blank line */
104         } else {
105             /* star data line */
106
107             /* get name */
108             end = front;
109             while ( end[0] != ',' ) {
110                 end++;
111             }
112             end[0] = '\0';
113             strcpy(name, front);
114             front = end;
115             front++;
116
117             sscanf(front, "%lf,%lf,%lf\n", 
118                    &right_ascension, &declination, &magnitude);
119
120             if ( strcmp(name, "Merak") == 0 ) {
121                 printf("\n*** Marking %s\n\n", name);
122                 ra_save = right_ascension;
123                 decl_save = declination;
124             }
125
126             if ( strcmp(name, "Rastaban") == 0 ) {
127                 printf("\n*** Marking %s\n\n", name);
128                 ra_save1 = right_ascension;
129                 decl_save1 = declination;
130             }
131
132             /* scale magnitudes to (0.0 - 1.0) */
133             magnitude = (0.0 - magnitude) / 5.0 + 1.0;
134
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;
139
140             printf("Found star: %d %s, %.3f %.3f %.3f\n", count,
141                name, right_ascension, declination, magnitude);
142
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) );
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     static double warp = 0;
199
200     f = &current_aircraft.flight;
201     v = &current_view;
202
203     printf("RENDERING STARS\n");
204
205     glDisable( GL_FOG );
206     glDisable( GL_LIGHTING );
207     glPushMatrix();
208
209     glTranslatef( v->view_pos.x, v->view_pos.y, v->view_pos.z );
210
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,
216         warp * RAD_TO_DEG);
217
218     glCallList(stars);
219
220     glPopMatrix();
221     glEnable( GL_LIGHTING );
222     glEnable( GL_FOG );
223 }
224
225
226 /* $Log$
227 /* Revision 1.6  1997/09/05 14:17:31  curt
228 /* More tweaking with stars.
229 /*
230  * Revision 1.5  1997/09/05 01:35:59  curt
231  * Working on getting stars right.
232  *
233  * Revision 1.4  1997/09/04 02:17:38  curt
234  * Shufflin' stuff.
235  *
236  * Revision 1.3  1997/08/29 17:55:28  curt
237  * Worked on properly aligning the stars.
238  *
239  * Revision 1.2  1997/08/27 21:32:30  curt
240  * Restructured view calculation code.  Added stars.
241  *
242  * Revision 1.1  1997/08/27 03:34:48  curt
243  * Initial revision.
244  *
245  */