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.

tri.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <GL/glut.h>
  28. #define SOLID 1
  29. #define LINE 2
  30. #define POINT 3
  31. GLenum rgb, doubleBuffer, windType;
  32. GLint windW, windH;
  33. GLenum dithering = GL_TRUE;
  34. GLenum showVerticies = GL_TRUE;
  35. GLenum hideBottomTriangle = GL_FALSE;
  36. GLenum outline = GL_TRUE;
  37. GLenum culling = GL_FALSE;
  38. GLenum winding = GL_FALSE;
  39. GLenum face = GL_FALSE;
  40. GLenum state = SOLID;
  41. GLenum aaMode = GL_FALSE;
  42. GLenum shade = GL_TRUE;
  43. GLint color1, color2, color3;
  44. float zRotation = 90.0;
  45. float zoom = 1.0;
  46. float boxA[3] = {-100, -100, 0};
  47. float boxB[3] = { 100, -100, 0};
  48. float boxC[3] = { 100, 100, 0};
  49. float boxD[3] = {-100, 100, 0};
  50. float p0[3] = {-125,-80, 0};
  51. float p1[3] = {-125, 80, 0};
  52. float p2[3] = { 172, 0, 0};
  53. #include "tkmap.c"
  54. static void Init(void)
  55. {
  56. float r, g, b;
  57. float percent1, percent2;
  58. GLint i, j;
  59. glClearColor(0.0, 0.0, 0.0, 0.0);
  60. glLineStipple(1, 0xF0F0);
  61. glEnable(GL_SCISSOR_TEST);
  62. if (!rgb) {
  63. for (j = 0; j <= 12; j++) {
  64. if (j <= 6) {
  65. percent1 = j / 6.0;
  66. r = 1.0 - 0.8 * percent1;
  67. g = 0.2 + 0.8 * percent1;
  68. b = 0.2;
  69. } else {
  70. percent1 = (j - 6) / 6.0;
  71. r = 0.2;
  72. g = 1.0 - 0.8 * percent1;
  73. b = 0.2 + 0.8 * percent1;
  74. }
  75. glutSetColor(j+18, r, g, b);
  76. for (i = 0; i < 16; i++) {
  77. percent2 = i / 15.0;
  78. glutSetColor(j*16+1+32, r*percent2, g*percent2, b*percent2);
  79. }
  80. }
  81. color1 = 18;
  82. color2 = 24;
  83. color3 = 30;
  84. }
  85. }
  86. static void Reshape(int width, int height)
  87. {
  88. windW = (GLint)width;
  89. windH = (GLint)height;
  90. }
  91. static void Key2(int key, int x, int y)
  92. {
  93. switch (key) {
  94. case GLUT_KEY_LEFT:
  95. zRotation += 0.5;
  96. break;
  97. case GLUT_KEY_RIGHT:
  98. zRotation -= 0.5;
  99. break;
  100. default:
  101. return;
  102. }
  103. glutPostRedisplay();
  104. }
  105. static void Key(unsigned char key, int x, int y)
  106. {
  107. switch (key) {
  108. case 27:
  109. exit(1);
  110. case 'Z':
  111. zoom *= 0.75;
  112. break;
  113. case 'z':
  114. zoom /= 0.75;
  115. if (zoom > 10) {
  116. zoom = 10;
  117. }
  118. break;
  119. case '1':
  120. glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  121. break;
  122. case '2':
  123. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  124. break;
  125. case '3':
  126. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  127. break;
  128. case '4':
  129. state = POINT;
  130. break;
  131. case '5':
  132. state = LINE;
  133. break;
  134. case '6':
  135. state = SOLID;
  136. break;
  137. case '7':
  138. culling = !culling;
  139. break;
  140. case '8':
  141. winding = !winding;
  142. break;
  143. case '9':
  144. face = !face;
  145. break;
  146. case 'v':
  147. showVerticies = !showVerticies;
  148. break;
  149. case 's':
  150. shade = !shade;
  151. (shade) ? glShadeModel(GL_SMOOTH) : glShadeModel(GL_FLAT);
  152. break;
  153. case 'h':
  154. hideBottomTriangle = !hideBottomTriangle;
  155. break;
  156. case 'o':
  157. outline = !outline;
  158. break;
  159. case 'm':
  160. dithering = !dithering;
  161. break;
  162. case '0':
  163. aaMode = !aaMode;
  164. if (aaMode) {
  165. glEnable(GL_POLYGON_SMOOTH);
  166. glEnable(GL_BLEND);
  167. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  168. if (!rgb) {
  169. color1 = 32;
  170. color2 = 128;
  171. color3 = 224;
  172. }
  173. } else {
  174. glDisable(GL_POLYGON_SMOOTH);
  175. glDisable(GL_BLEND);
  176. if (!rgb) {
  177. color1 = 18;
  178. color2 = 24;
  179. color3 = 30;
  180. }
  181. }
  182. break;
  183. default:
  184. return;
  185. }
  186. glutPostRedisplay();
  187. }
  188. static void BeginPrim(void)
  189. {
  190. switch (state) {
  191. case SOLID:
  192. glBegin(GL_POLYGON);
  193. break;
  194. case LINE:
  195. glBegin(GL_LINE_LOOP);
  196. break;
  197. case POINT:
  198. glBegin(GL_POINTS);
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. static void EndPrim(void)
  205. {
  206. glEnd();
  207. }
  208. static void Draw(void)
  209. {
  210. float scaleX, scaleY;
  211. glViewport(0, 0, windW, windH);
  212. glMatrixMode(GL_PROJECTION);
  213. glLoadIdentity();
  214. gluOrtho2D(-175, 175, -175, 175);
  215. glMatrixMode(GL_MODELVIEW);
  216. glScissor(0, 0, windW, windH);
  217. (culling) ? glEnable(GL_CULL_FACE) : glDisable(GL_CULL_FACE);
  218. (winding) ? glFrontFace(GL_CCW) : glFrontFace(GL_CW);
  219. (face) ? glCullFace(GL_FRONT) : glCullFace(GL_BACK);
  220. (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  221. glClear(GL_COLOR_BUFFER_BIT);
  222. SetColor(COLOR_GREEN);
  223. glBegin(GL_LINE_LOOP);
  224. glVertex3fv(boxA);
  225. glVertex3fv(boxB);
  226. glVertex3fv(boxC);
  227. glVertex3fv(boxD);
  228. glEnd();
  229. if (!hideBottomTriangle) {
  230. glPushMatrix();
  231. glScalef(zoom, zoom, zoom);
  232. glRotatef(zRotation, 0, 0, 1);
  233. SetColor(COLOR_BLUE);
  234. BeginPrim();
  235. glVertex3fv(p0);
  236. glVertex3fv(p1);
  237. glVertex3fv(p2);
  238. EndPrim();
  239. if (showVerticies) {
  240. (rgb) ? glColor3fv(RGBMap[COLOR_RED]) : glIndexf(color1);
  241. glRectf(p0[0]-2, p0[1]-2, p0[0]+2, p0[1]+2);
  242. (rgb) ? glColor3fv(RGBMap[COLOR_GREEN]) : glIndexf(color2);
  243. glRectf(p1[0]-2, p1[1]-2, p1[0]+2, p1[1]+2);
  244. (rgb) ? glColor3fv(RGBMap[COLOR_BLUE]) : glIndexf(color3);
  245. glRectf(p2[0]-2, p2[1]-2, p2[0]+2, p2[1]+2);
  246. }
  247. glPopMatrix();
  248. }
  249. scaleX = (float)(windW - 20) / 2 / 175 * (175 - 100) + 10;
  250. scaleY = (float)(windH - 20) / 2 / 175 * (175 - 100) + 10;
  251. glViewport(scaleX, scaleY, windW-2*scaleX, windH-2*scaleY);
  252. glMatrixMode(GL_PROJECTION);
  253. glLoadIdentity();
  254. gluOrtho2D(-100, 100, -100, 100);
  255. glMatrixMode(GL_MODELVIEW);
  256. glScissor(scaleX, scaleY, windW-2*scaleX, windH-2*scaleY);
  257. glPushMatrix();
  258. glScalef(zoom, zoom, zoom);
  259. glRotatef(zRotation, 0,0,1);
  260. glPointSize(10);
  261. glLineWidth(5);
  262. glEnable(GL_POINT_SMOOTH);
  263. glEnable(GL_LINE_STIPPLE);
  264. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  265. SetColor(COLOR_RED);
  266. BeginPrim();
  267. (rgb) ? glColor3fv(RGBMap[COLOR_RED]) : glIndexf(color1);
  268. glVertex3fv(p0);
  269. (rgb) ? glColor3fv(RGBMap[COLOR_GREEN]) : glIndexf(color2);
  270. glVertex3fv(p1);
  271. (rgb) ? glColor3fv(RGBMap[COLOR_BLUE]) : glIndexf(color3);
  272. glVertex3fv(p2);
  273. EndPrim();
  274. glPointSize(1);
  275. glLineWidth(1);
  276. glDisable(GL_POINT_SMOOTH);
  277. glDisable(GL_LINE_STIPPLE);
  278. glBlendFunc(GL_ONE, GL_ZERO);
  279. if (outline) {
  280. SetColor(COLOR_WHITE);
  281. glBegin(GL_LINE_LOOP);
  282. glVertex3fv(p0);
  283. glVertex3fv(p1);
  284. glVertex3fv(p2);
  285. glEnd();
  286. }
  287. glPopMatrix();
  288. glFlush();
  289. if (doubleBuffer) {
  290. glutSwapBuffers();
  291. }
  292. }
  293. static GLenum Args(int argc, char **argv)
  294. {
  295. GLint i;
  296. rgb = GL_TRUE;
  297. doubleBuffer = GL_FALSE;
  298. for (i = 1; i < argc; i++) {
  299. if (strcmp(argv[i], "-ci") == 0) {
  300. rgb = GL_FALSE;
  301. } else if (strcmp(argv[i], "-rgb") == 0) {
  302. rgb = GL_TRUE;
  303. } else if (strcmp(argv[i], "-sb") == 0) {
  304. doubleBuffer = GL_FALSE;
  305. } else if (strcmp(argv[i], "-db") == 0) {
  306. doubleBuffer = GL_TRUE;
  307. } else {
  308. printf("%s (Bad option).\n", argv[i]);
  309. return GL_FALSE;
  310. }
  311. }
  312. return GL_TRUE;
  313. }
  314. int main(int argc, char **argv)
  315. {
  316. glutInit(&argc, argv);
  317. if (Args(argc, argv) == GL_FALSE) {
  318. exit(1);
  319. }
  320. windW = 600;
  321. windH = 300;
  322. glutInitWindowPosition(0, 0); glutInitWindowSize( windW, windH);
  323. windType = (rgb) ? GLUT_RGB : GLUT_INDEX;
  324. windType |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  325. glutInitDisplayMode(windType);
  326. if (glutCreateWindow("Triangle Test") == GL_FALSE) {
  327. exit(1);
  328. }
  329. InitMap();
  330. Init();
  331. glutReshapeFunc(Reshape);
  332. glutKeyboardFunc(Key);
  333. glutSpecialFunc(Key2);
  334. glutDisplayFunc(Draw);
  335. glutMainLoop();
  336. return 0;
  337. }