glreadpixel返回故障结果

发布于 2025-01-19 18:35:18 字数 1641 浏览 5 评论 0原文

我想通过帧拾取的方式在OpenGl上进行3D拾取。这样我需要使用glReadPixel来获取当前屏幕上像素的一些信息,所以我按照以下方式进行测试,但得到了错误的结果。 首先,我使用回调glfwSetCursorPosCallback(window, mouse_callback)mouse_callback(GLFWwindow* window, double xpos, double ypos)来获取当前鼠标位置(屏幕位置)和像素的颜色(当我改变鼠标的位置时),然后使用 std::cout 打印它。

void mouse_callback(GLFWwindow* window, double xpos, double ypos)   
{
    uint32_t color;
    glm::vec4 c;
    GLfloat stencil;
    glReadPixels(xpos, ypos, 1, 1,GL_RGBA, GL_UNSIGNED_BYTE, &color);
    c.r = (color & 0xFF) / 255.0F;
    c.g = ((color >> 8) & 0xFF) / 255.0F;
    c.b = ((color >> 16) & 0xFF) / 255.0F;
    c.a = ((color >> 24) & 0xFF) / 255.0F;
    std::cout << "color[0]" << c.r<< std::endl;
    std::cout << "color[1]" << c.g << std::endl;
    std::cout << "color[2]" << c.b << std::endl;
    std::cout << "color[3]" << c.a << std::endl;
}

但问题是,当我渲染下面的场景并将鼠标放在该场景的不同部分时,结果似乎是错误的。从左上区域到右下区域,RBG 应该是 (1.0,0.0,0.0) , (0.6,0.0,0.4) , (0.4,0.0,0.6) , (0.2,0.0,0.8) , (0.0 ,0.0,1.0)。图片如下。

返回结果是(0.4,0.0,0.6),但正确的结果应该是(1.0,0.0,0.0)

返回结果为(0.0,0.0,0.1) 但正确的结果应该是 (0.4,0.0,0.6)

返回结果是 (0.8,0.0,0.2) 但正确的结果应该是 (0.4,0.0,0.6)

我尝试过使用简单的例子(画一个三角形)来测试glReadPixel,结果似乎是正确的。我不知道为什么使用我的场景时会出错。有人能给我一些建议吗?或者说如何进行框选拣选?谢谢你的帮助!

I want to do the 3D-picking on OpenGl by the way of frame pick. On this way I need to use glReadPixel to get some information of the pixels on the screen currently, so I test it on the following way but get the wrong result.
First , I use Callback glfwSetCursorPosCallback(window, mouse_callback)and mouse_callback(GLFWwindow* window, double xpos, double ypos) to get the current mouse position (screen positon) and the pixel's color(when I change the mouse's position) ,then use std::cout to print it .

void mouse_callback(GLFWwindow* window, double xpos, double ypos)   
{
    uint32_t color;
    glm::vec4 c;
    GLfloat stencil;
    glReadPixels(xpos, ypos, 1, 1,GL_RGBA, GL_UNSIGNED_BYTE, &color);
    c.r = (color & 0xFF) / 255.0F;
    c.g = ((color >> 8) & 0xFF) / 255.0F;
    c.b = ((color >> 16) & 0xFF) / 255.0F;
    c.a = ((color >> 24) & 0xFF) / 255.0F;
    std::cout << "color[0]" << c.r<< std::endl;
    std::cout << "color[1]" << c.g << std::endl;
    std::cout << "color[2]" << c.b << std::endl;
    std::cout << "color[3]" << c.a << std::endl;
}

But the problem is when I render the scene below, and put my mouse on different part of this scene , the result seems to be wrong. From left-top region to the right-bottom regionm,the RBG should be (1.0,0.0,0.0) , (0.6,0.0,0.4) , (0.4,0.0,0.6) , (0.2,0.0,0.8) , (0.0,0.0,1.0). The pictures are below .

the return result is (0.4,0.0,0.6) but the right result shoud be (1.0,0.0,0.0)

the return result is (0.0,0.0,0.1) but the right result shoud be (0.4,0.0,0.6)

the return result is (0.8,0.0,0.2) but the right result shoud be (0.4,0.0,0.6)

I have try to use the simple examples (draw a triangle) to test the glReadPixel, the rusult seems to be right. I do not know why it goes wrong when use my scence. Could anyone give me some advices ? Or how to do the frame pick picking ? Thank u for your help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔芯 2025-01-26 18:35:18

OpenGL使用坐标,其中原点(0,0)是窗口的左下,而 +y在上方。阅读时,您必须将其转换为OpenGL的坐标系,因为光标事件将(0,0)用作窗口的左上角,并且 +y是下降的。

void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
    int width, height;
    glfwGetWindowSize(window, &width, &height);
    int ixpos = (int)xpos;
    int iypos = height - 1 - (int)ypos;
    uint32_t color;
    glReadPixels(ixpos, iypos, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &color);
    ...
}

OpenGL uses coordinates where the origin (0, 0) is the bottom-left of the window, and +Y is up. You have to convert to OpenGL's coordinate system when reading, since the cursor events use (0, 0) as the top-left of the window, and +Y is down.

void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
    int width, height;
    glfwGetWindowSize(window, &width, &height);
    int ixpos = (int)xpos;
    int iypos = height - 1 - (int)ypos;
    uint32_t color;
    glReadPixels(ixpos, iypos, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &color);
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文