(quote from "Mathematics for 3D Game Programming and Computer Graphics Third Edition")
As the figure above showing,correct interpolation across the face of a triangle is not linear since equally spaced steps taken on the projection plane correspond to larger steps taken on the face of a triangle as the distance from the camera increases.
What follows in this passage will prove that the reciprocal of the z coordinate is correctly interpolated in a linear manner across the face of a triangle ,so is b/z( b is a vertex attribute value,like texcoord and so on).
So hardware first linearly interpolate 1/z when rasterizing a scanline(obtain the depth of each pixel),then linearly interpolate b/z to obtain the perspective-correct interpolated value of any vertex attribute b.
Proof:
Depth Interpolation:
Assuming that the segment(represent a triangle) does not belong to a line that passes through the origin, so it is visible( imaging that put a setsquare edge-on in front of you, then you can not tell whether it is a line or a setsquare).Then we can describe the line with the equation
ax+bz = c,where c != 0( equation 1,reference to the figure above).
Using similar triangles,we can derived the follow relationship.
(equation 2)
Assume that we konw the two endpoints of the line segment{(x1,z1),(x2,z2)} and their image on the projection plane{(p1,-e),(p2,-e)}(correspond to pixel 1 and pixel 2).Let
p3 = (1-t)p1 + t*p2(0<=t<=1) (eq 3)
conbining equation 1, 2 and 3, we can find the z coordinate of the point(x3,z3),where the ray cast through the point (p3,-e)intersects the face of the triangle.That is:
(eq 4)
Vertex Attribute Interpolation:
We would expect the interpolated attribute value b3 to form the same proportion with the total difference along the line segment as does the interpolated depth value z3. ( b1,b2 is the vertex attribute values of the two endpoints{(x1,z1),(x2,z2)} of the line segment )That is, the equation
(eq 5)
combining eq 4,5, we can finally get the equation
PS:Perspective-Correct Interpolation is now implemented by hardware!!!