Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

tessdemo.c 8.7KB

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