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.

tessdemo.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * A demo of the GLU polygon tesselation functions written by Bogdan Sikorski.
  3. * Updated for GLU 1.3 tessellation by Gareth Hughes <gareth@valinux.com>
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <GL/glut.h>
  9. #define MAX_POINTS 256
  10. #define MAX_CONTOURS 32
  11. #define MAX_TRIANGLES 256
  12. #ifndef GLCALLBACK
  13. #ifdef CALLBACK
  14. #define GLCALLBACK CALLBACK
  15. #else
  16. #define GLCALLBACK
  17. #endif
  18. #endif
  19. #ifdef GLU_VERSION_1_2
  20. typedef enum{ QUIT, TESSELATE, CLEAR } menu_entries;
  21. typedef enum{ DEFINE, TESSELATED } mode_type;
  22. static GLsizei width, height;
  23. static GLuint contour_cnt;
  24. static GLuint triangle_cnt;
  25. static mode_type mode;
  26. static int menu;
  27. static GLuint list_start;
  28. static GLfloat edge_color[3];
  29. static struct {
  30. GLfloat p[MAX_POINTS][2];
  31. GLuint point_cnt;
  32. } contours[MAX_CONTOURS];
  33. static struct {
  34. GLsizei no;
  35. GLfloat p[3][2];
  36. GLclampf color[3][3];
  37. } triangles[MAX_TRIANGLES];
  38. static void GLCALLBACK error_callback( GLenum err )
  39. {
  40. int len, i;
  41. char const *str;
  42. glColor3f( 0.9, 0.9, 0.9 );
  43. glRasterPos2i( 5, 5 );
  44. str = (const char *) gluErrorString( err );
  45. len = strlen( str );
  46. for ( i = 0 ; i < len ; i++ ) {
  47. glutBitmapCharacter( GLUT_BITMAP_9_BY_15, str[i] );
  48. }
  49. }
  50. static void GLCALLBACK begin_callback( GLenum mode )
  51. {
  52. /* Allow multiple triangles to be output inside the begin/end pair. */
  53. triangle_cnt = 0;
  54. triangles[triangle_cnt].no = 0;
  55. }
  56. static void GLCALLBACK edge_callback( GLenum flag )
  57. {
  58. /* Persist the edge flag across triangles. */
  59. if ( flag == GL_TRUE ) {
  60. edge_color[0] = 1.0;
  61. edge_color[1] = 1.0;
  62. edge_color[2] = 0.5;
  63. } else {
  64. edge_color[0] = 1.0;
  65. edge_color[1] = 0.0;
  66. edge_color[2] = 0.0;
  67. }
  68. }
  69. static void GLCALLBACK end_callback()
  70. {
  71. GLuint i;
  72. glBegin( GL_LINES );
  73. /* Output the three edges of each triangle as lines colored
  74. according to their edge flag. */
  75. for ( i = 0 ; i < triangle_cnt ; i++ ) {
  76. glColor3f( triangles[i].color[0][0],
  77. triangles[i].color[0][1],
  78. triangles[i].color[0][2] );
  79. glVertex2f( triangles[i].p[0][0], triangles[i].p[0][1] );
  80. glVertex2f( triangles[i].p[1][0], triangles[i].p[1][1] );
  81. glColor3f( triangles[i].color[1][0],
  82. triangles[i].color[1][1],
  83. triangles[i].color[1][2] );
  84. glVertex2f( triangles[i].p[1][0], triangles[i].p[1][1] );
  85. glVertex2f( triangles[i].p[2][0], triangles[i].p[2][1] );
  86. glColor3f( triangles[i].color[2][0],
  87. triangles[i].color[2][1],
  88. triangles[i].color[2][2] );
  89. glVertex2f( triangles[i].p[2][0], triangles[i].p[2][1] );
  90. glVertex2f( triangles[i].p[0][0], triangles[i].p[0][1] );
  91. }
  92. glEnd();
  93. }
  94. static void GLCALLBACK vertex_callback( void *data )
  95. {
  96. GLsizei no;
  97. GLfloat *p;
  98. p = (GLfloat *) data;
  99. no = triangles[triangle_cnt].no;
  100. triangles[triangle_cnt].p[no][0] = p[0];
  101. triangles[triangle_cnt].p[no][1] = p[1];
  102. triangles[triangle_cnt].color[no][0] = edge_color[0];
  103. triangles[triangle_cnt].color[no][1] = edge_color[1];
  104. triangles[triangle_cnt].color[no][2] = edge_color[2];
  105. /* After every three vertices, initialize the next triangle. */
  106. if ( ++(triangles[triangle_cnt].no) == 3 ) {
  107. triangle_cnt++;
  108. triangles[triangle_cnt].no = 0;
  109. }
  110. }
  111. static void GLCALLBACK combine_callback( GLdouble coords[3],
  112. GLdouble *vertex_data[4],
  113. GLfloat weight[4], void **data )
  114. {
  115. GLfloat *vertex;
  116. vertex = (GLfloat *) malloc( 2 * sizeof(GLfloat) );
  117. vertex[0] = (GLfloat) coords[0];
  118. vertex[1] = (GLfloat) coords[1];
  119. *data = vertex;
  120. }
  121. static void set_screen_wh( GLsizei w, GLsizei h )
  122. {
  123. width = w;
  124. height = h;
  125. }
  126. typedef void (GLAPIENTRY *callback_t)();
  127. static void tesse( void )
  128. {
  129. GLUtesselator *tobj;
  130. GLdouble data[3];
  131. GLuint i, j, point_cnt;
  132. list_start = glGenLists( 2 );
  133. tobj = gluNewTess();
  134. if ( tobj != NULL ) {
  135. gluTessNormal( tobj, 0.0, 0.0, 1.0 );
  136. gluTessCallback( tobj, GLU_TESS_BEGIN, (callback_t) glBegin );
  137. gluTessCallback( tobj, GLU_TESS_VERTEX, (callback_t) glVertex2fv );
  138. gluTessCallback( tobj, GLU_TESS_END, (callback_t) glEnd );
  139. gluTessCallback( tobj, GLU_TESS_ERROR, (callback_t) error_callback );
  140. gluTessCallback( tobj, GLU_TESS_COMBINE, (callback_t) combine_callback );
  141. glNewList( list_start, GL_COMPILE );
  142. gluBeginPolygon( tobj );
  143. for ( j = 0 ; j <= contour_cnt ; j++ ) {
  144. point_cnt = contours[j].point_cnt;
  145. gluNextContour( tobj, GLU_UNKNOWN );
  146. for ( i = 0 ; i < point_cnt ; i++ ) {
  147. data[0] = (GLdouble)( contours[j].p[i][0] );
  148. data[1] = (GLdouble)( contours[j].p[i][1] );
  149. data[2] = 0.0;
  150. gluTessVertex( tobj, data, contours[j].p[i] );
  151. }
  152. }
  153. gluEndPolygon( tobj );
  154. glEndList();
  155. gluTessCallback( tobj, GLU_TESS_BEGIN, (callback_t) begin_callback );
  156. gluTessCallback( tobj, GLU_TESS_VERTEX, (callback_t) vertex_callback );
  157. gluTessCallback( tobj, GLU_TESS_END, (callback_t) end_callback );
  158. gluTessCallback( tobj, GLU_TESS_EDGE_FLAG, (callback_t) edge_callback );
  159. glNewList( list_start + 1, GL_COMPILE );
  160. gluBeginPolygon( tobj );
  161. for ( j = 0 ; j <= contour_cnt ; j++ ) {
  162. point_cnt = contours[j].point_cnt;
  163. gluNextContour( tobj, GLU_UNKNOWN );
  164. for ( i = 0 ; i < point_cnt ; i++ ) {
  165. data[0] = (GLdouble)( contours[j].p[i][0] );
  166. data[1] = (GLdouble)( contours[j].p[i][1] );
  167. data[2] = 0.0;
  168. gluTessVertex( tobj, data, contours[j].p[i] );
  169. }
  170. }
  171. gluEndPolygon( tobj );
  172. glEndList();
  173. gluDeleteTess( tobj );
  174. glutMouseFunc( NULL );
  175. mode = TESSELATED;
  176. }
  177. }
  178. static void left_down( int x1, int y1 )
  179. {
  180. GLfloat P[2];
  181. GLuint point_cnt;
  182. /* translate GLUT into GL coordinates */
  183. P[0] = x1;
  184. P[1] = height - y1;
  185. point_cnt = contours[contour_cnt].point_cnt;
  186. contours[contour_cnt].p[point_cnt][0] = P[0];
  187. contours[contour_cnt].p[point_cnt][1] = P[1];
  188. glBegin( GL_LINES );
  189. if ( point_cnt ) {
  190. glVertex2fv( contours[contour_cnt].p[point_cnt-1] );
  191. glVertex2fv( P );
  192. } else {
  193. glVertex2fv( P );
  194. glVertex2fv( P );
  195. }
  196. glEnd();
  197. glFinish();
  198. contours[contour_cnt].point_cnt++;
  199. }
  200. static void middle_down( int x1, int y1 )
  201. {
  202. GLuint point_cnt;
  203. (void) x1;
  204. (void) y1;
  205. point_cnt = contours[contour_cnt].point_cnt;
  206. if ( point_cnt > 2 ) {
  207. glBegin( GL_LINES );
  208. glVertex2fv( contours[contour_cnt].p[0] );
  209. glVertex2fv( contours[contour_cnt].p[point_cnt-1] );
  210. contours[contour_cnt].p[point_cnt][0] = -1;
  211. glEnd();
  212. glFinish();
  213. contour_cnt++;
  214. contours[contour_cnt].point_cnt = 0;
  215. }
  216. }
  217. static void mouse_clicked( int button, int state, int x, int y )
  218. {
  219. x -= x%10;
  220. y -= y%10;
  221. switch ( button ) {
  222. case GLUT_LEFT_BUTTON:
  223. if ( state == GLUT_DOWN ) {
  224. left_down( x, y );
  225. }
  226. break;
  227. case GLUT_MIDDLE_BUTTON:
  228. if ( state == GLUT_DOWN ) {
  229. middle_down( x, y );
  230. }
  231. break;
  232. }
  233. }
  234. static void display( void )
  235. {
  236. GLuint i,j;
  237. GLsizei ii, jj;
  238. GLuint point_cnt;
  239. glClear( GL_COLOR_BUFFER_BIT );
  240. switch ( mode ) {
  241. case DEFINE:
  242. /* draw grid */
  243. glColor3f( 0.6, 0.5, 0.5 );
  244. glBegin( GL_LINES );
  245. for ( ii = 0 ; ii < width ; ii += 10 ) {
  246. for ( jj = 0 ; jj < height ; jj += 10 ) {
  247. glVertex2i( 0, jj );
  248. glVertex2i( width, jj );
  249. glVertex2i( ii, height );
  250. glVertex2i( ii, 0 );
  251. }
  252. }
  253. glEnd();
  254. glColor3f( 1.0, 1.0, 0.0 );
  255. for ( i = 0 ; i <= contour_cnt ; i++ ) {
  256. point_cnt = contours[i].point_cnt;
  257. glBegin( GL_LINES );
  258. switch ( point_cnt ) {
  259. case 0:
  260. break;
  261. case 1:
  262. glVertex2fv( contours[i].p[0] );
  263. glVertex2fv( contours[i].p[0] );
  264. break;
  265. case 2:
  266. glVertex2fv( contours[i].p[0] );
  267. glVertex2fv( contours[i].p[1] );
  268. break;
  269. default:
  270. --point_cnt;
  271. for ( j = 0 ; j < point_cnt ; j++ ) {
  272. glVertex2fv( contours[i].p[j] );
  273. glVertex2fv( contours[i].p[j+1] );
  274. }
  275. if ( contours[i].p[j+1][0] == -1 ) {
  276. glVertex2fv( contours[i].p[0] );
  277. glVertex2fv( contours[i].p[j] );
  278. }
  279. break;
  280. }
  281. glEnd();
  282. }
  283. glFinish();
  284. break;
  285. case TESSELATED:
  286. /* draw triangles */
  287. glColor3f( 0.7, 0.7, 0.0 );
  288. glCallList( list_start );
  289. glLineWidth( 2.0 );
  290. glCallList( list_start + 1 );
  291. glLineWidth( 1.0 );
  292. glFlush();
  293. break;
  294. }
  295. glColor3f( 1.0, 1.0, 0.0 );
  296. }
  297. static void clear( void )
  298. {
  299. contour_cnt = 0;
  300. contours[0].point_cnt = 0;
  301. triangle_cnt = 0;
  302. glutMouseFunc( mouse_clicked );
  303. mode = DEFINE;
  304. glDeleteLists( list_start, 2 );
  305. list_start = 0;
  306. }
  307. static void quit( void )
  308. {
  309. exit( 0 );
  310. }
  311. static void menu_selected( int entry )
  312. {
  313. switch ( entry ) {
  314. case CLEAR:
  315. clear();
  316. break;
  317. case TESSELATE:
  318. tesse();
  319. break;
  320. case QUIT:
  321. quit();
  322. break;
  323. }
  324. glutPostRedisplay();
  325. }
  326. static void key_pressed( unsigned char key, int x, int y )
  327. {
  328. (void) x;
  329. (void) y;
  330. switch ( key ) {
  331. case 'c':
  332. case 'C':
  333. clear();
  334. break;
  335. case 't':
  336. case 'T':
  337. tesse();
  338. break;
  339. case 27:
  340. case 'q':
  341. case 'Q':
  342. quit();
  343. break;
  344. }
  345. glutPostRedisplay();
  346. }
  347. static void myinit( void )
  348. {
  349. /* clear background to gray */
  350. glClearColor( 0.4, 0.4, 0.4, 0.0 );
  351. glShadeModel( GL_FLAT );
  352. glPolygonMode( GL_FRONT, GL_FILL );
  353. menu = glutCreateMenu( menu_selected );
  354. glutAddMenuEntry( "clear", CLEAR );
  355. glutAddMenuEntry( "tesselate", TESSELATE );
  356. glutAddMenuEntry( "quit", QUIT );
  357. glutAttachMenu( GLUT_RIGHT_BUTTON );
  358. glutMouseFunc( mouse_clicked );
  359. glutKeyboardFunc( key_pressed );
  360. contour_cnt = 0;
  361. mode = DEFINE;
  362. }
  363. static void reshape( GLsizei w, GLsizei h )
  364. {
  365. glViewport( 0, 0, w, h );
  366. glMatrixMode( GL_PROJECTION );
  367. glLoadIdentity();
  368. glOrtho( 0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0 );
  369. glMatrixMode( GL_MODELVIEW );
  370. glLoadIdentity();
  371. set_screen_wh( w, h );
  372. }
  373. #endif
  374. static void usage( void )
  375. {
  376. printf( "Use left mouse button to place vertices.\n" );
  377. printf( "Press middle mouse button when done.\n" );
  378. printf( "Select tesselate from the pop-up menu.\n" );
  379. }
  380. int main( int argc, char **argv )
  381. {
  382. const char *version = (const char *) gluGetString( GLU_VERSION );
  383. printf( "GLU version string: %s\n", version );
  384. if ( strstr( version, "1.0" ) || strstr( version, "1.1" ) ) {
  385. fprintf( stderr, "Sorry, this demo reqiures GLU 1.2 or later.\n" );
  386. exit( 1 );
  387. }
  388. usage();
  389. glutInitWindowSize( 400, 400 );
  390. glutInit( &argc, argv );
  391. glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
  392. glutCreateWindow( argv[0] );
  393. /* GH: Bit of a hack...
  394. */
  395. #ifdef GLU_VERSION_1_2
  396. myinit();
  397. glutDisplayFunc( display );
  398. glutReshapeFunc( reshape );
  399. glutMainLoop();
  400. #endif
  401. return 0;
  402. }