1 /*
2     OpenUniverse 1.0
3     Copyright (C) 2000  Raul Alonso <amil@las.es>
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 #include "ou.h"
21 
22 float star_size = 40.0;
23 
24 
InitStars(float magcorr,int read_file)25 void InitStars(float magcorr, int read_file)
26 {
27 	char datapath[100] = DATA_DIR;
28 	static char line[100], *tmp;
29 	FILE *f = NULL;
30 	static float dimension, old_mag, magnitude;
31 	static double p1[3], p2[3], p3[3], p4[3];
32 
33 	int i, j;
34 
35 	if (read_file) {
36 		sprintf(line, "%s/stars.dat", datapath);
37 		f = fopen(line, "rt");
38 		if (f == NULL) {
39 			error("Couldn't find star catalog file");
40 			shutdown(1);
41 		}
42 	}
43 	for (i = 0; i < NUMSTARS; i++) {
44 		if (read_file) {
45 			fgets(line, 100, f);
46 			if (feof(f))
47 				break;
48 			while (line[0] == '#')
49 				fgets(line, 100, f);
50 			if (feof(f))
51 				break;
52 			j = 0;
53 			while (line[j] != ',')
54 				j++;
55 			line[j] = '\0';
56 			tmp = line + j + 1;
57 			strcpy(stars[i].Name, line);
58 			sscanf(tmp, "%f,%f,%f,%c\n", &stars[i].ascension,
59 				   &stars[i].declination, &stars[i].magnitude,
60 				   &stars[i].type);
61 
62 			/* Translate declination/ascension coord into x,y,z */
63 			INITVECTOR(stars[i].pos,
64 					   UNIVERSERADIUS * cos(-stars[i].ascension) *
65 					   cos(stars[i].declination),
66 					   UNIVERSERADIUS * sin(stars[i].declination),
67 					   UNIVERSERADIUS * sin(-stars[i].ascension) *
68 					   cos(stars[i].declination));
69 		}
70 		red = 0;
71 		/* Print some stars in red color for accuracy testing */
72 		if (!strcmp(stars[i].Name, "Polaris")) {
73 			red = 1;
74 			polaris = i;
75 		}
76 		if (!strcmp(stars[i].Name, "Dubhe"))
77 			red = 1;
78 		if (!strcmp(stars[i].Name, "Merak"))
79 			red = 1;
80 		if (!strcmp(stars[i].Name, "Phecda"))
81 			red = 1;
82 		if (!strcmp(stars[i].Name, "Megrez"))
83 			red = 1;
84 		if (!strcmp(stars[i].Name, "Alioth"))
85 			red = 1;
86 		if (!strcmp(stars[i].Name, "Mizar"))
87 			red = 1;
88 
89 
90 		/* Translate magnitude scale into 0-1 color scale.  This is a bit
91 		   tricky and didn't find the right translation yet (it's very
92 		   monitor gamma dependant) */
93 /*
94  *	stars[i].color=1.0/(1.46+stars[i].magnitude)+0.2;
95  *	stars[i].color=pow(1.0/(stars[i].magnitude+2.46),0.5);
96  *	stars[i].color=0.8/log(1.46+stars[i].magnitude);
97  */
98 		magnitude = magcorr * 2.42 / ((2.46 + stars[i].magnitude) * 2.42);
99 		if (magnitude > 1.0)
100 			magnitude = 1.0;
101 
102 		/* Only 20 magnitudes levels to speed up rendering process */
103 		magnitude = (int) ceil(magnitude * 20.0) / 20.0;
104 		switch (stars[i].type) {
105 		case 'O':
106 			stars[i].color[0] = 0.8;
107 			stars[i].color[1] = 0.8;
108 			stars[i].color[2] = 1.0;
109 			break;
110 		case 'B':
111 			stars[i].color[0] = 0.9;
112 			stars[i].color[1] = 0.9;
113 			stars[i].color[2] = 1.0;
114 			break;
115 		case 'A':
116 			stars[i].color[0] = 1.0;
117 			stars[i].color[1] = 1.0;
118 			stars[i].color[2] = 1.0;
119 			break;
120 		case 'F':
121 			stars[i].color[0] = 1.0;
122 			stars[i].color[1] = 1.0;
123 			stars[i].color[2] = 0.8;
124 			break;
125 		case 'G':
126 			stars[i].color[0] = 1.0;
127 			stars[i].color[1] = 1.0;
128 			stars[i].color[2] = 0.7;
129 			break;
130 		case 'K':
131 			stars[i].color[0] = 1.0;
132 			stars[i].color[1] = 0.9;
133 			stars[i].color[2] = 0.8;
134 			break;
135 		case 'M':
136 		case 'C':
137 		case 'S':
138 			stars[i].color[0] = 1.0;
139 			stars[i].color[1] = 0.6;
140 			stars[i].color[2] = 0.6;
141 			break;
142 		}
143 		stars[i].color[0] *= magnitude;
144 		stars[i].color[1] *= magnitude;
145 		stars[i].color[2] *= magnitude;
146 	}
147 	if (read_file)
148 		fclose(f);
149 
150 
151 	old_mag = 100.0;
152 	i = 0;
153 	StarsDL = glGenLists(1);
154 	glNewList(StarsDL, GL_COMPILE);
155 	glEnable(GL_BLEND);
156 	glBindTexture(GL_TEXTURE_2D, planets[0].textures[0]);
157 	while (stars[i].magnitude < magcorr) {
158 
159 		dimension = star_size + 100.0 / (stars[i].magnitude + 2.4);
160 		dimension = 0.0035 + 0.001 / (stars[i].magnitude + 2.4);
161 		INITVECTOR(p1,
162 				   UNIVERSERADIUS *
163 				   cos(-(stars[i].ascension + dimension)) *
164 				   cos(stars[i].declination - dimension),
165 				   UNIVERSERADIUS * sin(stars[i].declination - dimension),
166 				   UNIVERSERADIUS *
167 				   sin(-(stars[i].ascension + dimension)) *
168 				   cos(stars[i].declination - dimension));
169 
170 		INITVECTOR(p2,
171 				   UNIVERSERADIUS *
172 				   cos(-(stars[i].ascension - dimension)) *
173 				   cos(stars[i].declination - dimension),
174 				   UNIVERSERADIUS * sin(stars[i].declination - dimension),
175 				   UNIVERSERADIUS *
176 				   sin(-(stars[i].ascension - dimension)) *
177 				   cos(stars[i].declination - dimension));
178 
179 		INITVECTOR(p3,
180 				   UNIVERSERADIUS *
181 				   cos(-(stars[i].ascension - dimension)) *
182 				   cos(stars[i].declination + dimension),
183 				   UNIVERSERADIUS * sin(stars[i].declination + dimension),
184 				   UNIVERSERADIUS *
185 				   sin(-(stars[i].ascension - dimension)) *
186 				   cos(stars[i].declination + dimension));
187 
188 		INITVECTOR(p4,
189 				   UNIVERSERADIUS *
190 				   cos(-(stars[i].ascension + dimension)) *
191 				   cos(stars[i].declination + dimension),
192 				   UNIVERSERADIUS * sin(stars[i].declination + dimension),
193 				   UNIVERSERADIUS *
194 				   sin(-(stars[i].ascension + dimension)) *
195 				   cos(stars[i].declination + dimension));
196 
197 
198 		glColor3f(stars[i].color[0], stars[i].color[1], stars[i].color[2]);
199 		glBegin(GL_QUADS);
200 		glTexCoord2f(0.0, 0.0);
201 		glVertex3f(p1[X], p1[Y], p1[Z]);
202 		glTexCoord2f(0.0, 1.0);
203 		glVertex3f(p2[X], p2[Y], p2[Z]);
204 		glTexCoord2f(1.0, 1.0);
205 		glVertex3f(p3[X], p3[Y], p3[Z]);
206 		glTexCoord2f(1.0, 0.0);
207 		glVertex3f(p4[X], p4[Y], p4[Z]);
208 		glEnd();
209 		i++;
210 	}
211 	glDisable(GL_BLEND);
212 	glDisable(GL_TEXTURE_2D);
213 	glBegin(GL_POINTS);
214 	while (i < NUMSTARS) {
215 		glColor3f(stars[i].color[0], stars[i].color[1], stars[i].color[2]);
216 		glVertex3dv(stars[i].pos);
217 		i++;
218 	}
219 	glEnd();
220 	glEndList();
221 }
222