This commit is contained in:
viktorljung
2016-01-21 10:16:03 +01:00
parent ce27b224df
commit f3bfe388af
6 changed files with 99 additions and 55 deletions
+1 -1
Submodule assets updated: 6ffb46e155...b6592dbb02
+1 -1
View File
@@ -25,7 +25,7 @@ public:
GLuint Advance; // Offset to advance to next glyph GLuint Advance; // Offset to advance to next glyph
}; };
FT_Face Face;
int FontSize = 16; int FontSize = 16;
+2 -9
View File
@@ -3,7 +3,6 @@
#include <ft2build.h> #include <ft2build.h>
#include FT_FREETYPE_H #include FT_FREETYPE_H
#include FT_GLYPH_H
#include "../OpenGL.h" #include "../OpenGL.h"
#include "../GLM.h" #include "../GLM.h"
@@ -21,17 +20,11 @@ public:
void Draw(RenderScene& scene); void Draw(RenderScene& scene);
private: 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; Font* font;
GLuint VAO, VBO; 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; ShaderProgram* m_TextProgram;
std::string text = "";
int counter = 0;
}; };
+72 -12
View File
@@ -11,14 +11,12 @@
<c:Camera> <c:Camera>
<Name>ActionCamera</Name> <Name>ActionCamera</Name>
</c:Camera> </c:Camera>
<c:Listener/>
<c:Model> <c:Model>
<Resource>Models/Camera.obj</Resource> <Resource>Models/Camera.obj</Resource>
<Visible>false</Visible>
</c:Model> </c:Model>
<c:Transform> <c:Transform>
<Position X="-1.04501271" Y="2.65480638" Z="9.08133221"/> <Position X="-0.504807115" Y="6.00582743" Z="9.68687439"/>
<Orientation X="-0.488691956" Y="-0.261693299" Z="4.35807976e-07"/> <Orientation X="-0.733036518" Y="0.000105090476" Z="5.12391125e-07"/>
</c:Transform> </c:Transform>
</Components> </Components>
<Children> <Children>
@@ -81,7 +79,7 @@
</c:RaptorCopter> </c:RaptorCopter>
<c:Transform> <c:Transform>
<Position X="0" Y="2.37320328" Z="0"/> <Position X="0" Y="2.37320328" Z="0"/>
<Orientation X="0" Y="6858.01416" Z="0"/> <Orientation X="0" Y="8252.15918" Z="0"/>
</c:Transform> </c:Transform>
</Components> </Components>
<Children> <Children>
@@ -96,7 +94,20 @@
<Position X="-2.5" Y="0" Z="0"/> <Position X="-2.5" Y="0" Z="0"/>
</c:Transform> </c:Transform>
</Components> </Components>
<Children/> <Children>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="0" G="0" R="1"/>
<Radius>8</Radius>
</c:PointLight>
<c:Transform>
<Position X="-5" Y="0" Z="0"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity> </Entity>
<Entity> <Entity>
<Components> <Components>
@@ -109,7 +120,20 @@
<Position X="0" Y="0" Z="-2.5"/> <Position X="0" Y="0" Z="-2.5"/>
</c:Transform> </c:Transform>
</Components> </Components>
<Children/> <Children>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="0" G="1" R="0"/>
<Radius>8</Radius>
</c:PointLight>
<c:Transform>
<Position X="0" Y="0" Z="-5"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity> </Entity>
<Entity> <Entity>
<Components> <Components>
@@ -122,7 +146,20 @@
<Position X="0" Y="0" Z="2.5"/> <Position X="0" Y="0" Z="2.5"/>
</c:Transform> </c:Transform>
</Components> </Components>
<Children/> <Children>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="1" G="0.00784313772" R="0"/>
<Radius>8</Radius>
</c:PointLight>
<c:Transform>
<Position X="0" Y="0" Z="5"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity> </Entity>
<Entity> <Entity>
<Components> <Components>
@@ -134,7 +171,19 @@
<Position X="2.5" Y="0" Z="0"/> <Position X="2.5" Y="0" Z="0"/>
</c:Transform> </c:Transform>
</Components> </Components>
<Children/> <Children>
<Entity>
<Components>
<c:PointLight>
<Radius>8</Radius>
</c:PointLight>
<c:Transform>
<Position X="5" Y="0" Z="0"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity> </Entity>
</Children> </Children>
</Entity> </Entity>
@@ -160,8 +209,8 @@
<Visible>false</Visible> <Visible>false</Visible>
</c:Model> </c:Model>
<c:Transform> <c:Transform>
<Position X="-0.144499183" Y="2.85344434" Z="8.00381374"/> <Position X="-1.27354908" Y="5.18422079" Z="10.6223917"/>
<Orientation X="-0.418875456" Y="-8.38464803e-06" Z="4.82539235e-08"/> <Orientation X="-0.436328411" Y="-0.0523675606" Z="1.67869363e-08"/>
</c:Transform> </c:Transform>
</Components> </Components>
<Children> <Children>
@@ -192,7 +241,18 @@
</c:Model> </c:Model>
<c:Transform> <c:Transform>
<Position X="-0.0013256073" Y="1.89540339" Z="6.53467274"/> <Position X="-0.0013256073" Y="1.89540339" Z="6.53467274"/>
<Orientation X="-1.06251872" Y="0.0644905791" Z="-0.639524579"/> <Orientation X="-0.859663308" Y="-0.100310192" Z="-0.404723018"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
</c:Model>
<c:Transform>
<Position X="-5.4082036" Y="0" Z="-1.84950542"/>
</c:Transform> </c:Transform>
</Components> </Components>
<Children/> <Children/>
+17 -12
View File
@@ -15,6 +15,10 @@ Font::Font(std::string path)
filePath = (*it).c_str(); filePath = (*it).c_str();
it++; it++;
if (it != tok.end()) { if (it != tok.end()) {
if((*it).c_str() == "") {
throw std::runtime_error("");
}
try { try {
FontSize = boost::lexical_cast<int>((*it).c_str()); FontSize = boost::lexical_cast<int>((*it).c_str());
} catch (boost::bad_lexical_cast const&) { } catch (boost::bad_lexical_cast const&) {
@@ -28,21 +32,22 @@ Font::Font(std::string path)
FT_Library library; FT_Library library;
FT_Face face;
if (FT_Init_FreeType(&library)) { if (FT_Init_FreeType(&library)) {
LOG_ERROR("FreeType error: init failed"); LOG_ERROR("FreeType error: init failed");
throw std::runtime_error("");; 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"); LOG_ERROR("FreeType error: loading font");
throw std::runtime_error("");; throw std::runtime_error("");;
} }
FT_Set_Char_Size(Face, 0, FontSize*64, 300, 300); // temp FT_Set_Char_Size(face, 0, FontSize*64, 300, 300); // temp
FT_Set_Pixel_Sizes(Face, 0, FontSize); // 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"); LOG_ERROR("FreeType error: loading char");
throw std::runtime_error("");; throw std::runtime_error("");;
} }
@@ -53,7 +58,7 @@ Font::Font(std::string path)
//Load character glyph //Load character glyph
if (FT_Load_Char(Face, c, FT_LOAD_RENDER)) { if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
continue; continue;
} }
@@ -66,12 +71,12 @@ Font::Font(std::string path)
GL_TEXTURE_2D, GL_TEXTURE_2D,
0, 0,
GL_RED, GL_RED,
Face->glyph->bitmap.width, face->glyph->bitmap.width,
Face->glyph->bitmap.rows, face->glyph->bitmap.rows,
0, 0,
GL_RED, GL_RED,
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
Face->glyph->bitmap.buffer face->glyph->bitmap.buffer
); );
// Set texture options // Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 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 // Now store character for later use
Character character = { Character character = {
texture, texture,
glm::ivec2(Face->glyph->bitmap.width, Face->glyph->bitmap.rows), glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(Face->glyph->bitmap_left, Face->glyph->bitmap_top), glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
Face->glyph->advance.x face->glyph->advance.x
}; };
m_Characters.insert(std::pair<GLchar, Character>(c, character)); m_Characters.insert(std::pair<GLchar, Character>(c, character));
} }
FT_Done_Face(face);
FT_Done_FreeType(library); FT_Done_FreeType(library);
GLERROR("Font Load"); GLERROR("Font Load");
} }
Font::~Font() Font::~Font()
{ {
FT_Done_Face(Face);
for (auto c : m_Characters) { for (auto c : m_Characters) {
glDeleteTextures(1, &c.second.TextureID); glDeleteTextures(1, &c.second.TextureID);
} }
+4 -18
View File
@@ -35,25 +35,17 @@ void TextRenderer::Draw(RenderScene& scene)
auto textJob = std::dynamic_pointer_cast<TextJob>(job); auto textJob = std::dynamic_pointer_cast<TextJob>(job);
if (textJob) { 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 penX = 0;
GLfloat penY = 0; GLfloat penY = 0;
float scale = 1.0/font->FontSize; 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; GLfloat stringWidth = 0.f;
for (std::string::const_iterator c = text.begin(); c != text.end(); c++) { for (std::string::const_iterator c = text.begin(); c != text.end(); c++) {
@@ -69,9 +61,6 @@ void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEn
penX = 0; penX = 0;
} }
// Activate corresponding render state
glEnable(GL_BLEND); glEnable(GL_BLEND);
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
@@ -94,7 +83,7 @@ void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEn
GLfloat w = ch.Size.x * scale; GLfloat w = ch.Size.x * scale;
GLfloat h = ch.Size.y * scale; GLfloat h = ch.Size.y * scale;
// Update VBO for each character
GLfloat vertices[6][4] = { GLfloat vertices[6][4] = {
{ xpos, ypos + h, 0.0, 0.0 }, { xpos, ypos + h, 0.0, 0.0 },
{ xpos, ypos, 0.0, 1.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 } { xpos + w, ypos + h, 1.0, 0.0 }
}; };
// Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.TextureID); glBindTexture(GL_TEXTURE_2D, ch.TextureID);
// Update content of VBO memory
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
// Render quad
glDrawArrays(GL_TRIANGLES, 0, 6); 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) penX += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64)
} }
glBindVertexArray(0); glBindVertexArray(0);