Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

tessdemo.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* $Id: tessdemo.c,v 1.11 2001/03/21 02:47:32 gareth Exp $ */
  2. /*
  3. * A demo of the GLU polygon tesselation functions written by Bogdan Sikorski.
  4. * Updated for GLU 1.3 tessellation by Gareth Hughes <gareth@valinux.com>
  5. */
  6. #include <GL/glut.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define MAX_POINTS 256
  11. #define MAX_CONTOURS 32
  12. #define MAX_TRIANGLES 256
  13. #ifndef GLCALLBACK
  14. #ifdef CALLBACK
  15. #define GLCALLBACK CALLBACK
  16. #else
  17. #define GLCALLBACK
  18. #endif
  19. #endif
  20. #ifdef GLU_VERSION_1_2
  21. typedef enum{ QUIT, TESSELATE, CLEAR } menu_entries;
  22. typedef enum{ DEFINE, TESSELATED } mode_type;
  23. static GLsizei width, height;
  24. static GLuint contour_cnt;
  25. static GLuint triangle_cnt;
  26. static mode_type mode;
  27. static int menu;
  28. static GLuint list_start;
  29. static GLfloat edge_color[3];
  30. static struct {
  31. GLfloat p[MAX_POINTS][2];
  32. GLuint point_cnt;
  33. } contours[MAX_CONTOURS];
  34. static struct {
  35. GLsizei no;
  36. GLfloat p[3][2];
  37. GLclampf color[3][3];
  38. } triangles[MAX_TRIANGLES];
  39. static void GLCALLBACK error_callback( GLenum err )
  40. {
  41. int len, i;
  42. char const *str;
  43. glColor3f( 0.9, 0.9, 0.9 );
  44. glRasterPos2i( 5, 5 );
  45. str = (const char *) gluErrorString( err );
  46. len = strlen( str );
  47. for ( i = 0 ; i < len ; i++ ) {
  48. glutBitmapCharacter( GLUT_BITMAP_9_BY_15, str[i] );
  49. }
  50. }
  51. static void GLCALLBACK begin_callback( GLenum mode )
  52. {
  53. /* Allow multiple triangles to be output inside the begin/end pair. */
  54. triangle_cnt = 0;
  55. triangles[triangle_cnt].no = 0;
  56. }
  57. static void GLCALLBACK edge_callback( GLenum flag )
  58. {
  59. /* Persist the edge flag across triangles. */
  60. if ( flag == GL_TRUE ) {
  61. edge_color[0] = 1.0;
  62. edge_color[1] = 1.0;
  63. edge_color[2] = 0.5;
  64. } else {
  65. edge_color[0] = 1.0;
  66. edge_color[1] = 0.0;
  67. edge_color[2] = 0.0;
  68. }
  69. }
  70. static void GLCALLBACK end_callback()
  71. {
  72. GLint i;
  73. glBegin( GL_LINES );
  74. /* Output the three edges of each triangle as lines colored
  75. according to their edge flag. */
  76. for ( i = 0 ; i < triangle_cnt ; i++ ) {
  77. glColor3f( triangles[i].color[0][0],
  78. triangles[i].color[0][1],
  79. triangles[i].color[0][2] );
  80. glVertex2f( triangles[i].p[0][0], triangles[i].p[0][1] );
  81. glVertex2f( triangles[i].p[1][0], triangles[i].p[1][1] );
  82. glColor3f( triangles[i].color[1][0],
  83. triangles[i].color[1][1],
  84. triangles[i].color[1][2] );
  85. glVertex2f( triangles[i].p[1][0], triangles[i].p[1][1] );
  86. glVertex2f( triangles[i].p[2][0], triangles[i].p[2][1] );
  87. glColor3f( triangles[i].color[2][0],
  88. triangles[i].color[2][1],
  89. triangles[i].color[2][2] );
  90. glVertex2f( triangles[i].p[2][0], triangles[i].p[2][1] );
  91. glVertex2f( triangles[i].p[0][0], triangles[i].p[0][1] );
  92. }
  93. glEnd();
  94. }
  95. static void GLCALLBACK vertex_callback( void *data )
  96. {
  97. GLsizei no;
  98. GLfloat *p;
  99. p = (GLfloat *) data;
  100. no = triangles[triangle_cnt].no;
  101. triangles[triangle_cnt].p[no][0] = p[0];
  102. triangles[triangle_cnt].p[no][1] = p[1];
  103. triangles[triangle_cnt].color[no][0] = edge_color[0];
  104. triangles[triangle_cnt].color[no][1] = edge_color[1];
  105. triangles[triangle_cnt].color[no][2] = edge_color[2];
  106. /* After every three vertices, initialize the next triangle. */
  107. if ( ++(triangles[triangle_cnt].no) == 3 ) {
  108. triangle_cnt++;
  109. triangles[triangle_cnt].no = 0;
  110. }
  111. }
  112. static void GLCALLBACK combine_callback( GLdouble coords[3],
  113. GLdouble *vertex_data[4],
  114. GLfloat weight[4], void **data )
  115. {
  116. GLfloat *vertex;
  117. vertex = (GLfloat *) malloc( 2 * sizeof(GLfloat) );
  118. vertex[0] = (GLfloat) coords[0];
  119. vertex[1] = (GLfloat) coords[1];
  120. *data = vertex;
  121. }
  122. static void set_screen_wh( GLsizei w, GLsizei h )
  123. {
  124. width = w;
  125. height = h;
  126. }
  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, glBegin );
  137. gluTessCallback( tobj, GLU_TESS_VERTEX, glVertex2fv );
  138. gluTessCallback( tobj, GLU_TESS_END, glEnd );
  139. gluTessCallback( tobj, GLU_TESS_ERROR, error_callback );
  140. gluTessCallback( tobj, GLU_TESS_COMBINE, 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, begin_callback );
  156. gluTessCallback( tobj, GLU_TESS_VERTEX, vertex_callback );
  157. gluTessCallback( tobj, GLU_TESS_END, end_callback );
  158. gluTessCallback( tobj, GLU_TESS_EDGE_FLAG, 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. GLuint point_cnt;
  238. glClear( GL_COLOR_BUFFER_BIT );
  239. switch ( mode ) {
  240. case DEFINE:
  241. /* draw grid */
  242. glColor3f( 0.6, 0.5, 0.5 );
  243. glBegin( GL_LINES );
  244. for ( i = 0 ; i < width ; i += 10 ) {
  245. for ( j = 0 ; j < height ; j += 10 ) {
  246. glVertex2i( 0, j );
  247. glVertex2i( width, j );
  248. glVertex2i( i, height );
  249. glVertex2i( i, 0 );
  250. }
  251. }
  252. glEnd();
  253. glColor3f( 1.0, 1.0, 0.0 );
  254. for ( i = 0 ; i <= contour_cnt ; i++ ) {
  255. point_cnt = contours[i].point_cnt;
  256. glBegin( GL_LINES );
  257. switch ( point_cnt ) {
  258. case 0:
  259. break;
  260. case 1:
  261. glVertex2fv( contours[i].p[0] );
  262. glVertex2fv( contours[i].p[0] );
  263. break;
  264. case 2:
  265. glVertex2fv( contours[i].p[0] );
  266. glVertex2fv( contours[i].p[1] );
  267. break;
  268. default:
  269. --point_cnt;
  270. for ( j = 0 ; j < point_cnt ; j++ ) {
  271. glVertex2fv( contours[i].p[j] );
  272. glVertex2fv( contours[i].p[j+1] );
  273. }
  274. if ( contours[i].p[j+1][0] == -1 ) {
  275. glVertex2fv( contours[i].p[0] );
  276. glVertex2fv( contours[i].p[j] );
  277. }
  278. break;
  279. }
  280. glEnd();
  281. }
  282. glFinish();
  283. break;
  284. case TESSELATED:
  285. /* draw triangles */
  286. glColor3f( 0.7, 0.7, 0.0 );
  287. glCallList( list_start );
  288. glLineWidth( 2.0 );
  289. glCallList( list_start + 1 );
  290. glLineWidth( 1.0 );
  291. glFlush();
  292. break;
  293. }
  294. glColor3f( 1.0, 1.0, 0.0 );
  295. }
  296. static void clear( void )
  297. {
  298. contour_cnt = 0;
  299. contours[0].point_cnt = 0;
  300. triangle_cnt = 0;
  301. glutMouseFunc( mouse_clicked );
  302. mode = DEFINE;
  303. glDeleteLists( list_start, 2 );
  304. list_start = 0;
  305. }
  306. static void quit( void )
  307. {
  308. exit( 0 );
  309. }
  310. static void menu_selected( int entry )
  311. {
  312. switch ( entry ) {
  313. case CLEAR:
  314. clear();
  315. break;
  316. case TESSELATE:
  317. tesse();
  318. break;
  319. case QUIT:
  320. quit();
  321. break;
  322. }
  323. glutPostRedisplay();
  324. }
  325. static void key_pressed( unsigned char key, int x, int y )
  326. {
  327. (void) x;
  328. (void) y;
  329. switch ( key ) {
  330. case 'c':
  331. case 'C':
  332. clear();
  333. break;
  334. case 't':
  335. case 'T':
  336. tesse();
  337. break;
  338. case 27:
  339. case 'q':
  340. case 'Q':
  341. quit();
  342. break;
  343. }
  344. glutPostRedisplay();
  345. }
  346. static void myinit( void )
  347. {
  348. /* clear background to gray */
  349. glClearColor( 0.4, 0.4, 0.4, 0.0 );
  350. glShadeModel( GL_FLAT );
  351. glPolygonMode( GL_FRONT, GL_FILL );
  352. menu = glutCreateMenu( menu_selected );
  353. glutAddMenuEntry( "clear", CLEAR );
  354. glutAddMenuEntry( "tesselate", TESSELATE );
  355. glutAddMenuEntry( "quit", QUIT );
  356. glutAttachMenu( GLUT_RIGHT_BUTTON );
  357. glutMouseFunc( mouse_clicked );
  358. glutKeyboardFunc( key_pressed );
  359. contour_cnt = 0;
  360. mode = DEFINE;
  361. }
  362. static void reshape( GLsizei w, GLsizei h )
  363. {
  364. glViewport( 0, 0, w, h );
  365. glMatrixMode( GL_PROJECTION );
  366. glLoadIdentity();
  367. glOrtho( 0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0 );
  368. glMatrixMode( GL_MODELVIEW );
  369. glLoadIdentity();
  370. set_screen_wh( w, h );
  371. }
  372. #endif
  373. static void usage( void )
  374. {
  375. printf( "Use left mouse button to place vertices.\n" );
  376. printf( "Press middle mouse button when done.\n" );
  377. printf( "Select tesselate from the pop-up menu.\n" );
  378. }
  379. int main( int argc, char **argv )
  380. {
  381. const char *version = (const char *) gluGetString( GLU_VERSION );
  382. printf( "GLU version string: %s\n", version );
  383. if ( strstr( version, "1.0" ) || strstr( version, "1.1" ) ) {
  384. fprintf( stderr, "Sorry, this demo reqiures GLU 1.2 or later.\n" );
  385. exit( 1 );
  386. }
  387. usage();
  388. glutInit( &argc, argv );
  389. glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
  390. glutInitWindowSize( 400, 400 );
  391. glutCreateWindow( argv[0] );
  392. /* GH: Bit of a hack...
  393. */
  394. #ifdef GLU_VERSION_1_2
  395. myinit();
  396. glutDisplayFunc( display );
  397. glutReshapeFunc( reshape );
  398. glutMainLoop();
  399. #endif
  400. return 0;
  401. }