]> git.mxchange.org Git - flightgear.git/blob - Scenery/stars.c
Restructured view calculation code. Added 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
35 #include <GL/glut.h>
36
37 #include "stars.h"
38
39 #include "../general.h"
40
41 #include "../GLUT/views.h"
42
43
44 static GLint stars;
45
46
47 /* Initialize the Star Management Subsystem */
48 void fgStarsInit() {
49     FILE *fd;
50     struct GENERAL *g;
51     char path[1024];
52     char line[256], name[256];
53     char *tmp_ptr;
54     double right_ascension, declination, magnitude;
55     GLfloat mag[4] = {0.0, 0.0, 0.0, 1.0};
56     int count;
57
58     g = &general;
59
60     /* build the full path name to the stars data base file */
61     path[0] = '\0';
62     strcat(path, g->root_dir);
63     strcat(path, "/Scenery/");
64     strcat(path, "Stars.dat");
65
66     printf("Loading Stars: %s\n", path);
67
68     if ( (fd = fopen(path, "r")) == NULL ) {
69         printf("Cannot open star file: '%s'\n", path);
70         return;
71     }
72
73     stars = glGenLists(1);
74     glNewList( stars, GL_COMPILE );
75     glBegin( GL_POINTS );
76
77     /* read in each line of the file */
78     count = 0;
79     while ( (fgets(line, 256, fd) != NULL) && (count < FG_MAX_STARS) ) {
80         tmp_ptr = line;
81         
82         /* advance to first non-whitespace character */
83         while ( (tmp_ptr[0] == ' ') || (tmp_ptr[0] == '\t') ) {
84             tmp_ptr++;
85         }
86
87         if ( tmp_ptr[0] == '#' ) {
88             /* comment */
89         } else if ( strlen(tmp_ptr) == 0 ) {
90             /* blank line */
91         } else {
92             /* star data line */
93             fscanf(fd, "%s %lf %lf %lf\n", 
94                    name, &right_ascension, &declination, &magnitude);
95             /* printf("Found star: %d %s, %.3f %.3f %.3f\n", count,
96                name, right_ascension, declination, magnitude); */
97             count++;
98             magnitude = magnitude * 0.8 + 0.2;
99             mag[0] = mag[1] = mag[2] = magnitude;
100             glColor3f( magnitude, magnitude, magnitude );
101             glVertex3f( 190000.0 * sin(right_ascension) * cos(declination),
102                         190000.0 * cos(right_ascension) * cos(declination),
103                         190000.0 * sin(declination) );
104
105         } /* if valid line */
106
107     } /* while */
108
109     glEnd();
110     glEndList();
111 }
112
113
114 /* Draw the Stars */
115 void fgStarsRender() {
116     struct VIEW *v;
117     GLfloat amb[3], diff[3];
118
119     v = &current_view;
120
121     printf("RENDERING STARS\n");
122
123     glDisable( GL_FOG );
124     glDisable( GL_LIGHTING );
125     glPushMatrix();
126
127     /* set lighting parameters for stars */
128     /* amb[0] = amb[1] = amb[2] = 1.0;
129     diff[0] = diff[1] = diff[2] = 1.0;
130     glLightfv(GL_LIGHT0, GL_AMBIENT, amb );
131     glLightfv(GL_LIGHT0, GL_DIFFUSE, diff ); */
132
133     glTranslatef(v->view_pos.x, v->view_pos.y, v->view_pos.z);
134
135     glCallList(stars);
136
137     glPopMatrix();
138     glEnable( GL_LIGHTING );
139     glEnable( GL_FOG );
140 }
141
142
143 /* $Log$
144 /* Revision 1.2  1997/08/27 21:32:30  curt
145 /* Restructured view calculation code.  Added stars.
146 /*
147  * Revision 1.1  1997/08/27 03:34:48  curt
148  * Initial revisio.
149  *
150  */