0

I am trying to get the 2d world coordinates on a 2D plane (Z = 0) where I clicked with the mouse in a 3D scene. I figured out that ray-casting would probably be the best method.

This code is that I scavenged from the Internet:

glm::vec3 Drawer::MouseToWorldCoords(glm::vec2 coords)
{
//getting camera position

glm::mat3 rotMat(view);
glm::vec3 d(view[3]);

glm::vec3 retVec = -d * rotMat;

//std::cout << " x " << retVec.x << " y " << retVec.y << " z " << retVec.z << std::endl;

//getting mouse coords
float x = 2.0 * coords.x / WINDOW_WIDTH - 1;
float y = -2.0 * coords.y / WINDOW_HEIGHT + 1;
float z = -1.0f;

//raycasting 
glm::vec4 ray(x, y, z,1.0f);

glm::vec4 ray_eye = inverse(proj) * ray;

ray_eye = glm::vec4(ray_eye.x,ray_eye.y, 1.0, 0.0);

glm::vec3 ray_world = glm::vec3((glm::inverse(view) * ray_eye));

ray_world = glm::normalize(ray_world);
//intersecting plane with ray

glm::vec3 ba = retVec - ray_world ;
float nDotA = glm::dot(glm::vec3(0.0f,0.0f,1.0f), ray_world);
float nDotBA = glm::dot(glm::vec3(0.0f,0.0f,1.0f), ba);

glm::vec3 intersect =  (ray_world + (((0.0f - nDotA) / nDotBA) * ba)) ;

return glm::vec3( -intersect.x * 10.0f,-intersect.y * 10.0f,0.0f  );
}

This snippet of code does not work the way it should though. As you can see in the image:

Top view of several cube objects

The program simply spawns cubes at the location returned by the function. To produce this result I clicked only on the edges of the screen (except for the 2 in the middle of course).

genpfault
  • 51,148
  • 11
  • 85
  • 139
Doyc
  • 11
  • 5
  • 1
    If you just want to get the mouse coordinates on the near plane, then I don't see why you would have to do a raycast. Shouldn't the result in ndc just be [x,y,0], where x,y are the values you already calculated? – BDL Aug 13 '16 at 09:29
  • Also if you want the full 3D (not just Z=0) then you can read the depth buffer and get the `z` coordinate for any (x,y) yo want ... If you want to to detect which object is clicked you can render index of object as color attachment and read it as well ... this way you got pixel perfect mouse selections almost without any effort. See the last part of [Improving performance of click detection on a staggered column isometric grid](http://stackoverflow.com/a/35917976/2521214) for this you can use mouse coordinates in pixels no recalculation is needed – Spektre Aug 22 '16 at 08:03

0 Answers0