1

I'm trying to create a ray to that translates my mouse coordinates to 3d world coordinates.

Cx = Mx / screenWidth * 2 - 1
Cy = -( My / screenHeight * 2 - 1 )

vNear = InverseViewProjectionMatrix * ( Cx, Cy, -1, 1 )
VFar = InverseViewProjectionMatrix * ( Cx, Cy, 1, 1 )

vNear /= vNear.w
vFar /= vFar.w

After testing the ray's vFar always appears to come from the same general direction

It seems like I need to add the camera perspective as I would expect vFar to always be behind my camera.

I'm not entirely sure how that should be added in. Here's my test code.

public void mouseToWorldCordinates(Window window,Camera camera, Vector2d mousePosition){
    float normalised_x = (float)((mousePosition.x / (window.getWidth()*2)) -1);
    float normalised_y = -(float)((mousePosition.y / (window.getHeight()*2)) -1);

    Vector4f mouse = new Vector4f(normalised_x,normalised_y,-1,1);

    Matrix4f projectionMatrix = new Matrix4f(transformation.getProjectionMatrix()).invert();
    Matrix4f mouse4f = new Matrix4f(mouse,new Vector4f(),new Vector4f(),new Vector4f());
    Matrix4f vNear4f = projectionMatrix.mul(mouse4f);
    Vector4f vNear = new Vector4f();
    vNear4f.getColumn(0,vNear);

    mouse.z = 1f;

    projectionMatrix = new Matrix4f(transformation.getProjectionMatrix()).invert();
    mouse4f = new Matrix4f(mouse,new Vector4f(),new Vector4f(),new Vector4f());
    Matrix4f vFar4f = projectionMatrix.mul(mouse4f);
    Vector4f vFar = new Vector4f();
    vFar4f.getColumn(0,vFar);

    vNear.div(vNear.w);
    vFar.div(vFar.w);

    lines[0] = vNear.x;
    lines[1] = vNear.y;
    lines[2] = vNear.z;

    lines[3] = vFar.x;
    lines[4] = vFar.y;
    lines[5] = vFar.z;

}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Bill Tudor
  • 97
  • 8
  • I think what you are trying to do is called [picking](https://stackoverflow.com/questions/28032910/opengl-picking-fastest-way/28033405)? – Neil Apr 10 '21 at 04:40
  • @Neil The term "picking" is not common for this. – Rabbid76 Apr 10 '21 at 06:13
  • See [Is it possble get which surface of cube will be click in OpenGL?](https://stackoverflow.com/questions/45893277/is-it-possble-get-which-surface-of-cube-will-be-click-in-opengl/45946943#45946943) – Rabbid76 Apr 10 '21 at 06:17
  • and also these: [depth buffer got by glReadPixels is always 1](https://stackoverflow.com/a/51130948/2521214) and [OpenGL 3D-raypicking with high poly meshes](https://stackoverflow.com/a/51764105/2521214) – Spektre Apr 10 '21 at 08:08

1 Answers1

2

The computation of normalised_x and normalised_y is wrong. Normalized device coordinates are in range [-1.0, 1.0]:

float normalised_x = 2.0f * (float)mousePosition.x / (float)window.getWidth() - 1.0f;
float normalised_y = 1.0f - 2.0f * (float)mousePosition.y / (float)window.getHeight();
Rabbid76
  • 202,892
  • 27
  • 131
  • 174