Ray picking origin in front of the camera

92 Views Asked by At

I am trying to implement a ray picking algorithm. I want to get a ray that allow me to then check, which vertex is under my cursor. I've followed a few tutorial but i get a strange result. My origin is located in the center of the screen, the direction of the ray then correctly go towards the point where my cursor is from. The problem is then that i can't pick anything that is close to my near plane.

I've attached a visualisation of the problem (Here the mouse is at the far end of the white ray) result

Here is what my code looks like in pseudo code (the mouse (0, 0) is in the bottom left)

mouse_x, mouse_y := get_mouse_pointer_position();

ndc := make_vector2(2.0 * mouse_x / window.width - 1, 2.0 * mouse_y / window.height - 1);
hcc := make_vector4(ndc.x, ndc.y, 1, 1);
projection := perspective(1.0, window.height / window.width, 500, 0.01);
projection_inv := inverse(projection);
ecc := projection_inv * hcc;

view := look_at_matrix(scene.camera_pos, scene.camera_target);
view_inv := inverse(view);
ray := normalize((view_inv * ecc).xyz);

return Ray(camera_pos, ray);

Changing the z coordinates in hcc doesn't seems to do a lot.

0

There are 0 best solutions below