2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that (i) the above copyright notices and this permission notice appear in
7 * all copies of the software and related documentation, and (ii) the name of
8 * Silicon Graphics may not be used in any advertising or
9 * publicity relating to the software without the specific, prior written
10 * permission of Silicon Graphics.
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
33 #define PI 3.141592657
49 #define MAXANGLES 6000
52 typedef struct _starRec
{
54 float x
[2], y
[2], z
[2];
55 float offsetX
, offsetY
, offsetR
, rotation
;
63 GLint starCount
= MAXSTARS
/ 2;
66 starRec stars
[MAXSTARS
];
67 float sinTable
[MAXANGLES
];
70 float Sin(float angle
)
73 return (sinTable
[(GLint
)angle
]);
76 float Cos(float angle
)
79 return (sinTable
[((GLint
)angle
+(MAXANGLES
/4))%MAXANGLES
]);
82 void NewStar(GLint n
, GLint d
)
86 stars
[n
].type
= CIRCLE
;
88 stars
[n
].type
= STREAK
;
90 stars
[n
].x
[0] = (float)(rand() % MAXPOS
- MAXPOS
/ 2);
91 stars
[n
].y
[0] = (float)(rand() % MAXPOS
- MAXPOS
/ 2);
92 stars
[n
].z
[0] = (float)(rand() % MAXPOS
+ d
);
93 if (rand()%4 == 0 && flag
== WEIRD
) {
94 stars
[n
].offsetX
= (float)(rand() % 100 - 100 / 2);
95 stars
[n
].offsetY
= (float)(rand() % 100 - 100 / 2);
96 stars
[n
].offsetR
= (float)(rand() % 25 - 25 / 2);
98 stars
[n
].offsetX
= 0.0;
99 stars
[n
].offsetY
= 0.0;
100 stars
[n
].offsetR
= 0.0;
104 void RotatePoint(float *x
, float *y
, float rotation
)
108 tmpX
= *x
* Cos(rotation
) - *y
* Sin(rotation
);
109 tmpY
= *y
* Cos(rotation
) + *x
* Sin(rotation
);
119 offset
= speed
* 60.0;
121 for (n
= 0; n
< starCount
; n
++) {
122 stars
[n
].x
[1] = stars
[n
].x
[0];
123 stars
[n
].y
[1] = stars
[n
].y
[0];
124 stars
[n
].z
[1] = stars
[n
].z
[0];
125 stars
[n
].x
[0] += stars
[n
].offsetX
;
126 stars
[n
].y
[0] += stars
[n
].offsetY
;
127 stars
[n
].z
[0] -= offset
;
128 stars
[n
].rotation
+= stars
[n
].offsetR
;
129 if (stars
[n
].rotation
> MAXANGLES
) {
130 stars
[n
].rotation
= 0.0;
132 else if (stars
[n
].rotation
< 0.0) {
133 stars
[n
].rotation
+= 360.0;
138 GLenum
StarPoint(GLint n
)
140 float x0
, y0
, x1
, y1
, width
;
143 x0
= stars
[n
].x
[0] * windW
/ stars
[n
].z
[0];
144 y0
= stars
[n
].y
[0] * windH
/ stars
[n
].z
[0];
145 RotatePoint(&x0
, &y0
, stars
[n
].rotation
);
149 if (x0
>= 0.0 && x0
< windW
&& y0
>= 0.0 && y0
< windH
) {
150 if (stars
[n
].type
== STREAK
) {
151 x1
= stars
[n
].x
[1] * windW
/ stars
[n
].z
[1];
152 y1
= stars
[n
].y
[1] * windH
/ stars
[n
].z
[1];
153 RotatePoint(&x1
, &y1
, stars
[n
].rotation
);
157 glLineWidth(MAXPOS
/100.0/stars
[n
].z
[0]+1.0);
158 glColor3f(1.0, (MAXWARP
-speed
)/MAXWARP
, (MAXWARP
-speed
)/MAXWARP
);
159 if (fabs(x0
-x1
) < 1.0 && fabs(y0
-y1
) < 1.0) {
170 width
= MAXPOS
/ 10.0 / stars
[n
].z
[0] + 1.0;
171 glColor3f(1.0, 0.0, 0.0);
173 for (i
= 0; i
< 8; i
++) {
174 float x
= x0
+ width
* Cos((float)i
*MAXANGLES
/8.0);
175 float y
= y0
+ width
* Sin((float)i
*MAXANGLES
/8.0);
190 glClear(GL_COLOR_BUFFER_BIT
);
192 for (n
= 0; n
< starCount
; n
++) {
193 if (stars
[n
].z
[0] > speed
|| (stars
[n
].z
[0] > 0.0 && speed
< MAXWARP
)) {
194 if (StarPoint(n
) == GL_FALSE
) {
203 static void Init(void)
208 srand((unsigned int) glutGet(GLUT_ELAPSED_TIME
) );
210 for (n
= 0; n
< MAXSTARS
; n
++) {
215 for (n
= 0; n
< MAXANGLES
; n
++) {
216 sinTable
[n
] = sin(angle
);
217 angle
+= PI
/ (MAXANGLES
/ 2.0);
220 glClearColor(0.0, 0.0, 0.0, 0.0);
222 glDisable(GL_DITHER
);
225 void Reshape(int width
, int height
)
228 windW
= (GLint
)width
;
229 windH
= (GLint
)height
;
231 glViewport(0, 0, windW
, windH
);
233 glMatrixMode(GL_PROJECTION
);
235 gluOrtho2D(-0.5, windW
+0.5, -0.5, windH
+0.5);
236 glMatrixMode(GL_MODELVIEW
);
239 static void Key(unsigned char key
, int x
, int y
)
246 flag
= (flag
== NORMAL
) ? WEIRD
: NORMAL
;
262 speed
= (float)(nitro
/ 10) + 1.0;
263 if (speed
> MAXWARP
) {
266 if (++nitro
> MAXWARP
*10) {
269 } else if (nitro
< 0) {
271 speed
= (float)(-nitro
/ 10) + 1.0;
272 if (speed
> MAXWARP
) {
283 static GLenum
Args(int argc
, char **argv
)
287 doubleBuffer
= GL_TRUE
;
289 for (i
= 1; i
< argc
; i
++) {
290 if (strcmp(argv
[i
], "-sb") == 0) {
291 doubleBuffer
= GL_FALSE
;
292 } else if (strcmp(argv
[i
], "-db") == 0) {
293 doubleBuffer
= GL_TRUE
;
299 void GLUTCALLBACK
glut_post_redisplay_p(void)
304 int main(int argc
, char **argv
)
308 glutInit(&argc
, argv
);
310 if (Args(argc
, argv
) == GL_FALSE
) {
316 glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
319 type
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
320 glutInitDisplayMode(type
);
322 if (glutCreateWindow("Stars") == GL_FALSE
) {
328 glutReshapeFunc(Reshape
);
329 glutKeyboardFunc(Key
);
330 glutDisplayFunc(Draw
);
331 glutIdleFunc(glut_post_redisplay_p
);