Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

tessdemo.c 8.6KB

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