In the last assignment, we drew a wire-frame triangle on the screen, which doesnt 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:
- Create the 2D bounding box of the triangle.
- 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.
- If it is inside, then compare the interpolated depth value at its position to the corresponding value from the depth buffer.
- 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 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 cpp
- get model matrix(float rotation angle): For this assignment you dont need to handle any rotation, just return an identity matrix. It is located in the file cpp
- get projection matrix(float eye fov, float aspect ratio, float zNear, float zFar): This is also left empty in the file 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.
- 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 assignments 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.
Reviews
There are no reviews yet.