From 8f5e37a68ca9f8dc2e2451b243005246625ffc35 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 13 Jun 2024 13:36:12 +0200 Subject: separate framebuffer initialization into separate file --- fb.c | 30 ++++++++++++++++++++++++++++++ fb.h | 6 ++++++ main.c | 18 ++---------------- makefile | 1 + 4 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 fb.c create mode 100644 fb.h diff --git a/fb.c b/fb.c new file mode 100644 index 0000000..479808c --- /dev/null +++ b/fb.c @@ -0,0 +1,30 @@ +#include +#include + +#include "fb.h" +#include "die.h" +#include "consts.h" + +GLuint init_fb() { + GLuint fbo; + glGenFramebuffers(1, &fbo); + + // configure newly created framebuffer + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + + GLuint buffer; + glGenTextures(1, &buffer); + glBindTexture(GL_TEXTURE_2D, buffer); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RES_H, RES_V, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, buffer, 0); + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + die("can't initialize framebuffer??\n"); + + // use default framebuffer again + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + return fbo; +} + diff --git a/fb.h b/fb.h new file mode 100644 index 0000000..cc0df69 --- /dev/null +++ b/fb.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +GLuint init_fb(); + diff --git a/main.c b/main.c index e65c829..075f264 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,7 @@ #include #include "consts.h" +#include "fb.h" #include "shader.h" #include "die.h" #include "uniform.h" @@ -81,22 +82,7 @@ int main(int argc, char** argv) { frag_shader(pass2_frag, pass2_frag_size) ); - GLuint fbo; - glGenFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - - GLuint last_frame; - glGenTextures(1, &last_frame); - glBindTexture(GL_TEXTURE_2D, last_frame); - // todo: what to do when the size changes? - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RES_H, RES_V, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, last_frame, 0); - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - die("can't initialize framebuffer??\n"); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - + GLuint fbo = init_fb(); // main draw loop while (!glfwWindowShouldClose(window)) { diff --git a/makefile b/makefile index 719f0f0..829b235 100644 --- a/makefile +++ b/makefile @@ -18,6 +18,7 @@ main: pass1_frag.o main: fill_vert.o main: pass2_frag.o main: uniform.o +main: fb.o # fix compile order main.o: fill_vert.h -- cgit v1.2.3