From 9ce0961712eb176b2f02b761c6e4e8491270e73b Mon Sep 17 00:00:00 2001 From: rachytski Date: Fri, 30 Nov 2012 11:05:02 +0300 Subject: [PATCH] refactored coordinate system setting. --- graphics/coordinates.cpp | 18 ++++++++++++++++++ graphics/coordinates.hpp | 13 +++++++++++++ graphics/graphics.pro | 4 ++++ graphics/opengl/base_texture.cpp | 1 - graphics/opengl/framebuffer.cpp | 8 -------- graphics/opengl/renderbuffer.cpp | 3 --- graphics/opengl/renderer.cpp | 27 +++++++++++++++++++++++++++ graphics/opengl/renderer.hpp | 9 +++++++++ graphics/render_target.cpp | 14 ++++++++++++++ graphics/render_target.hpp | 7 +++++-- iphone/Maps/Classes/RenderBuffer.hpp | 1 + iphone/Maps/Classes/RenderBuffer.mm | 14 +++++++++----- 12 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 graphics/coordinates.cpp create mode 100644 graphics/coordinates.hpp create mode 100644 graphics/render_target.cpp diff --git a/graphics/coordinates.cpp b/graphics/coordinates.cpp new file mode 100644 index 0000000000..c26babb775 --- /dev/null +++ b/graphics/coordinates.cpp @@ -0,0 +1,18 @@ +#include "coordinates.hpp" + +namespace graphics +{ + void getOrthoMatrix(math::Matrix & m, + float l, + float r, + float b, + float t, + float n, + float f) + { + m(0, 0) = 2 / (r - l); m(0, 1) = 0; m(0, 2) = 0; m(0, 3) = -(r + l) / (r - l); + m(1, 0) = 0; m(1, 1) = 2 / (t - b);m(1, 2) = 0; m(1, 3) = -(t + b) / (t - b); + m(2, 0) = 0; m(2, 1) = 0; m(2, 2) = -2 / (f - n); m(2, 3) = - (f + n) / (f - n); + m(3, 0) = 0; m(3, 1) = 0; m(3, 2) = 0; m(3, 3) = 1; + } +} diff --git a/graphics/coordinates.hpp b/graphics/coordinates.hpp new file mode 100644 index 0000000000..63d5eff7d3 --- /dev/null +++ b/graphics/coordinates.hpp @@ -0,0 +1,13 @@ +#include "../base/matrix.hpp" + +namespace graphics +{ + /// Calculate matrix for orthographic projection + void getOrthoMatrix(math::Matrix & m, + float left, + float right, + float bottom, + float top, + float near, + float far); +} diff --git a/graphics/graphics.pro b/graphics/graphics.pro index 492798e8c5..14bb00d442 100644 --- a/graphics/graphics.pro +++ b/graphics/graphics.pro @@ -67,6 +67,9 @@ SOURCES += \ image_info.cpp \ image_renderer.cpp \ display_list_renderer.cpp + render_context.cpp \ + coordinates.cpp \ + render_target.cpp \ HEADERS += \ opengl/opengl.hpp \ @@ -128,6 +131,7 @@ HEADERS += \ image_info.hpp \ display_list_renderer.hpp render_context.hpp \ + coordinates.hpp \ win32* { SOURCES += opengl/opengl_win32.cpp diff --git a/graphics/opengl/base_texture.cpp b/graphics/opengl/base_texture.cpp index c5a8229411..f68afb26b6 100644 --- a/graphics/opengl/base_texture.cpp +++ b/graphics/opengl/base_texture.cpp @@ -58,7 +58,6 @@ namespace graphics GL_TEXTURE_2D, id(), 0)); - utils::setupCoordinates(width(), height(), false); } void BaseTexture::detachFromFrameBuffer() diff --git a/graphics/opengl/framebuffer.cpp b/graphics/opengl/framebuffer.cpp index a641601cdf..a7bea0cb0a 100644 --- a/graphics/opengl/framebuffer.cpp +++ b/graphics/opengl/framebuffer.cpp @@ -54,15 +54,7 @@ namespace graphics GL_TEXTURE_2D, 0, 0)); - - utils::setupCoordinates(width(), height(), true); } - - /// !!! it's a must for a correct work. - /// update: it was necessary for multisampling, - /// but without it on KindleFire this function produces bug -/* if (m_id != 0) - checkStatus();*/ } void FrameBuffer::setRenderTarget(shared_ptr const & renderTarget) diff --git a/graphics/opengl/renderbuffer.cpp b/graphics/opengl/renderbuffer.cpp index 3b6e4e1c90..a91d050568 100644 --- a/graphics/opengl/renderbuffer.cpp +++ b/graphics/opengl/renderbuffer.cpp @@ -48,9 +48,6 @@ namespace graphics isDepthBuffer() ? GL_DEPTH_ATTACHMENT_MWM : GL_COLOR_ATTACHMENT0_MWM, GL_RENDERBUFFER_MWM, id())); - - if (!isDepthBuffer()) - utils::setupCoordinates(width(), height(), false); } void RenderBuffer::detachFromFrameBuffer() diff --git a/graphics/opengl/renderer.cpp b/graphics/opengl/renderer.cpp index f524924e25..f8702cd5f0 100644 --- a/graphics/opengl/renderer.cpp +++ b/graphics/opengl/renderer.cpp @@ -1,5 +1,6 @@ #include "../base/logging.hpp" #include "../render_context.hpp" +#include "../coordinates.hpp" #include "renderer.hpp" #include "data_traits.hpp" @@ -33,6 +34,7 @@ namespace graphics m_env(0), m_threadSlot(params.m_threadSlot) { + m_renderContext = params.m_renderContext; m_frameBuffer = params.m_frameBuffer; m_resourceManager = params.m_resourceManager; @@ -64,6 +66,20 @@ namespace graphics m_frameBuffer->setDepthBuffer(m_depthBuffer); processCommand(make_shared_ptr(new ChangeFrameBuffer(m_frameBuffer))); + + math::Matrix coordM; + + if (m_renderTarget != 0) + m_renderTarget->coordMatrix(coordM); + else + getOrthoMatrix(coordM, + 0, m_frameBuffer->width(), + m_frameBuffer->height(), 0, + -graphics::maxDepth, + graphics::maxDepth); + + processCommand(make_shared_ptr(new ChangeMatrix(EProjection, coordM))); + processCommand(make_shared_ptr(new ChangeMatrix(EModelView, math::Identity()))); } // checkStatus(); @@ -88,6 +104,15 @@ namespace graphics processCommand(make_shared_ptr(new UnbindRenderTarget(m_renderTarget))); } + Renderer::ChangeMatrix::ChangeMatrix(EMatrix mt, math::Matrix const & m) + : m_matrixType(mt), m_matrix(m) + {} + + void Renderer::ChangeMatrix::perform() + { + renderContext()->setMatrix(m_matrixType, m_matrix); + } + Renderer::DiscardFramebuffer::DiscardFramebuffer(bool doDiscardColor, bool doDiscardDepth) : m_doDiscardColor(doDiscardColor), m_doDiscardDepth(doDiscardDepth) {} @@ -260,6 +285,8 @@ namespace graphics } m_frameBuffer->makeCurrent(); + + OGLCHECK(glViewport(0, 0, m_frameBuffer->width(), m_frameBuffer->height())); } void Renderer::onSize(unsigned int width, unsigned int height) diff --git a/graphics/opengl/renderer.hpp b/graphics/opengl/renderer.hpp index abd0af9133..18ccb9b3dc 100644 --- a/graphics/opengl/renderer.hpp +++ b/graphics/opengl/renderer.hpp @@ -59,6 +59,15 @@ namespace graphics void perform(); }; + struct ChangeMatrix : Command + { + EMatrix m_matrixType; + math::Matrix m_matrix; + + ChangeMatrix(EMatrix mt, math::Matrix const & m); + void perform(); + }; + struct UnbindRenderTarget : Command { shared_ptr m_renderTarget; diff --git a/graphics/render_target.cpp b/graphics/render_target.cpp new file mode 100644 index 0000000000..d934f906f2 --- /dev/null +++ b/graphics/render_target.cpp @@ -0,0 +1,14 @@ +#include "render_target.hpp" +#include "coordinates.hpp" +#include "defines.hpp" + +namespace graphics +{ + RenderTarget::~RenderTarget() + {} + + void RenderTarget::coordMatrix(math::Matrix & m) + { + getOrthoMatrix(m, 0, width(), 0, height(), -maxDepth, maxDepth); + } +} diff --git a/graphics/render_target.hpp b/graphics/render_target.hpp index 7ef18ce2a0..a145f62ce8 100644 --- a/graphics/render_target.hpp +++ b/graphics/render_target.hpp @@ -1,15 +1,18 @@ #pragma once +#include "../base/matrix.hpp" + namespace graphics { class RenderTarget { public: - virtual ~RenderTarget() {} - /// attach render target to framebuffer and setup coordinate system + virtual ~RenderTarget(); virtual unsigned int id() const = 0; + /// attach render target to framebuffer and setup coordinate system virtual void attachToFrameBuffer() = 0; virtual void detachFromFrameBuffer() = 0; + virtual void coordMatrix(math::Matrix & m); virtual unsigned width() const = 0; virtual unsigned height() const = 0; }; diff --git a/iphone/Maps/Classes/RenderBuffer.hpp b/iphone/Maps/Classes/RenderBuffer.hpp index cce1082d53..12ec903cee 100644 --- a/iphone/Maps/Classes/RenderBuffer.hpp +++ b/iphone/Maps/Classes/RenderBuffer.hpp @@ -33,5 +33,6 @@ namespace iphone void attachToFrameBuffer(); void detachFromFrameBuffer(); + void coordMatrix(math::Matrix & m); }; } diff --git a/iphone/Maps/Classes/RenderBuffer.mm b/iphone/Maps/Classes/RenderBuffer.mm index c3fd3b01e2..53529762ac 100644 --- a/iphone/Maps/Classes/RenderBuffer.mm +++ b/iphone/Maps/Classes/RenderBuffer.mm @@ -3,7 +3,7 @@ #include "RenderBuffer.hpp" #include "../../../graphics/opengl/opengl.hpp" -#include "../../../graphics/opengl/utils.hpp" +#include "../../../graphics/coordinates.hpp" namespace iphone { @@ -70,14 +70,18 @@ namespace iphone GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, m_id)); - graphics::gl::utils::setupCoordinates(width(), height(), true); } void RenderBuffer::detachFromFrameBuffer() { OGLCHECK(glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, - GL_RENDERBUFFER_OES, - 0)); + GL_RENDERBUFFER_OES, + 0)); } -} \ No newline at end of file + + void RenderBuffer::coordMatrix(math::Matrix & m) + { + graphics::getOrthoMatrix(m, 0, width(), height(), 0, -graphics::maxDepth, graphics::maxDepth); + } +}