In 3D rendering (or geometry, for that matter), in the rasterization algorithm, when you project the vertices of a triangle onto a screen and then find that a pixel overlaps a 2D triangle, you often need to find the depth or z-coordinate of the triangle that overlaps pixel Typically, this method consists in calculating the barycentric coordinates of a pixel in a two-dimensional "projected" image of a triangle, and then use these coordinates to interpolate the triangles of the original z-coordinate vertices (before the vertices are projected).
Now it is written in all text books that you cannot directly interpolate the vertex coordinates of the vertices, but you need to do this instead:
(sorry, can't make Latex work?)
1 / z = w0 * 1 / v0.z + w1 * 1 / v1.z + w2 * 1 / v2.z
Where w0, w1 and w2 are the barycentric coordinates of the โpixelโ on the triangle.
Now, what I care for are two things:
- What would be the formal proof that the interpolation z does not work?
- what would be formal evidence to show that 1 / z is doing the right thing?
To show that this is not homework ;-) and that I did some work myself, I found the following explanation for question 2.
In principle, a triangle can be defined by a plane equation. So you can write:
Ax + By + Cz = D.
Then you isolate z to get z = (D - Ax - By) / C
Then you divide this formula into z in the same way as you do for the future, and if you develop, rearrange, etc., you will get:
1 / z = C / D + A / Dx / z + B / Dy / z.
Then call C '= C / DB' = B / D and A '= A / D you get:
1 / z = A'x / z + B'y / z + C '
It says that x / z and y / z are just the coordinates of the points on the triangles projected on the screen and that the equation on the right is an "affine" function, so 1 / z is a linear function ???
Doesn't this look like a demo for me? Or maybe this is the right idea, but cannot say, as you can tell, simply by looking at the equation that it is an affine function. If you multiply all the terms you just got:
A'x + B'y + C'z = 1.
These are just our initial equations (you just need to replace A 'B' and C 'with the correct term).