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.

583 lines
13 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 <unistd.h>
  40. #include <stdlib.h>
  41. #include "tk.h"
  42. #define PIXEL_CENTER(x) ((long)(x) + 0.5)
  43. #define GAP 10
  44. #define ROWS 3
  45. #define COLS 4
  46. #define OPENGL_WIDTH 48
  47. #define OPENGL_HEIGHT 13
  48. GLenum rgb, doubleBuffer, directRender, windType;
  49. GLint windW, windH;
  50. GLenum mode1, mode2;
  51. GLint boxW, boxH;
  52. GLubyte OpenGL_bits[] = {
  53. 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
  54. 0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01,
  55. 0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01,
  56. 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
  57. 0x3e, 0x8f, 0xb7, 0xf9, 0xfc, 0x01,
  58. 0x63, 0xdb, 0xb0, 0x8d, 0x0d, 0x00,
  59. 0x63, 0xdb, 0xb7, 0x8d, 0x0d, 0x00,
  60. 0x63, 0xdb, 0xb6, 0x8d, 0x0d, 0x00,
  61. 0x63, 0x8f, 0xf3, 0xcc, 0x0d, 0x00,
  62. 0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0a,
  63. 0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0e,
  64. 0x63, 0x00, 0x00, 0x8c, 0xed, 0x0e,
  65. 0x3e, 0x00, 0x00, 0xf8, 0x0c, 0x00,
  66. };
  67. static void Init(void)
  68. {
  69. mode1 = GL_TRUE;
  70. mode2 = GL_TRUE;
  71. }
  72. static void Reshape(int width, int height)
  73. {
  74. windW = (GLint)width;
  75. windH = (GLint)height;
  76. }
  77. static void RotateColorMask(void)
  78. {
  79. static GLint rotation = 0;
  80. rotation = (rotation + 1) & 0x3;
  81. switch (rotation) {
  82. case 0:
  83. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  84. glIndexMask( 0xff );
  85. break;
  86. case 1:
  87. glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
  88. glIndexMask(0xFE);
  89. break;
  90. case 2:
  91. glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
  92. glIndexMask(0xFD);
  93. break;
  94. case 3:
  95. glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
  96. glIndexMask(0xEF);
  97. break;
  98. }
  99. }
  100. static GLenum Key(int key, GLenum mask)
  101. {
  102. switch (key) {
  103. case TK_ESCAPE:
  104. tkQuit();
  105. case TK_1:
  106. mode1 = !mode1;
  107. break;
  108. case TK_2:
  109. mode2 = !mode2;
  110. break;
  111. case TK_3:
  112. RotateColorMask();
  113. break;
  114. default:
  115. return GL_FALSE;
  116. }
  117. return GL_TRUE;
  118. }
  119. static void Viewport(GLint row, GLint column)
  120. {
  121. GLint x, y;
  122. boxW = (windW - (COLS + 1) * GAP) / COLS;
  123. boxH = (windH - (ROWS + 1) * GAP) / ROWS;
  124. x = GAP + column * (boxW + GAP);
  125. y = GAP + row * (boxH + GAP);
  126. glViewport(x, y, boxW, boxH);
  127. glMatrixMode(GL_PROJECTION);
  128. glLoadIdentity();
  129. glOrtho(-boxW/2, boxW/2, -boxH/2, boxH/2, 0.0, 1.0);
  130. glMatrixMode(GL_MODELVIEW);
  131. glEnable(GL_SCISSOR_TEST);
  132. glScissor(x, y, boxW, boxH);
  133. }
  134. static void Point(void)
  135. {
  136. GLint i;
  137. glBegin(GL_POINTS);
  138. TK_SETCOLOR(windType, TK_WHITE);
  139. glVertex2i(0, 0);
  140. for (i = 1; i < 8; i++) {
  141. GLint j = i * 2;
  142. TK_SETCOLOR(windType, i + TK_RED - 1);
  143. glVertex2i(-j, -j);
  144. glVertex2i(-j, 0);
  145. glVertex2i(-j, j);
  146. glVertex2i(0, j);
  147. glVertex2i(j, j);
  148. glVertex2i(j, 0);
  149. glVertex2i(j, -j);
  150. glVertex2i(0, -j);
  151. }
  152. glEnd();
  153. }
  154. static void Lines(void)
  155. {
  156. GLint i;
  157. glPushMatrix();
  158. glTranslatef(-12, 0, 0);
  159. for (i = 1; i < 8; i++) {
  160. TK_SETCOLOR(windType, i + TK_RED - 1);
  161. glBegin(GL_LINES);
  162. glVertex2i(-boxW/4, -boxH/4);
  163. glVertex2i(boxW/4, boxH/4);
  164. glEnd();
  165. glTranslatef(4, 0, 0);
  166. }
  167. glPopMatrix();
  168. glBegin(GL_LINES);
  169. glVertex2i(0, 0);
  170. glEnd();
  171. }
  172. static void LineStrip(void)
  173. {
  174. glBegin(GL_LINE_STRIP);
  175. TK_SETCOLOR(windType, TK_RED);
  176. glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(-boxH/4));
  177. TK_SETCOLOR(windType, TK_GREEN);
  178. glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(boxH/4));
  179. TK_SETCOLOR(windType, TK_BLUE);
  180. glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(boxH/4));
  181. TK_SETCOLOR(windType, TK_WHITE);
  182. glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(-boxH/4));
  183. glEnd();
  184. glBegin(GL_LINE_STRIP);
  185. glVertex2i(0, 0);
  186. glEnd();
  187. }
  188. static void LineLoop(void)
  189. {
  190. glBegin(GL_LINE_LOOP);
  191. TK_SETCOLOR(windType, TK_RED);
  192. glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(-boxH/4));
  193. TK_SETCOLOR(windType, TK_GREEN);
  194. glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(boxH/4));
  195. TK_SETCOLOR(windType, TK_BLUE);
  196. glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(boxH/4));
  197. TK_SETCOLOR(windType, TK_WHITE);
  198. glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(-boxH/4));
  199. glEnd();
  200. glEnable(GL_LOGIC_OP);
  201. glLogicOp(GL_XOR);
  202. glEnable(GL_BLEND);
  203. glBlendFunc(GL_ONE, GL_ONE);
  204. TK_SETCOLOR(windType, TK_MAGENTA);
  205. glBegin(GL_LINE_LOOP);
  206. glVertex2f(PIXEL_CENTER(-boxW/8), PIXEL_CENTER(-boxH/8));
  207. glVertex2f(PIXEL_CENTER(-boxW/8), PIXEL_CENTER(boxH/8));
  208. glEnd();
  209. glBegin(GL_LINE_LOOP);
  210. glVertex2f(PIXEL_CENTER(-boxW/8), PIXEL_CENTER(boxH/8+5));
  211. glVertex2f(PIXEL_CENTER(boxW/8), PIXEL_CENTER(boxH/8+5));
  212. glEnd();
  213. glDisable(GL_LOGIC_OP);
  214. glDisable(GL_BLEND);
  215. TK_SETCOLOR(windType, TK_GREEN);
  216. glBegin(GL_POINTS);
  217. glVertex2i(0, 0);
  218. glEnd();
  219. glBegin(GL_LINE_LOOP);
  220. glVertex2i(0, 0);
  221. glEnd();
  222. }
  223. static void Bitmap(void)
  224. {
  225. glBegin(GL_LINES);
  226. TK_SETCOLOR(windType, TK_GREEN);
  227. glVertex2i(-boxW/2, 0);
  228. glVertex2i(boxW/2, 0);
  229. glVertex2i(0, -boxH/2);
  230. glVertex2i(0, boxH/2);
  231. TK_SETCOLOR(windType, TK_RED);
  232. glVertex2i(0, -3);
  233. glVertex2i(0, -3+OPENGL_HEIGHT);
  234. TK_SETCOLOR(windType, TK_BLUE);
  235. glVertex2i(0, -3);
  236. glVertex2i(OPENGL_WIDTH, -3);
  237. glEnd();
  238. TK_SETCOLOR(windType, TK_GREEN);
  239. glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE);
  240. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  241. glRasterPos2i(0, 0);
  242. glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, 0, 3, 0.0, 0.0, OpenGL_bits);
  243. }
  244. static void Triangles(void)
  245. {
  246. glBegin(GL_TRIANGLES);
  247. TK_SETCOLOR(windType, TK_GREEN);
  248. glVertex2i(-boxW/4, -boxH/4);
  249. TK_SETCOLOR(windType, TK_RED);
  250. glVertex2i(-boxW/8, -boxH/16);
  251. TK_SETCOLOR(windType, TK_BLUE);
  252. glVertex2i(boxW/8, -boxH/16);
  253. TK_SETCOLOR(windType, TK_GREEN);
  254. glVertex2i(-boxW/4, boxH/4);
  255. TK_SETCOLOR(windType, TK_RED);
  256. glVertex2i(-boxW/8, boxH/16);
  257. TK_SETCOLOR(windType, TK_BLUE);
  258. glVertex2i(boxW/8, boxH/16);
  259. glEnd();
  260. glBegin(GL_TRIANGLES);
  261. glVertex2i(0, 0);
  262. glVertex2i(-100, 100);
  263. glEnd();
  264. }
  265. static void TriangleStrip(void)
  266. {
  267. glBegin(GL_TRIANGLE_STRIP);
  268. TK_SETCOLOR(windType, TK_GREEN);
  269. glVertex2i(-boxW/4, -boxH/4);
  270. TK_SETCOLOR(windType, TK_RED);
  271. glVertex2i(-boxW/4, boxH/4);
  272. TK_SETCOLOR(windType, TK_BLUE);
  273. glVertex2i(0, -boxH/4);
  274. TK_SETCOLOR(windType, TK_WHITE);
  275. glVertex2i(0, boxH/4);
  276. TK_SETCOLOR(windType, TK_CYAN);
  277. glVertex2i(boxW/4, -boxH/4);
  278. TK_SETCOLOR(windType, TK_YELLOW);
  279. glVertex2i(boxW/4, boxH/4);
  280. glEnd();
  281. glBegin(GL_TRIANGLE_STRIP);
  282. glVertex2i(0, 0);
  283. glVertex2i(-100, 100);
  284. glEnd();
  285. }
  286. static void TriangleFan(void)
  287. {
  288. GLint vx[8][2];
  289. GLint x0, y0, x1, y1, x2, y2, x3, y3;
  290. GLint i;
  291. y0 = -boxH/4;
  292. y1 = y0 + boxH/2/3;
  293. y2 = y1 + boxH/2/3;
  294. y3 = boxH/4;
  295. x0 = -boxW/4;
  296. x1 = x0 + boxW/2/3;
  297. x2 = x1 + boxW/2/3;
  298. x3 = boxW/4;
  299. vx[0][0] = x0; vx[0][1] = y1;
  300. vx[1][0] = x0; vx[1][1] = y2;
  301. vx[2][0] = x1; vx[2][1] = y3;
  302. vx[3][0] = x2; vx[3][1] = y3;
  303. vx[4][0] = x3; vx[4][1] = y2;
  304. vx[5][0] = x3; vx[5][1] = y1;
  305. vx[6][0] = x2; vx[6][1] = y0;
  306. vx[7][0] = x1; vx[7][1] = y0;
  307. glBegin(GL_TRIANGLE_FAN);
  308. TK_SETCOLOR(windType, TK_WHITE);
  309. glVertex2i(0, 0);
  310. for (i = 0; i < 8; i++) {
  311. TK_SETCOLOR(windType, TK_WHITE-i);
  312. glVertex2iv(vx[i]);
  313. }
  314. glEnd();
  315. glBegin(GL_TRIANGLE_FAN);
  316. glVertex2i(0, 0);
  317. glVertex2i(-100, 100);
  318. glEnd();
  319. }
  320. static void Rect(void)
  321. {
  322. TK_SETCOLOR(windType, TK_GREEN);
  323. glRecti(-boxW/4, -boxH/4, boxW/4, boxH/4);
  324. }
  325. static void tprimPolygon(void)
  326. {
  327. GLint vx[8][2];
  328. GLint x0, y0, x1, y1, x2, y2, x3, y3;
  329. GLint i;
  330. y0 = -boxH/4;
  331. y1 = y0 + boxH/2/3;
  332. y2 = y1 + boxH/2/3;
  333. y3 = boxH/4;
  334. x0 = -boxW/4;
  335. x1 = x0 + boxW/2/3;
  336. x2 = x1 + boxW/2/3;
  337. x3 = boxW/4;
  338. vx[0][0] = x0; vx[0][1] = y1;
  339. vx[1][0] = x0; vx[1][1] = y2;
  340. vx[2][0] = x1; vx[2][1] = y3;
  341. vx[3][0] = x2; vx[3][1] = y3;
  342. vx[4][0] = x3; vx[4][1] = y2;
  343. vx[5][0] = x3; vx[5][1] = y1;
  344. vx[6][0] = x2; vx[6][1] = y0;
  345. vx[7][0] = x1; vx[7][1] = y0;
  346. glBegin(GL_POLYGON);
  347. for (i = 0; i < 8; i++) {
  348. TK_SETCOLOR(windType, TK_WHITE-i);
  349. glVertex2iv(vx[i]);
  350. }
  351. glEnd();
  352. glBegin(GL_POLYGON);
  353. glVertex2i(0, 0);
  354. glVertex2i(100, 100);
  355. glEnd();
  356. }
  357. static void Quads(void)
  358. {
  359. glBegin(GL_QUADS);
  360. TK_SETCOLOR(windType, TK_GREEN);
  361. glVertex2i(-boxW/4, -boxH/4);
  362. TK_SETCOLOR(windType, TK_RED);
  363. glVertex2i(-boxW/8, -boxH/16);
  364. TK_SETCOLOR(windType, TK_BLUE);
  365. glVertex2i(boxW/8, -boxH/16);
  366. TK_SETCOLOR(windType, TK_WHITE);
  367. glVertex2i(boxW/4, -boxH/4);
  368. TK_SETCOLOR(windType, TK_GREEN);
  369. glVertex2i(-boxW/4, boxH/4);
  370. TK_SETCOLOR(windType, TK_RED);
  371. glVertex2i(-boxW/8, boxH/16);
  372. TK_SETCOLOR(windType, TK_BLUE);
  373. glVertex2i(boxW/8, boxH/16);
  374. TK_SETCOLOR(windType, TK_WHITE);
  375. glVertex2i(boxW/4, boxH/4);
  376. glEnd();
  377. glBegin(GL_QUADS);
  378. glVertex2i(0, 0);
  379. glVertex2i(100, 100);
  380. glVertex2i(-100, 100);
  381. glEnd();
  382. }
  383. static void QuadStrip(void)
  384. {
  385. glBegin(GL_QUAD_STRIP);
  386. TK_SETCOLOR(windType, TK_GREEN);
  387. glVertex2i(-boxW/4, -boxH/4);
  388. TK_SETCOLOR(windType, TK_RED);
  389. glVertex2i(-boxW/4, boxH/4);
  390. TK_SETCOLOR(windType, TK_BLUE);
  391. glVertex2i(0, -boxH/4);
  392. TK_SETCOLOR(windType, TK_WHITE);
  393. glVertex2i(0, boxH/4);
  394. TK_SETCOLOR(windType, TK_CYAN);
  395. glVertex2i(boxW/4, -boxH/4);
  396. TK_SETCOLOR(windType, TK_YELLOW);
  397. glVertex2i(boxW/4, boxH/4);
  398. glEnd();
  399. glBegin(GL_QUAD_STRIP);
  400. glVertex2i(0, 0);
  401. glVertex2i(100, 100);
  402. glVertex2i(-100, 100);
  403. glEnd();
  404. }
  405. static void Draw(void)
  406. {
  407. glViewport(0, 0, windW, windH);
  408. glDisable(GL_SCISSOR_TEST);
  409. glPushAttrib(GL_COLOR_BUFFER_BIT);
  410. glColorMask(1, 1, 1, 1);
  411. glIndexMask(~0);
  412. glClearColor(0.0, 0.0, 0.0, 0.0);
  413. glClear(GL_COLOR_BUFFER_BIT);
  414. glPopAttrib();
  415. if (mode1) {
  416. glShadeModel(GL_SMOOTH);
  417. } else {
  418. glShadeModel(GL_FLAT);
  419. }
  420. if (mode2) {
  421. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  422. } else {
  423. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  424. }
  425. Viewport(0, 0); Point();
  426. Viewport(0, 1); Lines();
  427. Viewport(0, 2); LineStrip();
  428. Viewport(0, 3); LineLoop();
  429. Viewport(1, 0); Bitmap();
  430. Viewport(1, 1); TriangleFan();
  431. Viewport(1, 2); Triangles();
  432. Viewport(1, 3); TriangleStrip();
  433. Viewport(2, 0); Rect();
  434. Viewport(2, 1); tprimPolygon();
  435. Viewport(2, 2); Quads();
  436. Viewport(2, 3); QuadStrip();
  437. glFlush();
  438. if (doubleBuffer) {
  439. tkSwapBuffers();
  440. }
  441. }
  442. static GLenum Args(int argc, char **argv)
  443. {
  444. GLint i;
  445. rgb = GL_TRUE;
  446. doubleBuffer = GL_FALSE;
  447. directRender = GL_FALSE;
  448. for (i = 1; i < argc; i++) {
  449. if (strcmp(argv[i], "-ci") == 0) {
  450. rgb = GL_FALSE;
  451. } else if (strcmp(argv[i], "-rgb") == 0) {
  452. rgb = GL_TRUE;
  453. } else if (strcmp(argv[i], "-sb") == 0) {
  454. doubleBuffer = GL_FALSE;
  455. } else if (strcmp(argv[i], "-db") == 0) {
  456. doubleBuffer = GL_TRUE;
  457. } else if (strcmp(argv[i], "-dr") == 0) {
  458. directRender = GL_TRUE;
  459. } else if (strcmp(argv[i], "-ir") == 0) {
  460. directRender = GL_FALSE;
  461. } else {
  462. printf("%s (Bad option).\n", argv[i]);
  463. return GL_FALSE;
  464. }
  465. }
  466. return GL_TRUE;
  467. }
  468. void main(int argc, char **argv)
  469. {
  470. if (Args(argc, argv) == GL_FALSE) {
  471. tkQuit();
  472. }
  473. windW = 600;
  474. windH = 300;
  475. tkInitPosition(0, 0, windW, windH);
  476. windType = (rgb) ? TK_RGB : TK_INDEX;
  477. windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  478. windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  479. tkInitDisplayMode(windType);
  480. if (tkInitWindow("Primitive Test") == GL_FALSE) {
  481. tkQuit();
  482. }
  483. Init();
  484. tkExposeFunc(Reshape);
  485. tkReshapeFunc(Reshape);
  486. tkKeyDownFunc(Key);
  487. tkDisplayFunc(Draw);
  488. tkExec();
  489. }