backend: gl: fix read_pixel

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui
2022-01-19 17:47:55 +00:00
parent 4aac801a12
commit ef73668eb9

View File

@@ -1918,6 +1918,12 @@ bool gl_read_pixel(backend_t *base attr_unused, void *image_data, int x, int y,
struct color *output) {
struct backend_image *tex = image_data;
auto inner = (struct gl_texture *)tex->inner;
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
inner->texture, 0);
glReadBuffer(GL_COLOR_ATTACHMENT0);
GLfloat color[4];
glReadPixels(x, inner->y_inverted ? inner->height - y : y, 1, 1, GL_RGBA,
GL_FLOAT, color);
@@ -1926,6 +1932,9 @@ bool gl_read_pixel(backend_t *base attr_unused, void *image_data, int x, int y,
output->green = color[1];
output->blue = color[2];
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &fbo);
bool ret = glGetError() == GL_NO_ERROR;
gl_clear_err();
return ret;