【问题标题】:GLM unprojects returns wrong resultsGLM unprojects 返回错误的结果
【发布时间】:2021-11-14 20:16:07
【问题描述】:

在我的项目中,我使用了一个相当复杂的相机玻璃,它使轨迹球/轨迹球围绕中心旋转。到目前为止效果很好。对于我使用的 MVP 矩阵的创建:

// Get a handle for our "MVP" uniform
GLuint MatrixID = glGetUniformLocation(shaderProgram, "MVP");

// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 <-> 100 units
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 50.f);
// Camera matrix
glm::mat4 view = camera->getMatrix();
// Model matrix -> identity matrix (origin)
glm::mat4 model = glm::mat4(1.0f);

// our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = projection * view * model;

VertexShader 也不足为奇(这里只展示了一部分)v_position = MVP * vec4(vPos,1); 并且到目前为止运行良好。

要获得鼠标指向的规范化方向,我使用以下代码: direction = glm::normalize( glm::unProject(glm::vec3(mouseX,700 - mouseY, 1), model, projection, glm::vec4(0.0f, 0.0f, 700, 700))); - 这里 700 是屏幕的宽度和高度。

在其他时候,我尝试使用 glm intersectRayTriangle 函数进行交叉点测试: glm::intersectRayTriangle(cameraPosition, direction , v0, v1, v2, intersectionPos, dist) 这就是问题所在:我没有得到正确的结果,所以我猜在 unProject 代码的某个地方存在错误。也许有人对此有想法。

【问题讨论】:

    标签: c++ opengl glm-math


    【解决方案1】:

    显然,每个像素的视线将取决于view 矩阵,但在这里:

    direction = glm::normalize( glm::unProject(glm::vec3(mouseX,700 - mouseY, 1), model, projection, glm::vec4(0.0f, 0.0f, 700, 700))); 
    

    您根本没有考虑view 矩阵。

    您需要使用view * model 作为model 参数。或者如果你想要世界空间坐标,也许只是view

    glm::unProject 函数的参数名称实际上在这里具有误导性。让我引用glm the reference pages

    GLM_FUNC_DECL tvec3<T, P> glm::unProject  (   tvec3< T, P > const &   win,
                                                  tmat4x4< T, P > const & model,
                                                  tmat4x4< T, P > const & proj,
                                                  tvec4< U, P > const & viewport) 
    

    带有以下描述(强调我的):

    model 指定modelview矩阵

    由于 glm 试图模仿旧的遗留 GL 函数,并且 GL 中的固定函数管道始终用于将总变换保持为 ModelViewProjection 矩阵的分解形式,这对于任何了解旧 @ 的人来说都是可以理解的987654333@函数...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多