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
30 #include "glut_wrap.h"
34 #define PI 3.141592657
51 #define MAXANGLES 6000
54 typedef struct _starRec
{
56 float x
[2], y
[2], z
[2];
57 float offsetX
, offsetY
, offsetR
, rotation
;
64 GLenum flag
= NORMAL
, overlayInit
= GL_FALSE
;
65 GLint starCount
= MAXSTARS
/ 2;
68 starRec stars
[MAXSTARS
];
69 float sinTable
[MAXANGLES
];
72 static float Sin(float angle
)
75 return (sinTable
[(GLint
)angle
]);
78 static float Cos(float angle
)
81 return (sinTable
[((GLint
)angle
+(MAXANGLES
/4))%MAXANGLES
]);
84 static void NewStar(GLint n
, GLint d
)
88 stars
[n
].type
= CIRCLE
;
90 stars
[n
].type
= STREAK
;
92 stars
[n
].x
[0] = (float)(rand() % MAXPOS
- MAXPOS
/ 2);
93 stars
[n
].y
[0] = (float)(rand() % MAXPOS
- MAXPOS
/ 2);
94 stars
[n
].z
[0] = (float)(rand() % MAXPOS
+ d
);
95 if (rand()%4 == 0 && flag
== WEIRD
) {
96 stars
[n
].offsetX
= (float)(rand() % 100 - 100 / 2);
97 stars
[n
].offsetY
= (float)(rand() % 100 - 100 / 2);
98 stars
[n
].offsetR
= (float)(rand() % 25 - 25 / 2);
100 stars
[n
].offsetX
= 0.0;
101 stars
[n
].offsetY
= 0.0;
102 stars
[n
].offsetR
= 0.0;
106 static void RotatePoint(float *x
, float *y
, float rotation
)
110 tmpX
= *x
* Cos(rotation
) - *y
* Sin(rotation
);
111 tmpY
= *y
* Cos(rotation
) + *x
* Sin(rotation
);
116 static void MoveStars(void)
121 offset
= speed
* 60.0;
123 for (n
= 0; n
< starCount
; n
++) {
124 stars
[n
].x
[1] = stars
[n
].x
[0];
125 stars
[n
].y
[1] = stars
[n
].y
[0];
126 stars
[n
].z
[1] = stars
[n
].z
[0];
127 stars
[n
].x
[0] += stars
[n
].offsetX
;
128 stars
[n
].y
[0] += stars
[n
].offsetY
;
129 stars
[n
].z
[0] -= offset
;
130 stars
[n
].rotation
+= stars
[n
].offsetR
;
131 if (stars
[n
].rotation
> MAXANGLES
) {
132 stars
[n
].rotation
= 0.0;
137 static GLenum
StarPoint(GLint n
)
139 float x0
, y0
, x1
, y1
, width
;
142 x0
= stars
[n
].x
[0] * windW
/ stars
[n
].z
[0];
143 y0
= stars
[n
].y
[0] * windH
/ stars
[n
].z
[0];
144 RotatePoint(&x0
, &y0
, stars
[n
].rotation
);
148 if (x0
>= 0.0 && x0
< windW
&& y0
>= 0.0 && y0
< windH
) {
149 if (stars
[n
].type
== STREAK
) {
150 x1
= stars
[n
].x
[1] * windW
/ stars
[n
].z
[1];
151 y1
= stars
[n
].y
[1] * windH
/ stars
[n
].z
[1];
152 RotatePoint(&x1
, &y1
, stars
[n
].rotation
);
156 glLineWidth(MAXPOS
/100.0/stars
[n
].z
[0]+1.0);
157 glColor3f(1.0, (MAXWARP
-speed
)/MAXWARP
, (MAXWARP
-speed
)/MAXWARP
);
158 if (fabs(x0
-x1
) < 1.0 && fabs(y0
-y1
) < 1.0) {
169 width
= MAXPOS
/ 10.0 / stars
[n
].z
[0] + 1.0;
170 glColor3f(1.0, 0.0, 0.0);
172 for (i
= 0; i
< 8; i
++) {
173 float x
= x0
+ width
* Cos((float)i
*MAXANGLES
/8.0);
174 float y
= y0
+ width
* Sin((float)i
*MAXANGLES
/8.0);
185 static void ShowStars(void)
189 glClear(GL_COLOR_BUFFER_BIT
);
191 for (n
= 0; n
< starCount
; n
++) {
192 if (stars
[n
].z
[0] > speed
|| (stars
[n
].z
[0] > 0.0 && speed
< MAXWARP
)) {
193 if (StarPoint(n
) == GL_FALSE
) {
202 static void Init(void)
207 srand((unsigned int)time(NULL
));
209 for (n
= 0; n
< MAXSTARS
; n
++) {
214 for (n
= 0; n
< MAXANGLES
; n
++) {
215 sinTable
[n
] = sin(angle
);
216 angle
+= PI
/ (MAXANGLES
/ 2.0);
219 glClearColor(0.0, 0.0, 0.0, 0.0);
221 glDisable(GL_DITHER
);
224 static void Reshape(int width
, int height
)
227 windW
= (GLint
)width
;
228 windH
= (GLint
)height
;
230 glutUseLayer(GLUT_OVERLAY
);
232 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
);
237 overlayInit
= GL_FALSE
;
239 glutUseLayer(GLUT_NORMAL
);
241 glViewport(0, 0, windW
, windH
);
242 glMatrixMode(GL_PROJECTION
);
244 gluOrtho2D(-0.5, windW
+0.5, -0.5, windH
+0.5);
245 glMatrixMode(GL_MODELVIEW
);
248 static void Key(unsigned char key
, int x
, int y
)
255 flag
= (flag
== NORMAL
) ? WEIRD
: NORMAL
;
265 static void Idle(void)
268 if (overlayInit
== GL_FALSE
) {
269 glutUseLayer(GLUT_OVERLAY
);
270 glClear(GL_COLOR_BUFFER_BIT
);
271 /* glColor3f(1.0, 0.0, 0.0);*/
275 glVertex2i(windW
/4-10, windH
/4-10);
276 glVertex2i(windW
/2-10, windH
/4-10);
277 glVertex2i(windW
/2-10, windH
/2-10);
278 glVertex2i(windW
/4-10, windH
/2-10);
283 glVertex2i(windW
/4, windH
/4);
284 glVertex2i(windW
/2, windH
/4);
285 glVertex2i(windW
/2, windH
/2);
286 glVertex2i(windW
/4, windH
/2);
291 glVertex2i(windW
/4+10, windH
/4+10);
292 glVertex2i(windW
/2+10, windH
/4+10);
293 glVertex2i(windW
/2+10, windH
/2+10);
294 glVertex2i(windW
/4+10, windH
/2+10);
297 glutUseLayer(GLUT_NORMAL
);
298 overlayInit
= GL_TRUE
;
304 speed
= (float)(nitro
/ 10) + 1.0;
305 if (speed
> MAXWARP
) {
308 if (++nitro
> MAXWARP
*10) {
311 } else if (nitro
< 0) {
313 speed
= (float)(-nitro
/ 10) + 1.0;
314 if (speed
> MAXWARP
) {
325 static GLenum
Args(int argc
, char **argv
)
329 doubleBuffer
= GL_TRUE
;
331 for (i
= 1; i
< argc
; i
++) {
332 if (strcmp(argv
[i
], "-sb") == 0) {
333 doubleBuffer
= GL_FALSE
;
334 } else if (strcmp(argv
[i
], "-db") == 0) {
335 doubleBuffer
= GL_TRUE
;
341 int main(int argc
, char **argv
)
345 glutInit(&argc
, argv
);
347 if (!glutLayerGet(GLUT_OVERLAY_POSSIBLE
))
349 fprintf(stderr
, "Overlay not available\n");
353 if (Args(argc
, argv
) == GL_FALSE
) {
359 glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
362 type
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
363 glutInitDisplayMode(type
);
365 if (glutCreateWindow("Overlay Test") == GL_FALSE
) {
369 glutEstablishOverlay();
373 glutReshapeFunc(Reshape
);
374 glutKeyboardFunc(Key
);