gpt4 book ai didi

c++ - 屏幕坐标到世界坐标

转载 作者:可可西里 更新时间:2023-11-01 17:39:58 25 4
gpt4 key购买 nike

我想在 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 得到的世界 vector 。具有非常小的 x、y 和 z。如果我使用这个值来平移网格,网格会发生小的平移并且不会跟随鼠标指针。

第二个问题是,如 glm 文档( https://glm.g-truc.net/0.9.8/api/a00169.html#ga82a558de3ce42cbeed0f6ec292a4e1b3 )中所述,结果以对象坐标返回。因此,为了将屏幕转换为世界坐标,我应该使用一个网格的变换矩阵,但是如果有很多网格并且我想从屏幕转换为世界坐标会发生什么?我应该通过相机 View 矩阵乘以哪个模型矩阵来形成 ModelView 矩阵?

最佳答案

这个序列有几个问题:

       glfwGetCursorPos(window, &xpos, &ypos);
glReadPixels(xpos, ypos, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &zpos);
[...]
glm::vec3 win(xpos,height - ypos, zpos);

  • 窗口空间原点。 glReadPixels是 GL 函数,因此遵守 GL 的约定,原点位于左下角像素。当您为您的 win 切换到该约定时变量,您仍然使用错误的原点来读取深度缓冲区。

    此外,你的翻转是错误的。自 ypos应该在 [0,height-1] ,正确的公式是 height-1 - ypos ,所以你在这里也少了一个。 (我们稍后会看到这也不完全正确。)
  • “屏幕坐标”与像素坐标。您的代码假定您从 GLFW 返回的坐标以像素为单位。不是这种情况。 GLFW 使用 "virtual screen coordinates" 的概念不一定映射到像素:

    Pixels and screen coordinates may map 1:1 on your machine, but they won't on every other machine, for example on a Mac with a Retina display. The ratio between screen coordinates and pixels may also change at run-time depending on which monitor the window is currently considered to be on.



    GLFW 通常为一个窗口提供两种尺寸, glfwGetWindowSize 将返回上述虚拟屏幕坐标中的结果,而 glfwGetFramebufferSize 将返回以像素为单位的实际大小,与 OpenGL 相关。所以基本上,你必须查询两种尺寸,然后才能将鼠标坐标从屏幕坐标适本地缩放到你需要的实际像素。
  • 子像素位置。虽然 glReadPixels使用整数坐标寻址特定像素,整个转换数学使用浮点运算,可以表示任意子像素位置。 GL 的窗口空间被定义为整数坐标代表像素的角点,像素中心位于整数坐标的一半。您的 win变量将代表所述像素的左下角,但更有用的约定是使用像素中心,因此您最好添加 (0.5f, 0.5f, 0.0f) 的偏移量。至 win ,假设您指向像素中心。 (如果虚拟屏幕坐标的分辨率高于我们的像素,我们可以做得更好,这意味着我们已经获得了鼠标光标的子像素位置,但数学不会改变,因为我们仍然需要切换到GL 的修道院,其中整数表示边界而不是整数表示中心)。请注意,由于我们现在考虑一个来自 [0,w) 的空间。在 x[0,h)y ,这也会影响第 1 点。如果您单击像素 (0,0) ,它将有中心 (0.5, 0.5) ,以及 y翻转应该是 h-y所以h-0.5 (访问帧缓冲区像素时,应向下舍入到 h-1)。

  • 总而言之,您可以(从概念上)执行以下操作:
    glfwGetWindowSize(win, &screen_w, &screen_h); // better use the callback and cache the values 
    glfwGetFramebufferSize(win, &pixel_w, &pixel_h); // better use the callback and cache the values
    glfwGetCursorPos(window, &xpos, &ypos);
    glm::vec2 screen_pos=glm::vec2(xpos, ypos);
    glm::vec2 pixel_pos=screen_pos * glm::vec2(pixel_w, pixel_h) / glm::vec2(screen_w, screen_h); // note: not necessarily integer
    pixel_pos = pixel_pos + glm::vec2(0.5f, 0.5f); // shift to GL's center convention
    glm::vec3 win=glm::vec3(pixel_pos., pixel_h-pixel_pos.y, 0.0f);
    glReadPixels( (GLint)win.x, (GLint)win.y, ..., &win.z)
    // ... unproject win

    what model matrix should I multuply by camera view matrix to form ModelView matrix?



    没有任何。基本的坐标变换管道是
    object space -> {MODEL} -> World Space -> {VIEW} -> Eye Space -> {PROJ} -> Clip Space -> {perspective divide} -> NDC -> {Viewport/DepthRange} -> Window Space

    没有模型矩阵影响从世界到窗口空间的方式,因此反转它也不依赖于任何模型矩阵。

    that as said in the glm docs (https://glm.g-truc.net/0.9.8/api/a00169.html#ga82a558de3ce42cbeed0f6ec292a4e1b3) the result is returned in object coordinates.



    数学不关心您在哪些空间之间进行转换。该文档提到了对象空间,并且该函数使用了一个名为 modelView 的参数。 ,但你放什么矩阵完全无关紧要。推杆 view会有的。

    So in order to convert screen to world coordinates I should use a transform matrix from one mesh.



    好吧,你甚至可以这样做。您可以使用任何对象的任何模型矩阵,只要该矩阵不是奇异矩阵,并且只要您对 unproject 使用与稍后用于从对象空间到世界空间相同的矩阵。你甚至可以组成一个随机矩阵,如果你确保它是规则的。 (好吧,如果矩阵是病态的,则可能存在数值问题)。这里的关键是当你指定 (V*M) 和 P 作为 glm::unproject 的矩阵时,它会在内部计算 (V*M)^-1 * P^-1 * ndc_pos这是 M^-1 * V^-1 & P^-1 * ndc_pos .如果将结果从对象空间转换回世界空间,则将其乘以 M再次,导致 M * M^-1 * V^-1 & P^-1 * ndc_pos ,这当然只是 V^-1 & P^-1 * ndc_pos如果您不输入 M,您将直接获得首先进入unproject。你只是增加了更多的计算工作,并为数值问题引入了更多的潜力......

    关于c++ - 屏幕坐标到世界坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45796287/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com