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.

401 lines
8.9 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 <stdlib.h>
  40. #include "tk.h"
  41. #define SOLID 1
  42. #define LINE 2
  43. #define POINT 3
  44. GLenum rgb, doubleBuffer, directRender, windType;
  45. GLint windW, windH;
  46. GLenum dithering = GL_TRUE;
  47. GLenum showVerticies = GL_TRUE;
  48. GLenum hideBottomTriangle = GL_FALSE;
  49. GLenum outline = GL_TRUE;
  50. GLenum culling = GL_FALSE;
  51. GLenum winding = GL_FALSE;
  52. GLenum face = GL_FALSE;
  53. GLenum state = SOLID;
  54. GLenum aaMode = GL_FALSE;
  55. GLenum shade = GL_TRUE;
  56. GLint color1, color2, color3;
  57. float zRotation = 90.0;
  58. float zoom = 1.0;
  59. float boxA[3] = {-100, -100, 0};
  60. float boxB[3] = { 100, -100, 0};
  61. float boxC[3] = { 100, 100, 0};
  62. float boxD[3] = {-100, 100, 0};
  63. float p0[3] = {-125,-80, 0};
  64. float p1[3] = {-125, 80, 0};
  65. float p2[3] = { 172, 0, 0};
  66. static void Init(void)
  67. {
  68. float r, g, b;
  69. float percent1, percent2;
  70. GLint i, j;
  71. glClearColor(0.0, 0.0, 0.0, 0.0);
  72. glLineStipple(1, 0xF0F0);
  73. glEnable(GL_SCISSOR_TEST);
  74. if (!rgb) {
  75. for (j = 0; j <= 12; j++) {
  76. if (j <= 6) {
  77. percent1 = j / 6.0;
  78. r = 1.0 - 0.8 * percent1;
  79. g = 0.2 + 0.8 * percent1;
  80. b = 0.2;
  81. } else {
  82. percent1 = (j - 6) / 6.0;
  83. r = 0.2;
  84. g = 1.0 - 0.8 * percent1;
  85. b = 0.2 + 0.8 * percent1;
  86. }
  87. tkSetOneColor(j+18, r, g, b);
  88. for (i = 0; i < 16; i++) {
  89. percent2 = i / 15.0;
  90. tkSetOneColor(j*16+1+32, r*percent2, g*percent2, b*percent2);
  91. }
  92. }
  93. color1 = 18;
  94. color2 = 24;
  95. color3 = 30;
  96. }
  97. }
  98. static void Reshape(int width, int height)
  99. {
  100. windW = (GLint)width;
  101. windH = (GLint)height;
  102. }
  103. static GLenum Key(int key, GLenum mask)
  104. {
  105. switch (key) {
  106. case TK_ESCAPE:
  107. tkQuit();
  108. case TK_LEFT:
  109. zRotation += 0.5;
  110. break;
  111. case TK_RIGHT:
  112. zRotation -= 0.5;
  113. break;
  114. case TK_Z:
  115. zoom *= 0.75;
  116. break;
  117. case TK_z:
  118. zoom /= 0.75;
  119. if (zoom > 10) {
  120. zoom = 10;
  121. }
  122. break;
  123. case TK_1:
  124. glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  125. break;
  126. case TK_2:
  127. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  128. break;
  129. case TK_3:
  130. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  131. break;
  132. case TK_4:
  133. state = POINT;
  134. break;
  135. case TK_5:
  136. state = LINE;
  137. break;
  138. case TK_6:
  139. state = SOLID;
  140. break;
  141. case TK_7:
  142. culling = !culling;
  143. break;
  144. case TK_8:
  145. winding = !winding;
  146. break;
  147. case TK_9:
  148. face = !face;
  149. break;
  150. case TK_v:
  151. showVerticies = !showVerticies;
  152. break;
  153. case TK_s:
  154. shade = !shade;
  155. (shade) ? glShadeModel(GL_SMOOTH) : glShadeModel(GL_FLAT);
  156. break;
  157. case TK_h:
  158. hideBottomTriangle = !hideBottomTriangle;
  159. break;
  160. case TK_o:
  161. outline = !outline;
  162. break;
  163. case TK_m:
  164. dithering = !dithering;
  165. break;
  166. case TK_0:
  167. aaMode = !aaMode;
  168. if (aaMode) {
  169. glEnable(GL_POLYGON_SMOOTH);
  170. glEnable(GL_BLEND);
  171. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  172. if (!rgb) {
  173. color1 = 32;
  174. color2 = 128;
  175. color3 = 224;
  176. }
  177. } else {
  178. glDisable(GL_POLYGON_SMOOTH);
  179. glDisable(GL_BLEND);
  180. if (!rgb) {
  181. color1 = 18;
  182. color2 = 24;
  183. color3 = 30;
  184. }
  185. }
  186. break;
  187. default:
  188. return GL_FALSE;
  189. }
  190. return GL_TRUE;
  191. }
  192. static void BeginPrim(void)
  193. {
  194. switch (state) {
  195. case SOLID:
  196. glBegin(GL_POLYGON);
  197. break;
  198. case LINE:
  199. glBegin(GL_LINE_LOOP);
  200. break;
  201. case POINT:
  202. glBegin(GL_POINTS);
  203. break;
  204. }
  205. }
  206. static void EndPrim(void)
  207. {
  208. glEnd();
  209. }
  210. static void Draw(void)
  211. {
  212. float scaleX, scaleY;
  213. glViewport(0, 0, windW, windH);
  214. glMatrixMode(GL_PROJECTION);
  215. glLoadIdentity();
  216. gluOrtho2D(-175, 175, -175, 175);
  217. glMatrixMode(GL_MODELVIEW);
  218. glScissor(0, 0, windW, windH);
  219. (culling) ? glEnable(GL_CULL_FACE) : glDisable(GL_CULL_FACE);
  220. (winding) ? glFrontFace(GL_CCW) : glFrontFace(GL_CW);
  221. (face) ? glCullFace(GL_FRONT) : glCullFace(GL_BACK);
  222. (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  223. glClear(GL_COLOR_BUFFER_BIT);
  224. TK_SETCOLOR(windType, TK_GREEN);
  225. glBegin(GL_LINE_LOOP);
  226. glVertex3fv(boxA);
  227. glVertex3fv(boxB);
  228. glVertex3fv(boxC);
  229. glVertex3fv(boxD);
  230. glEnd();
  231. if (!hideBottomTriangle) {
  232. glPushMatrix();
  233. glScalef(zoom, zoom, zoom);
  234. glRotatef(zRotation, 0, 0, 1);
  235. TK_SETCOLOR(windType, TK_BLUE);
  236. BeginPrim();
  237. glVertex3fv(p0);
  238. glVertex3fv(p1);
  239. glVertex3fv(p2);
  240. EndPrim();
  241. if (showVerticies) {
  242. (rgb) ? glColor3fv(tkRGBMap[TK_RED]) : glIndexf(color1);
  243. glRectf(p0[0]-2, p0[1]-2, p0[0]+2, p0[1]+2);
  244. (rgb) ? glColor3fv(tkRGBMap[TK_GREEN]) : glIndexf(color2);
  245. glRectf(p1[0]-2, p1[1]-2, p1[0]+2, p1[1]+2);
  246. (rgb) ? glColor3fv(tkRGBMap[TK_BLUE]) : glIndexf(color3);
  247. glRectf(p2[0]-2, p2[1]-2, p2[0]+2, p2[1]+2);
  248. }
  249. glPopMatrix();
  250. }
  251. scaleX = (float)(windW - 20) / 2 / 175 * (175 - 100) + 10;
  252. scaleY = (float)(windH - 20) / 2 / 175 * (175 - 100) + 10;
  253. glViewport(scaleX, scaleY, windW-2*scaleX, windH-2*scaleY);
  254. glMatrixMode(GL_PROJECTION);
  255. glLoadIdentity();
  256. gluOrtho2D(-100, 100, -100, 100);
  257. glMatrixMode(GL_MODELVIEW);
  258. glScissor(scaleX, scaleY, windW-2*scaleX, windH-2*scaleY);
  259. glPushMatrix();
  260. glScalef(zoom, zoom, zoom);
  261. glRotatef(zRotation, 0,0,1);
  262. glPointSize(10);
  263. glLineWidth(5);
  264. glEnable(GL_POINT_SMOOTH);
  265. glEnable(GL_LINE_STIPPLE);
  266. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  267. TK_SETCOLOR(windType, TK_RED);
  268. BeginPrim();
  269. (rgb) ? glColor3fv(tkRGBMap[TK_RED]) : glIndexf(color1);
  270. glVertex3fv(p0);
  271. (rgb) ? glColor3fv(tkRGBMap[TK_GREEN]) : glIndexf(color2);
  272. glVertex3fv(p1);
  273. (rgb) ? glColor3fv(tkRGBMap[TK_BLUE]) : glIndexf(color3);
  274. glVertex3fv(p2);
  275. EndPrim();
  276. glPointSize(1);
  277. glLineWidth(1);
  278. glDisable(GL_POINT_SMOOTH);
  279. glDisable(GL_LINE_STIPPLE);
  280. glBlendFunc(GL_ONE, GL_ZERO);
  281. if (outline) {
  282. TK_SETCOLOR(windType, TK_WHITE);
  283. glBegin(GL_LINE_LOOP);
  284. glVertex3fv(p0);
  285. glVertex3fv(p1);
  286. glVertex3fv(p2);
  287. glEnd();
  288. }
  289. glPopMatrix();
  290. glFlush();
  291. if (doubleBuffer) {
  292. tkSwapBuffers();
  293. }
  294. }
  295. static GLenum Args(int argc, char **argv)
  296. {
  297. GLint i;
  298. rgb = GL_TRUE;
  299. doubleBuffer = GL_FALSE;
  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. if (Args(argc, argv) == GL_FALSE) {
  324. tkQuit();
  325. }
  326. windW = 600;
  327. windH = 300;
  328. tkInitPosition(0, 0, windW, windH);
  329. windType = (rgb) ? TK_RGB : TK_INDEX;
  330. windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  331. windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  332. tkInitDisplayMode(windType);
  333. if (tkInitWindow("Triangle Test") == GL_FALSE) {
  334. tkQuit();
  335. }
  336. Init();
  337. tkExposeFunc(Reshape);
  338. tkReshapeFunc(Reshape);
  339. tkKeyDownFunc(Key);
  340. tkDisplayFunc(Draw);
  341. tkExec();
  342. }