Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Build:
- $ cc tess.c -o tess -lGL -lGLU -lglut
- You must have glut library (free glut) installed
- */
- #include <GL/glut.h>
- #include <math.h>
- #include <stdio.h>
- static int wnd_width = 600;
- static int wnd_height = 600;
- static GLfloat g_nearPlane = 1;
- static GLfloat g_farPlane = 1000;
- static GLfloat camera_x = 0, camera_y = 0, camera_z = -20, camera_a = 0, camera_b = 0;
- static GLint scene_id;
- GLdouble vertices[][3] =
- {
- /* contour */
- {-10.0, -10.0, 0.0},
- {-10.0, +10.0, 0.0},
- {+10.0, +10.0, 0.0},
- {+10.0, -10.0, 0.0},
- /* first hole */
- {-5 + -1.5, 5 + -1.5, 0.0},
- {-5 + -1.5, 5 + 1.5, 0.0},
- {-5 + 1.5, 5 + 1.5, 0.0},
- {-5 + 1.5, 5 + -1.5, 0.0},
- /* second hole */
- {-1.0, 5 + -1.0, 0.0},
- {-1.0, 5 + 1.0, 0.0},
- { 1.0, 5 + 1.0, 0.0},
- { 1.0, 5 + -1.0, 0.0},
- /* triangle hole */
- {5 + -1.0, 5 + -1.0, 0.0},
- {5 + 0.0, 5 + 1.0, 0.0},
- {5 + 1.0, 5 + -1.0, 0.0},
- {-10.0, -4.0, 0.0},
- { 0.0, -4.0, 0.0},
- {0.0, -3.0, 0.0},
- {-10.0, -3.0, 0.0}
- };
- void
- begin_cb(GLenum which)
- {
- glBegin(which);
- }
- void
- end_cb(void *p)
- {
- glEnd();
- }
- void
- vertex_cb(void *p)
- {
- glVertex3dv(p);
- }
- void
- combine_cb(void *p)
- {
- }
- void
- error_cb(void *p)
- {
- }
- void
- make_scene()
- {
- GLUtesselator *tess;
- GLUquadric *dsk;
- scene_id = glGenLists(1);
- glEnable(GL_DEPTH_TEST);
- glNewList(scene_id, GL_COMPILE);
- /* disk */
- glTranslatef(-5.0, 5.0, 0.1);
- glColor3f(0.3f, 0.7f, 1.0f);
- dsk = gluNewQuadric();
- gluDisk(dsk, 0.7, 1.2, 20, 5);
- gluDeleteQuadric(dsk);
- glTranslatef(5.0, -5.0, -0.1);
- tess = gluNewTess();
- gluTessCallback(tess, GLU_TESS_BEGIN, begin_cb);
- gluTessCallback(tess, GLU_TESS_END, end_cb);
- gluTessCallback(tess, GLU_TESS_VERTEX, vertex_cb);
- gluTessCallback(tess, GLU_TESS_COMBINE, combine_cb);
- gluTessCallback(tess, GLU_TESS_ERROR, error_cb);
- glColor3f(1.0f, 0.0f, 0.0f);
- gluTessBeginPolygon(tess, NULL);
- gluTessBeginContour(tess);
- gluTessVertex(tess, vertices[0], vertices[0]);
- gluTessVertex(tess, vertices[1], vertices[1]);
- gluTessVertex(tess, vertices[2], vertices[2]);
- gluTessVertex(tess, vertices[3], vertices[3]);
- gluTessEndContour(tess);
- gluTessBeginContour(tess);
- gluTessVertex(tess, vertices[4], vertices[4]);
- gluTessVertex(tess, vertices[5], vertices[5]);
- gluTessVertex(tess, vertices[6], vertices[6]);
- gluTessVertex(tess, vertices[7], vertices[7]);
- gluTessEndContour(tess);
- gluTessBeginContour(tess);
- gluTessVertex(tess, vertices[8], vertices[8]);
- gluTessVertex(tess, vertices[9], vertices[9]);
- gluTessVertex(tess, vertices[10], vertices[10]);
- gluTessVertex(tess, vertices[11], vertices[11]);
- gluTessEndContour(tess);
- gluTessBeginContour(tess);
- gluTessVertex(tess, vertices[12], vertices[12]);
- gluTessVertex(tess, vertices[13], vertices[13]);
- gluTessVertex(tess, vertices[14], vertices[14]);
- gluTessEndContour(tess);
- gluTessBeginContour(tess);
- gluTessVertex(tess, vertices[15], vertices[15]);
- gluTessVertex(tess, vertices[16], vertices[16]);
- gluTessVertex(tess, vertices[17], vertices[17]);
- gluTessVertex(tess, vertices[18], vertices[18]);
- gluTessEndContour(tess);
- gluTessEndPolygon(tess);
- gluDeleteTess(tess);
- glEndList();
- }
- void
- display()
- {
- int i, j, k;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glRotatef(-camera_a, 0.0f, 1.0f, 0.0f);
- glRotatef(-camera_b, 1.0f, 0.0f, 0.0f);
- glTranslatef(camera_x, camera_y, camera_z);
- glCallList(scene_id);
- glutSwapBuffers();
- }
- void
- reshape(GLint width, GLint height)
- {
- wnd_width = width;
- wnd_height = height;
- glViewport(0, 0, wnd_width, wnd_height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(65.0, (float)wnd_width / wnd_height, g_nearPlane, g_farPlane);
- glMatrixMode(GL_MODELVIEW);
- }
- void
- pmotion(int x, int y)
- {
- camera_a = (x - wnd_width / 2) / 3;
- camera_b = (y - wnd_height / 2) / 3;
- glutPostRedisplay();
- }
- void
- mbutton(int button, int state, int x, int y)
- {
- GLfloat step = 1.0;
- if (state != GLUT_UP) {
- if (button == 3) {
- camera_x -= step * sin(camera_a * M_PI / 180);
- camera_z += step * cos(camera_a * M_PI / 180);
- }
- if (button == 4) {
- camera_x += step * sin(camera_a * M_PI / 180);
- camera_z -= step * cos(camera_a * M_PI / 180);
- }
- }
- glutPostRedisplay();
- }
- void
- keyboard(unsigned char key, int x, int y)
- {
- switch (key) {
- case 'w':
- case 'W':
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- glDisable(GL_DEPTH_TEST);
- break;
- case 'f':
- case 'F':
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- glEnable(GL_DEPTH_TEST);
- break;
- }
- glutPostRedisplay();
- }
- int
- main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(wnd_width, wnd_height);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("example");
- make_scene();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMotionFunc(pmotion);
- glutMouseFunc(mbutton);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement