# Lecture_007_Depth_and_Transparency

Painter's algorithm: with $n$ triangles

• sort triangles with $O(n\log n)$

• $O(n)$ storage

Z-buffer: with $n$ fragments

• $O(1)$ per pixel

• $O(n)$

• realistically, with supersample, your z-buffer is per sample not pixel.

Model transparency: with some probability we don't see the object at all, with $\alpha$ probability, we see the object.

• object edge: can be thought of a layer of transparency object

• combine transparency: say you have a $\alpha_1$ in front and a $\alpha_2$ in the back, the combined probability is $\alpha_1 + (1 - \alpha_1)\alpha_2$ (the probability of hitting the first layer plus the probability of miss the first layer and hit the second)

• But this isn't good, we need to assume two events are independent (well, in some case it is a bad assumption, but let's go with it)
• One example of non-independent is: when two triangles both occupy unoccupied space in one pixel. In this case, we will underestimate coverage.
• With assumption of independence (both color value and opaque value), we can use the following equation for calculate color: $c_{12} = \frac{\alpha_1 c_1 + (1 - \alpha_1)\alpha_2 c_2}{\alpha_1 + (1 - \alpha_1)\alpha_2}$
• We can transform the above equation be defining pre-multiplied alpha $C_1 = \alpha_1 \cdot c_1, C_2 = \alpha_2 \cdot c_2$ (but with pre-multiplied alpha, we lose feudality because we are storing (r, g, b, a) into (r, g, b))
• depth order: if we rasterize front to back, then we can "early out"

So in practice:

1. render opaque objects only with z-buffer
2. render transparency back to front with painter's algorithm (sorting transparent triangles first) with no update to z-buffer but with depth test.

For better version of painter's algorithm: search Order-independent transparency.

Alpha Blending:

C_{\text{frame buffer}} = \alpha_{scr} \cdot C_{src} + (1 - \alpha_{scr})\cdot C_{\text{frame buffer}}

C_{\text{frame buffer}} = \max(C_{max}, \alpha_{scr} \cdot C_{src} + C_{\text{frame buffer}})

## Occlusion and Depth Buffer

Depth Buffer: we keep track the depth of the closest triangle seen so far 0. initialize depth for each pixel (super sample) to infinity

1. randomly select a not drawn triangle from buffer
2. for each pixel (super sample), draw that triangle if its depth value is smaller than stored value, don't draw if its depth value is larger than stored value
3. update new depth buffer if pixel (super sample) get drawn
4. repeat until all triangles are drawn

The above technique will work fine with supersample. You obtain multiple copies of frame buffer and then merge them into one.

Space: constant space per sample for depth buffer, don't depend on overlapping primitives

Time: constant time per covered sample

## Transparency and Alpha

### Non-Premultiplied Alpha

Over operator for non-premultiplied alpha: non-commutative blending of tinted glass

Given $A = (A_r, A_g, A_b)$ with alpha $\alpha_A$ and $B = (A_r, A_g, A_b)$ with alpha $\alpha_B$, to compute $B$ over $A$, we get: $C = \alpha_BB+ (1 - \alpha_B)\alpha_AA, \alpha_C = \alpha_B + (1 - \alpha_B)\alpha_A$

### Premultiplied Alpha

Premultiplied Alpha: compute $B$ over $A$

• $A' = (\alpha_A A_r, \alpha_A A_g, \alpha_A A_b, \alpha_A)$

• $B' = (\alpha_B B_r, \alpha_B B_g, \alpha_B B_b, \alpha_B)$

• $C' = B' + (1 - \alpha_B) A'$

• $(C_r, C_g, C_b, \alpha_C) \xrightarrow{\text{becomes}}(\frac{C_r}{\alpha_C}, \frac{C_g}{\alpha_C}, \frac{C_b}{\alpha_C})$

• This is exactly how we compose RGB value (we are expressing color in homogeneous coordinates)

Premultiplied alpha is closed under composition

Render mixture of opaque and transparent triangles

1. render all opaque primitives in any order
2. disable write to depth buffer, render semi-transparent triangles in back-to-front order. If depth test passed, triangle is over color buffer, otherwise don't draw. (we need to sort semi-transparent triangles and hope they don't intersect)

## Full Rasterization Pipeline

Steps:

1. Transform triangle vertices into camera-centered world space (inverse of camera transform)
2. Apply perspective projection into normalized coordinate space
3. Clipping: discard triangles lie outside (culling) and clip triangles to box (possibly generate new triangles)
4. Transform normalized coordinates into screen coordinates and to image coordinates
5. Pre-compute data in Barycentric Coordinates
6. Sample Coverage
7. Filtering, MIP Map, Sample Texture, Interpolation
8. Depth Test and Update Depth Value

Table of Content