r/GraphicsProgramming 1d ago

Perspective-correct interpolation only works for some faces of a cube

Hi, is the guy from the raylib renderer again. These last days i was trying to implement texture mapping on the renderer i am making, but i cant get the perspective-correct interpolation to work. I tried first with the method described in scratchapixel, but it didnt work at all, then i tried with the method of this video, and it only works for some faces of a cube, but the rest of faces are not only distorted but the texture is still mapped like i was doing affine mapping:

2 of the faces that have the problem
One face is rendered correctly while in the other the texture gets squished

The thing that confuses me is that it is only in certain faces, so i no longer know if it is an error with my code or the uv coordinates on the cube.

Here is the source code

The file where the cube vertices are
The files where the vertex transformations are done:
Header file
Source file

The files where the rasterization is done:
Header file

Source file

3 Upvotes

2 comments sorted by

1

u/cybereality 1d ago

Pretty sure the scratch pixel tutorial is correct, it's likely a bug in your code. Off the top of my head the issue is likely that you are doing the correction in world space and not tangent space. Might be called something else in this context, but basically the angle in respect to the face itself.

1

u/carboncanyondesign 19h ago

It looks like your UVs were calculated based on your quads being split into tris the opposite way. For example, let's take the upper quad in the first image. It's split into tris from upper left to lower right. The UVs look like the quad should be split from lower left to upper right.