## Homework 2: Due Wednesday February 21, 2018, by class time (10/100 points)

### Homework 2 Overview

In this project, you will load a mesh stored in the .ply file format, render it as a 3D wireframe model using Vertex Buffer Objects and also add keyboard control that lets us interact with the .ply files. A few optional preparation steps are suggested. You will not turn in the code which you generate in your preparatory steps.

### Preparation

• Read section 3.6 of your text: Code to render a cube is described in that section.

• Some starter code, a working implementation of this cube program, which runs in the zoolab has been created. You can get this starter code here [ Starter Code ] .
Compile the cube program and make sure it runs okay in the zoolab. As mentioned previously, I will help mostly with questions about getting the starter code working in the zoolab. However, one problem that some students may have problems on their home machines is that OpenGL 3.2 has problems with glGenVertexArrays under Linux. You can find some fixes to this bug on the class [ FAQ Page ]

• Modify your program to read in .ply files and store them in a vertex list data structure. A vertex list is described in some detail in Section 3.6.3 of the text. You can get 43 PLY files to work with [ Here ] . Further explanations about the format for PLY files are given below.

• Modify your program to render wireframe drawings of your .ply files from your vertex list using Vertex Buffer Objects (VBOs) and glDrawArrays as with the cube. Here's an example wireframe drawing of the cow.ply file:

• Set up a Current Transform Matrix (CTM) as described in section 3.11 of the text. The starter code includes [ Angel.h ] which already includes [ mat.h ] and [ vec.h] You may use the matrix and vector manipulation methods in these header files for your work.

• Randomly assign colors of edges: Define a set of 8 unique colors. Draw each edge of the wireframe mesh by picking one of these edges randomly

• Implement 3D Shearing: Implement shearing along the X axis. Make the shearing factor h a variable that you can modify to alter the amount of shearing

• Twist the mesh: Twist is similar to rotation about the origin except that the amount of rotation increases by a factor f the farther a point is from the origin. Implement a "Twist" function around the y axis that can be applied to your mesh. Make the twist factor f a variable that you can modify to alter the amount of twist applied to the mesh.

• Implement keyboard controls that enable you to perform the keyboard controls described below in the section "Behavior of your submitted program".

### Behavior of your submitted program

• User hits 'W' (Draw your wireframe) at a suitable initial position from the viewer.

• User hits 'N' (Draw next wireframe) Organize the PLY files in a list going from 1-43. Hitting N should load and draw the next wireframe model to the current one in your list of PLY files. You can hardcode filenames if you want. The PLY files may not all be of the same size. So to properly set up the viewing position using LookAt, you may have to calculate the bounding box of the mesh and then set your view distance to a suitable multiple of the bounding box

• User hits 'P' (Draw previous wireframe) Organize the PLY files in a list going from 1-43. Hitting P should load and draw the previous wireframe model to the current one in your list of PLY files.

• User hits 'X' (Translate your wireframe in the +ve X direction) Continously move your wireframe some small units along the +ve X axis and redraw it. Use the idle function to animate this. The ply file should continue to slide along the +ve X axis till the user hits 'X' again. Essentially, the 'X' key acts as a toggle key. If the ply file is stationary and the user hits the 'X' key, the ply file should continue to slide along the +ve X axis until the user hits 'X' again. Camera position remains fixed for this translation and all other translations below. The exact amount to move the ply file before redrawing will affect how much and how much your translation is apparent depends on how far you positioned your wireframe from the viewer. So, it's left to you as a design choice to pick an appropriate distance to translate the wireframe along the +ve X axis each time the user hits 'X'.

• User hits 'x' (Translate your wireframe in the -ve X direction) Use the idle function to continuously move your wireframe some units along the -ve X axis. The number of units to translate your wireframe each time the user hits 'x' is left to you as a design choice.

• User hits 'Y' (Translate your wireframe in the +ve Y direction) Use the idle function to continuously move your wireframe some units along the +ve Y axis. The number of units to translate your wireframe each time the user hits 'Y' is left to you as a design choice.

• User hits 'y' (Translate your wireframe in the -ve y direction) Use the idle function to continuously move your wireframe some units along the -ve Y axis. The number of units to translate your wireframe each time the user hits 'y' is left to you as a design choice.

