1

我正在使用 ImGUI 为图像创建颜色选择器 OpenGL 应用程序。我设法通过将图像加载到 aglTexImage2D并使用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;
    }

任何帮助表示赞赏!

4

1 回答 1

1

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

您设置了一个仅包含一个纹理缓冲区的新帧缓冲区。这没关系,所以glCheckFramebufferStatus等于GL_FRAMEBUFFER_COMPLETE。但是您的帧缓冲区没有附加渲染缓冲区。如果你想在屏幕上渲染你的图像,你应该使用默认的帧缓冲区。此帧缓冲区是从您的 GL 上下文创建的。但是,文档说:默认帧缓冲区无法更改其缓冲区附件,[...] https://www.khronos.org/opengl/wiki/Framebuffer. 因此,将纹理或渲染缓冲区附加到默认 FB 肯定是不可能的。但是,您可以像以前一样生成一个新的 FB,对其进行渲染,最后将结果(或对缓冲区进行 blit)渲染到您的默认 FB。也许这种技术的一个很好的起点是https://learnopengl.com/Advanced-Lighting/Deferred-Shading

此外,如果您打算只从 GPU 读回渲染值,则使用渲染缓冲区而不是纹理的性能更高。您甚至可以将多个渲染缓冲区附加到您的帧缓冲区(如在延迟着色中)。示例:您可以使用第二个渲染缓冲区来渲染对象/实例 ID(因此,渲染缓冲区将是单通道整数),您的第一个渲染缓冲区将用于正常绘图。使用glReadPixels读取第二个渲染缓冲区,您可以直接读取在例如鼠标位置绘制的实例。这样,您可以非常有效地启用鼠标拾取。

于 2020-12-21T06:03:37.527 回答