【问题标题】:How to get world coordinates from the screen coordinates如何从屏幕坐标中获取世界坐标
【发布时间】:2021-10-22 11:21:18
【问题描述】:

我正在尝试从屏幕坐标中获取世界坐标。

这是我的过程。

  1. 通过鼠标点击获取屏幕坐标

  2. 将投影与视图矩阵相乘

    FinalMatrix = projectionMatrix * viewMatrix;
    

3)逆矩阵

    finalMatrix = glm::inverse(finalMatrix);
  1. 获取世界坐标中的位置

    glm::vec4 worldPosition = finalMatrix * glm::vec4(screenPoint.x - 1920.0 / 2.0,  -1.0 * (screenPoint.y - 1080.0 / 2.0) , screenPoint.z, 1.0);
    

但最终位置与鼠标点击的世界位置不匹配

【问题讨论】:

  • 您要进行采摘吗? (,确定鼠标指针所在的形状。)
  • @Neil 是的,我正在尝试采摘。

标签: c++ opengl glm-math


【解决方案1】:

我假设screenPoint.xy 是鼠标在窗口坐标(像素)中的位置。而screenPoint.z 是深度缓冲区的深度。您必须将位置转换为标准化设备坐标。 NDC 在 (-1, -1, -1) 范围内:

glm::vec3 fC = screenPoint;
glm::vec3 ndc = glm::vec3(fC.x / 1920.0, 1.0 - fC.y / 1080.0, fC.z) * 2.0 - 1.0;
glm::vec4 worldPosition = finalMatrix * glm::vec4(ndc, 1);

worldPosition 是一个Homogeneous coordinates。您必须将它除以它的 w 组件才能获得 Cartesian coordinate(参见 Perspective divide):

glm::vec3 p = glm::vec3(worldPosition) / worldPosition.w;

另见OpenGL - Mouse coordinates to Space coordinates

【讨论】:

    猜你喜欢
    • 2019-08-22
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2017-12-11
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多