jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc. |
| 3 | * |
| 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. |
| 11 | * |
| 12 | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF |
| 13 | * ANY KIND, |
| 14 | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 15 | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 16 | * |
| 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 |
| 22 | * OF THIS SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * Nov 20, 1995 use stdlib's rand()/srand() instead of random()/srand48(), etc. |
| 27 | */ |
| 28 | |
Gareth Hughes | ba58a66 | 2000-10-27 02:49:17 +0000 | [diff] [blame^] | 29 | /* |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 30 | * Modified by Li Wei(liwei@aiar.xjtu.edu.cn) to be able to run in Windows |
| 31 | * 6/13 |
| 32 | * |
| 33 | * Modified by Brian Paul to compile with Windows OR Unix. 7/23/97 |
| 34 | */ |
| 35 | |
| 36 | |
| 37 | #define _HPUX_SOURCE |
| 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #include <math.h> |
| 43 | #include <GL/glut.h> |
| 44 | |
| 45 | #ifndef RAND_MAX |
| 46 | # define RAND_MAX 32767 |
| 47 | #endif |
| 48 | |
| 49 | |
| 50 | #define XSIZE 100 |
| 51 | #define YSIZE 75 |
| 52 | |
| 53 | #define RINGS 5 |
| 54 | #define BLUERING 0 |
| 55 | #define BLACKRING 1 |
| 56 | #define REDRING 2 |
| 57 | #define YELLOWRING 3 |
| 58 | #define GREENRING 4 |
| 59 | |
| 60 | #define BACKGROUND 8 |
| 61 | |
| 62 | |
| 63 | GLenum rgb, doubleBuffer; |
| 64 | |
| 65 | #include "tkmap.c" |
| 66 | |
| 67 | unsigned char rgb_colors[RINGS][3]; |
| 68 | int mapped_colors[RINGS]; |
| 69 | float dests[RINGS][3]; |
| 70 | float offsets[RINGS][3]; |
| 71 | float angs[RINGS]; |
| 72 | float rotAxis[RINGS][3]; |
| 73 | int iters[RINGS]; |
| 74 | GLuint theTorus; |
| 75 | |
| 76 | |
| 77 | void FillTorus(float rc, int numc, float rt, int numt) |
| 78 | { |
| 79 | int i, j, k; |
| 80 | double s, t; |
| 81 | double x, y, z; |
| 82 | double pi, twopi; |
| 83 | |
| 84 | pi = 3.14159265358979323846; |
| 85 | twopi = 2 * pi; |
Gareth Hughes | ba58a66 | 2000-10-27 02:49:17 +0000 | [diff] [blame^] | 86 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 87 | for (i = 0; i < numc; i++) { |
| 88 | glBegin(GL_QUAD_STRIP); |
| 89 | for (j = 0; j <= numt; j++) { |
| 90 | for (k = 1; k >= 0; k--) { |
| 91 | s = (i + k) % numc + 0.5; |
| 92 | t = j % numt; |
| 93 | |
| 94 | x = cos(t*twopi/numt) * cos(s*twopi/numc); |
| 95 | y = sin(t*twopi/numt) * cos(s*twopi/numc); |
| 96 | z = sin(s*twopi/numc); |
| 97 | glNormal3f(x, y, z); |
| 98 | |
| 99 | x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt); |
| 100 | y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt); |
| 101 | z = rc * sin(s*twopi/numc); |
| 102 | glVertex3f(x, y, z); |
| 103 | } |
| 104 | } |
| 105 | glEnd(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | float Clamp(int iters_left, float t) |
| 110 | { |
| 111 | |
| 112 | if (iters_left < 3) { |
| 113 | return 0.0; |
| 114 | } |
| 115 | return (iters_left-2)*t/iters_left; |
| 116 | } |
| 117 | |
| 118 | void DrawScene(void) |
| 119 | { |
| 120 | int i, j; |
| 121 | GLboolean goIdle; |
| 122 | |
| 123 | goIdle = GL_TRUE; |
| 124 | for (i = 0; i < RINGS; i++) { |
| 125 | if (iters[i]) { |
| 126 | for (j = 0; j < 3; j++) { |
| 127 | offsets[i][j] = Clamp(iters[i], offsets[i][j]); |
| 128 | } |
| 129 | angs[i] = Clamp(iters[i], angs[i]); |
| 130 | iters[i]--; |
| 131 | goIdle = GL_FALSE; |
| 132 | } |
| 133 | } |
| 134 | if (goIdle) { |
| 135 | glutIdleFunc(NULL); |
| 136 | } |
| 137 | |
| 138 | glPushMatrix(); |
Gareth Hughes | ba58a66 | 2000-10-27 02:49:17 +0000 | [diff] [blame^] | 139 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 140 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
| 141 | gluLookAt(0,0,10, 0,0,0, 0,1,0); |
| 142 | |
| 143 | for (i = 0; i < RINGS; i++) { |
| 144 | if (rgb) { |
| 145 | glColor3ubv(rgb_colors[i]); |
| 146 | } else { |
| 147 | glIndexi(mapped_colors[i]); |
| 148 | } |
| 149 | glPushMatrix(); |
| 150 | glTranslatef(dests[i][0]+offsets[i][0], dests[i][1]+offsets[i][1], |
| 151 | dests[i][2]+offsets[i][2]); |
| 152 | glRotatef(angs[i], rotAxis[i][0], rotAxis[i][1], rotAxis[i][2]); |
| 153 | glCallList(theTorus); |
| 154 | glPopMatrix(); |
| 155 | } |
| 156 | |
| 157 | glPopMatrix(); |
| 158 | |
| 159 | glFlush(); |
| 160 | if (doubleBuffer) { |
| 161 | glutSwapBuffers(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | float MyRand(void) |
| 166 | { |
| 167 | return 10.0 * ( (float) rand() / (float) RAND_MAX - 0.5 ); |
| 168 | } |
| 169 | |
| 170 | void GLUTCALLBACK glut_post_redisplay_p(void) |
| 171 | { |
| 172 | glutPostRedisplay(); |
| 173 | } |
| 174 | |
| 175 | void ReInit(void) |
| 176 | { |
| 177 | int i; |
| 178 | float deviation; |
| 179 | |
| 180 | deviation = MyRand() / 2; |
| 181 | deviation = deviation * deviation; |
| 182 | for (i = 0; i < RINGS; i++) { |
| 183 | offsets[i][0] = MyRand(); |
| 184 | offsets[i][1] = MyRand(); |
| 185 | offsets[i][2] = MyRand(); |
| 186 | angs[i] = 260.0 * MyRand(); |
| 187 | rotAxis[i][0] = MyRand(); |
| 188 | rotAxis[i][1] = MyRand(); |
| 189 | rotAxis[i][2] = MyRand(); |
| 190 | iters[i] = (deviation * MyRand() + 60.0); |
| 191 | } |
| 192 | glutIdleFunc(glut_post_redisplay_p); |
| 193 | } |
| 194 | |
| 195 | void Init(void) |
| 196 | { |
| 197 | float base, height; |
| 198 | float aspect, x, y; |
| 199 | int i; |
| 200 | |
| 201 | float top_y = 1.0; |
| 202 | float bottom_y = 0.0; |
| 203 | float top_z = 0.15; |
| 204 | float bottom_z = 0.69; |
| 205 | float spacing = 2.5; |
| 206 | static float lmodel_ambient[] = {0.0, 0.0, 0.0, 0.0}; |
| 207 | static float lmodel_twoside[] = {GL_FALSE}; |
| 208 | static float lmodel_local[] = {GL_FALSE}; |
| 209 | static float light0_ambient[] = {0.1, 0.1, 0.1, 1.0}; |
| 210 | static float light0_diffuse[] = {1.0, 1.0, 1.0, 0.0}; |
| 211 | static float light0_position[] = {0.8660254, 0.5, 1, 0}; |
| 212 | static float light0_specular[] = {1.0, 1.0, 1.0, 0.0}; |
| 213 | static float bevel_mat_ambient[] = {0.0, 0.0, 0.0, 1.0}; |
| 214 | static float bevel_mat_shininess[] = {40.0}; |
| 215 | static float bevel_mat_specular[] = {1.0, 1.0, 1.0, 0.0}; |
| 216 | static float bevel_mat_diffuse[] = {1.0, 0.0, 0.0, 0.0}; |
| 217 | |
| 218 | srand( (unsigned int) glutGet(GLUT_ELAPSED_TIME) ); |
| 219 | |
| 220 | ReInit(); |
| 221 | for (i = 0; i < RINGS; i++) { |
| 222 | rgb_colors[i][0] = rgb_colors[i][1] = rgb_colors[i][2] = 0; |
| 223 | } |
| 224 | rgb_colors[BLUERING][2] = 255; |
| 225 | rgb_colors[REDRING][0] = 255; |
| 226 | rgb_colors[GREENRING][1] = 255; |
| 227 | rgb_colors[YELLOWRING][0] = 255; |
| 228 | rgb_colors[YELLOWRING][1] = 255; |
| 229 | mapped_colors[BLUERING] = COLOR_BLUE; |
| 230 | mapped_colors[REDRING] = COLOR_RED; |
| 231 | mapped_colors[GREENRING] = COLOR_GREEN; |
| 232 | mapped_colors[YELLOWRING] = COLOR_YELLOW; |
| 233 | mapped_colors[BLACKRING] = COLOR_BLACK; |
| 234 | |
| 235 | dests[BLUERING][0] = -spacing; |
| 236 | dests[BLUERING][1] = top_y; |
| 237 | dests[BLUERING][2] = top_z; |
| 238 | |
| 239 | dests[BLACKRING][0] = 0.0; |
| 240 | dests[BLACKRING][1] = top_y; |
| 241 | dests[BLACKRING][2] = top_z; |
| 242 | |
| 243 | dests[REDRING][0] = spacing; |
| 244 | dests[REDRING][1] = top_y; |
| 245 | dests[REDRING][2] = top_z; |
| 246 | |
| 247 | dests[YELLOWRING][0] = -spacing / 2.0; |
| 248 | dests[YELLOWRING][1] = bottom_y; |
| 249 | dests[YELLOWRING][2] = bottom_z; |
| 250 | |
| 251 | dests[GREENRING][0] = spacing / 2.0; |
| 252 | dests[GREENRING][1] = bottom_y; |
| 253 | dests[GREENRING][2] = bottom_z; |
| 254 | |
Gareth Hughes | ba58a66 | 2000-10-27 02:49:17 +0000 | [diff] [blame^] | 255 | base = 2.0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 256 | height = 2.0; |
| 257 | theTorus = glGenLists(1); |
| 258 | glNewList(theTorus, GL_COMPILE); |
| 259 | FillTorus(0.1, 8, 1.0, 25); |
| 260 | glEndList(); |
| 261 | |
| 262 | x = (float)XSIZE; |
| 263 | y = (float)YSIZE; |
| 264 | aspect = x / y; |
| 265 | glEnable(GL_CULL_FACE); |
| 266 | glCullFace(GL_BACK); |
| 267 | glEnable(GL_DEPTH_TEST); |
| 268 | glClearDepth(1.0); |
| 269 | |
| 270 | if (rgb) { |
| 271 | glClearColor(0.5, 0.5, 0.5, 0.0); |
| 272 | glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); |
| 273 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); |
| 274 | glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular); |
| 275 | glLightfv(GL_LIGHT0, GL_POSITION, light0_position); |
| 276 | glEnable(GL_LIGHT0); |
| 277 | |
| 278 | glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local); |
| 279 | glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside); |
| 280 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); |
| 281 | glEnable(GL_LIGHTING); |
| 282 | |
| 283 | glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient); |
| 284 | glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess); |
| 285 | glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular); |
| 286 | glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse); |
| 287 | |
| 288 | glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); |
| 289 | glEnable(GL_COLOR_MATERIAL); |
| 290 | glShadeModel(GL_SMOOTH); |
| 291 | } else { |
| 292 | glClearIndex(BACKGROUND); |
| 293 | glShadeModel(GL_FLAT); |
| 294 | } |
| 295 | |
| 296 | glMatrixMode(GL_PROJECTION); |
| 297 | gluPerspective(45, 1.33, 0.1, 100.0); |
| 298 | glMatrixMode(GL_MODELVIEW); |
| 299 | } |
| 300 | |
| 301 | void Reshape(int width, int height) |
| 302 | { |
| 303 | |
| 304 | glViewport(0, 0, width, height); |
| 305 | } |
| 306 | |
| 307 | void Key(unsigned char key, int x, int y) |
| 308 | { |
| 309 | |
| 310 | switch (key) { |
| 311 | case 27: |
| 312 | exit(1); |
| 313 | case 32: |
| 314 | ReInit(); |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | GLenum Args(int argc, char **argv) |
| 320 | { |
| 321 | GLint i; |
| 322 | |
| 323 | rgb = GL_TRUE; |
Gareth Hughes | ba58a66 | 2000-10-27 02:49:17 +0000 | [diff] [blame^] | 324 | doubleBuffer = GL_TRUE; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 325 | |
| 326 | for (i = 1; i < argc; i++) { |
| 327 | if (strcmp(argv[i], "-ci") == 0) { |
| 328 | rgb = GL_FALSE; |
| 329 | } else if (strcmp(argv[i], "-rgb") == 0) { |
| 330 | rgb = GL_TRUE; |
| 331 | } else if (strcmp(argv[i], "-sb") == 0) { |
| 332 | doubleBuffer = GL_FALSE; |
| 333 | } else if (strcmp(argv[i], "-db") == 0) { |
| 334 | doubleBuffer = GL_TRUE; |
| 335 | } else { |
| 336 | printf("%s (Bad option).\n", argv[i]); |
| 337 | return GL_FALSE; |
| 338 | } |
| 339 | } |
| 340 | return GL_TRUE; |
| 341 | } |
| 342 | |
| 343 | int main(int argc, char **argv) |
| 344 | { |
| 345 | GLenum type; |
| 346 | |
| 347 | glutInit(&argc, argv); |
| 348 | |
| 349 | if (Args(argc, argv) == GL_FALSE) { |
| 350 | exit(1); |
| 351 | } |
| 352 | |
| 353 | glutInitWindowPosition(0, 0); glutInitWindowSize( 400, 300); |
| 354 | |
| 355 | type = GLUT_DEPTH; |
| 356 | type |= (rgb) ? GLUT_RGB : GLUT_INDEX; |
| 357 | type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; |
| 358 | glutInitDisplayMode(type); |
| 359 | |
| 360 | if (glutCreateWindow("Olympic") == GL_FALSE) { |
| 361 | exit(1); |
| 362 | } |
| 363 | |
| 364 | InitMap(); |
| 365 | |
| 366 | Init(); |
| 367 | |
| 368 | glutReshapeFunc(Reshape); |
| 369 | glutKeyboardFunc(Key); |
| 370 | glutDisplayFunc(DrawScene); |
| 371 | glutIdleFunc(glut_post_redisplay_p); |
| 372 | |
| 373 | glutMainLoop(); |
| 374 | return 0; |
| 375 | } |