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.

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