Original Post
Hi,
I know this has been discussed but want to confirm that I'm doing it the right way. I want to get the coordinate under the mouse pointer for the z=0 plane. This is my code:
So now if i build a ray with worldCoordFar & workdCoordNear points and intersect it with a the z=0 plane, would I get the correct coordinate?
I'm just bit worried about /w division which I'm not sure where to put when unprojecting from 2d to 3d.
Thanks,
Ruben
I know this has been discussed but want to confirm that I'm doing it the right way. I want to get the coordinate under the mouse pointer for the z=0 plane. This is my code:
mat4 projectionMatrix = GetPerspectiveMatrix();
mat4 projectionMatrixInverse = glm::inverse(projectionMatrix);
mat4 viewMatrix = GetViewMatrix();
mat4 viewMatrixInverse = glm::inverse(viewMatrix);
vec2 screenCoord = MathLib::vec2(x, y);
vec2 normalizedCoord2D = MathLib::vec2(2.0 * screenCoord.x / viewportSize.x - 1.0, 2.0 * screenCoord.y / viewportSize.y - 1.0);
vec4 worldCoordNear;
vec4 worldCoordFar;
{
vec4 normalizedCoordNear = vec4(normalizedCoord2D.x, normalizedCoord2D.y, -1.0, -1.0);
vec4 clipCoordNear = normalizedCoordNear;
vec4 eyeCoordNear = projectionMatrixInverse * clipCoordNear;
worldCoordNear = viewMatrixInverse * eyeCoordNear;
}
{
vec4 normalizedCoordFar = vec4(normalizedCoord2D.x, normalizedCoord2D.y, -1.0, 1.0);
vec4 clipCoordFar = normalizedCoordFar;
vec4 eyeCoordFar = projectionMatrixInverse * clipCoordFar;
worldCoordFar = viewMatrixInverse * eyeCoordFar;
}
So now if i build a ray with worldCoordFar & workdCoordNear points and intersect it with a the z=0 plane, would I get the correct coordinate?
I'm just bit worried about /w division which I'm not sure where to put when unprojecting from 2d to 3d.
Thanks,
Ruben