Search code examples
c++openglglm-math

How to get world coordinates from the screen coordinates


I am trying to get world coordinates from the screen coordinates.

This is my process.

  1. Get the Screen coordinates from the mouse click

  2. Multiply projection with view matrix

    FinalMatrix = projectionMatrix * viewMatrix;
    

3)Inverse the matrix

    finalMatrix = glm::inverse(finalMatrix);
  1. Get the position in world coordinates

    glm::vec4 worldPosition = finalMatrix * glm::vec4(screenPoint.x - 1920.0 / 2.0,  -1.0 * (screenPoint.y - 1080.0 / 2.0) , screenPoint.z, 1.0);
    

But the final position does not match the world position of mouse click


Solution

  • I assume that screenPoint.xy is the poisition of the mouse in window coordinates (pixel). And screenPoint.z is the depth from the depth buffer. You must transform the position to Normalized Device Cooridnates. NDC are in range (-1, -1, -1):

    glm::vec3 fC = screenPoint;
    glm::vec3 ndc = glm::vec3(fC.x / 1920.0, 1.0 - fC.y / 1080.0, fC.z) * 2.0 - 1.0;
    glm::vec4 worldPosition = finalMatrix * glm::vec4(ndc, 1);
    

    worldPosition is a Homogeneous coordinates. You must divide it by it's w component to get a Cartesian coordinate (see Perspective divide):

    glm::vec3 p = glm::vec3(worldPosition) / worldPosition.w;
    

    See also OpenGL - Mouse coordinates to Space coordinates.