0
votes

I have a plane in 3D space, which is facing the camera that I want to be able to place at the same position as where I click. However, the position of the plane overshoots the mouse cursor. This is for a dynamic GUI that I want to be able to move about and interact with the widgets on the UI.

void mouse::unProjectMouse(float width, float height, camera* viewportCamera)
{
    if(NULL == viewportCamera)
    {
        std::cout<<CNTRLCALL<<"camera failed! failed to un-project mouse";
    } else {
        glm::vec4 viewport = glm::vec4(0, 0, width, height);
        glm::mat4 tmpView = viewportCamera->updateView();
        glm::mat4 tmpProj = viewportCamera->updateProjection();
        glm::vec3 screenPos = glm::vec3(mouseX, height-mouseY - 1.0f, 1.0f);

        glm::vec3 worldPos = glm::unProject(screenPos, tmpView, tmpProj, viewport);

        worldPos = worldPos / (worldPos.z * -1.0f);

        mouseWorldX = worldPos.x;
        mouseWorldY = worldPos.y;
        mouseWorldZ = worldPos.z;
    }
}

Here we see the mapping from viewport coords to world coords

I am at a loss here as to why the plane is not aligning correctly with the mouse.

1
How did you arrive at the value of 1.0f for screenPos.z? and what's this about? "worldPos = worldPos / (worldPos.z * -1.0f)". There's no need to mess around with what unProject gives you back. Just make sure your inputs are correct.Aeluned
I left it as 1.0f as I did not have an input for the z component, what would you recommend would be a suitable input for this. I also did the alteration to the worldPos z component to get 1 unit in front of the camera, for testing.GenericController
The z component matters here and this is what's causing your unProject to fail. Trying setting z to 0. Also, remove the division you're doing. That's not correct.Aeluned
I've done these two things. However the unproject x only ranges from [-49.x - -50] and the y only deviates between tiny decimal places. This can't be correct still. Any further suggestions?GenericController
Thanks for your input, I've fixed it now. Cheers AelunedGenericController

1 Answers

0
votes

This is the fix:

  void mouse::unProjectMouse(float width, float height, camera* viewportCamera)
{
if(NULL == viewportCamera)
{
    std::cout<<CNTRLCALL<<"camera failed! failed to un-project mouse";
} else {


    glm::vec4 viewport = glm::vec4(0.0f, 0.0f, width, height);
    glm::mat4 tmpView = glm::lookAt(glm::vec3(viewportCamera->getCameraPosX(),viewportCamera->getCameraPosY(),viewportCamera->getCameraPosZ()),
                                    glm::vec3(viewportCamera->getForward().x,viewportCamera->getForward().y,viewportCamera->getForward().z),glm::vec3(0,1,0));
    glm::mat4 tmpProj = glm::perspective( 90.0f, width/height, 0.1f, 1000.0f);
    glm::vec3 screenPos = glm::vec3(mouseX, height-mouseY - 1, 0.0f);

    glm::vec3 worldPos = glm::unProject(screenPos, tmpView, tmpProj, viewport);

    mouseWorldX = worldPos.x;
    mouseWorldY = worldPos.y;
    mouseWorldZ = worldPos.z;
}

}

And then to align the object to the cursors positon, the Z element of the camera had to be exact:

UIcamera->translateCameraX(0.0f);
UIcamera->translateCameraY(0.0f);
UIcamera->translateCameraZ(0.744f);