From 4285f7549441f4234c639385fad27cace8692a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 13 Dec 2013 11:40:16 +0100 Subject: [PATCH 01/10] Text: improve AbstractFont documentation. --- src/Text/AbstractFont.h | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Text/AbstractFont.h b/src/Text/AbstractFont.h index 24d8ecdea..21f620c8c 100644 --- a/src/Text/AbstractFont.h +++ b/src/Text/AbstractFont.h @@ -152,17 +152,28 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin { /** @brief Close font */ void close(); - /** @brief Font size */ + /** + * @brief Font size + * + * Returns scale in which @ref lineHeight() and @ref glyphAdvance() is + * returned. + */ Float size() const { return _size; } - /** @brief Line height */ + /** + * @brief Line height + * + * Returns line height scaled to font size. + * @see @ref size() + */ Float lineHeight() const { return _lineHeight; } /** * @brief Glyph ID for given character * - * @note This function is not meant to be used in performance-critical - * code, only for font observations and conversions. + * @note This function is meant to be used only for font observations + * and conversions. In performance-critical code the @ref layout() + * function should be used instead. */ UnsignedInt glyphId(char32_t character); @@ -170,9 +181,11 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin { * @brief Glyph advance * @param glyph Glyph ID * - * @note This function is not meant to be used in performance-critical - * code, only for font observations and conversions. - * @see @ref glyphId() + * Returns glyph advance scaled to font size. + * @note This function is meant to be used only for font observations + * and conversions. In performance-critical code the @ref layout() + * function should be used instead. + * @see @ref glyphId(), @ref size() */ Vector2 glyphAdvance(UnsignedInt glyph); From 5e1e95bae683e9661a76569b4fcaffc969e47c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 13 Dec 2013 01:45:29 +0100 Subject: [PATCH 02/10] Text: test line advance behavior when rendering scaled text. Currently the test fails, because line advance doesn't take text size into account. --- src/Text/Test/RendererGLTest.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Text/Test/RendererGLTest.cpp b/src/Text/Test/RendererGLTest.cpp index 7822b74ab..26be89dcf 100644 --- a/src/Text/Test/RendererGLTest.cpp +++ b/src/Text/Test/RendererGLTest.cpp @@ -264,7 +264,7 @@ void RendererGLTest::mutableText() { void RendererGLTest::multiline() { class Layouter: public Text::AbstractLayouter { public: - explicit Layouter(UnsignedInt glyphs): AbstractLayouter(glyphs) {} + explicit Layouter(UnsignedInt glyphCount): AbstractLayouter(glyphCount) {} private: std::tuple doRenderGlyph(UnsignedInt) override { @@ -284,7 +284,7 @@ void RendererGLTest::multiline() { std::pair doOpenFile(const std::string&, Float) { _opened = true; - return {0, 3.0f}; + return {0.5f, 0.75f}; } UnsignedInt doGlyphId(char32_t) override { return 0; } @@ -303,7 +303,12 @@ void RendererGLTest::multiline() { std::vector indices; std::vector positions, textureCoordinates; std::tie(positions, textureCoordinates, indices, rectangle) = Text::Renderer2D::render(font, - *static_cast(nullptr), 0.0f, "abcd\nef\n\nghi", Alignment::MiddleCenter); + *static_cast(nullptr), 2.0f, "abcd\nef\n\nghi", Alignment::MiddleCenter); + + /* We're rendering text at 2.0f size and the font is scaled to 0.3f, so the + line advance should be 0.75f*2.0f/0.5f = 3.0f */ + CORRADE_COMPARE(font.size(), 0.5f); + CORRADE_COMPARE(font.lineHeight(), 0.75f); /* Bounds */ CORRADE_COMPARE(rectangle, Range2D({-3.5f, -5.0f}, {3.5f, 5.0f})); From 4396506e95d7ec28e476e0ba3c697faddd479eba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 13 Dec 2013 01:48:02 +0100 Subject: [PATCH 03/10] Text: fix rendering of scaled multi-line text. Take text size into account when advancing lines. I would probably not run into this issue if I would visually test this on anything else than unscaled pixel art font. --- src/Text/Renderer.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Text/Renderer.cpp b/src/Text/Renderer.cpp index 605ed274e..36d25aa09 100644 --- a/src/Text/Renderer.cpp +++ b/src/Text/Renderer.cpp @@ -58,16 +58,18 @@ struct Vertex { Vector2 position, textureCoordinates; }; -std::tuple, Range2D> renderVerticesInternal(AbstractFont& font, const GlyphCache& cache, Float size, const std::string& text, const Alignment alignment) { +std::tuple, Range2D> renderVerticesInternal(AbstractFont& font, const GlyphCache& cache, const Float size, const std::string& text, const Alignment alignment) { /* Output data, reserve memory as when the text would be ASCII-only. In reality the actual vertex count will be smaller, but allocating more at once is better than reallocating many times later. */ std::vector vertices; vertices.reserve(text.size()*4); - /* Total rendered bounds, intial line position, last+1 vertex on previous line */ + /* Total rendered bounds, intial line position, line increment, last+1 + vertex on previous line */ Range2D rectangle; Vector2 linePosition; + const Vector2 lineAdvance = Vector2::yAxis(font.lineHeight()*size/font.size()); std::size_t lastLineLastVertex = 0; /* Temp buffer so we don't allocate for each new line */ @@ -146,7 +148,7 @@ std::tuple, Range2D> renderVerticesInternal(AbstractFont& fo /* Move to next line */ } while(prevPos = pos+1, - linePosition -= Vector2::yAxis(font.lineHeight()), + linePosition -= lineAdvance, lastLineLastVertex = vertices.size(), pos != std::string::npos); From 768a8ccc1cf76dec41ab2dbfbe2d53620a029603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 13 Dec 2013 15:26:39 +0100 Subject: [PATCH 04/10] Text: minor cleanup. --- src/Text/Renderer.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Text/Renderer.cpp b/src/Text/Renderer.cpp index 36d25aa09..09f98c970 100644 --- a/src/Text/Renderer.cpp +++ b/src/Text/Renderer.cpp @@ -201,7 +201,6 @@ std::tuple renderInternal(AbstractFont& font, const GlyphCache& c vertexBuffer.setData(vertices, usage); const UnsignedInt glyphCount = vertices.size()/4; - const UnsignedInt vertexCount = glyphCount*4; const UnsignedInt indexCount = glyphCount*6; /* Render indices and upload them */ @@ -215,7 +214,7 @@ std::tuple renderInternal(AbstractFont& font, const GlyphCache& c Mesh mesh; mesh.setPrimitive(MeshPrimitive::Triangles) .setIndexCount(indexCount) - .setIndexBuffer(indexBuffer, 0, indexType, 0, vertexCount); + .setIndexBuffer(indexBuffer, 0, indexType, 0, vertices.size()); return std::make_tuple(std::move(mesh), rectangle); } From e2d2d094b6ba31e855f161148142ab72b04e8d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 13 Dec 2013 15:20:19 +0100 Subject: [PATCH 05/10] Text: harden and clean up Renderer test. --- src/Text/Test/RendererGLTest.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Text/Test/RendererGLTest.cpp b/src/Text/Test/RendererGLTest.cpp index 26be89dcf..f42391635 100644 --- a/src/Text/Test/RendererGLTest.cpp +++ b/src/Text/Test/RendererGLTest.cpp @@ -169,7 +169,8 @@ void RendererGLTest::renderMesh() { Mesh mesh; Buffer vertexBuffer, indexBuffer; Range2D bounds; - std::tie(mesh, bounds) = Text::Renderer3D::render(font, *static_cast(nullptr), 0.25f, "abc", vertexBuffer, indexBuffer, BufferUsage::StaticDraw, Alignment::TopCenter); + std::tie(mesh, bounds) = Text::Renderer3D::render(font, *static_cast(nullptr), + 0.25f, "abc", vertexBuffer, indexBuffer, BufferUsage::StaticDraw, Alignment::TopCenter); MAGNUM_VERIFY_NO_ERROR(); /* Alignment offset */ @@ -181,7 +182,8 @@ void RendererGLTest::renderMesh() { /** @todo How to verify this on ES? */ #ifndef MAGNUM_TARGET_GLES /* Vertex buffer contents */ - Containers::Array vertices = vertexBuffer.data(); + const Containers::Array vertices = vertexBuffer.data(); + CORRADE_COMPARE(vertices.size(), 3*4*(2 + 2)); CORRADE_COMPARE(std::vector(vertices.begin(), vertices.end()), (std::vector{ 0.0f + offset.x(), 0.5f + offset.y(), 0.0f, 10.0f, 0.0f + offset.x(), 0.0f + offset.y(), 0.0f, 0.0f, @@ -199,7 +201,8 @@ void RendererGLTest::renderMesh() { 5.0f + offset.x(), -0.5f + offset.y(), 18.0f, 0.0f })); - Containers::Array indices = indexBuffer.data(); + const Containers::Array indices = indexBuffer.data(); + CORRADE_COMPARE(indices.size(), 3*6); CORRADE_COMPARE(std::vector(indices.begin(), indices.end()), (std::vector{ 0, 1, 2, 1, 3, 2, 4, 5, 6, 5, 7, 6, From 8331696707a5cdee856d82a4e47572256ade6b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 13 Dec 2013 15:20:39 +0100 Subject: [PATCH 06/10] Text: test that proper index type is used in Renderer. Something smells fishy here. --- src/Text/Test/RendererGLTest.cpp | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/Text/Test/RendererGLTest.cpp b/src/Text/Test/RendererGLTest.cpp index f42391635..c3a8b1e9d 100644 --- a/src/Text/Test/RendererGLTest.cpp +++ b/src/Text/Test/RendererGLTest.cpp @@ -34,6 +34,7 @@ class RendererGLTest: public Magnum::Test::AbstractOpenGLTester { void renderData(); void renderMesh(); + void renderMeshIndexType(); void mutableText(); void multiline(); @@ -42,6 +43,7 @@ class RendererGLTest: public Magnum::Test::AbstractOpenGLTester { RendererGLTest::RendererGLTest() { addTests({&RendererGLTest::renderData, &RendererGLTest::renderMesh, + &RendererGLTest::renderMeshIndexType, &RendererGLTest::mutableText, &RendererGLTest::multiline}); @@ -211,6 +213,45 @@ void RendererGLTest::renderMesh() { #endif } +void RendererGLTest::renderMeshIndexType() { + #ifndef MAGNUM_TARGET_GLES + TestFont font; + Mesh mesh; + Buffer vertexBuffer, indexBuffer; + + /* Sizes: four vertices per glyph, each vertex has 2D position and 2D + texture coordinates, each float is four bytes; six indices per glyph. */ + + /* 8-bit indices (exactly 256 vertices) */ + std::tie(mesh, std::ignore) = Text::Renderer3D::render(font, *static_cast(nullptr), + 1.0f, std::string(64, 'a'), vertexBuffer, indexBuffer, BufferUsage::StaticDraw); + MAGNUM_VERIFY_NO_ERROR(); + Containers::Array indicesByte = indexBuffer.data(); + CORRADE_COMPARE(vertexBuffer.size(), 256*(2 + 2)*4); + CORRADE_COMPARE(indicesByte.size(), 64*6); + CORRADE_COMPARE(std::vector(indicesByte.begin(), indicesByte.begin()+18), (std::vector{ + 0, 1, 2, 1, 3, 2, + 4, 5, 6, 5, 7, 6, + 8, 9, 10, 9, 11, 10 + })); + + /* 16-bit indices (260 vertices) */ + std::tie(mesh, std::ignore) = Text::Renderer3D::render(font, *static_cast(nullptr), + 1.0f, std::string(65, 'a'), vertexBuffer, indexBuffer, BufferUsage::StaticDraw); + MAGNUM_VERIFY_NO_ERROR(); + Containers::Array indicesShort = indexBuffer.data(); + CORRADE_COMPARE(vertexBuffer.size(), 260*(2 + 2)*4); + CORRADE_COMPARE(indicesShort.size(), 65*6); + CORRADE_COMPARE(std::vector(indicesShort.begin(), indicesShort.begin()+18), (std::vector{ + 0, 1, 2, 1, 3, 2, + 4, 5, 6, 5, 7, 6, + 8, 9, 10, 9, 11, 10 + })); + #else + CORRADE_SKIP("Can't verify buffer contents on OpenGL ES."); + #endif +} + void RendererGLTest::mutableText() { TestFont font; Text::Renderer2D renderer(font, *static_cast(nullptr), 0.25f); From 6dd40e744d05d13d55e2907660ac4a1716eba551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 13 Dec 2013 15:21:47 +0100 Subject: [PATCH 07/10] Text: it's totally fine to have 256 vertices indexed with UnsignedByte. --- src/Text/Renderer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Text/Renderer.cpp b/src/Text/Renderer.cpp index 09f98c970..9b112e40e 100644 --- a/src/Text/Renderer.cpp +++ b/src/Text/Renderer.cpp @@ -176,11 +176,11 @@ std::pair, Mesh::IndexType> renderIndicesIntern Containers::Array indices; Mesh::IndexType indexType; - if(vertexCount < 255) { + if(vertexCount <= 256) { indexType = Mesh::IndexType::UnsignedByte; indices = Containers::Array(indexCount*sizeof(UnsignedByte)); createIndices(indices, glyphCount); - } else if(vertexCount < 65535) { + } else if(vertexCount <= 65536) { indexType = Mesh::IndexType::UnsignedShort; indices = Containers::Array(indexCount*sizeof(UnsignedShort)); createIndices(indices, glyphCount); From 28de7cb72d60d47bcc6e2f75fca30f101e2d8038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 13 Dec 2013 15:25:31 +0100 Subject: [PATCH 08/10] Text: fix array index type in Renderer. When we have 256 vertices and >256 indices, we need more than 8 bits to index the index array. The tests are passing again. --- src/Text/Renderer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Text/Renderer.cpp b/src/Text/Renderer.cpp index 9b112e40e..0ce12aeba 100644 --- a/src/Text/Renderer.cpp +++ b/src/Text/Renderer.cpp @@ -44,7 +44,7 @@ template void createIndices(void* output, const UnsignedInt glyphCount) 1---3 1 3---4 */ const T vertex = T(i)*4; - const T pos = T(i)*6; + const UnsignedInt pos = T(i)*6; out[pos] = vertex; out[pos+1] = vertex+1; out[pos+2] = vertex+2; From 0670f59c955143f18b242ef9b26c6a21653948ff Mon Sep 17 00:00:00 2001 From: Miguel Martin Date: Sat, 14 Dec 2013 20:12:21 +1030 Subject: [PATCH 09/10] Added colour uniform to flat shader (event when texturing is enabled) This enables blending and transparency Default colour is white (and fully opaque) --- src/Shaders/Flat.cpp | 7 ++++++- src/Shaders/Flat.frag | 16 ++++++++++++---- src/Shaders/Flat.h | 5 +++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Shaders/Flat.cpp b/src/Shaders/Flat.cpp index a6f94d830..d729f85ed 100644 --- a/src/Shaders/Flat.cpp +++ b/src/Shaders/Flat.cpp @@ -78,7 +78,7 @@ template Flat::Flat(const Flags flags): tran #endif { transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); - if(!(flags & Flag::Textured)) colorUniform = uniformLocation("color"); + colorUniform = uniformLocation("color"); } #ifndef MAGNUM_TARGET_GLES @@ -87,6 +87,11 @@ template Flat::Flat(const Flags flags): tran { if(flags & Flag::Textured) setUniform(uniformLocation("textureData"), TextureLayer); } + + /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ + #ifdef MAGNUM_TARGET_GLES + setColor(Color4(1.0f)); // Default to white, with full transperancy (so we can see the texture) + #endif } template class Flat<2>; diff --git a/src/Shaders/Flat.frag b/src/Shaders/Flat.frag index 35e85bfcd..c2ea645cf 100644 --- a/src/Shaders/Flat.frag +++ b/src/Shaders/Flat.frag @@ -34,12 +34,20 @@ layout(binding = 0) uniform sampler2D textureData; #else uniform sampler2D textureData; #endif -#else +#endif + #ifdef EXPLICIT_UNIFORM_LOCATION +# ifndef GL_ES +layout(location = 1) uniform vec4 color = vec4(1.0f, 1.0f, 1.0f, 1.0f); +# else layout(location = 1) uniform vec4 color; +# endif #else -uniform lowp vec4 color; -#endif +# ifndef GL_ES +uniform lowp vec4 color = vec4(1.0f, 1.0f, 1.0f, 1.0f); +# else +unfirom lowp vec4 color; +# endif #endif #ifdef TEXTURED @@ -52,7 +60,7 @@ out lowp vec4 fragmentColor; void main() { #ifdef TEXTURED - fragmentColor = texture(textureData, interpolatedTextureCoordinates); + fragmentColor = color * texture(textureData, interpolatedTextureCoordinates); #else fragmentColor = color; #endif diff --git a/src/Shaders/Flat.h b/src/Shaders/Flat.h index 8e2b69627..955a62f2e 100644 --- a/src/Shaders/Flat.h +++ b/src/Shaders/Flat.h @@ -124,7 +124,8 @@ template class MAGNUM_SHADERS_EXPORT Flat: public Abstra * @brief Set color * @return Reference to self (for method chaining) * - * Has no effect if @ref Flag::Textured is set. + * Color will be multiplied with texture + * if @ref Flag::Textured is set. */ Flat& setColor(const Color4& color); @@ -144,7 +145,7 @@ typedef Flat<3> Flat3D; CORRADE_ENUMSET_OPERATORS(Implementation::FlatFlags) template inline Flat& Flat::setColor(const Color4& color) { - if(!(_flags & Flag::Textured)) setUniform(colorUniform, color); + setUniform(colorUniform, color); return *this; } From 1251d44aecf17dedd8a3a04f5429422d95846029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 14 Dec 2013 15:55:57 +0100 Subject: [PATCH 10/10] Use "" instead of <> for inter-project includes. --- src/OpenGL.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/OpenGL.h b/src/OpenGL.h index 534cc43d2..796f1ad42 100644 --- a/src/OpenGL.h +++ b/src/OpenGL.h @@ -33,7 +33,7 @@ /* Desktop OpenGL */ #ifndef MAGNUM_TARGET_GLES - #include + #include "OpenGL/GL/gl_magnum.h" /* NaCl has its own gl2.h, the official one causes linker issues. Additionaly to NaCl's gl2ext.h we are including our own to prevent undeclared symbol @@ -45,19 +45,19 @@ #include #include #undef __gl2ext_h_ - #include + #include "OpenGL/GLES2/gl2ext.h" /* Generic OpenGL ES */ #else - #include + #include "OpenGL/KHR/khrplatform.h" #ifndef MAGNUM_TARGET_GLES2 - #include - #include + #include "OpenGL/GLES3/gl3platform.h" + #include "OpenGL/GLES3/gl3.h" #else - #include - #include + #include "OpenGL/GLES2/gl2platform.h" + #include "OpenGL/GLES2/gl2.h" #endif - #include + #include "OpenGL/GLES2/gl2ext.h" #endif #endif