Assignment 4: 3D Viewing and Polygon Clipping ITCS 6120/8120 Computer Graphics Fall 2009 Revision: 2/15/2016 6:25 PM Professor Zachary Wartell Due Date: Friday, December 11 2009 11:59PM. Associated Material: Textbook Chapter 5,6,7 Chapter 3 of the OpenGL Programming Guide (Redbook) Overview: The purpose of this assignment is to render a 3D scene in OpenGL consisting of a number of polygonal tetrahedron (a 4 sided figure, each face is a triangle) and a wire frame box and to implement the 3D polygon clipping algorithm so that the displayed tetrahedron’s triangle faces are clipped to the boundary of the wire frame box. The user interface (UI) should allow the following. 1) 2) 3) The UI should allow the view of the scene to be rotated around the scene. The view should always look at a fixed point in the center of the scene but the UI can rotate the view up/down and left/right. Key’s ‘a’ and ‘d’ should rotate left/right Key’s ‘s’ and ‘w’ rotate up and down. The position and size of the wire-frame box should be controllable by a user interface. keys ‘h’ and ‘k’ move box in world x-axis keys ‘j’ and ‘u’ move box in world y-axis keys ‘l’ and ‘o’ move box in world z-axis keys ‘1’ and ‘2’ scale box in world x-axis keys ‘3’ and ‘4’ scale box in world y-axis keys ‘5’ and ‘6’ scale box in world z-axis The user interface should allow enabling and disabling of the clipping of the tetrahedron’s triangles to the boundaries of the wire-frame box. ‘space bar’ toggles clipping on and off An example picture of the application is below. The figure above shows what would be displayed when clipping is disabled. The figure below shows what would be displayed when clipping is enabled. Here, tetrahedrons outside the clipping box are not displayed at all. The triangle faces of the tetrahedron inside the box are clipped to the front face of the box. Note, that the dashed lines shown in this picture would not appear on your program’s screen. They are simply drawn here to indicate that part of the tetrahedron was clipped away. Details: 1) The scene should contain at least 5 tetrahedron at different positions. Each face of the tetrahedron must be in a different color. Setting up 3D Viewing Chapter 3 of the OpenGL Programming Guide covers OpenGL 3D viewing and transformations. Chapter 3’s cube.c example is a good starting point. This code is available in the GLUT source code distribution under glut\progs\redbook. Add hidden surface elimination Enable OpenGL depthbuffering. (see glEnable, GL_DEPTH_BUFFER and GL_DEPTH_BUFFER_BIT; also note that glutInitDisplayMode must be told to allocate the depthbuffer for a GLUT window). disable backface culling (see glDisable, GL_CULL_FACE) Section 9-14 of the textbook covers the basics of setting up hidden surface elimination. As does Chapter 10 of the OpenGL Programming Guide. Drawing the Clipped Tetrahedrons To draw the tetrahedron faces use GL_POLYGONS. (After running your clipping code, the faces won’t necessarily be triangles, so plan your data structure accordingly). Each face should be a different color. Your data structure should store the vertex coordinates in a local coordinate system associated with an individual clipped tetrahedron object. I’ll refer to this as the tetrahedron’s own coordinate system. Each 3D object should contain a location data member that specifies the location of the object’s own coordinate system in world space. You should render the vertices by passing OpenGL the coordinate’s in the object’s own coordinate system and using glTranslate, etc. to tell OpenGL to transform the coordinates for rendering purposes. 2) You should draw the wire frame box by using the coordinates of a unit cube for a GL_LINES primitive and using glTranslate and glScale to alter its rendered size and position. For the box’s coordinates in its own coordinate system use corner vertices (-1,1,-1) and (1,1,1). 3) To implement the view rotation the target point of the gluLookAt function’s view specification is constant while the eye point location should move over the surface of a sphere. An easy way to do this is to use the parametric equation for the surface of a sphere (see textbook Appendix A-9) to map two angle values to the x,y,z coordinates of a point on a sphere. The eyepoint is assigned the x,y,z coordinates computed from the two angle values which are controlled by the user interface—one angle is controlled by the up/down key and the other is controlled by the left/right key. 4) To perform the clipping you must do the following. First you must implement the Sutherland-Hodgeman polygon clipping algorithm for the normalized 3D cube whose opposite corner have vertices (-1,-1,-1) and (1,1,1). This requires extending the Sutherland-Hodgeman 2D polygon clipping algorithm as described in your textbook (Chapter 6, pg 333). Second, you must transform the coordinates of the faces of the tetrahedron from each tetrahedron’s own coordinate system into the wire frame box’s normalized coordinate system. This is because the standard Sutherland-Hodgeman implementation is optimized to clip to the normalized cube with corners (-1,-1,-1) and (1,1,1). The transformation matrix needed is: Mclip←tetrahdron = Mclip←box · Mbox←world · Mworld←tetrahedron Mworld←tetrahdron accounts for the transform from a tetrahedron’s own coordinates to world coordinates. Mbox←world accounts for the transform from world coordinates to box coordinates. Mclip←box maps a cube who size is the boxes current world coordinate size onto the normalized cube. After this transformation, you perform the clipping algorithm on the face’s vertices in normalized clipping coordinates (Clip for short). Note, the transformation, Mclip←tetrahdron, is affine--in particular it’s a concatenation of a sequence of translates and scales. Therefore, the w coordinate will always be 1 after transforming a vertex coordinate. This implies that when clipping, you do not have to clip in 4d homogeneous coordinates but rather can just clip in the x,y,z coordinates. This fact makes your clipping algorithm simpler to implement than the one required for view frustum clipping in the graphics pipeline because view clipping includes a projection and hence clips in 4D coordinates. After clipping a polygon, you have a new set of vertices in Clip coordinates. To render these coordinates, the new polygon vertices must be transformed from Clip coordinates back to each tetrahedron’s own coordinate system (Mtetrahedron←clip = (Mclip←tetrahedron)-1) and stored in the clipped tetrahedron object. These will be rendered as described in Item 1. I suggest creating two copies of the set of tetrahedron objects. The first copy always stores the unclipped tetrahedrons. The second copy stores the clipped tetrahedrons and is regenerated every time the clip box is moved or resized. (All tetrahedron objects store their coordinates relative to each object’s own coordinate system). Note, that you will have to either write code to construct these transformation matrices and to transform the vertices and between coordinate spaces or you may use the GLM (OpenGL Mathematics) library available at http://glm.g-truc.net/. Note, GLM is an industry strength library--very powerful but somewhat complex. It implements all the operators found in the OpenGL Shading Language (http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.30.08.withchanges.pdf) Hint: If you decide not to use GLM then: Build a 4 by 4 matrix class with various methods for assigning a matrix a particular type of transformation (translate by x,y,z, etc.) and multiplying matrices together and as well as other matrix operations. Similarly you’ll probably need a class for representing 3D point coordinates and functions that can multiply a 3D point by a matrix, etc. (For rendering purposes, you again use GL transform routines so the graphics pipeline does the transformations during rendering). Hand-in Directions: Turn in using SVN. Grading: 1. Rendering 40% a. 40% clipping box b. 60% tetrahedrons 2. Interaction 20% a. 60% view rotation b. 40% clipping box translate and scale 3. Clipping 40% a. 20% clipping against all 6 faces works b. 40% clipping implemented with coordinate transformation c. 40% clipping algorithm optimized for normalized cube