Question

Question image

Perspective-Correct Barycentric Interpolation in Triangles

Original question: I've seen how weights in a line in 3D space are perspective-corrected anc displayed on a screen as shown in the image. But I can't seem to find a proper explanation on how you'd do the same but for a point in a triangle whose weights would be it's barycentric coordinates. 1 image plane (X,Z₁) S 1 (1-t) (1-q) (X,Z) S q t X So 0 (X,Z) Z (coordinate system not to scale)

Expert Verified Solution

thumb_up100%(1 rated)

The image illustrates a 2D side view of a perspective projection. It shows a line segment in "view space" with endpoints (X0,Z0)(X_0, Z_0) and (X1,Z1)(X_1, Z_1), with an intermediate point (X,Z)(X, Z) defined by a weight tt. These are projected onto an image plane at Z=1Z=1, resulting in screen coordinates S0,S,S1S_0, S, S_1. The diagram demonstrates that the interpolation weight qq in screen space is not equal to the weight tt in world/view space due to the non-linear nature of the perspective divide (1/Z1/Z).

Answer

To perform perspective-correct interpolation in a triangle, you must interpolate the values divided by depth (Ii/ZiI_i/Z_i) and the reciprocal of depth (1/Zi1/Z_i) using screen-space barycentric coordinates (λ0,λ1,λ2)(\lambda_0, \lambda_1, \lambda_2). The final attribute is recovered by dividing the interpolated product by the interpolated reciprocal depth.

Explanation

  1. Analyze the non-linearity of projection In perspective projection, a 3D point (X,Y,Z)(X, Y, Z) projects to screen coordinates sx=X/Zs_x = X/Z and sy=Y/Zs_y = Y/Z. Because ZZ varies linearly across a triangle in 3D space, 1/Z1/Z varies linearly in screen space. Linear interpolation of an attribute AA directly in screen space leads to "texture warping" because screen horizontal/vertical steps do not correspond to equal steps in 3D space.

  2. Define the perspective-correct formulation Let a triangle have vertices V0,V1,V2V_0, V_1, V_2 with depths Z0,Z1,Z2Z_0, Z_1, Z_2 and attribute values I0,I1,I2I_0, I_1, I_2. In screen space, we calculate barycentric coordinates (λ0,λ1,λ2)(\lambda_0, \lambda_1, \lambda_2) such that λi=1\sum \lambda_i = 1. The attribute II at a pixel is found using: I(s)Z(s)=λ0I0Z0+λ1I1Z1+λ2I2Z2\frac{I(s)}{Z(s)} = \lambda_0 \frac{I_0}{Z_0} + \lambda_1 \frac{I_1}{Z_1} + \lambda_2 \frac{I_2}{Z_2} The interpolated attribute divided by depth is a linear combination of the vertices' attributes divided by their respective depths.

  3. Interpolate the reciprocal depth To isolate I(s)I(s), we need the depth Z(s)Z(s) at that specific pixel. We use the property that 1/Z1/Z is linear in screen space: 1Z(s)=λ01Z0+λ11Z1+λ21Z2\frac{1}{Z(s)} = \lambda_0 \frac{1}{Z_0} + \lambda_1 \frac{1}{Z_1} + \lambda_2 \frac{1}{Z_2} The reciprocal of the projected depth is the linearly interpolated sum of the reciprocal depths of the vertices.

  4. Recover the final attribute value Finally, we divide the result of Step 2 by the result of Step 3. This cancels out the 1/Z1/Z term, leaving the perspective-correct attribute. I(s)=i=02λiIiZii=02λi1ZiI(s) = \frac{\sum_{i=0}^2 \lambda_i \frac{I_i}{Z_i}}{\sum_{i=0}^2 \lambda_i \frac{1}{Z_i}} The final attribute is the weighted sum of depth-divided attributes normalized by the weighted sum of reciprocal depths.

Final Answer

The perspective-correct barycentric interpolation for an attribute II across a triangle is given by: Ipixel=λ0I0Z0+λ1I1Z1+λ2I2Z2λ0Z0+λ1Z1+λ2Z2\boxed{I_{pixel} = \frac{\frac{\lambda_0 I_0}{Z_0} + \frac{\lambda_1 I_1}{Z_1} + \frac{\lambda_2 I_2}{Z_2}}{\frac{\lambda_0}{Z_0} + \frac{\lambda_1}{Z_1} + \frac{\lambda_2}{Z_2}}}

Common Mistakes

  • Linear Screen Interpolation: Assuming that λ0I0+λ1I1+λ2I2\lambda_0 I_0 + \lambda_1 I_1 + \lambda_2 I_2 will yield the correct result. This causes "affine mapping" artifacts, which are highly visible in low-poly models with textures (textures appear to "swim" or bend as the camera moves).
  • Using ZZ instead of WW: In modern graphics APIs (OpenGL/DirectX), the value used for the division is usually the clip-space WW component. While WW is proportional to view-space ZZ, using the wrong depth buffer value (like a non-linear ZbufferZ_{buffer} value) will break the math.

Related Topics: Rasterization, Hyperbolic Interpolation, Texture Mapping, Homogeneous Coordinates.

FAQ

Why can't I simply interpolate attributes linearly in screen space?

Perspective projection makes depth Z non-linear in screen space, so linear interpolation causes texture warping (affine mapping artifacts). You must interpolate attributes divided by depth.

What is the formula for perspective-correct barycentric interpolation?

The correct formula is: I_pixel = (λ0 I0/Z0 + λ1 I1/Z1 + λ2 I2/Z2) / (λ0/Z0 + λ1/Z1 + λ2/Z2), where λ are screen-space barycentric coordinates.

What common mistake leads to affine mapping artifacts?

Using simple linear interpolation of attributes (λ0 I0 + λ1 I1 + λ2 I2) instead of the depth-weighted method. This ignores the non-linear 1/Z term.

chat