$40
Assignment 3: Triangles and Z-buffering CS180/CS280
Notes:
• Be sure to read the Programming and Collaboration Policy on course website. • Any updates or correction will be posted on Piazza, so check there occasionally.
• You must do your own work independently.
• Read through this document carefully before posting questions on Piazza.
• Submit your assignment on GauchoSpace before the due date.
1 Overview
In the last assignment, we drew a wire-frame triangle on the screen, which doesn’t look so interesting. This time we will move one step forward – draw solid triangles on the screen, in other words, rasterize a triangle. Last time, after the view-port transformation, we called the rasterize wireframe(const Triangle& t), this time you need to implement the function rasterize triangle(const Triangle& t). The general workflow inside this function is:
1) Create the 2D bounding box of the triangle.
2) Iterate through all the pixels (using their integer indices) that are inside this bounding box. Then, use the screen-space coordinate of the center of the pixel to check if the center point is inside the triangle.
3) If it is inside, then compare the interpolated depth value at its position to the corresponding value from the depth buffer.
4) If the current point is closer to the camera, set the pixel color and update the depth buffer.
Functions that you need to edit:
• rasterize triangle: Perform the triangle rasterization algorithm, (the workflow which is mentioned above). It is located in the file rasterizer.cpp
• static bool insideTriangle(int x, int y, const Vector3f* v): Test if a point is inside a triangle. You can change the definition of this function. That means, you can update the return type, or the function parameters the way you want, as mentioned this function should be called from rasterize triangle(const Triangle& t). It is located in the file rasterizer.cpp
• get model matrix(float rotation angle): For this assignment you don’t need to handle any rotation, just return an identity matrix. It is located in the file main.cpp
• get projection matrix(float eye fov, float aspect ratio, float zNear, float zFar): This is also left empty in the file main.cpp. Cpoy-paste your implementation of this function from second assignment.
Since we only know the depth value at the three vertices of the triangle. For the pixels inside the triangle, we need to do the interpolation to get its depth value. We have handled this part for you, since it is the topic that has not been covered in the lecture yet. The interpolated depth value is saved in the variable z_interpolated. You can find this part of the code in the comments in the rasterize triangle(const Triangle& t)
Pay attention to how we initialize the depth buffer and the sign of your z values.
Finally, we provide two hard-coded triangles to test your implementation, if you do it correctly, you will see the output image like this:
The text will not be there and also note how one triangle is on top of another. You will get an incorrect result if the depth testing part is not implemented correctly.
2 Getting started
Download and use our updated skeleton code package either on your own machine or in the virtual machine. The dependencies are exactly the same as used in the second assignment, so make sure to follow the same steps to run the code as mentioned in the second assignment. Do note that this assignment’s skeleton is slightly different and rotation will not work, but the commands to build and run the code is exactly like how it was in second assignment(except the rotate part). Please refer to the second assignment for more instructions.
3 Grading and Submission Grading:
• [5 points] Submission is in the correct format, includes all necessary files. Code can compile and run.
• [20 points] Implement the triangle rasterization algorithm correctly.
• [10 points] Implement the inside triangle test correctly.
• [10 points] Implement the z-buffer algorithm correctly. The triangles are drawn on screen in correct order.
• [Bonus 5 points] Anti-aliasing by super-sampling: You may notice that the result image is quite jaggy when we zoom in. We can solve this by supersampling. Sample each pixel by 2*2 samples and compare the result before and after. You need to create a larger frame buffer to do this. One way to think about is that instead of considering only the center of the pixel, each pixel will now have 4 samples (if 2*2 is used). Color of the pixel will be the average of all the samples’ colors.
Submission:
After you finish the assignment, clean your project, remember to include the CMakeLists.txt in your folder, whether you modified it or not. Add a short PDF report (one page strictly) in the directory, write down your full name and perm number inside it. Add your ouput image in the report. Tell us whether you did the bonus part or not, and briefly describe what you have implemented in each function. Then compress the entire folder and rename it with your name, like ”Subramaniyam.zip”. Submit the .zip file on GauchoSpace.