From f3bfe388af515eb0997c144e07d78c1cfa3b0b19 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Thu, 21 Jan 2016 10:16:03 +0100 Subject: [PATCH] Clean up --- assets | 2 +- include/Engine/Rendering/Font.h | 2 +- include/Engine/Rendering/TextRenderer.h | 13 +-- resources/Schema/Entities/RenderingWorld.xml | 84 +++++++++++++++++--- src/Engine/Rendering/Font.cpp | 29 ++++--- src/Engine/Rendering/TextRenderer.cpp | 24 ++---- 6 files changed, 99 insertions(+), 55 deletions(-) diff --git a/assets b/assets index 6ffb46e1..b6592dbb 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 6ffb46e155c8f013241cd1507098c94900ec2448 +Subproject commit b6592dbb0216bbac00ccea43a0afd0e27d0b185e diff --git a/include/Engine/Rendering/Font.h b/include/Engine/Rendering/Font.h index 5a1a6bdd..89a21eb0 100644 --- a/include/Engine/Rendering/Font.h +++ b/include/Engine/Rendering/Font.h @@ -25,7 +25,7 @@ public: GLuint Advance; // Offset to advance to next glyph }; - FT_Face Face; + int FontSize = 16; diff --git a/include/Engine/Rendering/TextRenderer.h b/include/Engine/Rendering/TextRenderer.h index 8094dda0..cdc1d8c7 100644 --- a/include/Engine/Rendering/TextRenderer.h +++ b/include/Engine/Rendering/TextRenderer.h @@ -2,8 +2,7 @@ #define TextRenderer_h__ #include -#include FT_FREETYPE_H -#include FT_GLYPH_H +#include FT_FREETYPE_H #include "../OpenGL.h" #include "../GLM.h" @@ -21,17 +20,11 @@ public: void Draw(RenderScene& scene); private: + void renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix); + Font* font; - GLuint VAO, VBO; - - void RenderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix); - ShaderProgram* m_TextProgram; - - std::string text = ""; - - int counter = 0; }; diff --git a/resources/Schema/Entities/RenderingWorld.xml b/resources/Schema/Entities/RenderingWorld.xml index 56bbd794..69b18982 100644 --- a/resources/Schema/Entities/RenderingWorld.xml +++ b/resources/Schema/Entities/RenderingWorld.xml @@ -11,14 +11,12 @@ ActionCamera - Models/Camera.obj - false - - + + @@ -81,7 +79,7 @@ - + @@ -96,7 +94,20 @@ - + + + + + + 8 + + + + + + + + @@ -109,7 +120,20 @@ - + + + + + + 8 + + + + + + + + @@ -122,7 +146,20 @@ - + + + + + + 8 + + + + + + + + @@ -134,7 +171,19 @@ - + + + + + 8 + + + + + + + + @@ -160,8 +209,8 @@ false - - + + @@ -192,7 +241,18 @@ - + + + + + + + + + Models/Core/UnitCube.obj + + + diff --git a/src/Engine/Rendering/Font.cpp b/src/Engine/Rendering/Font.cpp index 8af013e8..940694ea 100644 --- a/src/Engine/Rendering/Font.cpp +++ b/src/Engine/Rendering/Font.cpp @@ -15,6 +15,10 @@ Font::Font(std::string path) filePath = (*it).c_str(); it++; if (it != tok.end()) { + if((*it).c_str() == "") { + throw std::runtime_error(""); + } + try { FontSize = boost::lexical_cast((*it).c_str()); } catch (boost::bad_lexical_cast const&) { @@ -28,21 +32,22 @@ Font::Font(std::string path) FT_Library library; + FT_Face face; if (FT_Init_FreeType(&library)) { LOG_ERROR("FreeType error: init failed"); throw std::runtime_error("");; } - if (FT_New_Face(library, filePath.c_str(), 0, &Face)) { + if (FT_New_Face(library, filePath.c_str(), 0, &face)) { LOG_ERROR("FreeType error: loading font"); throw std::runtime_error("");; } - FT_Set_Char_Size(Face, 0, FontSize*64, 300, 300); // temp - FT_Set_Pixel_Sizes(Face, 0, FontSize); // + FT_Set_Char_Size(face, 0, FontSize*64, 300, 300); // temp + FT_Set_Pixel_Sizes(face, 0, FontSize); // - if (FT_Load_Char(Face, 'X', FT_LOAD_RENDER)) { + if (FT_Load_Char(face, 'X', FT_LOAD_RENDER)) { LOG_ERROR("FreeType error: loading char"); throw std::runtime_error("");; } @@ -53,7 +58,7 @@ Font::Font(std::string path) //Load character glyph - if (FT_Load_Char(Face, c, FT_LOAD_RENDER)) { + if (FT_Load_Char(face, c, FT_LOAD_RENDER)) { continue; } @@ -66,12 +71,12 @@ Font::Font(std::string path) GL_TEXTURE_2D, 0, GL_RED, - Face->glyph->bitmap.width, - Face->glyph->bitmap.rows, + face->glyph->bitmap.width, + face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE, - Face->glyph->bitmap.buffer + face->glyph->bitmap.buffer ); // Set texture options glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -81,22 +86,22 @@ Font::Font(std::string path) // Now store character for later use Character character = { texture, - glm::ivec2(Face->glyph->bitmap.width, Face->glyph->bitmap.rows), - glm::ivec2(Face->glyph->bitmap_left, Face->glyph->bitmap_top), - Face->glyph->advance.x + glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows), + glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top), + face->glyph->advance.x }; m_Characters.insert(std::pair(c, character)); } + FT_Done_Face(face); FT_Done_FreeType(library); GLERROR("Font Load"); } Font::~Font() { - FT_Done_Face(Face); for (auto c : m_Characters) { glDeleteTextures(1, &c.second.TextureID); } diff --git a/src/Engine/Rendering/TextRenderer.cpp b/src/Engine/Rendering/TextRenderer.cpp index 0bd58465..a8dfc083 100644 --- a/src/Engine/Rendering/TextRenderer.cpp +++ b/src/Engine/Rendering/TextRenderer.cpp @@ -35,25 +35,17 @@ void TextRenderer::Draw(RenderScene& scene) auto textJob = std::dynamic_pointer_cast(job); if (textJob) { - RenderText(textJob->Content, textJob->Resource, textJob->Alignment, textJob->Color, textJob->Matrix, scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix()); + renderText(textJob->Content, textJob->Resource, textJob->Alignment, textJob->Color, textJob->Matrix, scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix()); } } } -void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix) +void TextRenderer::renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix) { GLfloat penX = 0; GLfloat penY = 0; float scale = 1.0/font->FontSize; - - FT_Bool use_kerning = FT_HAS_KERNING(font->Face); - FT_UInt previous = 0; - FT_UInt num_glyphs = 0; - FT_UInt glyph_index; - - FT_Vector pos[128]; - GLfloat stringWidth = 0.f; for (std::string::const_iterator c = text.begin(); c != text.end(); c++) { @@ -67,10 +59,7 @@ void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEn penX = -stringWidth; } else { penX = 0; - } - - - // Activate corresponding render state + } glEnable(GL_BLEND); glDisable(GL_CULL_FACE); @@ -94,7 +83,7 @@ void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEn GLfloat w = ch.Size.x * scale; GLfloat h = ch.Size.y * scale; - // Update VBO for each character + GLfloat vertices[6][4] = { { xpos, ypos + h, 0.0, 0.0 }, { xpos, ypos, 0.0, 1.0 }, @@ -105,15 +94,12 @@ void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEn { xpos + w, ypos + h, 1.0, 0.0 } }; - // Render glyph texture over quad glBindTexture(GL_TEXTURE_2D, ch.TextureID); - // Update content of VBO memory + glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); glBindBuffer(GL_ARRAY_BUFFER, 0); - // Render quad glDrawArrays(GL_TRIANGLES, 0, 6); - // Now advance cursors for next glyph (note that advance is number of 1/64 pixels) penX += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64) } glBindVertexArray(0);