1

I am trying to get a direction vector to where my cursor is on the screen, however it gives me large values instead of the actual values. When doing mouse picking I am getting extremely small numbers for my world coordinates such as

Mouse is pointing at World X: 4.03225e-05 Y: -0.00048387 Z: -1

Am I doing something wrong here, I have adapted this code from here. I am using glm::intersectLineTriangle() to test if the line is pointing at the triangles world coordinates in question.

Here is the relevant code:

double mouse_x, mouse_y;
glfwGetCursorPos(GL::g_window, &mouse_x, &mouse_y);

int width, height;
glfwGetFramebufferSize(GL::g_window, &width, &height);

// these positions must be in range [-1, 1] (!!!), not [0, width] and [0, height]
float mouseX = (float)mouse_x / ((float)width  * 0.5f) - 1.0f;
float mouseY = (float)mouse_y / ((float)height * 0.5f) - 1.0f;

glm::mat4 invVP = glm::inverse(projection * view);
glm::vec4 screenPos = glm::vec4(mouseX, -mouseY, 1.0f, 1.0f);
glm::vec4 worldPos = invVP * screenPos;

glm::vec3 mouseClickVec = glm::normalize(glm::vec3(worldPos));

std::cout << "Mouse is pointing at World X: " << mouseClickVec.x <<  " Y: " << mouseClickVec.y << " Z: " << mouseClickVec.z << '\n';

glm::vec3 intersect;

if(glm::intersectLineTriangle(glm::vec3(0.0f, 0.0f, 0.0f), mouseClickVec, m_vertices_world[0], m_vertices_world[0], m_vertices_world[0], intersect))
{
 std::cout << "Intersect at X: " << intersect.x <<  " Y: " << intersect.y << " Z: " << intersect.z << '\n';
 setColor(glm::vec4(0.0f, 0.0f, 1.0f, 1.0f));
}
else
{
    setColor(glm::vec4(1.0f, 0.0f, 0.0f, 1.0f));
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
rial
  • 705
  • 7
  • 17
  • 1
    Do you use perspective projection? – Rabbid76 Oct 17 '21 at 20:15
  • orthographic projection. ```view = GL::g_camera.getViewMatrix(); projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -100.f, 100.0f);``` Sorry for the late reply – rial Oct 17 '21 at 20:55

1 Answers1

1

When using orthographic (parallel) projection, the point of view is not (0, 0, 0), (not in view space and of course not in world space). You have to create a ray from the near plane (-1) to the far plane (1):

glm::vec4 worldPosFar = invVP * glm::vec4(mouseX, -mouseY, 1.0f, 1.0f);
glm::vec3 mouseClickVecFar = glm::normalize(glm::vec3(worldPosFar));

glm::vec4 worldPosNear = invVP * glm::vec4(mouseX, -mouseY, -1.0f, 1.0f);
glm::vec3 mouseClickVecNear = glm::normalize(glm::vec3(worldPosNear));

if (glm::intersectLineTriangle(mouseClickVecNear, mouseClickVecFar,
    m_vertices_world[0], m_vertices_world[0], m_vertices_world[0], intersect))
{
    // [...]
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Ahh i see. I am stilling facing a similar issue though. Are the ```mouseClickVecNear``` and ```mouseClickVecFar``` vectors supposed to have the same values. The complete class with your addition is [here](https://pastebin.com/JRfdwuvT) – rial Oct 17 '21 at 21:20
  • 1
    They cannot be the same. Compare `glm::vec4(mouseX, -mouseY, 1.0f, 1.0f)` and `glm::vec4(mouseX, -mouseY, -1.0f, 1.0f)`. – Rabbid76 Oct 17 '21 at 21:32
  • I tested, it's the same ```Near: 0.00580628 -0.00508049 0.99997 Far: 0.00580628 -0.00508049 -0.99997``` – rial Oct 17 '21 at 21:39
  • @rial No it is not the same. The z component is different! So it seams you'r looking at the xy plane. – Rabbid76 Oct 18 '21 at 05:13
  • 1
    Yes my apologies @Rabbid76 I don't know how i missed that – rial Oct 18 '21 at 15:18