From 6b8af9cf83851c075c6c9514b1deaa931c2b19a4 Mon Sep 17 00:00:00 2001 From: Thomas Guillermo Albers Raviola Date: Fri, 16 Jan 2026 23:02:32 +0100 Subject: Initial commit --- 09-september/tomcat/not_in_use/fbo.c | 108 +++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 09-september/tomcat/not_in_use/fbo.c (limited to '09-september/tomcat/not_in_use/fbo.c') diff --git a/09-september/tomcat/not_in_use/fbo.c b/09-september/tomcat/not_in_use/fbo.c new file mode 100644 index 0000000..5880276 --- /dev/null +++ b/09-september/tomcat/not_in_use/fbo.c @@ -0,0 +1,108 @@ +#include "fbo.h" +#include "renderer.h" +#include "../util/util.h" + +#include +#include + +Fbo *fbo_new(const char *name, GLint width, GLint height) +{ + Fbo *fbo; + + if(strlen(name) >= MAX_PATH_LENGTH) + Util_FatalError("Fbo name is too long: %s\n", name); + + if(render.num_fbos >= MAX_FBOS) + return NULL; + + fbo = malloc(sizeof(Fbo)); + memset(fbo, 0, sizeof(Fbo)); + + glGenFramebuffers(1, &fbo->frame_buffer); + + return fbo; +} + +void fbo_attach_buffer(Fbo *fbo, GLenum format, int index) +{ + GLenum attachment; + GLuint *buffer; + + switch(format) + { + case GL_RGB: + case GL_RGBA: + fbo->color_format = format; + buffer = &fbo->color_buffer[index]; + attachment = GL_COLOR_ATTACHMENT0 + index; + break; + + case GL_DEPTH_COMPONENT: + fbo->depth_format = format; + buffer = &fbo->depth_buffer; + attachment = GL_DEPTH_ATTACHMENT; + break; + + default: + Util_FatalError("Invalid fbo buffer format\n"); + } + + if(*buffer == 0) + { + glGenRenderbuffers(1, buffer); + glBindRenderbuffer(GL_RENDERBUFFER, *buffer); + glRenderbufferStorage(GL_RENDERBUFFER, format, fbo->width, fbo->height); + + fbo_bind(fbo); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, *buffer); + fbo_bind(NULL); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + } +} + +void fbo_attach_texture(Fbo *fbo, Texture *t, GLenum attachment) +{ + int index; + + fbo_bind(fbo); + if(attachment >= GL_COLOR_ATTACHMENT0 && attachment < GL_COLOR_ATTACHMENT0 + 8) + { + glFramebufferTexture(GL_FRAMEBUFFER, attachment, t->tex_id, 0); + glDrawBuffers(1, &attachment); + + index = attachment - GL_COLOR_ATTACHMENT0; + fbo->color_textures[index] = t; + } + + fbo_bind(NULL); +} + +void fbo_bind(Fbo *fbo) +{ + if(fbo) + { + glBindFramebuffer(GL_FRAMEBUFFER, fbo->frame_buffer); + glViewport(0, 0, fbo->width, fbo->height); + } + else + { + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glViewport(0, 0, render.window->Width, render.window->Height); + } +} + +void fbo_purge(Fbo *fbo) +{ + int i; + + for(i = 0; i < 8; i++) + if(fbo->color_buffer[i]) + { + glDeleteRenderbuffers(1, &fbo->color_buffer[i]); + } + if(fbo->depth_buffer) + glDeleteRenderbuffers(1, &fbo->depth_buffer); + + if(fbo->frame_buffer) + glDeleteFramebuffers(1, &fbo->frame_buffer); +} -- cgit v1.2.3