• User hits 'Z' (Translate your wireframe in the +ve Z direction) Use the idle function to continuously move your wireframe some units along the +ve Z axis. The number of units to translate your wireframe each time the user hits 'Z' is left to you as a design choice.

• User hits 'z' (Translate your wireframe in the -ve Z direction) Use the idle function to continuously move your wireframe some units along the -ve Z axis. The number of units to translate your wireframe each time the user hits 'z' is left to you as a design choice.

Hint: Use double buffering (glutSwapBuffers( )) to make the rotation smooth. You can continously update the new wireframe positions and redisplay the meshes in the glutIdleFunc function.

• User hits Key 'c': Toggle between 1 color and randomly selected colors for edges: When toggled ON, the mesh edges are drawn using randomly assigned colors. When OFF, the mesh edges are drawn using red.

• User hits Key 'h': Increment the amount of shearing of the wireframe along the X axis by a small amount. Repeatedly hitting the 'h' key should shear the wireframe by a bit more and more. Note that after you shear the mesh, performing a transform (e.g. rotation, scale or translate) should transform the sheared mesh.

• User hits Key 'H': Decrease the amount of shearing of the wireframe along the X axis by a small amount. Repeatedly hitting the 'H' key should shear the wireframe by a bit less and less. Note that after you shear the mesh, performing a transform (e.g. rotation, scale or translate) should transform the sheared mesh.

• User hits Key 't': Increment the amount of twisting of the wireframe around the Y axis by a small amount. Repeatedly hitting the 't' key should twist the wireframe by a bit more and more. Note that after you twist the mesh, performing a transform (e.g. rotation, scale or translate) should transform the twisted mesh.

• User hits Key 'T': Decrease the amount of twisting of the wireframe around the Y axis by a small amount. Repeatedly hitting the 'T' key should twist the wireframe by a bit less and less. Note that after you twist the mesh, performing a transform (e.g. rotation, scale or translate) should transform the twisted mesh.

Also note that it should be possible to apply twists to sheared meshes, and vice versa. In general, it should be possible to concatenate transforms.

### PLY File format

This file format, also known as the Stanford Triangle Format, looks like:

 File Text What you should do Ply If not present, exit Format ascii 1.0 Skip line element vertex 758 Read # of vertices (758) property float32 x property float32 y property float32 z Skip these lines element face 1140 Read # of polygons (1140) property list uint8 int32 vertex_indices Skip line end_header End of header section ? skip line 6.5 -7.2 1.1 0.5 0.8 -1.5 1.2 9.0 5.5 etc. Coords of Vertex #0 Coords of Vertex #1 Coords of Vertex #2 And so on, until Vertex #757 3 1 9 8 3 5 10 5 3 7 0 9 etc. First no.=#vertices in polygon. In our case, it?s always 3. Then the next three numbers tell you which vertices make up that polygon. So, triangle #1 is made from vertices #1, #9, and #8.

• After changing x, y or z location, or rotating the wireframe (R key), W resets position and rotation by drawing the wireframe at origin and with no rotation applied. W does NOT reset shear and twist effects.

• Pressing 'X' and then 'x' moves the PLY file along +x then -x. Translations are generally concatenated. Hitting 'X' and then 'Y' moves the PLY file along the +x direction and then WITHOUT RETURNING TO ORIGIN, moves the PLY file along +y direction.

• If the PLY file is translating when the 'R' key is hit, just stop the translation and rotate the PLY file about the Y axis AT THE CURRENT LOCATION. Do NOT reset the PLY file to origin before applying the rotation.

• The 'R' key is hit once and then the current PLY file is rotated clockwise 360 degrees. After this 360 degree rotation, this current PLY file is erased and the next PLY file is drawn at the same position and then rotated ANTI-CLOCKWISE 360 degrees. This file is then erased and the next PLY file is drawn and rotated CLOCKWISE 360 degrees, and so on. Essentially, PLY file 1 is rotated clockwise, PLY file 2 is rotated anti-clockwise, PLY file 3 is rotated clockwise, etc. If the user hits 'R' once and just watches, PLY files 1-43 will eventually be displayed one by one WITHOUT ANY ADDITIONAL keys being pressed.

• If shearing is applied and you decrease shearing (H key), the shearing should be reduced till a value of 0 (no shearing). Trying to reduce shearing further beyond 0 should have no effect. The same goes for twist. Trying to reduce twist beyond 0 should have no effect.