如何从 OpenGL 中的帧缓冲区纹理中采样像素?

How to sample a pixel from a framebuffer texture in OpenGL?

我正在使用 ImGUI 为图像创建一个颜色选择器 OpenGL 应用程序。我设法通过将图像加载到 glTexImage2D 并使用 ImGUI::Image() 来加载图像。 现在我想实现一个方法,它可以在单击鼠标左键的情况下确定像素的颜色。

这是我加载纹理,然后将其分配给帧缓冲区的方法:

bool LoadTextureFromFile(const char *filename, GLuint *out_texture, int *out_width, int *out_height,ImVec2 mousePosition ) {

    // Reading the image into a GL_TEXTURE_2D
    int image_width = 0;
    int image_height = 0;
    unsigned char *image_data = stbi_load(filename, &image_width, &image_height, NULL, 4);
    if (image_data == NULL)
        return false;

    GLuint image_texture;
    glGenTextures(1, &image_texture);
    glBindTexture(GL_TEXTURE_2D, image_texture);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    stbi_image_free(image_data);
    glBindTexture(GL_TEXTURE_2D, 0);

    *out_texture = image_texture;
    *out_width = image_width;
    *out_height = image_height;

    // Assigning texture to Frame Buffer
    unsigned int fbo;
    glGenFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, image_texture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, image_texture, 0);

    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE){
        std::cout<< "Frame buffer is done."<< std::endl;
    }
    return true;
}

不幸的是,上面的代码导致了一个完全空白的屏幕。我想,我在设置帧缓冲区时遗漏了一些东西。

这是我想使用鼠标坐标对帧缓冲区纹理进行采样的方法:

    void readPixelFromImage(ImVec2 mousePosition) {
        unsigned char pixels[4];
        glReadBuffer(GL_COLOR_ATTACHMENT0);
        glReadPixels(GLint(mousePosition.x), GLint(mousePosition.y), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    
        std::cout << "r: " << static_cast<int>(pixels[0]) << '\n';
        std::cout << "g: " << static_cast<int>(pixels[1]) << '\n';
        std::cout << "b: " << static_cast<int>(pixels[2]) << '\n';
        std::cout << "a: " << static_cast<int>(pixels[3]) << '\n' << std::endl;
    }

感谢任何帮助!

您的代码中确实缺少一些东西:

您设置了一个只包含一个纹理缓冲区的新帧缓冲区。这没关系,所以 glCheckFramebufferStatus 等于 GL_FRAMEBUFFER_COMPLETE。但是您的帧缓冲区没有附加渲染缓冲区。如果你想在屏幕上呈现你的图像,你应该使用默认的帧缓冲区。这个帧缓冲区是从您的 GL 上下文创建的。 但是,文档说:默认帧缓冲区无法更改其缓冲区附件,[...] https://www.khronos.org/opengl/wiki/Framebuffer. So attaching a texture or renderbuffer to the default FB is certainly not possible. You could, however, generate a new FB as you did, render to it, and finally render the outcome (or blit the buffers) to your default FB. Maybe a good starting point for this technique is https://learnopengl.com/Advanced-Lighting/Deferred-Shading

此外,如果您打算只从 GPU 读回渲染值,使用 renderbuffer 而不是 texture[=23 会更高效=].您甚至可以将多个渲染缓冲区附加到您的帧缓冲区(如在延迟着色中)。示例:您可以使用第二个渲染缓冲区来渲染一个 object/instance id(因此,渲染缓冲区将是单通道整数),并且您的第一个渲染缓冲区将用于正常绘制。使用 glReadPixels 读取第二个渲染缓冲区,您可以直接读取哪个实例被绘制在例如鼠标位置。这样,您就可以非常有效地启用鼠标拾取。