Introduction
Typically in computer graphics, we wish to interpolate values associated with perspectively-projected vertices of a line segment. The dimension N eye-space vertices and are projected onto a dimension N-1 hyperplane called screen-space. The values associated with each vertex (e.g. color, texture-coords, normals) are typically interpolated along the line segment based on the screen-space projected coordinates of the corresponding vertices. However, the values required at each interpolated screen-space point need to be the values as if interpolated in eye-space and projected onto the interpolated position.
The relationship
between interpolation in screen-space and in eye-space is nonlinear. Linearly
interpolating the The resulting values from linearly interpolating some
fraction t between two screen-space vertices do not match the eye-space
interpolated values of the eye-space point projecting on the interpolated
screen-space point. In order to obtain the proper interpolated values,
we need to delve into the details of the relationship between interpolating
in screen-space and in eye-space.
In order to derive a relationship between the two spaces, we will examine a simple case of only two dimensions (N=2). Eye-space points (X,Z) will be represented by upper-case letters, and screen-space points (x,z) will be represented by lower-case. (X,Z)-points represent points Z units away from the eye-plane (Z=0) that will be projected onto the Z=1 plane with respect to the eye (center-of-projection) at the origin (0,0). The viewing direction is in the +Z direction.
The projection
of a 2D eye-space point (X,Z) to a 1D screen-space point x (onto z=1 plane)
is quite simple. given the eye-space point (X,Z) we can compute x using
similar triangles:
x/1 = X/Z x = X/Z
Given an eye-space edge from point (X1,Z1) to (X2,Z2), we can interpolate between them in eye-space as follows:
E(T) = (1-T)*(X1,Z1) + T*(X2,Z2) [ X' ] [ (1-T)*X1 + T*X2 ] [ Z' ] = [ (1-T)*Z1 + T*Z2 ]The projected edge endpoints x1 and x2 in screen-space are interpolated as follows:
S(t) = x' = (1-t)*x1 + t*x2
Now we can project the interpolated point along the edge into screen-space from the interpolation equation E(t) and the perspective equation. Given to edge endpoints and a parametric t value, we obtain an interpolated eye-space point (X',Z'). We can project this point into screen-space as x'=X'/Z'.
We can derive a relationship between interpolation in screen-space and in eye-space by relating their paremetric interpolating values t and T respectively. We will do this by observing their relationship when they are equivalent points; in other words, when the projected E(t) equals S(t).
S(t) = Projected E(T) x' = X'/Z' (1-t)*x1 + t*x2 = ((1-T)*X1 + T*X2) / ((1-T)*Z1 + T*Z2) from the projection equations: x = X/Z X = x*Z so X1 = x1*Z1 X2 = x2*Z2 by substitution: (1-t)*x1 + t*x2 = ((1-T)*x1*Z1 + T*x2*Z2) / ((1-T)*Z1 + T*Z2) = [ ((1-T)*Z1) / ((1-T)*Z1 + T*Z2) ] * x1 + [ (T*Z2) / ((1-T)*Z1 + T*Z2) ] * x2 we now have an equation of the form: a*x1 + b*x2 = A*x1 + B*x2 where a=A and b=B for this equality to hold.We can show a relationship between t and T by solving for T in either equation:
so we will use b=B:
t = (T*Z2) / ((1-T)*Z1 + T*Z2) t*((1-T)*Z1 + T*Z2) = Z2*T t*(Z1 - Z1*T + Z2*T) = Z1*t - Z1*t*T + Z2*t*T = Z2*t*T - Z1*t*T - Z2*T = -Z1*t T*(Z2*t - Z1*t - Z2) = T = -Z1*t / (Z2*t - Z1*t - Z2) = Z1*t / (Z1*t + Z2 - Z2*t) T = Z1*t / (Z1*t + Z2*(1-t))From the first line and the last line we can clearly see that t and T are not linearly related:
t = (T*Z2) / ((1-T)*Z1 + T*Z2) T = Z1*t / (Z1*t + Z2*(1-t))
We must now show how the eye-space and screen-space points are related. We will interpolate a value in eye-space and show the corresponding interpolation in screen-space in terms of eye-space coordinates.
Given two eye-space points E1 and E2, we can interpolate as follows:
E(T) = E1 * [ (1-t)*Z2 / (Z1*t + Z2*(1-t)) ] + E2 * [ Z1*t / (Z1*t + Z2*(1-t)) ] = [ E1 * (1-t)*Z2 + E2 * Z1*t ] / (Z1*t + Z2*(1-t))Now if we divide numerator and denominator by Z1*Z2:
= [ (E1/Z1)*(1-t) + (E2/Z2)*t ] / [ (1/Z1)*(1-t) + (1/Z2)*t ]The numerator and denominator are quite interesting now. We can see that the interpolation along an edge in eye-space corresponds to an interpolation of the projected edge in screen-space divided by the interpolation of 1/Z. The numerator is simply the interpolation of the projected points where E/Z is the projection of E into screen-space. The denominator is the interpolation of 1/Z.
In other words, when interpolating values in screen-space, in order to compute the appropriate corresponding values in eye-space we must interpolate the projected values and the 1/Z terms and divide for each interpolated sample point.