From 97da2df0a454dd65056eb6004e71036e1391c897 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Wed, 27 Apr 2022 18:40:28 +0200 Subject: [PATCH] Found a better solution to the single-pixel alignment rendering issue. --- es-core/src/components/ButtonComponent.cpp | 1 - es-core/src/components/ImageComponent.cpp | 7 ++++++- es-core/src/components/NinePatchComponent.cpp | 1 - es-core/src/components/TextComponent.cpp | 7 ++++++- es-core/src/components/primary/CarouselComponent.h | 1 - es-core/src/renderers/Renderer.h | 2 +- es-core/src/renderers/RendererOpenGL.cpp | 8 +++++--- es-core/src/renderers/RendererOpenGL.h | 2 +- 8 files changed, 19 insertions(+), 10 deletions(-) diff --git a/es-core/src/components/ButtonComponent.cpp b/es-core/src/components/ButtonComponent.cpp index 658263210..00147f2f3 100644 --- a/es-core/src/components/ButtonComponent.cpp +++ b/es-core/src/components/ButtonComponent.cpp @@ -133,7 +133,6 @@ void ButtonComponent::render(const glm::mat4& parentTrans) glm::vec3 centerOffset {(mSize.x - mTextCache->metrics.size.x) / 2.0f, (mSize.y - mTextCache->metrics.size.y) / 2.0f, 0.0f}; trans = glm::translate(trans, centerOffset); - trans[3] = glm::round(trans[3]); if (Settings::getInstance()->getBool("DebugText")) { mRenderer->drawRect(centerOffset.x, 0.0f, mTextCache->metrics.size.x, mSize.y, diff --git a/es-core/src/components/ImageComponent.cpp b/es-core/src/components/ImageComponent.cpp index b6890a079..e515b3158 100644 --- a/es-core/src/components/ImageComponent.cpp +++ b/es-core/src/components/ImageComponent.cpp @@ -397,7 +397,12 @@ void ImageComponent::render(const glm::mat4& parentTrans) return; glm::mat4 trans {parentTrans * getTransform()}; - mRenderer->setMatrix(trans); + + // Don't round vertices if scaled as it may lead to single-pixel alignment issues. + if (mScale == 1.0f) + mRenderer->setMatrix(trans, true); + else + mRenderer->setMatrix(trans, false); if (mTexture && mOpacity > 0.0f) { if (Settings::getInstance()->getBool("DebugImage")) { diff --git a/es-core/src/components/NinePatchComponent.cpp b/es-core/src/components/NinePatchComponent.cpp index 2e0dcdc64..582c37bf8 100644 --- a/es-core/src/components/NinePatchComponent.cpp +++ b/es-core/src/components/NinePatchComponent.cpp @@ -132,7 +132,6 @@ void NinePatchComponent::render(const glm::mat4& parentTrans) glm::mat4 trans {parentTrans * getTransform()}; if (mTexture && mVertices != nullptr) { - trans[3] = glm::round(trans[3]); mRenderer->setMatrix(trans); mVertices->opacity = mOpacity; mTexture->bind(); diff --git a/es-core/src/components/TextComponent.cpp b/es-core/src/components/TextComponent.cpp index a01c5a4cb..708aab85d 100644 --- a/es-core/src/components/TextComponent.cpp +++ b/es-core/src/components/TextComponent.cpp @@ -202,7 +202,12 @@ void TextComponent::render(const glm::mat4& parentTrans) mRenderer->drawRect(0.0f, 0.0f, mSize.x, mSize.y, 0x0000FF33, 0x0000FF33); trans = glm::translate(trans, glm::vec3 {0.0f, std::round(yOff), 0.0f}); - mRenderer->setMatrix(trans); + + // Don't round vertices if scaled as it may lead to single-pixel alignment issues. + if (mScale == 1.0f) + mRenderer->setMatrix(trans, true); + else + mRenderer->setMatrix(trans, false); // Draw the text area, where the text actually is located. if (Settings::getInstance()->getBool("DebugText")) { diff --git a/es-core/src/components/primary/CarouselComponent.h b/es-core/src/components/primary/CarouselComponent.h index c3f12b39a..f49ad4713 100644 --- a/es-core/src/components/primary/CarouselComponent.h +++ b/es-core/src/components/primary/CarouselComponent.h @@ -9,7 +9,6 @@ #ifndef ES_CORE_COMPONENTS_CAROUSEL_COMPONENT_H #define ES_CORE_COMPONENTS_CAROUSEL_COMPONENT_H -#include "Log.h" #include "Sound.h" #include "animations/LambdaAnimation.h" #include "components/IList.h" diff --git a/es-core/src/renderers/Renderer.h b/es-core/src/renderers/Renderer.h index 5631dd7be..0d7a5dbe2 100644 --- a/es-core/src/renderers/Renderer.h +++ b/es-core/src/renderers/Renderer.h @@ -196,7 +196,7 @@ public: const unsigned int numVertices, const BlendFactor srcBlendFactor = BlendFactor::SRC_ALPHA, const BlendFactor dstBlendFactor = BlendFactor::ONE_MINUS_SRC_ALPHA) = 0; - virtual void setMatrix(const glm::mat4& matrix) = 0; + virtual void setMatrix(const glm::mat4& matrix, bool roundVertices = true) = 0; virtual void setScissor(const Rect& scissor) = 0; virtual void setSwapInterval() = 0; virtual void swapBuffers() = 0; diff --git a/es-core/src/renderers/RendererOpenGL.cpp b/es-core/src/renderers/RendererOpenGL.cpp index 4abf0ea26..89745991b 100644 --- a/es-core/src/renderers/RendererOpenGL.cpp +++ b/es-core/src/renderers/RendererOpenGL.cpp @@ -275,10 +275,12 @@ void RendererOpenGL::destroyContext() mSDLContext = nullptr; } -void RendererOpenGL::setMatrix(const glm::mat4& matrix) +void RendererOpenGL::setMatrix(const glm::mat4& matrix, bool roundVertices) { - // Set matrix for use with shader. - mTrans = getProjectionMatrix() * matrix; + mTrans = matrix; + if (roundVertices) + mTrans[3] = glm::round(mTrans[3]); + mTrans = getProjectionMatrix() * mTrans; } void RendererOpenGL::setScissor(const Rect& scissor) diff --git a/es-core/src/renderers/RendererOpenGL.h b/es-core/src/renderers/RendererOpenGL.h index ec3a4d7ed..5384e19dd 100644 --- a/es-core/src/renderers/RendererOpenGL.h +++ b/es-core/src/renderers/RendererOpenGL.h @@ -38,7 +38,7 @@ public: bool createContext() override; void destroyContext() override; - void setMatrix(const glm::mat4& matrix) override; + void setMatrix(const glm::mat4& matrix, bool roundVertices = true) override; void setScissor(const Rect& scissor) override; void setSwapInterval() override; void swapBuffers() override;