Clone of mesa.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tessdemo.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* $Id: tessdemo.c,v 1.8 2000/07/11 14:11:58 brianp Exp $ */
  2. /*
  3. * A demo of the GLU polygon tesselation functions written by Bogdan Sikorski.
  4. */
  5. #include <GL/glut.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define MAX_POINTS 200
  10. #define MAX_CONTOURS 50
  11. static int menu;
  12. typedef enum
  13. { QUIT, TESSELATE, CLEAR }
  14. menu_entries;
  15. typedef enum
  16. { DEFINE, TESSELATED }
  17. mode_type;
  18. struct
  19. {
  20. GLint p[MAX_POINTS][2];
  21. GLuint point_cnt;
  22. }
  23. contours[MAX_CONTOURS];
  24. static GLuint contour_cnt;
  25. static GLsizei width, height;
  26. static mode_type mode;
  27. struct
  28. {
  29. GLsizei no;
  30. GLfloat color[3];
  31. GLint p[3][2];
  32. GLclampf p_color[3][3];
  33. }
  34. triangle;
  35. static void GLCALLBACK
  36. my_error(GLenum err)
  37. {
  38. int len, i;
  39. char const *str;
  40. glColor3f(0.9, 0.9, 0.9);
  41. glRasterPos2i(5, 5);
  42. str = (const char *) gluErrorString(err);
  43. len = strlen(str);
  44. for (i = 0; i < len; i++)
  45. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, str[i]);
  46. }
  47. static void GLCALLBACK
  48. begin_callback(GLenum mode)
  49. {
  50. triangle.no = 0;
  51. }
  52. static void GLCALLBACK
  53. edge_callback(GLenum flag)
  54. {
  55. if (flag == GL_TRUE) {
  56. triangle.color[0] = 1.0;
  57. triangle.color[1] = 1.0;
  58. triangle.color[2] = 0.5;
  59. }
  60. else {
  61. triangle.color[0] = 1.0;
  62. triangle.color[1] = 0.0;
  63. triangle.color[2] = 0.0;
  64. }
  65. }
  66. static void GLCALLBACK
  67. end_callback()
  68. {
  69. glBegin(GL_LINES);
  70. glColor3f(triangle.p_color[0][0], triangle.p_color[0][1],
  71. triangle.p_color[0][2]);
  72. glVertex2i(triangle.p[0][0], triangle.p[0][1]);
  73. glVertex2i(triangle.p[1][0], triangle.p[1][1]);
  74. glColor3f(triangle.p_color[1][0], triangle.p_color[1][1],
  75. triangle.p_color[1][2]);
  76. glVertex2i(triangle.p[1][0], triangle.p[1][1]);
  77. glVertex2i(triangle.p[2][0], triangle.p[2][1]);
  78. glColor3f(triangle.p_color[2][0], triangle.p_color[2][1],
  79. triangle.p_color[2][2]);
  80. glVertex2i(triangle.p[2][0], triangle.p[2][1]);
  81. glVertex2i(triangle.p[0][0], triangle.p[0][1]);
  82. glEnd();
  83. }
  84. static void GLCALLBACK
  85. vertex_callback(void *data)
  86. {
  87. GLsizei no;
  88. GLint *p;
  89. p = (GLint *) data;
  90. no = triangle.no;
  91. triangle.p[no][0] = p[0];
  92. triangle.p[no][1] = p[1];
  93. triangle.p_color[no][0] = triangle.color[0];
  94. triangle.p_color[no][1] = triangle.color[1];
  95. triangle.p_color[no][2] = triangle.color[2];
  96. ++(triangle.no);
  97. }
  98. static void
  99. set_screen_wh(GLsizei w, GLsizei h)
  100. {
  101. width = w;
  102. height = h;
  103. }
  104. static void
  105. tesse(void)
  106. {
  107. GLUtriangulatorObj *tobj;
  108. GLdouble data[3];
  109. GLuint i, j, point_cnt;
  110. tobj = gluNewTess();
  111. if (tobj != NULL) {
  112. glClear(GL_COLOR_BUFFER_BIT);
  113. glColor3f(0.7, 0.7, 0.0);
  114. gluTessCallback(tobj, GLU_BEGIN, glBegin);
  115. gluTessCallback(tobj, GLU_END, glEnd);
  116. gluTessCallback(tobj, GLU_ERROR, my_error);
  117. gluTessCallback(tobj, GLU_VERTEX, glVertex2iv);
  118. gluBeginPolygon(tobj);
  119. for (j = 0; j <= contour_cnt; j++) {
  120. point_cnt = contours[j].point_cnt;
  121. gluNextContour(tobj, GLU_UNKNOWN);
  122. for (i = 0; i < point_cnt; i++) {
  123. data[0] = (GLdouble) (contours[j].p[i][0]);
  124. data[1] = (GLdouble) (contours[j].p[i][1]);
  125. data[2] = 0.0;
  126. gluTessVertex(tobj, data, contours[j].p[i]);
  127. }
  128. }
  129. gluEndPolygon(tobj);
  130. glLineWidth(2.0);
  131. gluTessCallback(tobj, GLU_BEGIN, begin_callback);
  132. gluTessCallback(tobj, GLU_END, end_callback);
  133. gluTessCallback(tobj, GLU_VERTEX, vertex_callback);
  134. gluTessCallback(tobj, GLU_EDGE_FLAG, edge_callback);
  135. gluBeginPolygon(tobj);
  136. for (j = 0; j <= contour_cnt; j++) {
  137. point_cnt = contours[j].point_cnt;
  138. gluNextContour(tobj, GLU_UNKNOWN);
  139. for (i = 0; i < point_cnt; i++) {
  140. data[0] = (GLdouble) (contours[j].p[i][0]);
  141. data[1] = (GLdouble) (contours[j].p[i][1]);
  142. data[2] = 0.0;
  143. gluTessVertex(tobj, data, contours[j].p[i]);
  144. }
  145. }
  146. gluEndPolygon(tobj);
  147. gluDeleteTess(tobj);
  148. glutMouseFunc(NULL);
  149. glColor3f(1.0, 1.0, 0.0);
  150. glLineWidth(1.0);
  151. mode = TESSELATED;
  152. }
  153. }
  154. static void
  155. left_down(int x1, int y1)
  156. {
  157. GLint P[2];
  158. GLuint point_cnt;
  159. /* translate GLUT into GL coordinates */
  160. P[0] = x1;
  161. P[1] = height - y1;
  162. point_cnt = contours[contour_cnt].point_cnt;
  163. contours[contour_cnt].p[point_cnt][0] = P[0];
  164. contours[contour_cnt].p[point_cnt][1] = P[1];
  165. glBegin(GL_LINES);
  166. if (point_cnt) {
  167. glVertex2iv(contours[contour_cnt].p[point_cnt - 1]);
  168. glVertex2iv(P);
  169. }
  170. else {
  171. glVertex2iv(P);
  172. glVertex2iv(P);
  173. }
  174. glEnd();
  175. glFinish();
  176. ++(contours[contour_cnt].point_cnt);
  177. }
  178. static void
  179. middle_down(int x1, int y1)
  180. {
  181. GLuint point_cnt;
  182. point_cnt = contours[contour_cnt].point_cnt;
  183. if (point_cnt > 2) {
  184. glBegin(GL_LINES);
  185. glVertex2iv(contours[contour_cnt].p[0]);
  186. glVertex2iv(contours[contour_cnt].p[point_cnt - 1]);
  187. contours[contour_cnt].p[point_cnt][0] = -1;
  188. glEnd();
  189. glFinish();
  190. contour_cnt++;
  191. contours[contour_cnt].point_cnt = 0;
  192. }
  193. }
  194. static void
  195. mouse_clicked(int button, int state, int x, int y)
  196. {
  197. x -= x % 10;
  198. y -= y % 10;
  199. switch (button) {
  200. case GLUT_LEFT_BUTTON:
  201. if (state == GLUT_DOWN)
  202. left_down(x, y);
  203. break;
  204. case GLUT_MIDDLE_BUTTON:
  205. if (state == GLUT_DOWN)
  206. middle_down(x, y);
  207. break;
  208. }
  209. }
  210. static void
  211. display(void)
  212. {
  213. GLuint i, j;
  214. GLuint point_cnt;
  215. glClear(GL_COLOR_BUFFER_BIT);
  216. switch (mode) {
  217. case DEFINE:
  218. /* draw grid */
  219. glColor3f(0.6, 0.5, 0.5);
  220. glBegin(GL_LINES);
  221. for (i = 0; i < width; i += 10)
  222. for (j = 0; j < height; j += 10) {
  223. glVertex2i(0, j);
  224. glVertex2i(width, j);
  225. glVertex2i(i, height);
  226. glVertex2i(i, 0);
  227. }
  228. glEnd();
  229. glColor3f(1.0, 1.0, 0.0);
  230. for (i = 0; i <= contour_cnt; i++) {
  231. point_cnt = contours[i].point_cnt;
  232. glBegin(GL_LINES);
  233. switch (point_cnt) {
  234. case 0:
  235. break;
  236. case 1:
  237. glVertex2iv(contours[i].p[0]);
  238. glVertex2iv(contours[i].p[0]);
  239. break;
  240. case 2:
  241. glVertex2iv(contours[i].p[0]);
  242. glVertex2iv(contours[i].p[1]);
  243. break;
  244. default:
  245. --point_cnt;
  246. for (j = 0; j < point_cnt; j++) {
  247. glVertex2iv(contours[i].p[j]);
  248. glVertex2iv(contours[i].p[j + 1]);
  249. }
  250. if (contours[i].p[j + 1][0] == -1) {
  251. glVertex2iv(contours[i].p[0]);
  252. glVertex2iv(contours[i].p[j]);
  253. }
  254. break;
  255. }
  256. glEnd();
  257. }
  258. glFinish();
  259. break;
  260. case TESSELATED:
  261. /* draw lines */
  262. tesse();
  263. break;
  264. }
  265. glColor3f(1.0, 1.0, 0.0);
  266. }
  267. static void
  268. clear(void)
  269. {
  270. contour_cnt = 0;
  271. contours[0].point_cnt = 0;
  272. glutMouseFunc(mouse_clicked);
  273. mode = DEFINE;
  274. display();
  275. }
  276. static void
  277. quit(void)
  278. {
  279. exit(0);
  280. }
  281. static void
  282. menu_selected(int entry)
  283. {
  284. switch (entry) {
  285. case CLEAR:
  286. clear();
  287. break;
  288. case TESSELATE:
  289. tesse();
  290. break;
  291. case QUIT:
  292. quit();
  293. break;
  294. }
  295. }
  296. static void
  297. key_pressed(unsigned char key, int x, int y)
  298. {
  299. switch (key) {
  300. case 't':
  301. case 'T':
  302. tesse();
  303. glFinish();
  304. break;
  305. case 'q':
  306. case 'Q':
  307. quit();
  308. break;
  309. case 'c':
  310. case 'C':
  311. clear();
  312. break;
  313. }
  314. }
  315. static void
  316. myinit(void)
  317. {
  318. /* clear background to gray */
  319. glClearColor(0.4, 0.4, 0.4, 0.0);
  320. glShadeModel(GL_FLAT);
  321. menu = glutCreateMenu(menu_selected);
  322. glutAddMenuEntry("clear", CLEAR);
  323. glutAddMenuEntry("tesselate", TESSELATE);
  324. glutAddMenuEntry("quit", QUIT);
  325. glutAttachMenu(GLUT_RIGHT_BUTTON);
  326. glutMouseFunc(mouse_clicked);
  327. glutKeyboardFunc(key_pressed);
  328. contour_cnt = 0;
  329. glPolygonMode(GL_FRONT, GL_FILL);
  330. mode = DEFINE;
  331. }
  332. static void
  333. reshape(GLsizei w, GLsizei h)
  334. {
  335. glViewport(0, 0, w, h);
  336. glMatrixMode(GL_PROJECTION);
  337. glLoadIdentity();
  338. glOrtho(0.0, (GLdouble) w, 0.0, (GLdouble) h, -1.0, 1.0);
  339. glMatrixMode(GL_MODELVIEW);
  340. glLoadIdentity();
  341. set_screen_wh(w, h);
  342. }
  343. static void
  344. usage(void)
  345. {
  346. printf("Use left mouse button to place vertices.\n");
  347. printf("Press middle mouse button when done.\n");
  348. printf("Select tesselate from the pop-up menu.\n");
  349. }
  350. /* Main Loop
  351. * Open window with initial window size, title bar,
  352. * RGBA display mode, and handle input events.
  353. */
  354. int
  355. main(int argc, char **argv)
  356. {
  357. usage();
  358. glutInit(&argc, argv);
  359. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  360. glutInitWindowSize(400, 400);
  361. glutCreateWindow(argv[0]);
  362. myinit();
  363. glutDisplayFunc(display);
  364. glutReshapeFunc(reshape);
  365. glutMainLoop();
  366. return 0;
  367. }