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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* $Id: tessdemo.c,v 1.12 2002/07/12 15:54:02 brianp 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. typedef void (GLAPIENTRY *callback_t)();
  128. static void tesse( void )
  129. {
  130. GLUtesselator *tobj;
  131. GLdouble data[3];
  132. GLuint i, j, point_cnt;
  133. list_start = glGenLists( 2 );
  134. tobj = gluNewTess();
  135. if ( tobj != NULL ) {
  136. gluTessNormal( tobj, 0.0, 0.0, 1.0 );
  137. gluTessCallback( tobj, GLU_TESS_BEGIN, (callback_t) glBegin );
  138. gluTessCallback( tobj, GLU_TESS_VERTEX, (callback_t) glVertex2fv );
  139. gluTessCallback( tobj, GLU_TESS_END, (callback_t) glEnd );
  140. gluTessCallback( tobj, GLU_TESS_ERROR, (callback_t) error_callback );
  141. gluTessCallback( tobj, GLU_TESS_COMBINE, (callback_t) combine_callback );
  142. glNewList( list_start, GL_COMPILE );
  143. gluBeginPolygon( tobj );
  144. for ( j = 0 ; j <= contour_cnt ; j++ ) {
  145. point_cnt = contours[j].point_cnt;
  146. gluNextContour( tobj, GLU_UNKNOWN );
  147. for ( i = 0 ; i < point_cnt ; i++ ) {
  148. data[0] = (GLdouble)( contours[j].p[i][0] );
  149. data[1] = (GLdouble)( contours[j].p[i][1] );
  150. data[2] = 0.0;
  151. gluTessVertex( tobj, data, contours[j].p[i] );
  152. }
  153. }
  154. gluEndPolygon( tobj );
  155. glEndList();
  156. gluTessCallback( tobj, GLU_TESS_BEGIN, (callback_t) begin_callback );
  157. gluTessCallback( tobj, GLU_TESS_VERTEX, (callback_t) vertex_callback );
  158. gluTessCallback( tobj, GLU_TESS_END, (callback_t) end_callback );
  159. gluTessCallback( tobj, GLU_TESS_EDGE_FLAG, (callback_t) edge_callback );
  160. glNewList( list_start + 1, GL_COMPILE );
  161. gluBeginPolygon( tobj );
  162. for ( j = 0 ; j <= contour_cnt ; j++ ) {
  163. point_cnt = contours[j].point_cnt;
  164. gluNextContour( tobj, GLU_UNKNOWN );
  165. for ( i = 0 ; i < point_cnt ; i++ ) {
  166. data[0] = (GLdouble)( contours[j].p[i][0] );
  167. data[1] = (GLdouble)( contours[j].p[i][1] );
  168. data[2] = 0.0;
  169. gluTessVertex( tobj, data, contours[j].p[i] );
  170. }
  171. }
  172. gluEndPolygon( tobj );
  173. glEndList();
  174. gluDeleteTess( tobj );
  175. glutMouseFunc( NULL );
  176. mode = TESSELATED;
  177. }
  178. }
  179. static void left_down( int x1, int y1 )
  180. {
  181. GLfloat P[2];
  182. GLuint point_cnt;
  183. /* translate GLUT into GL coordinates */
  184. P[0] = x1;
  185. P[1] = height - y1;
  186. point_cnt = contours[contour_cnt].point_cnt;
  187. contours[contour_cnt].p[point_cnt][0] = P[0];
  188. contours[contour_cnt].p[point_cnt][1] = P[1];
  189. glBegin( GL_LINES );
  190. if ( point_cnt ) {
  191. glVertex2fv( contours[contour_cnt].p[point_cnt-1] );
  192. glVertex2fv( P );
  193. } else {
  194. glVertex2fv( P );
  195. glVertex2fv( P );
  196. }
  197. glEnd();
  198. glFinish();
  199. contours[contour_cnt].point_cnt++;
  200. }
  201. static void middle_down( int x1, int y1 )
  202. {
  203. GLuint point_cnt;
  204. (void) x1;
  205. (void) y1;
  206. point_cnt = contours[contour_cnt].point_cnt;
  207. if ( point_cnt > 2 ) {
  208. glBegin( GL_LINES );
  209. glVertex2fv( contours[contour_cnt].p[0] );
  210. glVertex2fv( contours[contour_cnt].p[point_cnt-1] );
  211. contours[contour_cnt].p[point_cnt][0] = -1;
  212. glEnd();
  213. glFinish();
  214. contour_cnt++;
  215. contours[contour_cnt].point_cnt = 0;
  216. }
  217. }
  218. static void mouse_clicked( int button, int state, int x, int y )
  219. {
  220. x -= x%10;
  221. y -= y%10;
  222. switch ( button ) {
  223. case GLUT_LEFT_BUTTON:
  224. if ( state == GLUT_DOWN ) {
  225. left_down( x, y );
  226. }
  227. break;
  228. case GLUT_MIDDLE_BUTTON:
  229. if ( state == GLUT_DOWN ) {
  230. middle_down( x, y );
  231. }
  232. break;
  233. }
  234. }
  235. static void display( void )
  236. {
  237. GLuint i,j;
  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 ( i = 0 ; i < width ; i += 10 ) {
  246. for ( j = 0 ; j < height ; j += 10 ) {
  247. glVertex2i( 0, j );
  248. glVertex2i( width, j );
  249. glVertex2i( i, height );
  250. glVertex2i( i, 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. glutInit( &argc, argv );
  390. glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
  391. glutInitWindowSize( 400, 400 );
  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. }