I have an issue, I am drawing a cube in 3d. I am using z-buffer for hidden surfaces removal. But the hidden faces are drawn (i see the inside of the cube). After some research And I found that this problem comes from the fact that the perspective projection matrix change the sign of the z , so we need to compare -z , instead of z , with the correspoding depth in depth Buffer (after a multiplication by the following matrix z becomes z' = n + f − fn/z). I am using the following perspective projection matrix:
perspective(aspectRatio, far, near, fov) {
const tan = Math.tan(fov / 2);
return [
[1 / (aspectRatio * tan), 0, 0, 0],
[0, 1 / tan, 0, 0],
[0, 0, (far + near) / (near - far), (2 * far * near) / (far - near)],
[0, 0, 1, 0],
];
}
.where fov is the field of view , near is the distance from near plane, far is the distance from far plane. I took this matrix from the fundamentals of computer graphics book by steve marschener fouth edition. Can you please give me more details, is the z value is affected by the camera matrix and the viewport matrix, wha I need to compare -z instead of z.