// OpenGL Tutorial // Polygons.c - TJ VERSION // http://www.eecs.tulane.edu/www/Terry/OpenGL/Introduction.html /************************************************************************* This example extends Hello_World.c to accept and handle events. The user should try produce each of the following events: key press, mouse movement, mouse button press, mouse button release, reshape window, and expose window. *************************************************************************/ // gcc polygons.c -lm -L/usr/lib -lGLU -lGL -lglut -L/usr/X11R6/lib -lX11 -lXext -lXi -lXmu #include #include #include #include void init() { // Clear the window glClear(GL_COLOR_BUFFER_BIT); } void reshape(int width, int height) { // Set the new viewport size glViewport(0, 0, (GLint)width, (GLint)height); // Choose the projection matrix to be the matrix // manipulated by the following calls glMatrixMode(GL_PROJECTION); // Set the projection matrix to be the identity matrix glLoadIdentity(); // Define the dimensions of the Orthographic Viewing Volume glOrtho(-8.0, 8.0, -8.0, 8.0, -8.0, 8.0); // Choose the modelview matrix to be the matrix // manipulated by further calls glMatrixMode(GL_MODELVIEW); } void keypressed(unsigned char key, int x, int y) { if (key == (unsigned char) 27) exit(0); } void draw(void) { // Clear the RGB buffer and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Set the modelview matrix to be the identity matrix glLoadIdentity(); // Translate and rotate the object glTranslatef(-2.5, 0.0, 0.0); glRotatef(-30, 1.0, 0.0, 0.0); glRotatef(30, 0.0, 1.0, 0.0); glRotatef(30, 0.0, 0.0, 1.0); glColor3f(1.0, 0.0, 1.0); // Draw the sides of the three-sided pyramid glBegin(GL_TRIANGLE_FAN); glVertex3d(0, 4, 0); glVertex3d(0, -4, -4); glVertex3d(-4, -4, 4); glVertex3d(4, -4, 4); glVertex3d(0, -4, -4); glEnd(); glColor3f(0.0, 1.0, 1.0); // Draw the base of the pyramid glBegin(GL_TRIANGLES); glVertex3d(0, -4, -4); glVertex3d(-4, -4, 4); glVertex3d(4, -4, 4); glEnd(); glLoadIdentity(); glTranslatef(2.5, 0.0, 0.0); glRotatef(45, 1.0, 0.0, 0.0); glRotatef(45, 0.0, 1.0, 0.0); glRotatef(45, 0.0, 0.0, 1.0); glColor3f(0.0, 1.0, 0.0); // Draw the sides of the cube glBegin(GL_QUAD_STRIP); glVertex3d(3, 3, -3); glVertex3d(3, -3, -3); glVertex3d(-3, 3, -3); glVertex3d(-3, -3, -3); glVertex3d(-3, 3, 3); glVertex3d(-3, -3, 3); glVertex3d(3, 3, 3); glVertex3d(3, -3, 3); glVertex3d(3, 3, -3); glVertex3d(3, -3, -3); glEnd(); glColor3f(0.0, 0.0, 1.0); // Draw the top and bottom of the cube glBegin(GL_QUADS); glVertex3d(-3, -3, -3); glVertex3d(3, -3, -3); glVertex3d(3, -3, 3); glVertex3d(-3, -3, 3); glVertex3d(-3, 3, -3); glVertex3d(3, 3, -3); glVertex3d(3, 3, 3); glVertex3d(-3, 3, 3); glEnd(); // Flush the buffer to force drawing of all objects thus far glFlush(); } int main(int argc, char **argv) { // Set top left corner of window to be at location (20, 20) // Set the window size to be 500x500 pixels // Open a window and name it // Open a window, name it "Events" glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(20,20); glutCreateWindow("Points_Lines"); // Set the clear color to black glClearColor(0.0, 0.0, 0.0, 0.0); // Set the shading model glShadeModel(GL_FLAT); // Set the polygon mode to fill glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // Enable depth testing for hidden line removal glEnable(GL_DEPTH_TEST); init(); // Assign reshape() to be the function called whenever // a reshape event occurs glutReshapeFunc(reshape); // Assign keypressed() to be the function called whenever // a key is pressed glutKeyboardFunc(keypressed); // Assign draw() to be the function called whenever a display // event occurs, generally after a resize or expose event glutDisplayFunc(draw); // Pass program control to GLUT's event handling code // In other words, loop forever glutMainLoop(); return 0; }