From a45b4fba8bd4231d89c2aadf1d6118d6c9e06333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 23 Dec 2012 12:41:44 +0100 Subject: [PATCH] Using Rectangle in framebuffer. --- src/AbstractFramebuffer.cpp | 22 +++++++-------- src/AbstractFramebuffer.h | 39 +++++++++++---------------- src/DefaultFramebuffer.cpp | 3 +-- src/Framebuffer.cpp | 5 ++-- src/Framebuffer.h | 4 +-- src/Implementation/FramebufferState.h | 4 +-- 6 files changed, 32 insertions(+), 45 deletions(-) diff --git a/src/AbstractFramebuffer.cpp b/src/AbstractFramebuffer.cpp index 999f64c6e..06b86d8e8 100644 --- a/src/AbstractFramebuffer.cpp +++ b/src/AbstractFramebuffer.cpp @@ -78,25 +78,22 @@ AbstractFramebuffer::Target AbstractFramebuffer::bindInternal() { } #endif -void AbstractFramebuffer::blit(AbstractFramebuffer& source, AbstractFramebuffer& destination, const Vector2i& sourceBottomLeft, const Vector2i& sourceTopRight, const Vector2i& destinationBottomLeft, const Vector2i& destinationTopRight, AbstractFramebuffer::BlitMask mask, AbstractFramebuffer::BlitFilter filter) { +void AbstractFramebuffer::blit(AbstractFramebuffer& source, AbstractFramebuffer& destination, const Rectanglei& sourceRectangle, const Rectanglei& destinationRectangle, AbstractFramebuffer::BlitMask mask, AbstractFramebuffer::BlitFilter filter) { source.bindInternal(AbstractFramebuffer::Target::Read); destination.bindInternal(AbstractFramebuffer::Target::Draw); /** @todo Get some extension wrangler instead to avoid undeclared glBlitFramebuffer() on ES2 */ #ifndef MAGNUM_TARGET_GLES2 - glBlitFramebuffer(sourceBottomLeft.x(), sourceBottomLeft.y(), sourceTopRight.x(), sourceTopRight.y(), destinationBottomLeft.x(), destinationBottomLeft.y(), destinationTopRight.x(), destinationTopRight.y(), static_cast(mask), static_cast(filter)); + glBlitFramebuffer(sourceRectangle.left(), sourceRectangle.bottom(), sourceRectangle.right(), sourceRectangle.top(), destinationRectangle.left(), destinationRectangle.bottom(), destinationRectangle.right(), destinationRectangle.top(), static_cast(mask), static_cast(filter)); #else - static_cast(sourceBottomLeft); - static_cast(sourceTopRight); - static_cast(destinationBottomLeft); - static_cast(destinationTopRight); + static_cast(sourceRectangle); + static_cast(destinationRectangle); static_cast(mask); static_cast(filter); #endif } -void AbstractFramebuffer::setViewport(const Vector2i& position, const Vector2i& size) { - _viewportPosition = position; - _viewportSize = size; +void AbstractFramebuffer::setViewport(const Rectanglei& rectangle) { + _viewport = rectangle; /* Update the viewport if the framebuffer is currently bound */ if(Context::current()->state()->framebuffer->drawBinding == _id) @@ -110,13 +107,12 @@ void AbstractFramebuffer::setViewportInternal() { CORRADE_INTERNAL_ASSERT(state->drawBinding == _id); /* Already up-to-date, nothing to do */ - if(state->viewportPosition == _viewportPosition && state->viewportSize == _viewportSize) + if(state->viewport == _viewport) return; /* Update the state and viewport */ - state->viewportPosition = _viewportPosition; - state->viewportSize = _viewportSize; - glViewport(_viewportPosition.x(), _viewportPosition.y(), _viewportSize.x(), _viewportSize.y()); + state->viewport = _viewport; + glViewport(_viewport.left(), _viewport.bottom(), _viewport.width(), _viewport.height()); } #endif diff --git a/src/AbstractFramebuffer.h b/src/AbstractFramebuffer.h index c6f34a106..f2efec6b1 100644 --- a/src/AbstractFramebuffer.h +++ b/src/AbstractFramebuffer.h @@ -21,7 +21,7 @@ #include -#include "Math/Vector2.h" +#include "Math/Geometry/Rectangle.h" #include "AbstractImage.h" #include "Buffer.h" @@ -139,11 +139,8 @@ class MAGNUM_EXPORT AbstractFramebuffer { * @brief Copy block of pixels * @param source Source framebuffer * @param destination Destination framebuffer - * @param sourceBottomLeft Bottom left coordinates of source rectangle - * @param sourceTopRight Top right coordinates of source rectangle - * @param destinationBottomLeft Bottom left coordinates of destination rectangle - * @param destinationTopRight Top right coordinates of destination - * rectangle + * @param sourceRectangle Source rectangle + * @param destinationRectangle Destination rectangle * @param mask Which buffers to perform blit operation on * @param filter Interpolation filter * @@ -157,16 +154,13 @@ class MAGNUM_EXPORT AbstractFramebuffer { * @requires_gl30 %Extension @extension{EXT,framebuffer_blit} * @requires_gles30 %Extension @es_extension{ANGLE,framebuffer_blit} */ - static void blit(AbstractFramebuffer& source, AbstractFramebuffer& destination, const Vector2i& sourceBottomLeft, const Vector2i& sourceTopRight, const Vector2i& destinationBottomLeft, const Vector2i& destinationTopRight, BlitMask mask, BlitFilter filter); + static void blit(AbstractFramebuffer& source, AbstractFramebuffer& destination, const Rectanglei& sourceRectangle, const Rectanglei& destinationRectangle, BlitMask mask, BlitFilter filter); /** * @brief Copy block of pixels * @param source Source framebuffer * @param destination Destination framebuffer - * @param bottomLeft Bottom left coordinates of source and - * destination rectangle - * @param topRight Top right coordinates of source and - * destination rectangle + * @param rectangle Source and destination rectangle * @param mask Which buffers to perform blit operation on * * Convenience alternative to above function when source rectangle is @@ -178,8 +172,8 @@ class MAGNUM_EXPORT AbstractFramebuffer { * @requires_gl30 %Extension @extension{EXT,framebuffer_blit} * @requires_gles30 %Extension @es_extension{ANGLE,framebuffer_blit} */ - inline static void blit(AbstractFramebuffer& source, AbstractFramebuffer& destination, const Vector2i& bottomLeft, const Vector2i& topRight, BlitMask mask) { - blit(source, destination, bottomLeft, topRight, bottomLeft, topRight, mask, BlitFilter::NearestNeighbor); + inline static void blit(AbstractFramebuffer& source, AbstractFramebuffer& destination, const Rectanglei& rectangle, BlitMask mask) { + blit(source, destination, rectangle, rectangle, mask, BlitFilter::NearestNeighbor); } explicit AbstractFramebuffer() = default; @@ -196,21 +190,18 @@ class MAGNUM_EXPORT AbstractFramebuffer { */ void bind(Target target); - /** @brief Viewport position */ - inline Vector2i viewportPosition() const { return _viewportPosition; } - - /** @brief Viewport size */ - inline Vector2i viewportSize() const { return _viewportSize; } + /** @brief Viewport rectangle */ + inline Rectanglei viewport() const { return _viewport; } /** - * @brief Set viewport size + * @brief Set viewport * - * Saves the viewport size to be used at later time in bind(). If the + * Saves the viewport to be used at later time in bind(). If the * framebuffer is currently bound, updates the viewport to given - * dimensions. + * rectangle. * @see @fn_gl{Viewport} */ - void setViewport(const Vector2i& position, const Vector2i& size); + void setViewport(const Rectanglei& rectangle); /** * @brief Clear specified buffers in framebuffer @@ -231,6 +222,7 @@ class MAGNUM_EXPORT AbstractFramebuffer { * @param image %Image where to put the data * * @see @fn_gl{BindFramebuffer}, @fn_gl{ReadPixels} + * @todo Read size, format & type from image? */ void read(const Vector2i& offset, const Vector2i& size, AbstractImage::Format format, AbstractImage::Type type, Image2D* image); @@ -246,6 +238,7 @@ class MAGNUM_EXPORT AbstractFramebuffer { * * @see @fn_gl{BindFramebuffer}, @fn_gl{ReadPixels} * @requires_gles30 Pixel buffer objects are not available in OpenGL ES 2.0. + * @todo Read size, format & type from image? */ void read(const Vector2i& offset, const Vector2i& size, AbstractImage::Format format, AbstractImage::Type type, BufferImage2D* image, Buffer::Usage usage); #endif @@ -269,7 +262,7 @@ class MAGNUM_EXPORT AbstractFramebuffer { static ReadBufferImplementation readBufferImplementation; GLuint _id; - Vector2i _viewportPosition, _viewportSize; + Rectanglei _viewport; #endif private: diff --git a/src/DefaultFramebuffer.cpp b/src/DefaultFramebuffer.cpp index 38081e96b..a8b0649c4 100644 --- a/src/DefaultFramebuffer.cpp +++ b/src/DefaultFramebuffer.cpp @@ -50,8 +50,7 @@ void DefaultFramebuffer::initializeContextBasedFunctionality(Context* context) { /* Initial framebuffer size */ GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); - defaultFramebuffer._viewportPosition = state->viewportPosition = {viewport[0], viewport[1]}; - defaultFramebuffer._viewportSize = state->viewportSize = {viewport[2], viewport[3]}; + defaultFramebuffer._viewport = state->viewport = Rectanglei::fromSize({viewport[0], viewport[1]}, {viewport[2], viewport[3]}); } } diff --git a/src/Framebuffer.cpp b/src/Framebuffer.cpp index afe16cd3d..8d68e73dc 100644 --- a/src/Framebuffer.cpp +++ b/src/Framebuffer.cpp @@ -34,9 +34,8 @@ Framebuffer::Texture1DImplementation Framebuffer::texture1DImplementation = &Fra Framebuffer::Texture2DImplementation Framebuffer::texture2DImplementation = &Framebuffer::texture2DImplementationDefault; Framebuffer::Texture3DImplementation Framebuffer::texture3DImplementation = &Framebuffer::texture3DImplementationDefault; -Framebuffer::Framebuffer(const Vector2i& viewportPosition, const Vector2i& viewportSize) { - _viewportPosition = viewportPosition; - _viewportSize = viewportSize; +Framebuffer::Framebuffer(const Rectanglei& viewport) { + _viewport = viewport; glGenFramebuffers(1, &_id); } diff --git a/src/Framebuffer.h b/src/Framebuffer.h index ec638d3b6..0fd057e81 100644 --- a/src/Framebuffer.h +++ b/src/Framebuffer.h @@ -95,9 +95,9 @@ class MAGNUM_EXPORT Framebuffer: public AbstractFramebuffer { * @brief Constructor * * Generates new OpenGL framebuffer. - * @see @fn_gl{GenFramebuffers} + * @see setViewport(), @fn_gl{GenFramebuffers} */ - explicit Framebuffer(const Vector2i& viewportPosition, const Vector2i& viewportSize); + explicit Framebuffer(const Rectanglei& viewport); /** * @brief Destructor diff --git a/src/Implementation/FramebufferState.h b/src/Implementation/FramebufferState.h index 93b83f1ad..b5d7cf891 100644 --- a/src/Implementation/FramebufferState.h +++ b/src/Implementation/FramebufferState.h @@ -15,7 +15,7 @@ GNU Lesser General Public License version 3 for more details. */ -#include "Math/Vector2.h" +#include "Math/Geometry/Rectangle.h" #include "Magnum.h" namespace Magnum { namespace Implementation { @@ -24,7 +24,7 @@ struct FramebufferState { inline FramebufferState(): readBinding(0), drawBinding(0), renderbufferBinding(0) {} GLuint readBinding, drawBinding, renderbufferBinding; - Vector2i viewportPosition, viewportSize; + Rectanglei viewport; }; }}