I'd like to construct a world ray given a mouse click, which can be used to run intersection checks.
The ray should be generated, given glm::vec2
mouse coordinate and glm::mat4
projection and view matrix's. Taking into account near and far planes. How can this be implemented in C?
Edit:
I have constructed a mouse ray from mousePosition
, view
and projection
. the coordinates it returns are close to what I'm after however mouseRay.z
isn't behaving at all.
glm::vec2 mousePosition = { .x, .y };
glm::vec2 normalisedCoords = {
-1.0f + 2.0f * position.x / width,
1.0f + -2.0f * position.y / height
};
glm::vec4 clipCoords = { normalisedCoords.x, normalisedCoords.y, -1, 1 };
glm::mat4 invertedProjection = glm::inverse(projection);
glm::vec4 eyeCoords = invertedProjection * clipCoords;
glm::mat4 invertedView = glm::inverse(view);
glm::vec4 rayWorld = invertedView * eyeCoords;
glm::vec3 mouseRay = { rayWorld.x, rayWorld.y, rayWorld.z };
printf("x: %f, y: %f, z: %f\n\r", rayWorld.x, rayWorld.y, rayWorld.z);