How should I take FOV into account when interpolating UV coordinates? This picture shows the result today. Everything works fine with a narrow fov, but when I make it really wide, things deviate. The black line is expected to be straight. I'm writing a software renderer myself, so I can't just invoke some OpenGL function. Any advice is appreciated.
1 Answers
UV interpolation needs to take into account the depth of the vertices.
You need to perform perspective-correct interpolation, which involves dividing by the w coordinate of the interpolated homogeneous vector.
The "folded plane" effect of affine interpolation becomes more pronounced at wide FOV or when the camera is close to an object, but it's not caused by the wide FOV itself. It's just that narrow views approximate an orthographic camera, which doesn't need perspective correction since it doesn't experience perspective.
Wikipedia gives an equivalent formulation without homogeneous coordinates. if the endpoints you're interpolating have coordinates u_0 and u_1, and an interpolation factor a, then your interpolated u_a lies at
for affine texture mapping,
which becomes
for perspective-correct mapping,
where z_0 and z_1 are the distances of the endpoints from the viewer, along the axis perpendicular to the image plane.

- 134,153
- 22
- 242
- 357
-
What's the 'w' coordinate in this context? – Markus Jevring Mar 07 '14 at 20:37
-
2I've added a link to the Wikipedia article on homogeneous coordinates above. In short, it's typical to represent your 3D vertex positions with 4-component vectors, with the 4th component (conventionally called "w") set to 1. This way, transformations including translations and perspective projection can be performed by multiplying the vector by a 4x4 matrix. After projecting the vector in this fashion, the w coordinate of the result contains useful information about the perspective distortion, used for correcting artifacts like the one you're experiencing. – DMGregory Mar 08 '14 at 01:18
-
Unfortunately for me, I don't work with homogenous coordinates, for a variety of reasons. I'm fairly sure that this would be sufficient answer for someone who does, though =) – Markus Jevring Mar 08 '14 at 13:09
-
Added a version that doesn't use homogeneous coordinates, care of Wikipedia. – DMGregory Mar 08 '14 at 16:42
-
I'm trying that last formula (applied to my code, obviously), but I'm not getting the desired results. Must be something wrong on my end. I'm marking this as solved, as this is no doubt the correct way. I'm just making a mistake somewhere... – Markus Jevring Mar 08 '14 at 20:20
-
I finally did it. There was nothing mathematically wrong with my approach. I simply re-inverted the values in a copy constructor I was using. =) – Markus Jevring Mar 09 '14 at 10:34
-
Congratulations - well done! :) – DMGregory Mar 09 '14 at 12:20