0

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Tackwin
  • 69
  • 1
  • 4
  • If the ray is originating at the camera position, then it should look like a point no matter what direction it's going. If you can see the ray as a line, it must not be originating at the camera position. – Hymns For Disco Jun 12 '22 at 13:17
  • I don't think this is true if the ray direction is not parallel to a line of sight no @HymnsForDisco ? For instance if the ray direction was wrong and pointed to the right then i would see a line going from the center of the view to the right. Otherwise i must be missing something. – Tackwin Jun 12 '22 at 13:53
  • 1
    The rays are not parallel to the line of sight. They are parallel to the line of sight in the orthographic projection, but they are not parallel in perspective projection, all rays come from one point, from the camera position. See [OpenGL - Mouse coordinates to Space coordinates](https://stackoverflow.com/questions/46749675/opengl-mouse-coordinates-to-space-coordinates/46752492#46752492) – Rabbid76 Jun 12 '22 at 13:57

0 Answers0