I am projecting 3D points onto a 2D plane using this formula, and I need help with clipping the points that are behind the camera.
(pseudo code of what i currently have)
camera_position = [0.0, -5.0, -10.0]
camera_rotation = [0.0, 0.0, 0.0]
function project_point(vec3 point) {
add_vec3(point, camera_position)
rotate_y(point, camera_rotation.y)
rotate_x(point, camera_rotation.x)
rotate_z(point, camera_rotation.z)
projected_x = (point.x * FOV) / (point.z == 0 ? 0.00001 : point.z);
projected_y = (point.y * FOV) / (point.z == 0 ? 0.00001 : point.z);
projected_vec2 = projected_x+SCREEN_WIDTH_HALF, projected_y+SCREEN_HEIGHT_HALF
return projected_vec2
}
function main() {
3d_point = [ 0, 0, 5 ]
// Clipping point here?
project_point(3d_point)
draw_pixel(project_point.x, project_point.y)
}
From what i understand the correct way to clip the point is to check if it is behind the camera, and then if it is, not render it. Doing all this before doing the projection math.
Is this the correct way to do it?