nasty bug in Texture::create
[laserbrain_demo] / src / rtarg.cc
index f90c1d3..12a4ddb 100644 (file)
@@ -1,3 +1,4 @@
+#include <assert.h>
 #include "rtarg.h"
 
 struct RTStackItem {
@@ -51,9 +52,11 @@ bool push_render_target(RenderTarget *rt)
                vp[2] = prev->get_width();
                vp[3] = prev->get_height();
        } else {
-               memcpy(vp, rstack[rtop].saved_vp, 4 * sizeof(int));
+               glGetIntegerv(GL_VIEWPORT, vp);
        }
        rstack[++rtop].rt = rt;
+
+       rt->bind();
        return true;
 }
 
@@ -79,6 +82,7 @@ RenderTarget::RenderTarget()
 {
        fbo = 0;
        rbdepth = 0;
+       rbdepth_fmt = 0;
        width = height = tex_width = tex_height = 0;
        auto_vport = true;
        rtcount = 0;
@@ -87,6 +91,7 @@ RenderTarget::RenderTarget()
                tex[i] = 0;
                own_texture[i] = true;
        }
+       depth = 0;
 }
 
 RenderTarget::~RenderTarget()
@@ -118,6 +123,7 @@ bool RenderTarget::create_mrt(int xsz, int ysz, int num, unsigned int fmt, unsig
                        tex[i]->create(tex_width, tex_height, TEX_2D, fmt);
                        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D,
                                        tex[i]->get_id(), 0);
+                       glBindTexture(GL_TEXTURE_2D, 0);
                        own_texture[i] = true;
                }
        } else {
@@ -131,30 +137,31 @@ bool RenderTarget::create_mrt(int xsz, int ysz, int num, unsigned int fmt, unsig
        }
 
        if(flags & (RT_DEPTH | RT_STENCIL)) {
-               unsigned int fmt, attachment;
+               unsigned int attachment;
 
                glGenRenderbuffers(1, &rbdepth);
                glBindRenderbuffer(GL_RENDERBUFFER, rbdepth);
 
                switch(flags & (RT_DEPTH | RT_STENCIL)) {
                case RT_DEPTH:
-                       fmt = GL_DEPTH_COMPONENT24;
+                       rbdepth_fmt = GL_DEPTH_COMPONENT24;
                        attachment = GL_DEPTH_ATTACHMENT;
                        break;
 
                case RT_STENCIL:
-                       fmt = GL_STENCIL_INDEX8;
+                       rbdepth_fmt = GL_STENCIL_INDEX8;
                        attachment = GL_STENCIL_ATTACHMENT;
                        break;
 
                case RT_DEPTH | RT_STENCIL:
-                       fmt = GL_DEPTH24_STENCIL8;
+                       rbdepth_fmt = GL_DEPTH24_STENCIL8;
                        attachment = GL_DEPTH_STENCIL_ATTACHMENT;
                        break;
                }
 
-               glRenderbufferStorage(GL_RENDERBUFFER, fmt, width, height);
+               glRenderbufferStorage(GL_RENDERBUFFER, rbdepth_fmt, width, height);
                glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, rbdepth);
+               glBindRenderbuffer(GL_RENDERBUFFER, 0);
 
        } else if(depth) {
                attach_depth_texture(depth);
@@ -182,6 +189,36 @@ void RenderTarget::destroy()
        }
 }
 
+bool RenderTarget::resize(int xsz, int ysz)
+{
+       int new_tx = next_pow2(xsz);
+       int new_ty = next_pow2(ysz);
+
+       if(new_tx != tex_width || new_ty != tex_height) {
+               tex_width = new_tx;
+               tex_height = new_ty;
+
+               for(int i=0; i<4; i++) {
+                       if(tex[i]) {
+                               tex[i]->create(new_tx, new_ty, TEX_2D, tex[i]->get_format());
+                       }
+               }
+
+               if(depth) {
+                       depth->create(new_tx, new_ty, TEX_2D, depth->get_format());
+               }
+       }
+
+       if(rbdepth) {
+               glBindRenderbuffer(GL_RENDERBUFFER, rbdepth);
+               glRenderbufferStorage(GL_RENDERBUFFER, rbdepth_fmt, xsz, ysz);
+       }
+
+       width = xsz;
+       height = ysz;
+       return true;
+}
+
 int RenderTarget::get_width() const
 {
        return width;
@@ -282,7 +319,7 @@ static void attach_depth_texture(Texture *tex)
 
        default:
                error_log("failed to attach depth/stencil texture: unexpected texture format: %x\n", fmt);
-               break;
+               return;
        }
 
        glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex->get_id(), 0);