Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

olympic.c 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. goIdle = GL_TRUE;
  100. for (i = 0; i < RINGS; i++) {
  101. if (iters[i]) {
  102. for (j = 0; j < 3; j++) {
  103. offsets[i][j] = Clamp(iters[i], offsets[i][j]);
  104. }
  105. angs[i] = Clamp(iters[i], angs[i]);
  106. iters[i]--;
  107. goIdle = GL_FALSE;
  108. }
  109. }
  110. if (goIdle) {
  111. glutIdleFunc(NULL);
  112. }
  113. glPushMatrix();
  114. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  115. gluLookAt(0,0,10, 0,0,0, 0,1,0);
  116. for (i = 0; i < RINGS; i++) {
  117. if (rgb) {
  118. glColor3ubv(rgb_colors[i]);
  119. } else {
  120. glIndexi(mapped_colors[i]);
  121. }
  122. glPushMatrix();
  123. glTranslatef(dests[i][0]+offsets[i][0], dests[i][1]+offsets[i][1],
  124. dests[i][2]+offsets[i][2]);
  125. glRotatef(angs[i], rotAxis[i][0], rotAxis[i][1], rotAxis[i][2]);
  126. glCallList(theTorus);
  127. glPopMatrix();
  128. }
  129. glPopMatrix();
  130. glFlush();
  131. if (doubleBuffer) {
  132. glutSwapBuffers();
  133. }
  134. }
  135. float MyRand(void)
  136. {
  137. return 10.0 * ( (float) rand() / (float) RAND_MAX - 0.5 );
  138. }
  139. void GLUTCALLBACK glut_post_redisplay_p(void)
  140. {
  141. glutPostRedisplay();
  142. }
  143. void ReInit(void)
  144. {
  145. int i;
  146. float deviation;
  147. deviation = MyRand() / 2;
  148. deviation = deviation * deviation;
  149. for (i = 0; i < RINGS; i++) {
  150. offsets[i][0] = MyRand();
  151. offsets[i][1] = MyRand();
  152. offsets[i][2] = MyRand();
  153. angs[i] = 260.0 * MyRand();
  154. rotAxis[i][0] = MyRand();
  155. rotAxis[i][1] = MyRand();
  156. rotAxis[i][2] = MyRand();
  157. iters[i] = (deviation * MyRand() + 60.0);
  158. }
  159. glutIdleFunc(glut_post_redisplay_p);
  160. }
  161. void Init(void)
  162. {
  163. float base, height;
  164. float aspect, x, y;
  165. int i;
  166. float top_y = 1.0;
  167. float bottom_y = 0.0;
  168. float top_z = 0.15;
  169. float bottom_z = 0.69;
  170. float spacing = 2.5;
  171. static float lmodel_ambient[] = {0.0, 0.0, 0.0, 0.0};
  172. static float lmodel_twoside[] = {GL_FALSE};
  173. static float lmodel_local[] = {GL_FALSE};
  174. static float light0_ambient[] = {0.1, 0.1, 0.1, 1.0};
  175. static float light0_diffuse[] = {1.0, 1.0, 1.0, 0.0};
  176. static float light0_position[] = {0.8660254, 0.5, 1, 0};
  177. static float light0_specular[] = {1.0, 1.0, 1.0, 0.0};
  178. static float bevel_mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  179. static float bevel_mat_shininess[] = {40.0};
  180. static float bevel_mat_specular[] = {1.0, 1.0, 1.0, 0.0};
  181. static float bevel_mat_diffuse[] = {1.0, 0.0, 0.0, 0.0};
  182. srand( (unsigned int) glutGet(GLUT_ELAPSED_TIME) );
  183. ReInit();
  184. for (i = 0; i < RINGS; i++) {
  185. rgb_colors[i][0] = rgb_colors[i][1] = rgb_colors[i][2] = 0;
  186. }
  187. rgb_colors[BLUERING][2] = 255;
  188. rgb_colors[REDRING][0] = 255;
  189. rgb_colors[GREENRING][1] = 255;
  190. rgb_colors[YELLOWRING][0] = 255;
  191. rgb_colors[YELLOWRING][1] = 255;
  192. mapped_colors[BLUERING] = COLOR_BLUE;
  193. mapped_colors[REDRING] = COLOR_RED;
  194. mapped_colors[GREENRING] = COLOR_GREEN;
  195. mapped_colors[YELLOWRING] = COLOR_YELLOW;
  196. mapped_colors[BLACKRING] = COLOR_BLACK;
  197. dests[BLUERING][0] = -spacing;
  198. dests[BLUERING][1] = top_y;
  199. dests[BLUERING][2] = top_z;
  200. dests[BLACKRING][0] = 0.0;
  201. dests[BLACKRING][1] = top_y;
  202. dests[BLACKRING][2] = top_z;
  203. dests[REDRING][0] = spacing;
  204. dests[REDRING][1] = top_y;
  205. dests[REDRING][2] = top_z;
  206. dests[YELLOWRING][0] = -spacing / 2.0;
  207. dests[YELLOWRING][1] = bottom_y;
  208. dests[YELLOWRING][2] = bottom_z;
  209. dests[GREENRING][0] = spacing / 2.0;
  210. dests[GREENRING][1] = bottom_y;
  211. dests[GREENRING][2] = bottom_z;
  212. base = 2.0;
  213. height = 2.0;
  214. theTorus = glGenLists(1);
  215. glNewList(theTorus, GL_COMPILE);
  216. FillTorus(0.1, 8, 1.0, 25);
  217. glEndList();
  218. x = (float)XSIZE;
  219. y = (float)YSIZE;
  220. aspect = x / y;
  221. glEnable(GL_CULL_FACE);
  222. glCullFace(GL_BACK);
  223. glEnable(GL_DEPTH_TEST);
  224. glClearDepth(1.0);
  225. if (rgb) {
  226. glClearColor(0.5, 0.5, 0.5, 0.0);
  227. glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  228. glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  229. glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
  230. glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  231. glEnable(GL_LIGHT0);
  232. glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local);
  233. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  234. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  235. glEnable(GL_LIGHTING);
  236. glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient);
  237. glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess);
  238. glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular);
  239. glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse);
  240. glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  241. glEnable(GL_COLOR_MATERIAL);
  242. glShadeModel(GL_SMOOTH);
  243. } else {
  244. glClearIndex(BACKGROUND);
  245. glShadeModel(GL_FLAT);
  246. }
  247. glMatrixMode(GL_PROJECTION);
  248. gluPerspective(45, 1.33, 0.1, 100.0);
  249. glMatrixMode(GL_MODELVIEW);
  250. }
  251. void Reshape(int width, int height)
  252. {
  253. glViewport(0, 0, width, height);
  254. }
  255. void Key(unsigned char key, int x, int y)
  256. {
  257. switch (key) {
  258. case 27:
  259. exit(1);
  260. case 32:
  261. ReInit();
  262. break;
  263. }
  264. }
  265. GLenum Args(int argc, char **argv)
  266. {
  267. GLint i;
  268. rgb = GL_TRUE;
  269. doubleBuffer = GL_TRUE;
  270. for (i = 1; i < argc; i++) {
  271. if (strcmp(argv[i], "-ci") == 0) {
  272. rgb = GL_FALSE;
  273. } else if (strcmp(argv[i], "-rgb") == 0) {
  274. rgb = GL_TRUE;
  275. } else if (strcmp(argv[i], "-sb") == 0) {
  276. doubleBuffer = GL_FALSE;
  277. } else if (strcmp(argv[i], "-db") == 0) {
  278. doubleBuffer = GL_TRUE;
  279. } else {
  280. printf("%s (Bad option).\n", argv[i]);
  281. return GL_FALSE;
  282. }
  283. }
  284. return GL_TRUE;
  285. }
  286. int main(int argc, char **argv)
  287. {
  288. GLenum type;
  289. glutInit(&argc, argv);
  290. if (Args(argc, argv) == GL_FALSE) {
  291. exit(1);
  292. }
  293. glutInitWindowPosition(0, 0); glutInitWindowSize( 400, 300);
  294. type = GLUT_DEPTH;
  295. type |= (rgb) ? GLUT_RGB : GLUT_INDEX;
  296. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  297. glutInitDisplayMode(type);
  298. if (glutCreateWindow("Olympic") == GL_FALSE) {
  299. exit(1);
  300. }
  301. InitMap();
  302. Init();
  303. glutReshapeFunc(Reshape);
  304. glutKeyboardFunc(Key);
  305. glutDisplayFunc(DrawScene);
  306. glutIdleFunc(glut_post_redisplay_p);
  307. glutMainLoop();
  308. return 0;
  309. }