Repository navigation
Lab 2: Rasterizing a Triangle and Line
In this lab, you will learn how to render a triangle and a line using rasterization, which is the method used by modern GPUs to render complex scenes in real time.
Important Note: If you clone the repository using git from the command line, use git clone --recursive https://github.com/arthurfirmino/icg.git to correctly download all dependencies.
Ensure you have cloned or downloaded the latest version of the icg repository. Open the CMakeLists.txt file that is in the ROOT of this repository (not one of the ones in the sub-folders) as a project in QT Creator.
If no files appear under the "icg" project in the sidebar, make sure that QT Creator has been configured with the location of CMake by checking if there is an entry under Projects -> Manage Kits -> CMake with the value /usr/local/bin/cmake. Please ask for assistance if you can't successfully build and run the provided code.
You are provided with skeleton code in icg/rasterize/main.cpp that you can fill in to complete this exercise.
Your task is to rasterize a triangle into the image, as well as a line using Bresenham's Line Algorithm.
You should only need to complete the TODO items in the code to complete the exercise.
Rasterization makes use of a coordinate system called barycentric coordinates where a position on a plane is represented as a linear combination of three points (a triangle). You can calculate the barycentric coordinate C1 of point P that corresponds to point V1 on the triangle (V1, V2, V3) as the ratio of the area of triangle (P, V2, V3) to the area of (V1, V2, V3). Note that these are signed areas, so the value of the area can be negative.
For more information on barycentric coordinates see the course notes or this Wikipedia page.
Rasterization makes use of the fact that the three barycentric coordinates of a point lie in the interval (0,1) and sum to one only when the point is inside the triangle. The resulting algorithm is simple: for each pixel and each triangle, calculate the barycentric coordinates of the current pixel for the current triangle, and test whether they lie in the interval (0,1). If they do, the pixel is covered by the triangle.
Bresenham's Algorithm is an efficient way of rasterizing a line. See this Wikipedia page for an explanation of the algorithm.