Source code of Windows XP (NT5)
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.

402 lines
9.6 KiB

  1. /*
  2. * (c) Copyright 1993, Silicon Graphics, Inc.
  3. * ALL RIGHTS RESERVED
  4. * Permission to use, copy, modify, and distribute this software for
  5. * any purpose and without fee is hereby granted, provided that the above
  6. * copyright notice appear in all copies and that both the copyright notice
  7. * and this permission notice appear in supporting documentation, and that
  8. * the name of Silicon Graphics, Inc. not be used in advertising
  9. * or publicity pertaining to distribution of the software without specific,
  10. * written prior permission.
  11. *
  12. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  16. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  21. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * US Government Users Restricted Rights
  26. * Use, duplication, or disclosure by the Government is subject to
  27. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. * clause at DFARS 252.227-7013 and/or in similar or successor
  30. * clauses in the FAR or the DOD or NASA FAR Supplement.
  31. * Unpublished-- rights reserved under the copyright laws of the
  32. * United States. Contractor/manufacturer is Silicon Graphics,
  33. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  34. *
  35. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36. */
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <math.h>
  40. #include <sys/types.h>
  41. #ifdef __unix
  42. #include <sys/time.h>
  43. #else
  44. #include <stdlib.h>
  45. #include <time.h>
  46. #endif
  47. #include "tk.h"
  48. #ifdef __unix
  49. extern double drand48(void);
  50. extern void srand48(long seedval);
  51. #endif
  52. #define XSIZE 100
  53. #define YSIZE 75
  54. #define RINGS 5
  55. #define BLUERING 0
  56. #define BLACKRING 1
  57. #define REDRING 2
  58. #define YELLOWRING 3
  59. #define GREENRING 4
  60. #define BACKGROUND 8
  61. enum {
  62. BLACK = 0,
  63. RED,
  64. GREEN,
  65. YELLOW,
  66. BLUE,
  67. MAGENTA,
  68. CYAN,
  69. WHITE
  70. };
  71. typedef short Point[2];
  72. GLenum rgb, doubleBuffer, directRender;
  73. unsigned char rgb_colors[RINGS][3];
  74. int mapped_colors[RINGS];
  75. float dests[RINGS][3];
  76. float offsets[RINGS][3];
  77. float angs[RINGS];
  78. float rotAxis[RINGS][3];
  79. int iters[RINGS];
  80. GLuint theTorus;
  81. void FillTorus(float rc, int numc, float rt, int numt)
  82. {
  83. int i, j, k;
  84. double s, t;
  85. double x, y, z;
  86. double pi, twopi;
  87. pi = 3.14159265358979323846;
  88. twopi = 2 * pi;
  89. for (i = 0; i < numc; i++) {
  90. glBegin(GL_QUAD_STRIP);
  91. for (j = 0; j <= numt; j++) {
  92. for (k = 1; k >= 0; k--) {
  93. s = (i + k) % numc + 0.5;
  94. t = j % numt;
  95. x = cos(t*twopi/numt) * cos(s*twopi/numc);
  96. y = sin(t*twopi/numt) * cos(s*twopi/numc);
  97. z = sin(s*twopi/numc);
  98. glNormal3f(x, y, z);
  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. float Clamp(int iters_left, float t)
  109. {
  110. if (iters_left < 3) {
  111. return 0.0;
  112. }
  113. return (iters_left-2)*t/iters_left;
  114. }
  115. void DrawScene(void)
  116. {
  117. int i, j;
  118. for (i = 0; i < RINGS; i++) {
  119. if (iters[i]) {
  120. for (j = 0; j < 3; j++) {
  121. offsets[i][j] = Clamp(iters[i], offsets[i][j]);
  122. }
  123. angs[i] = Clamp(iters[i], angs[i]);
  124. iters[i]--;
  125. }
  126. }
  127. glPushMatrix();
  128. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  129. gluLookAt(0,0,10, 0,0,0, 0,1,0);
  130. for (i = 0; i < RINGS; i++) {
  131. if (rgb) {
  132. glColor3ubv(rgb_colors[i]);
  133. } else {
  134. glIndexi(mapped_colors[i]);
  135. }
  136. glPushMatrix();
  137. glTranslatef(dests[i][0]+offsets[i][0], dests[i][1]+offsets[i][1],
  138. dests[i][2]+offsets[i][2]);
  139. glRotatef(angs[i], rotAxis[i][0], rotAxis[i][1], rotAxis[i][2]);
  140. glCallList(theTorus);
  141. glPopMatrix();
  142. }
  143. glPopMatrix();
  144. glFlush();
  145. if (doubleBuffer) {
  146. tkSwapBuffers();
  147. }
  148. }
  149. float MyRand(void)
  150. {
  151. #ifdef __unix
  152. return 10.0 * (drand48() - 0.5);
  153. #else
  154. return 10.0 * ( ((float) rand())/((float) RAND_MAX) - 0.5);
  155. #endif
  156. }
  157. void ReInit(void)
  158. {
  159. int i;
  160. float deviation;
  161. deviation = MyRand() / 2;
  162. deviation = deviation * deviation;
  163. for (i = 0; i < RINGS; i++) {
  164. offsets[i][0] = MyRand();
  165. offsets[i][1] = MyRand();
  166. offsets[i][2] = MyRand();
  167. angs[i] = 260.0 * MyRand();
  168. rotAxis[i][0] = MyRand();
  169. rotAxis[i][1] = MyRand();
  170. rotAxis[i][2] = MyRand();
  171. iters[i] = (deviation * MyRand() + 60.0);
  172. }
  173. }
  174. void Init(void)
  175. {
  176. int gid;
  177. float base, height;
  178. float aspect, x, y;
  179. int i;
  180. #ifdef __unix
  181. struct timeval t;
  182. struct timezone tz;
  183. #else
  184. time_t timer, timerr;
  185. #endif
  186. float sc = 10;
  187. float top_y = 1.0;
  188. float bottom_y = 0.0;
  189. float top_z = 0.15;
  190. float bottom_z = 0.69;
  191. float spacing = 2.5;
  192. static float lmodel_ambient[] = {0.0, 0.0, 0.0, 0.0};
  193. static float lmodel_twoside[] = {GL_FALSE};
  194. static float lmodel_local[] = {GL_FALSE};
  195. static float light0_ambient[] = {0.1, 0.1, 0.1, 1.0};
  196. static float light0_diffuse[] = {1.0, 1.0, 1.0, 0.0};
  197. static float light0_position[] = {0.8660254, 0.5, 1, 0};
  198. static float light0_specular[] = {1.0, 1.0, 1.0, 0.0};
  199. static float bevel_mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  200. static float bevel_mat_shininess[] = {40.0};
  201. static float bevel_mat_specular[] = {1.0, 1.0, 1.0, 0.0};
  202. static float bevel_mat_diffuse[] = {1.0, 0.0, 0.0, 0.0};
  203. #ifdef __unix
  204. gettimeofday(&t, &tz);
  205. srand48(t.tv_usec);
  206. #else
  207. time( &timer );
  208. srand( timer );
  209. #endif
  210. ReInit();
  211. for (i = 0; i < RINGS; i++) {
  212. rgb_colors[i][0] = rgb_colors[i][1] = rgb_colors[i][2] = 0;
  213. }
  214. rgb_colors[BLUERING][2] = 255;
  215. rgb_colors[REDRING][0] = 255;
  216. rgb_colors[GREENRING][1] = 255;
  217. rgb_colors[YELLOWRING][0] = 255;
  218. rgb_colors[YELLOWRING][1] = 255;
  219. mapped_colors[BLUERING] = BLUE;
  220. mapped_colors[REDRING] = RED;
  221. mapped_colors[GREENRING] = GREEN;
  222. mapped_colors[YELLOWRING] = YELLOW;
  223. mapped_colors[BLACKRING] = BLACK;
  224. dests[BLUERING][0] = -spacing;
  225. dests[BLUERING][1] = top_y;
  226. dests[BLUERING][2] = top_z;
  227. dests[BLACKRING][0] = 0.0;
  228. dests[BLACKRING][1] = top_y;
  229. dests[BLACKRING][2] = top_z;
  230. dests[REDRING][0] = spacing;
  231. dests[REDRING][1] = top_y;
  232. dests[REDRING][2] = top_z;
  233. dests[YELLOWRING][0] = -spacing / 2.0;
  234. dests[YELLOWRING][1] = bottom_y;
  235. dests[YELLOWRING][2] = bottom_z;
  236. dests[GREENRING][0] = spacing / 2.0;
  237. dests[GREENRING][1] = bottom_y;
  238. dests[GREENRING][2] = bottom_z;
  239. base = 2.0;
  240. height = 2.0;
  241. theTorus = glGenLists(1);
  242. glNewList(theTorus, GL_COMPILE);
  243. FillTorus(0.1, 8, 1.0, 25);
  244. glEndList();
  245. x = (float)XSIZE;
  246. y = (float)YSIZE;
  247. aspect = x / y;
  248. glEnable(GL_CULL_FACE);
  249. glCullFace(GL_BACK);
  250. glEnable(GL_DEPTH_TEST);
  251. glClearDepth(1.0);
  252. if (rgb) {
  253. glClearColor(0.5, 0.5, 0.5, 0.0);
  254. glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  255. glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  256. glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
  257. glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  258. glEnable(GL_LIGHT0);
  259. glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local);
  260. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  261. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  262. glEnable(GL_LIGHTING);
  263. glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient);
  264. glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess);
  265. glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular);
  266. glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse);
  267. glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  268. glEnable(GL_COLOR_MATERIAL);
  269. glShadeModel(GL_SMOOTH);
  270. } else {
  271. glClearIndex(BACKGROUND);
  272. glShadeModel(GL_FLAT);
  273. }
  274. glMatrixMode(GL_PROJECTION);
  275. gluPerspective(45, 1.33, 0.1, 100.0);
  276. glMatrixMode(GL_MODELVIEW);
  277. }
  278. void Reshape(int width, int height)
  279. {
  280. glViewport(0, 0, width, height);
  281. }
  282. GLenum Key(int key, GLenum mask)
  283. {
  284. switch (key) {
  285. case TK_ESCAPE:
  286. tkQuit();
  287. case TK_SPACE:
  288. ReInit();
  289. break;
  290. default:
  291. return GL_FALSE;
  292. }
  293. return GL_TRUE;
  294. }
  295. GLenum Args(int argc, char **argv)
  296. {
  297. GLint i;
  298. rgb = GL_TRUE;
  299. doubleBuffer = GL_TRUE;
  300. directRender = GL_FALSE;
  301. for (i = 1; i < argc; i++) {
  302. if (strcmp(argv[i], "-ci") == 0) {
  303. rgb = GL_FALSE;
  304. } else if (strcmp(argv[i], "-rgb") == 0) {
  305. rgb = GL_TRUE;
  306. } else if (strcmp(argv[i], "-sb") == 0) {
  307. doubleBuffer = GL_FALSE;
  308. } else if (strcmp(argv[i], "-db") == 0) {
  309. doubleBuffer = GL_TRUE;
  310. } else if (strcmp(argv[i], "-dr") == 0) {
  311. directRender = GL_TRUE;
  312. } else if (strcmp(argv[i], "-ir") == 0) {
  313. directRender = GL_FALSE;
  314. } else {
  315. printf("%s (Bad option).\n", argv[i]);
  316. return GL_FALSE;
  317. }
  318. }
  319. return GL_TRUE;
  320. }
  321. void main(int argc, char **argv)
  322. {
  323. GLenum type;
  324. if (Args(argc, argv) == GL_FALSE) {
  325. tkQuit();
  326. }
  327. tkInitPosition(10, 30, 400, 300);
  328. type = (rgb) ? TK_RGB : TK_INDEX;
  329. type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  330. type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  331. type |= TK_DEPTH16;
  332. tkInitDisplayMode(type);
  333. if (tkInitWindow("Olympic") == GL_FALSE) {
  334. tkQuit();
  335. }
  336. Init();
  337. tkExposeFunc(Reshape);
  338. tkReshapeFunc(Reshape);
  339. tkKeyDownFunc(Key);
  340. tkIdleFunc(DrawScene);
  341. tkExec();
  342. }