glm :: unProject问题,屏幕空间对齐错误 [英] glm::unProject issues, world to screen space alignment error

查看:246
本文介绍了glm :: unProject问题,屏幕空间对齐错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在3D空间中有一个平面,该平面面向我希望能够与单击位置相同的位置的相机.但是,平面的位置会超出鼠标光标.这是针对动态GUI的,我希望它能够在UI上移动并与之交互.

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;
    }
}

我不知道为什么飞机无法与鼠标正确对齐.

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

推荐答案

此为解决方法:

  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;
}

}

然后要将对象对准光标所在的位置,相机的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);

这篇关于glm :: unProject问题,屏幕空间对齐错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