Depth buffer

A simple and efficient method to resolve this problem is to manage the depth problem on a pixel-by-pixel basis by maintaining a depth buffer (also called z-buffer), which for every pixel records the distance of the triangle from the focal point to the intersection point of the ray that intersects the triangle at that pixel. In other words, if this were the ray casting approach, it would be distance along the ray from the focal point to the intersection point. Using this method, the triangles can be rendered in arbitrary order. The method is also commonly applied to compute the effect of shadows by determining depth order from a light source, rather than the viewpoint. Objects that are closer to the light cast a shadow on further objects.

The depth buffer stores a positive real number (floating point number in practice) at every pixel location. Before any triangles have been rendered, a maximum value (floating-point infinity) is stored at every location to reflect that no surface has yet been encountered at each pixel. At any time in the rendering process, each value in the depth buffer records the distance of the point on the most recently rendered triangle to the focal point, for the corresponding pixel in the image. Initially, all depths are at maximum to reflect that no triangles were rendered yet.

Figure 7.7: If $ p$ is inside of the triangle, then it must be to the right of each of the edge vectors, $ e_1$, $ e_2$ and $ e_3$. Barycentric coordinates specify the location of every point $ p$ in a triangle as a weighted average of its vertices $ p_1$, $ p_2$, and $ p_3$.

Each triangle is rendered by calculating a rectangular part of the image that fully contains it. This is called a bounding box. The box is quickly determined by transforming all three of the triangle vertices to determine the minimum and maximum values for $ i$ and $ j$ (the row and column indices). An iteration is then performed over all pixels inside of the bounding box to determine which ones lie in inside the triangle and should therefore be rendered. This can be quickly determined by forming the three edge vectors shown in Figure 7.7 as

\begin{displaymath}\begin{array}{l} e_1 = p_2 - p_1  e_2 = p_3 - p_2  e_3 = p_1 - p_3 . \end{array}\end{displaymath} (7.8)

The point $ p$ lies inside of the triangle if and only if

$\displaystyle (p - p_1) \times e_1 < 0$    , $\displaystyle (p - p_2) \times e_2 < 0$    , $\displaystyle (p - p_3) \times e_3 < 0,$ (7.9)

in which $ \times$ denotes the standard vector cross product. These three conditions ensure that $ p$ is ``to the left'' of each edge vector.

Steven M LaValle 2020-01-06