【发布时间】:2018-01-29 11:31:12
【问题描述】:
我想将屏幕坐标转换为OpenGL 中的世界坐标。我为此使用glm(我也在使用glfw)
这是我的代码:
static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT) {
if(GLFW_PRESS == action){
int height = 768, width =1024;
double xpos,ypos,zpos;
glfwGetCursorPos(window, &xpos, &ypos);
glReadPixels(xpos, ypos, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &zpos);
glm::mat4 m_projection = glm::perspective(glm::radians(45.0f), (float)(1024/768), 0.1f, 1000.0f);
glm::vec3 win(xpos,height - ypos, zpos);
glm::vec4 viewport(0.0f,0.0f,(float)width, (float)height);
glm::vec3 world = glm::unProject(win, mesh.getView() * mesh.getTransform(),m_projection,viewport);
std::cout << "screen " << xpos << " " << ypos << " " << zpos << std::endl;
std::cout << "world " << world.x << " " << world.y << " " << world.z << std::endl;
}
}
}
现在,我有两个问题,第一个是我从glm::unProject 得到的世界向量的 x、y 和 z 非常小。如果我使用此值来平移网格,则网格会受到较小的平移并且不会跟随鼠标指针。
第二个问题是,正如 glm 文档 (https://glm.g-truc.net/0.9.8/api/a00169.html#ga82a558de3ce42cbeed0f6ec292a4e1b3) 中所说,结果以对象坐标返回。因此,为了将屏幕转换为世界坐标,我应该使用一个网格的变换矩阵,但是如果一个有很多网格并且我想从屏幕转换为世界坐标会发生什么?我应该用相机视图矩阵乘以什么模型矩阵来形成模型视图矩阵?
【问题讨论】:
标签: c++ opengl matrix projection glm-math