]> git.tdb.fi Git - libs/gl.git/commitdiff
Add viewport support to Framebuffer
authorMikko Rasa <tdb@tdb.fi>
Sun, 12 Jan 2014 12:14:31 +0000 (14:14 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 12 Jan 2014 12:14:31 +0000 (14:14 +0200)
source/framebuffer.cpp
source/framebuffer.h

index 672495182f7f641bbf419c2ea5b35dd28af6c28e..8c618ce51b7112ba96f4332cb1e08de683744397 100644 (file)
@@ -55,10 +55,9 @@ Framebuffer::Framebuffer(unsigned i):
        if(id)
                throw invalid_argument("System framebuffer must have id 0");
 
-       int viewport[4];
-       glGetIntegerv(GL_VIEWPORT, viewport);
-       width = viewport[2];
-       height = viewport[3];
+       glGetIntegerv(GL_VIEWPORT, &view.left);
+       width = view.width;
+       height = view.height;
 }
 
 Framebuffer::Framebuffer():
@@ -133,6 +132,7 @@ void Framebuffer::update_attachment(unsigned mask) const
 
 void Framebuffer::check_size()
 {
+       bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
        for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
                if(i->type)
                {
@@ -152,8 +152,8 @@ void Framebuffer::check_size()
                                width = static_cast<TextureCube *>(i->tex)->get_size();
                                height = width;
                        }
-                       if(current()==this)
-                               glViewport(0, 0, width, height);
+                       if(full_viewport)
+                               reset_viewport();
                        break;
                }
 }
@@ -224,6 +224,22 @@ void Framebuffer::require_complete() const
                throw framebuffer_incomplete(status);
 }
 
+void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
+{
+       view.left = l;
+       view.bottom = b;
+       view.width = w;
+       view.height = h;
+
+       if(current()==this)
+               glViewport(view.left, view.bottom, view.width, view.height);
+}
+
+void Framebuffer::reset_viewport()
+{
+       viewport(0, 0, width, height);
+}
+
 void Framebuffer::clear(BufferBits bits)
 {
        BindRestore _bind(this);
@@ -273,8 +289,9 @@ void Framebuffer::bind() const
                        update_attachment(dirty);
                        dirty = 0;
                }
+
                if(width && height)
-                       glViewport(0, 0, width, height);
+                       glViewport(view.left, view.bottom, view.width, view.height);
        }
 }
 
@@ -323,5 +340,13 @@ void Framebuffer::Attachment::clear()
        type = 0;
 }
 
+
+Framebuffer::Viewport::Viewport():
+       left(0),
+       bottom(0),
+       width(0),
+       height(0)
+{ }
+
 } // namespace GL
 } // namespace Msp
index 3bf439f98cc62ca3250369abe61cdaf6c5c9de9c..f34ecf5c6eeea0204eaa685dfee5e7f4537e75b9 100644 (file)
@@ -98,10 +98,21 @@ private:
                void clear();
        };
 
+       struct Viewport
+       {
+               int left;
+               int bottom;
+               unsigned width;
+               unsigned height;
+
+               Viewport();
+       };
+
        unsigned id;
        std::vector<Attachment> attachments;
        unsigned width;
        unsigned height;
+       Viewport view;
        mutable unsigned dirty;
 
        Framebuffer(unsigned);
@@ -131,6 +142,9 @@ public:
        isn't. */
        void require_complete() const;
 
+       void viewport(int, int, unsigned, unsigned);
+       void reset_viewport();
+
        void clear(BufferBits);
 
        /** Blits a region from another framebuffer into this one.  If the source