Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

olympic.c 8.9KB

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