From 08b8252f73ad5f0198994835fbd27edf9522ef4d Mon Sep 17 00:00:00 2001 From: DarkContact Date: Wed, 10 Nov 2021 01:35:48 +0300 Subject: [PATCH] Fix font rendering (#2653) * Fix subpixel font rendering * remove const --- examples/10-font/font.cpp | 5 ++++- examples/11-fontsdf/fontsdf.cpp | 5 ++++- examples/common/cube_atlas.cpp | 36 ++++++--------------------------- examples/common/cube_atlas.h | 3 --- 4 files changed, 14 insertions(+), 35 deletions(-) diff --git a/examples/10-font/font.cpp b/examples/10-font/font.cpp index 720135130..368f3f429 100644 --- a/examples/10-font/font.cpp +++ b/examples/10-font/font.cpp @@ -266,7 +266,10 @@ public: float view[16]; bx::mtxLookAt(view, eye, at); - const float centering = 0.5f; + float centering = 0.0f; + if (bgfx::getRendererType() == bgfx::RendererType::Direct3D9) { + centering = -0.5f; + } // Setup a top-left ortho matrix for screen space drawing. const bgfx::Caps* caps = bgfx::getCaps(); diff --git a/examples/11-fontsdf/fontsdf.cpp b/examples/11-fontsdf/fontsdf.cpp index 4687a5d80..83ba8231c 100644 --- a/examples/11-fontsdf/fontsdf.cpp +++ b/examples/11-fontsdf/fontsdf.cpp @@ -253,7 +253,10 @@ public: float view[16]; bx::mtxLookAt(view, eye, at); - const float centering = 0.5f; + float centering = 0.0f; + if (bgfx::getRendererType() == bgfx::RendererType::Direct3D9) { + centering = -0.5f; + } // Setup a top-left ortho matrix for screen space drawing. const bgfx::Caps* caps = bgfx::getCaps(); diff --git a/examples/common/cube_atlas.cpp b/examples/common/cube_atlas.cpp index 610c5f011..b96ef8567 100644 --- a/examples/common/cube_atlas.cpp +++ b/examples/common/cube_atlas.cpp @@ -259,7 +259,7 @@ Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount) BX_ASSERT(_textureSize >= 64 && _textureSize <= 4096, "Invalid _textureSize %d.", _textureSize); BX_ASSERT(_maxRegionsCount >= 64 && _maxRegionsCount <= 32000, "Invalid _maxRegionsCount %d.", _maxRegionsCount); - init(); + m_texelSize = float(UINT16_MAX) / float(m_textureSize); m_layers = new PackedLayer[6]; for (int ii = 0; ii < 6; ++ii) @@ -287,7 +287,7 @@ Atlas::Atlas(uint16_t _textureSize, const uint8_t* _textureBuffer, uint16_t _reg { BX_ASSERT(_regionCount <= 64 && _maxRegionsCount <= 4096, "_regionCount %d, _maxRegionsCount %d", _regionCount, _maxRegionsCount); - init(); + m_texelSize = float(UINT16_MAX) / float(m_textureSize); m_regions = new AtlasRegion[_regionCount]; m_textureBuffer = new uint8_t[getTextureBufferSize()]; @@ -313,30 +313,6 @@ Atlas::~Atlas() delete [] m_textureBuffer; } -void Atlas::init() -{ - m_texelSize = float(UINT16_MAX) / float(m_textureSize); - float texelHalf = m_texelSize/2.0f; - switch (bgfx::getRendererType() ) - { - case bgfx::RendererType::Direct3D9: - m_texelOffset[0] = 0.0f; - m_texelOffset[1] = 0.0f; - break; - - case bgfx::RendererType::Direct3D11: - case bgfx::RendererType::Direct3D12: - m_texelOffset[0] = texelHalf; - m_texelOffset[1] = texelHalf; - break; - - default: - m_texelOffset[0] = texelHalf; - m_texelOffset[1] = -texelHalf; - break; - } -} - uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type, uint16_t outline) { if (m_regionCount >= m_maxRegionCount) @@ -469,10 +445,10 @@ static void writeUV(uint8_t* _vertexBuffer, int16_t _x, int16_t _y, int16_t _z, void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const { - int16_t x0 = (int16_t)( ( (float)_region.x * m_texelSize + m_texelOffset[0]) - float(INT16_MAX) ); - int16_t y0 = (int16_t)( ( (float)_region.y * m_texelSize + m_texelOffset[1]) - float(INT16_MAX) ); - int16_t x1 = (int16_t)( ( ( (float)_region.x + _region.width) * m_texelSize + m_texelOffset[0]) - float(INT16_MAX) ); - int16_t y1 = (int16_t)( ( ( (float)_region.y + _region.height) * m_texelSize + m_texelOffset[1]) - float(INT16_MAX) ); + int16_t x0 = (int16_t)( ( (float)_region.x * m_texelSize) - float(INT16_MAX) ); + int16_t y0 = (int16_t)( ( (float)_region.y * m_texelSize) - float(INT16_MAX) ); + int16_t x1 = (int16_t)( ( ( (float)_region.x + _region.width) * m_texelSize) - float(INT16_MAX) ); + int16_t y1 = (int16_t)( ( ( (float)_region.y + _region.height) * m_texelSize) - float(INT16_MAX) ); int16_t ww = (int16_t)( (float(INT16_MAX) / 4.0f) * (float)_region.getComponentIndex() ); _vertexBuffer += _offset; diff --git a/examples/common/cube_atlas.h b/examples/common/cube_atlas.h index 9a33a7611..c48b63eaa 100644 --- a/examples/common/cube_atlas.h +++ b/examples/common/cube_atlas.h @@ -135,8 +135,6 @@ public: } private: - void init(); - struct PackedLayer; PackedLayer* m_layers; AtlasRegion* m_regions; @@ -148,7 +146,6 @@ private: bgfx::TextureHandle m_textureHandle; uint16_t m_textureSize; float m_texelSize; - float m_texelOffset[2]; uint16_t m_regionCount; uint16_t m_maxRegionCount;