From 357acfb941a8b5b4a6fb36e2134707d46e028d88 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 12 Jan 2014 14:14:31 +0200 Subject: [PATCH] Add viewport support to Framebuffer --- source/framebuffer.cpp | 39 ++++++++++++++++++++++++++++++++------- source/framebuffer.h | 14 ++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/source/framebuffer.cpp b/source/framebuffer.cpp index 67249518..8c618ce5 100644 --- a/source/framebuffer.cpp +++ b/source/framebuffer.cpp @@ -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::iterator i=attachments.begin(); i!=attachments.end(); ++i) if(i->type) { @@ -152,8 +152,8 @@ void Framebuffer::check_size() width = static_cast(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 diff --git a/source/framebuffer.h b/source/framebuffer.h index 3bf439f9..f34ecf5c 100644 --- a/source/framebuffer.h +++ b/source/framebuffer.h @@ -98,10 +98,21 @@ private: void clear(); }; + struct Viewport + { + int left; + int bottom; + unsigned width; + unsigned height; + + Viewport(); + }; + unsigned id; std::vector 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 -- 2.43.0