aboutsummaryrefslogtreecommitdiff
path: root/09-september/tomcat/not_in_use/fbo.c
diff options
context:
space:
mode:
Diffstat (limited to '09-september/tomcat/not_in_use/fbo.c')
-rw-r--r--09-september/tomcat/not_in_use/fbo.c108
1 files changed, 108 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);
+}