]> git.mxchange.org Git - flightgear.git/blob - Scenery/stars.c
af868804e09e191a4fe05204d97b778a13653193
[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 *tmp_ptr;
61     double right_ascension, declination, magnitude;
62     GLfloat mag[4] = {0.0, 0.0, 0.0, 1.0};
63     int count;
64
65     g = &general;
66
67     /* build the full path name to the stars data base file */
68     path[0] = '\0';
69     strcat(path, g->root_dir);
70     strcat(path, "/Scenery/");
71     strcat(path, "Stars.dat");
72
73     printf("Loading Stars: %s\n", path);
74
75     if ( (fd = fopen(path, "r")) == NULL ) {
76         printf("Cannot open star file: '%s'\n", path);
77         return;
78     }
79
80     stars = glGenLists(1);
81     glNewList( stars, GL_COMPILE );
82     glBegin( GL_POINTS );
83
84     /* read in each line of the file */
85     count = 0;
86     while ( (fgets(line, 256, fd) != NULL) && (count < FG_MAX_STARS) ) {
87         tmp_ptr = line;
88         
89         /* advance to first non-whitespace character */
90         while ( (tmp_ptr[0] == ' ') || (tmp_ptr[0] == '\t') ) {
91             tmp_ptr++;
92         }
93
94         if ( tmp_ptr[0] == '#' ) {
95             /* comment */
96         } else if ( strlen(tmp_ptr) == 0 ) {
97             /* blank line */
98         } else {
99             /* star data line */
100             fscanf(fd, "%s %lf %lf %lf\n", 
101                    name, &right_ascension, &declination, &magnitude);
102             /* printf("Found star: %d %s, %.3f %.3f %.3f\n", count,
103                name, right_ascension, declination, magnitude); */
104             count++;
105             magnitude = magnitude * 0.8 + 0.2;
106             mag[0] = mag[1] = mag[2] = magnitude;
107             glColor3f( mag[0], mag[1], mag[2] );
108             glVertex3f( 190000.0 * sin(right_ascension) * cos(declination),
109                         190000.0 * cos(right_ascension) * cos(declination),
110                         190000.0 * sin(declination) );
111
112         } /* if valid line */
113
114     } /* while */
115
116     glEnd();
117     glEndList();
118 }
119
120
121 /* Draw the Stars */
122 void fgStarsRender() {
123     struct FLIGHT *f;
124     struct VIEW *v;
125     double angle;
126
127     f = &current_aircraft.flight;
128     v = &current_view;
129
130     printf("RENDERING STARS\n");
131
132     glDisable( GL_FOG );
133     glDisable( GL_LIGHTING );
134     glPushMatrix();
135
136     /* set lighting parameters for stars */
137     /* amb[0] = amb[1] = amb[2] = 1.0;
138     diff[0] = diff[1] = diff[2] = 1.0;
139     glLightfv(GL_LIGHT0, GL_AMBIENT, amb );
140     glLightfv(GL_LIGHT0, GL_DIFFUSE, diff ); */
141
142     glTranslatef( v->view_pos.x, v->view_pos.y, v->view_pos.z );
143
144     angle = FG_2PI * fmod(DaysSinceEpoch(time(NULL)), 1.0);
145     glRotatef( -angle * RAD_TO_DEG, 0.0, 0.0, 1.0 );
146     printf("Rotating stars by %.2f\n", -angle * RAD_TO_DEG);
147
148     glCallList(stars);
149
150     glPopMatrix();
151     glEnable( GL_LIGHTING );
152     glEnable( GL_FOG );
153 }
154
155
156 /* $Log$
157 /* Revision 1.3  1997/08/29 17:55:28  curt
158 /* Worked on properly aligning the stars.
159 /*
160  * Revision 1.2  1997/08/27 21:32:30  curt
161  * Restructured view calculation code.  Added stars.
162  *
163  * Revision 1.1  1997/08/27 03:34:48  curt
164  * Initial revision.
165  *
166  */