I'm currently trying to understand how works perspective correction texture mapping. I saw that actually it works by interpolating the z value of the three point of the triangle which the current rendered pixel is in, then, it divide the u and v affine texture coordinates by the interpolated z value in order to get the perspective corrected values of u and v. I really can't wrap my head around this division by z to do perspective correction. Can someone explain me the point and the math behind it which makes it do the job ?
Thanks guys !
Okay, after reading your advices guys, i dived into perspective correction algorithm. I think i have understood what is done, but i want to expose it to you in order to see if it the right way to think. When rendering pixels on screen you're in raster space, if you interpolate u and v values you will get affine texturing cause it does not take into account depth of the vertices. The solution is to do so : working in a "specific space" of three dimensions : (u,v,z). Each vertices can be represented in this space cause it have u and v tex coordinates and a z coordinate. The idea is the following : you divide each u and v component of the vertices by z. The effect will be a perspective projection on the plane defined by the equation 0*u + 0*v + 1*z = 1. Doing so makes us able to do interpolation into raster space. Then, when we want to get the perspective corrected texel for a specific pixel of the current renderer triangle in raster space, we just have to multiply the interpolated u/z and v/z by interplated z (actually the algorithm divide u/z and v/z by interpolated 1/z which leads exacly to a multiplication by interpolated z). But this multiplication by interpolated z can be viewed as a scalar which multiplies the interpolated vector (u/z, v/z, 1) which will put back the vector from the plane at z=1 to the right place into the original three dimensions space giving us the u and v coordinates interpolated into the three dimension space, taking into account the z coordinates and leading to a correct perspective corrected texture mapping. Am i right ?