|
| 1 | + |
| 2 | +/* Copyright (c) Mark J. Kilgard, 1994. */ |
| 3 | + |
| 4 | +/* This program is freely distributable without licensing fees |
| 5 | + and is provided without guarantee or warrantee expressed or |
| 6 | + implied. This program is -not- in the public domain. */ |
| 7 | + |
| 8 | +#include <stdlib.h> |
| 9 | +#include <stdio.h> |
| 10 | +#ifndef WIN32 |
| 11 | +#include <unistd.h> |
| 12 | +#else |
| 13 | +#define random rand |
| 14 | +#define srandom srand |
| 15 | +#endif |
| 16 | +#include <math.h> |
| 17 | +#include <GL/glut.h> |
| 18 | + |
| 19 | +/* Some <math.h> files do not define M_PI... */ |
| 20 | +#ifndef M_PI |
| 21 | +#define M_PI 3.14159265 |
| 22 | +#endif |
| 23 | +#ifndef M_PI_2 |
| 24 | +#define M_PI_2 1.57079632 |
| 25 | +#endif |
| 26 | + |
| 27 | +GLboolean moving = GL_FALSE; |
| 28 | + |
| 29 | +#define MAX_PLANES 15 |
| 30 | + |
| 31 | +struct { |
| 32 | + float speed; /* zero speed means not flying */ |
| 33 | + GLfloat red, green, blue; |
| 34 | + float theta; |
| 35 | + float x, y, z, angle; |
| 36 | +} planes[MAX_PLANES]; |
| 37 | + |
| 38 | +#define v3f glVertex3f /* v3f was the short IRIS GL name for |
| 39 | +glVertex3f */ |
| 40 | + |
| 41 | +void |
| 42 | +draw(void) |
| 43 | +{ |
| 44 | + GLfloat red, green, blue; |
| 45 | + int i; |
| 46 | + |
| 47 | + glClear(GL_DEPTH_BUFFER_BIT); |
| 48 | + /* paint black to blue smooth shaded polygon for background */ |
| 49 | + glDisable(GL_DEPTH_TEST); |
| 50 | + glShadeModel(GL_SMOOTH); |
| 51 | + glBegin(GL_POLYGON); |
| 52 | + glColor3f(0.0, 0.0, 0.0); |
| 53 | + v3f(-20, 20, -19); |
| 54 | + v3f(20, 20, -19); |
| 55 | + glColor3f(0.0, 0.0, 1.0); |
| 56 | + v3f(20, -20, -19); |
| 57 | + v3f(-20, -20, -19); |
| 58 | + glEnd(); |
| 59 | + /* paint planes */ |
| 60 | + glEnable(GL_DEPTH_TEST); |
| 61 | + glShadeModel(GL_FLAT); |
| 62 | + for (i = 0; i < MAX_PLANES; i++) |
| 63 | + if (planes[i].speed != 0.0) { |
| 64 | + glPushMatrix(); |
| 65 | + glTranslatef(planes[i].x, planes[i].y, planes[i].z); |
| 66 | + glRotatef(290.0, 1.0, 0.0, 0.0); |
| 67 | + glRotatef(planes[i].angle, 0.0, 0.0, 1.0); |
| 68 | + glScalef(1.0 / 3.0, 1.0 / 4.0, 1.0 / 4.0); |
| 69 | + glTranslatef(0.0, -4.0, -1.5); |
| 70 | + glBegin(GL_TRIANGLE_STRIP); |
| 71 | + /* left wing */ |
| 72 | + v3f(-7.0, 0.0, 2.0); |
| 73 | + v3f(-1.0, 0.0, 3.0); |
| 74 | + glColor3f(red = planes[i].red, green = planes[i].green, |
| 75 | + blue = planes[i].blue); |
| 76 | + v3f(-1.0, 7.0, 3.0); |
| 77 | + /* left side */ |
| 78 | + glColor3f(0.6 * red, 0.6 * green, 0.6 * blue); |
| 79 | + v3f(0.0, 0.0, 0.0); |
| 80 | + v3f(0.0, 8.0, 0.0); |
| 81 | + /* right side */ |
| 82 | + v3f(1.0, 0.0, 3.0); |
| 83 | + v3f(1.0, 7.0, 3.0); |
| 84 | + /* final tip of right wing */ |
| 85 | + glColor3f(red, green, blue); |
| 86 | + v3f(7.0, 0.0, 2.0); |
| 87 | + glEnd(); |
| 88 | + glPopMatrix(); |
| 89 | + } |
| 90 | + glutSwapBuffers(); |
| 91 | +} |
| 92 | + |
| 93 | +void |
| 94 | +tick_per_plane(int i) |
| 95 | +{ |
| 96 | + float theta = planes[i].theta += planes[i].speed; |
| 97 | + planes[i].z = -9 + 4 * cos(theta); |
| 98 | + planes[i].x = 4 * sin(2 * theta); |
| 99 | + planes[i].y = sin(theta / 3.4) * 3; |
| 100 | + planes[i].angle = ((atan(2.0) + M_PI_2) * sin(theta) - M_PI_2) * 180 / M_PI; |
| 101 | + if (planes[i].speed < 0.0) |
| 102 | + planes[i].angle += 180; |
| 103 | +} |
| 104 | + |
| 105 | +void |
| 106 | +add_plane(void) |
| 107 | +{ |
| 108 | + int i; |
| 109 | + |
| 110 | + for (i = 0; i < MAX_PLANES; i++) |
| 111 | + if (planes[i].speed == 0) { |
| 112 | + |
| 113 | +#define SET_COLOR(r,g,b) \ |
| 114 | +planes[i].red=r; planes[i].green=g; planes[i].blue=b; |
| 115 | + |
| 116 | + switch (random() % 6) { |
| 117 | + case 0: |
| 118 | + SET_COLOR(1.0, 0.0, 0.0); /* red */ |
| 119 | + break; |
| 120 | + case 1: |
| 121 | + SET_COLOR(1.0, 1.0, 1.0); /* white */ |
| 122 | + break; |
| 123 | + case 2: |
| 124 | + SET_COLOR(0.0, 1.0, 0.0); /* green */ |
| 125 | + break; |
| 126 | + case 3: |
| 127 | + SET_COLOR(1.0, 0.0, 1.0); /* magenta */ |
| 128 | + break; |
| 129 | + case 4: |
| 130 | + SET_COLOR(1.0, 1.0, 0.0); /* yellow */ |
| 131 | + break; |
| 132 | + case 5: |
| 133 | + SET_COLOR(0.0, 1.0, 1.0); /* cyan */ |
| 134 | + break; |
| 135 | + } |
| 136 | + planes[i].speed = ((float) (random() % 20)) * 0.001 + 0.02; |
| 137 | + if (random() & 0x1) |
| 138 | + planes[i].speed *= -1; |
| 139 | + planes[i].theta = ((float) (random() % 257)) * 0.1111; |
| 140 | + tick_per_plane(i); |
| 141 | + if (!moving) |
| 142 | + glutPostRedisplay(); |
| 143 | + return; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +void |
| 148 | +remove_plane(void) |
| 149 | +{ |
| 150 | + int i; |
| 151 | + |
| 152 | + for (i = MAX_PLANES - 1; i >= 0; i--) |
| 153 | + if (planes[i].speed != 0) { |
| 154 | + planes[i].speed = 0; |
| 155 | + if (!moving) |
| 156 | + glutPostRedisplay(); |
| 157 | + return; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +void |
| 162 | +tick(void) |
| 163 | +{ |
| 164 | + int i; |
| 165 | + |
| 166 | + for (i = 0; i < MAX_PLANES; i++) |
| 167 | + if (planes[i].speed != 0.0) |
| 168 | + tick_per_plane(i); |
| 169 | +} |
| 170 | + |
| 171 | +void |
| 172 | +animate(void) |
| 173 | +{ |
| 174 | + tick(); |
| 175 | + glutPostRedisplay(); |
| 176 | +} |
| 177 | + |
| 178 | +void |
| 179 | +visible(int state) |
| 180 | +{ |
| 181 | + if (state == GLUT_VISIBLE) { |
| 182 | + if (moving) |
| 183 | + glutIdleFunc(animate); |
| 184 | + } else { |
| 185 | + if (moving) |
| 186 | + glutIdleFunc(NULL); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +/* ARGSUSED1 */ |
| 191 | +void |
| 192 | +keyboard(unsigned char ch, int x, int y) |
| 193 | +{ |
| 194 | + switch (ch) { |
| 195 | + case ' ': |
| 196 | + if (!moving) { |
| 197 | + tick(); |
| 198 | + glutPostRedisplay(); |
| 199 | + } |
| 200 | + break; |
| 201 | + case 27: /* ESC */ |
| 202 | + exit(0); |
| 203 | + break; |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +#define ADD_PLANE 1 |
| 208 | +#define REMOVE_PLANE 2 |
| 209 | +#define MOTION_ON 3 |
| 210 | +#define MOTION_OFF 4 |
| 211 | +#define QUIT 5 |
| 212 | + |
| 213 | +void |
| 214 | +menu(int item) |
| 215 | +{ |
| 216 | + switch (item) { |
| 217 | + case ADD_PLANE: |
| 218 | + add_plane(); |
| 219 | + break; |
| 220 | + case REMOVE_PLANE: |
| 221 | + remove_plane(); |
| 222 | + break; |
| 223 | + case MOTION_ON: |
| 224 | + moving = GL_TRUE; |
| 225 | + glutChangeToMenuEntry(3, "Motion off", MOTION_OFF); |
| 226 | + glutIdleFunc(animate); |
| 227 | + break; |
| 228 | + case MOTION_OFF: |
| 229 | + moving = GL_FALSE; |
| 230 | + glutChangeToMenuEntry(3, "Motion", MOTION_ON); |
| 231 | + glutIdleFunc(NULL); |
| 232 | + break; |
| 233 | + case QUIT: |
| 234 | + exit(0); |
| 235 | + break; |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +int |
| 240 | +main(int argc, char *argv[]) |
| 241 | +{ |
| 242 | + glutInit(&argc, argv); |
| 243 | + /* use multisampling if available */ |
| 244 | + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE); |
| 245 | + glutCreateWindow("glutplane"); |
| 246 | + glutDisplayFunc(draw); |
| 247 | + glutKeyboardFunc(keyboard); |
| 248 | + glutVisibilityFunc(visible); |
| 249 | + glutCreateMenu(menu); |
| 250 | + glutAddMenuEntry("Add plane", ADD_PLANE); |
| 251 | + glutAddMenuEntry("Remove plane", REMOVE_PLANE); |
| 252 | + glutAddMenuEntry("Motion", MOTION_ON); |
| 253 | + glutAddMenuEntry("Quit", QUIT); |
| 254 | + glutAttachMenu(GLUT_RIGHT_BUTTON); |
| 255 | + /* setup OpenGL state */ |
| 256 | + glClearDepth(1.0); |
| 257 | + glClearColor(0.0, 0.0, 0.0, 0.0); |
| 258 | + glMatrixMode(GL_PROJECTION); |
| 259 | + glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 20); |
| 260 | + glMatrixMode(GL_MODELVIEW); |
| 261 | + /* add three initial random planes */ |
| 262 | + srandom(getpid()); |
| 263 | + add_plane(); |
| 264 | + add_plane(); |
| 265 | + add_plane(); |
| 266 | + /* start event processing */ |
| 267 | + glutMainLoop(); |
| 268 | + return 0; /* ANSI C requires main to return int. */ |
| 269 | +} |
0 commit comments