first of all, sorry for the bad english.

I need to convert my mouse coordinates to world coordinates, I'm trying to use the glm unproject method. 1280x720 is the size of the window, It's hard coded just for the test. The View and Projection are my matrices, they're fine and are not related to the problem (or so I guess).

EDIT: Success. The solution is:

int x,y;
glfwGetMousePos(&x, &y);

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport); 

y = viewport[3] - y;
float depth;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);

mousePos = glm::unProject(vec3(x, y, depth), View, Projection, vec4(viewport[0], viewport[1], viewport[2], viewport[3]));

This method seem to have worked.

  • \$\begingroup\$ The depth buffer is non-linear, and I don't believe that glReadPixels linearizes it. Even if it was, it would be representing world units but the other coordinates you're giving to unProject are in screen/pixel units. In any case, this is a really awkward and inefficient (glReadPixels is slow) way to do what is probably best done with ray casting. \$\endgroup\$ Sep 18, 2013 at 21:55
  • \$\begingroup\$ I suspected this might be a problem, but I have tried so many ways, none have worked. I'm currently trying to reverse the coords translation with the projection and view matrices, without success. \$\endgroup\$
    – SharkDX
    Sep 18, 2013 at 22:09


Browse other questions tagged .