I am trying to write an affine texture mapper for my software renderer and it seems to work but not as I excpected, this is what I get after drawing a triangle between P0(48,48) P1(32,16) P2(16,48) on a 64x64 screen using an 8x8 texture
result : https://pasteboard.co/IPMCf0r.png
texture: https://pasteboard.co/IPMCFiM.png
I am using the scanline algorithm to draw the triangle and interpolate texture coordinates.
after interpolating the (u,v) at a pixel I get the corresponding textel position like this:
1 2 3
int textureX = u * (texture.width - 1) + 0.5f;
int textureY = v * (texture.height - 1) + 0.5f;
// plot the pixel with color texture[textureX][textureY];
what could the problem be.
UPDATE:
I was interpolating U and V but that was a mistake because they are not linear functions,
I had to interpolate U/Z and V/Z instead and then multiply both of them by Z to get the correct result.