From 0d7baef932717db7ec0cbbcec131686cf8b23495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 4 Mar 2013 10:50:00 +0100 Subject: [PATCH] Text: no need to explicitly specify Z coordinate in 3D text. The coordinate is always 0 and Mesh can be configured to add it implicitly. The code is now nearly the same for 2D and 3D, will redo it without templates later. --- src/Text/TextRenderer.cpp | 61 +++++++++++++++------------------------ src/Text/TextRenderer.h | 2 +- 2 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/Text/TextRenderer.cpp b/src/Text/TextRenderer.cpp index 806069b7f..c21983470 100644 --- a/src/Text/TextRenderer.cpp +++ b/src/Text/TextRenderer.cpp @@ -26,7 +26,6 @@ #include "Context.h" #include "Extensions.h" #include "Mesh.h" -#include "Swizzle.h" #include "Shaders/AbstractVectorShader.h" #include "Text/Font.h" @@ -149,31 +148,19 @@ template void createIndices(void* output, const UnsignedInt glyphCount) } } -template typename DimensionTraits::VectorType point(const Vector2& vec); - -template<> inline Vector2 point<2>(const Vector2& vec) { - return vec; -} - -template<> inline Vector3 point<3>(const Vector2& vec) { - return {vec, 1.0f}; -} - -template struct Vertex { - typename DimensionTraits::VectorType position; - Vector2 texcoords; +struct Vertex { + Vector2 position, texcoords; }; } -template std::tuple::VectorType>, std::vector, std::vector, Rectangle> TextRenderer::render(Font& font, Float size, const std::string& text) { +template std::tuple, std::vector, std::vector, Rectangle> TextRenderer::render(Font& font, Float size, const std::string& text) { TextLayouter layouter(font, size, text); const UnsignedInt vertexCount = layouter.glyphCount()*4; /* Output data */ - std::vector::VectorType> positions; - std::vector texcoords; + std::vector positions, texcoords; positions.reserve(vertexCount); texcoords.reserve(vertexCount); @@ -186,10 +173,10 @@ template std::tuple(quadPosition.topLeft()), - point(quadPosition.bottomLeft()), - point(quadPosition.topRight()), - point(quadPosition.bottomRight()), + quadPosition.topLeft(), + quadPosition.bottomLeft(), + quadPosition.topRight(), + quadPosition.bottomRight(), }); texcoords.insert(texcoords.end(), { textureCoordinates.topLeft(), @@ -208,8 +195,7 @@ template std::tuple(positions[1]), swizzle<'x', 'y'>(positions[positions.size()-2])}; + if(layouter.glyphCount()) rectangle = {positions[1], positions[positions.size()-2]}; return std::make_tuple(std::move(positions), std::move(texcoords), std::move(indices), rectangle); } @@ -221,7 +207,7 @@ template std::tuple TextRenderer> vertices; + std::vector vertices; vertices.reserve(vertexCount); /* Render all glyphs */ @@ -233,10 +219,10 @@ template std::tuple TextRenderer(quadPosition.topLeft()), textureCoordinates.topLeft()}, - {point(quadPosition.bottomLeft()), textureCoordinates.bottomLeft()}, - {point(quadPosition.topRight()), textureCoordinates.topRight()}, - {point(quadPosition.bottomRight()), textureCoordinates.bottomRight()} + {quadPosition.topLeft(), textureCoordinates.topLeft()}, + {quadPosition.bottomLeft(), textureCoordinates.bottomLeft()}, + {quadPosition.topRight(), textureCoordinates.topRight()}, + {quadPosition.bottomRight(), textureCoordinates.bottomRight()} }); /* Advance cursor position to next character */ @@ -269,15 +255,14 @@ template std::tuple TextRenderer(vertices[1].position), swizzle<'x', 'y'>(vertices[vertices.size()-2].position)}; + if(layouter.glyphCount()) rectangle = {vertices[1].position, vertices[vertices.size()-2].position}; /* Configure mesh */ Mesh mesh; mesh.setPrimitive(Mesh::Primitive::Triangles) ->setIndexCount(indexCount) ->addInterleavedVertexBuffer(vertexBuffer, 0, - typename Shaders::AbstractVectorShader::Position(), + typename Shaders::AbstractVectorShader::Position(Shaders::AbstractVectorShader::Position::Components::Two), typename Shaders::AbstractVectorShader::TextureCoordinates()) ->setIndexBuffer(indexBuffer, 0, indexType, 0, vertexCount); @@ -295,7 +280,7 @@ template TextRenderer::TextRenderer(Font& fo _mesh.setPrimitive(Mesh::Primitive::Triangles) ->addInterleavedVertexBuffer(&vertexBuffer, 0, - typename Shaders::AbstractVectorShader::Position(), + typename Shaders::AbstractVectorShader::Position(Shaders::AbstractVectorShader::Position::Components::Two), typename Shaders::AbstractVectorShader::TextureCoordinates()); } @@ -306,7 +291,7 @@ template void TextRenderer::reserve(const ui const UnsignedInt indexCount = glyphCount*6; /* Allocate vertex buffer, reset vertex count */ - vertexBuffer.setData(vertexCount*sizeof(Vertex), nullptr, vertexBufferUsage); + vertexBuffer.setData(vertexCount*sizeof(Vertex), nullptr, vertexBufferUsage); _mesh.setVertexCount(0); /* Allocate index buffer, reset index count and reconfigure buffer binding */ @@ -343,7 +328,7 @@ template void TextRenderer::render(const std CORRADE_ASSERT(layouter.glyphCount() <= _capacity, "Text::TextRenderer::render(): capacity" << _capacity << "too small to render" << layouter.glyphCount() << "glyphs", ); /* Render all glyphs */ - Vertex* const vertices = static_cast*>(vertexBuffer.map(0, layouter.glyphCount()*4*sizeof(Vertex), + Vertex* const vertices = static_cast(vertexBuffer.map(0, layouter.glyphCount()*4*sizeof(Vertex), Buffer::MapFlag::InvalidateBuffer|Buffer::MapFlag::Write)); Vector2 cursorPosition; for(UnsignedInt i = 0; i != layouter.glyphCount(); ++i) { @@ -358,10 +343,10 @@ template void TextRenderer::render(const std _rectangle.topRight() = quadPosition.topRight(); const std::size_t vertex = i*4; - vertices[vertex] = {point(quadPosition.topLeft()), textureCoordinates.topLeft()}; - vertices[vertex+1] = {point(quadPosition.bottomLeft()), textureCoordinates.bottomLeft()}; - vertices[vertex+2] = {point(quadPosition.topRight()), textureCoordinates.topRight()}; - vertices[vertex+3] = {point(quadPosition.bottomRight()), textureCoordinates.bottomRight()}; + vertices[vertex] = {quadPosition.topLeft(), textureCoordinates.topLeft()}; + vertices[vertex+1] = {quadPosition.bottomLeft(), textureCoordinates.bottomLeft()}; + vertices[vertex+2] = {quadPosition.topRight(), textureCoordinates.topRight()}; + vertices[vertex+3] = {quadPosition.bottomRight(), textureCoordinates.bottomRight()}; /* Advance cursor position to next character */ cursorPosition += advance; diff --git a/src/Text/TextRenderer.h b/src/Text/TextRenderer.h index 4fdb27f28..33a7362c2 100644 --- a/src/Text/TextRenderer.h +++ b/src/Text/TextRenderer.h @@ -109,7 +109,7 @@ template class MAGNUM_TEXT_EXPORT TextRenderer { * Returns tuple with vertex positions, texture coordinates, indices * and rectangle spanning the rendered text. */ - static std::tuple::VectorType>, std::vector, std::vector, Rectangle> render(Font& font, Float size, const std::string& text); + static std::tuple, std::vector, std::vector, Rectangle> render(Font& font, Float size, const std::string& text); /** * @brief Render text