From 22b2fd486431f9d7cce9f0a6dbca6a3e0306ffce Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 19 Jul 2022 21:04:43 +0200 Subject: [PATCH] Keep global state clean Unbind the FBO+texture after initialization, bind it before rendering, unbind it after, re-bind the texture to scrape the data, unbind the texture. Signed-off-by: David Oberhollenzer --- shadermeh.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/shadermeh.c b/shadermeh.c index ec3ff9b..d879669 100644 --- a/shadermeh.c +++ b/shadermeh.c @@ -232,10 +232,13 @@ int main(int argc, char **argv) glBindTexture(GL_TEXTURE_2D, fbo_tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); glBindFramebuffer(GL_FRAMEBUFFER, fbo); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, fbo_tex, 0); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); } else { window_show(wnd); } @@ -244,6 +247,10 @@ int main(int argc, char **argv) while (to_stdout || window_handle_events()) { /* render image to FBO */ clock_gettime(CLOCK_MONOTONIC_RAW, &frame_start); + + if (to_stdout) + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); @@ -255,9 +262,14 @@ int main(int argc, char **argv) /* get image from FBO, dump to stdout */ if (to_stdout) { + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glBindTexture(GL_TEXTURE_2D, fbo_tex); + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, fb32); + glBindTexture(GL_TEXTURE_2D, 0); + convert_for_ffmpeg(fb32, fb24, width, height); if (write_retry(STDOUT_FILENO, fb24,