:PROPERTIES:
:ID: 49e87a3f-2792-4d1c-adc4-ae354bd8b48d
:mtime: 20240309072540
:ctime: 20240309072526
:END:
#+title: rasterization
#+filetags: :public:graphics:project:
* Rasterization
** Rasteriation Algorithm
#+BEGIN_SRC python
FOR every triangle in the scene:
transform its vertices using MVP matrices
IF the triangle is within a view frustum:
clip the triangle to the screen border
FOR each fragment in the triangle:
interpolate fragment position and attributes between vertices
compute fragment colour
IF the fragment is closer to the camera than any pixel drawn so far:
update the screen pixel with the fragment colour
END IF;
END FOR;
END IF;
END FOR;
#+END_SRC
** Interpolating inside Triangles (Barycentric Coordinates)
Homogenous
barycentric
coordinates are
used to
interpolate
colours, normals,
texture
coordinates and
other attributes
inside the triangle
*** Cartesian to Barycentric Coordinates
\[\begin{bmatrix} \lambda_{1} \\ lambda_{2} \end{bmatrix} = T^{-1}(\vec{r} - \vec{r}_{3})\]
where
\[\mathbf{T} = (\vec{r}_{1} - \vec{r}_{3}|\vec{r}_{2} - \vec{r}_{3})\]
You can also convert cartesian to barycentric using the implicit line equation
\[\alpha = \frac{f_{cb}(\vec{r})}{f_{cb}(\vec{r}_{a})}\]
\[\beta = \frac{f_{ac}(\vec{r})}{f_{ac}(\vec{r}_{b})}\]
\[f_{ab}(\vec{r}) = (\vec{r}_{a} - \vec{r}_{b}) \cdot \vec{r} + det(\vec{r}_{a}, \vec{r}_{b}) \]