- moved fbtex to post.c, made it a ping-pong buffer, added fbos for it.
[demo_prior] / src / post.c
index c1a2a23..b78284a 100644 (file)
@@ -1,7 +1,76 @@
+#include <assert.h>
 #include "opengl.h"
 #include "texture.h"
 #include "post.h"
 #include "demo.h"
+#include "opt.h"
+
+static unsigned int fbtex_gltexid[2];
+static unsigned int fbo[2], rbuf_depth[2];
+struct texture fbtex[2];
+int fbtex_cur;
+
+int post_init(void)
+{
+       int i;
+
+       glGenTextures(2, fbtex_gltexid);
+       glGenRenderbuffers(2, rbuf_depth);
+
+       for(i=0; i<2; i++) {
+               fbtex[i].id = fbtex_gltexid[i];
+               fbtex[i].width = 0;
+               fbtex[i].height = 0;
+               fbtex[i].pixels = 0;
+
+               glBindTexture(GL_TEXTURE_2D, fbtex[i].id);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+       }
+       return 0;
+}
+
+void post_cleanup(void)
+{
+       if(fbtex_gltexid[0]) {
+               glDeleteTextures(2, fbtex_gltexid);
+       }
+       if(rbuf_depth[0]) {
+               glDeleteRenderbuffers(2, rbuf_depth);
+       }
+       if(fbo[0]) {
+               glDeleteFramebuffers(2, fbo);
+       }
+}
+
+void post_reshape(int x, int y)
+{
+       int i, ifmt;
+
+       if(x != fbtex[0].width || y != fbtex[0].height) {
+               ifmt = opt.srgb ? GL_SRGB_ALPHA : GL_RGBA;
+
+               for(i=0; i<2; i++) {
+                       glBindTexture(GL_TEXTURE_2D, fbtex[i].id);
+                       glTexImage2D(GL_TEXTURE_2D, 0, ifmt, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+                       fbtex[i].width = x;
+                       fbtex[i].height = y;
+
+                       glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth[i]);
+                       glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, x, y);
+               }
+
+               if(!fbo[0]) {
+                       glGenFramebuffers(2, fbo);
+                       for(i=0; i<2; i++) {
+                               glBindFramebuffer(GL_FRAMEBUFFER, fbo[i]);
+                               glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbtex_gltexid[i], 0);
+                               glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth[i]);
+                       }
+                       glBindFramebuffer(GL_FRAMEBUFFER, 0);
+               }
+       }
+}
 
 void overlay(unsigned int tex, float aspect, float alpha)
 {