diff options
Diffstat (limited to '09-september/tomcat/not_in_use')
| -rw-r--r-- | 09-september/tomcat/not_in_use/fbo.c | 108 | ||||
| -rw-r--r-- | 09-september/tomcat/not_in_use/fbo.h | 35 |
2 files changed, 143 insertions, 0 deletions
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 <string.h> +#include <stdlib.h> + +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); +} diff --git a/09-september/tomcat/not_in_use/fbo.h b/09-september/tomcat/not_in_use/fbo.h new file mode 100644 index 0000000..244cb7f --- /dev/null +++ b/09-september/tomcat/not_in_use/fbo.h @@ -0,0 +1,35 @@ +#ifndef FBO_H +#define FBO_H + +#include "../shared.h" + +struct _Texture; + +typedef struct _Fbo +{ + char _name[MAX_PATH_LENGTH]; + + GLuint frame_buffer; /** Actual fbo object**/ + + /** color_texture when you want to render to a + texture and color_buffer when you want to render + to a RenderBuffer **/ + struct _Texture *color_textures[8]; + GLuint color_buffer[8]; + GLenum color_format; + + struct _Texture *depth_texture; + GLuint depth_buffer; + GLenum depth_format; + + GLint width; + GLint height; +} Fbo; + +extern Fbo *fbo_new(const char *name, GLint width, GLint height); +extern void fbo_attach_buffer(Fbo *fbo, GLenum format, int index); +extern void fbo_attach_texture(Fbo *fbo, struct _Texture *t, GLenum attachment); +extern void fbo_bind(Fbo *fbo); +extern void fbo_purge(Fbo *fbo); + +#endif // FBO_H |
