Advertisement
Guest User

Untitled

a guest
Nov 27th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.02 KB | None | 0 0
  1. /*
  2. Build:
  3. $ cc tess.c -o tess -lGL -lGLU -lglut
  4. You must have glut library (free glut) installed
  5. */
  6.  
  7. #include <GL/glut.h>
  8. #include <math.h>
  9. #include <stdio.h>
  10.  
  11. static int wnd_width = 600;
  12. static int wnd_height = 600;
  13.  
  14. static GLfloat g_nearPlane = 1;
  15. static GLfloat g_farPlane = 1000;
  16.  
  17. static GLfloat camera_x = 0, camera_y = 0, camera_z = -20, camera_a = 0, camera_b = 0;
  18.  
  19. static GLint scene_id;
  20.  
  21. GLdouble vertices[][3] =
  22. {
  23.     /* contour */
  24.     {-10.0, -10.0, 0.0},
  25.     {-10.0, +10.0, 0.0},
  26.     {+10.0, +10.0, 0.0},
  27.     {+10.0, -10.0, 0.0},
  28.  
  29.     /* first hole */
  30.     {-5 + -1.5, 5 + -1.5, 0.0},
  31.     {-5 + -1.5, 5 +  1.5, 0.0},
  32.     {-5 +  1.5, 5 +  1.5, 0.0},
  33.     {-5 +  1.5, 5 + -1.5, 0.0},
  34.  
  35.     /* second hole */
  36.     {-1.0, 5 + -1.0, 0.0},
  37.     {-1.0, 5 +  1.0, 0.0},
  38.     { 1.0, 5 +  1.0, 0.0},
  39.     { 1.0, 5 + -1.0, 0.0},
  40.  
  41.     /* triangle hole */
  42.     {5 + -1.0, 5 + -1.0, 0.0},
  43.     {5 +  0.0, 5 +  1.0, 0.0},
  44.     {5 +  1.0, 5 + -1.0, 0.0},
  45.  
  46.     {-10.0, -4.0, 0.0},
  47.     { 0.0, -4.0, 0.0},
  48.     {0.0, -3.0, 0.0},
  49.     {-10.0, -3.0, 0.0}
  50. };
  51.  
  52. void
  53. begin_cb(GLenum which)
  54. {
  55.     glBegin(which);
  56. }
  57.  
  58. void
  59. end_cb(void *p)
  60. {
  61.     glEnd();
  62. }
  63.  
  64. void
  65. vertex_cb(void *p)
  66. {
  67.     glVertex3dv(p);
  68. }
  69.  
  70. void
  71. combine_cb(void *p)
  72. {
  73. }
  74.  
  75. void
  76. error_cb(void *p)
  77. {
  78. }
  79.  
  80. void
  81. make_scene()
  82. {
  83.     GLUtesselator *tess;
  84.     GLUquadric *dsk;
  85.  
  86.     scene_id = glGenLists(1);
  87.  
  88.     glEnable(GL_DEPTH_TEST);
  89.  
  90.     glNewList(scene_id, GL_COMPILE);
  91.  
  92.     /* disk */
  93.     glTranslatef(-5.0, 5.0, 0.1);
  94.     glColor3f(0.3f, 0.7f, 1.0f);
  95.     dsk = gluNewQuadric();
  96.     gluDisk(dsk, 0.7, 1.2, 20, 5);
  97.     gluDeleteQuadric(dsk);
  98.     glTranslatef(5.0, -5.0, -0.1);
  99.  
  100.     tess = gluNewTess();
  101.     gluTessCallback(tess, GLU_TESS_BEGIN,   begin_cb);
  102.     gluTessCallback(tess, GLU_TESS_END,     end_cb);
  103.     gluTessCallback(tess, GLU_TESS_VERTEX,  vertex_cb);
  104.     gluTessCallback(tess, GLU_TESS_COMBINE, combine_cb);
  105.     gluTessCallback(tess, GLU_TESS_ERROR,   error_cb);
  106.  
  107.  
  108.     glColor3f(1.0f, 0.0f, 0.0f);
  109.     gluTessBeginPolygon(tess, NULL);
  110.     gluTessBeginContour(tess);
  111.         gluTessVertex(tess, vertices[0], vertices[0]);
  112.         gluTessVertex(tess, vertices[1], vertices[1]);
  113.         gluTessVertex(tess, vertices[2], vertices[2]);
  114.         gluTessVertex(tess, vertices[3], vertices[3]);
  115.     gluTessEndContour(tess);
  116.  
  117.     gluTessBeginContour(tess);
  118.         gluTessVertex(tess, vertices[4], vertices[4]);
  119.         gluTessVertex(tess, vertices[5], vertices[5]);
  120.         gluTessVertex(tess, vertices[6], vertices[6]);
  121.         gluTessVertex(tess, vertices[7], vertices[7]);
  122.     gluTessEndContour(tess);
  123.  
  124.     gluTessBeginContour(tess);
  125.         gluTessVertex(tess, vertices[8], vertices[8]);
  126.         gluTessVertex(tess, vertices[9], vertices[9]);
  127.         gluTessVertex(tess, vertices[10], vertices[10]);
  128.         gluTessVertex(tess, vertices[11], vertices[11]);
  129.     gluTessEndContour(tess);
  130.  
  131.     gluTessBeginContour(tess);
  132.         gluTessVertex(tess, vertices[12], vertices[12]);
  133.         gluTessVertex(tess, vertices[13], vertices[13]);
  134.         gluTessVertex(tess, vertices[14], vertices[14]);
  135.     gluTessEndContour(tess);
  136.  
  137.     gluTessBeginContour(tess);
  138.         gluTessVertex(tess, vertices[15], vertices[15]);
  139.         gluTessVertex(tess, vertices[16], vertices[16]);
  140.         gluTessVertex(tess, vertices[17], vertices[17]);
  141.         gluTessVertex(tess, vertices[18], vertices[18]);
  142.     gluTessEndContour(tess);
  143.  
  144.     gluTessEndPolygon(tess);
  145.     gluDeleteTess(tess);
  146.  
  147.     glEndList();
  148. }
  149.  
  150. void
  151. display()
  152. {
  153.     int i, j, k;
  154.  
  155.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  156.  
  157.     glLoadIdentity();
  158.  
  159.     glRotatef(-camera_a, 0.0f, 1.0f, 0.0f);
  160.     glRotatef(-camera_b, 1.0f, 0.0f, 0.0f);
  161.     glTranslatef(camera_x, camera_y, camera_z);
  162.  
  163.     glCallList(scene_id);
  164.  
  165.     glutSwapBuffers();
  166. }
  167.  
  168.  
  169. void
  170. reshape(GLint width, GLint height)
  171. {
  172.     wnd_width = width;
  173.     wnd_height = height;
  174.  
  175.     glViewport(0, 0, wnd_width, wnd_height);
  176.     glMatrixMode(GL_PROJECTION);
  177.     glLoadIdentity();
  178.     gluPerspective(65.0, (float)wnd_width / wnd_height, g_nearPlane, g_farPlane);
  179.     glMatrixMode(GL_MODELVIEW);
  180. }
  181.  
  182. void
  183. pmotion(int x, int y)
  184. {
  185.     camera_a = (x - wnd_width / 2) / 3;
  186.     camera_b = (y - wnd_height / 2) / 3;
  187.  
  188.     glutPostRedisplay();
  189. }
  190.  
  191. void
  192. mbutton(int button, int state, int x, int y)
  193. {
  194.     GLfloat step = 1.0;
  195.  
  196.     if (state != GLUT_UP) {
  197.         if (button == 3) {
  198.             camera_x -= step * sin(camera_a * M_PI / 180);
  199.             camera_z += step * cos(camera_a * M_PI / 180);
  200.         }
  201.         if (button == 4) {
  202.             camera_x += step * sin(camera_a * M_PI / 180);
  203.             camera_z -= step * cos(camera_a * M_PI / 180);
  204.         }
  205.     }
  206.     glutPostRedisplay();
  207. }
  208.  
  209. void
  210. keyboard(unsigned char key, int x, int y)
  211. {
  212.     switch (key) {
  213.         case 'w':
  214.         case 'W':
  215.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  216.             glDisable(GL_DEPTH_TEST);
  217.             break;
  218.         case 'f':
  219.         case 'F':
  220.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  221.             glEnable(GL_DEPTH_TEST);
  222.             break;
  223.     }
  224.     glutPostRedisplay();
  225. }
  226.  
  227. int
  228. main(int argc, char *argv[])
  229. {
  230.     glutInit(&argc, argv);
  231.     glutInitWindowSize(wnd_width, wnd_height);
  232.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  233.     glutCreateWindow("example");
  234.  
  235.     make_scene();
  236.  
  237.     glutDisplayFunc(display);
  238.     glutReshapeFunc(reshape);
  239.     glutMotionFunc(pmotion);
  240.     glutMouseFunc(mbutton);
  241.     glutKeyboardFunc(keyboard);
  242.  
  243.     glutMainLoop();
  244.  
  245.     return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement