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
};
FT_Face Face;
int FontSize = 16;
+3 -10
View File
@@ -2,8 +2,7 @@
#define TextRenderer_h__
#include <ft2build.h>
#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;
};
+72 -12
View File
@@ -11,14 +11,12 @@
<c:Camera>
<Name>ActionCamera</Name>
</c:Camera>
<c:Listener/>
<c:Model>
<Resource>Models/Camera.obj</Resource>
<Visible>false</Visible>
</c:Model>
<c:Transform>
<Position X="-1.04501271" Y="2.65480638" Z="9.08133221"/>
<Orientation X="-0.488691956" Y="-0.261693299" Z="4.35807976e-07"/>
<Position X="-0.504807115" Y="6.00582743" Z="9.68687439"/>
<Orientation X="-0.733036518" Y="0.000105090476" Z="5.12391125e-07"/>
</c:Transform>
</Components>
<Children>
@@ -81,7 +79,7 @@
</c:RaptorCopter>
<c:Transform>
<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>
</Components>
<Children>
@@ -96,7 +94,20 @@
<Position X="-2.5" Y="0" Z="0"/>
</c:Transform>
</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>
<Components>
@@ -109,7 +120,20 @@
<Position X="0" Y="0" Z="-2.5"/>
</c:Transform>
</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>
<Components>
@@ -122,7 +146,20 @@
<Position X="0" Y="0" Z="2.5"/>
</c:Transform>
</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>
<Components>
@@ -134,7 +171,19 @@
<Position X="2.5" Y="0" Z="0"/>
</c:Transform>
</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>
</Children>
</Entity>
@@ -160,8 +209,8 @@
<Visible>false</Visible>
</c:Model>
<c:Transform>
<Position X="-0.144499183" Y="2.85344434" Z="8.00381374"/>
<Orientation X="-0.418875456" Y="-8.38464803e-06" Z="4.82539235e-08"/>
<Position X="-1.27354908" Y="5.18422079" Z="10.6223917"/>
<Orientation X="-0.436328411" Y="-0.0523675606" Z="1.67869363e-08"/>
</c:Transform>
</Components>
<Children>
@@ -192,7 +241,18 @@
</c:Model>
<c:Transform>
<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>
</Components>
<Children/>
+17 -12
View File
@@ -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<int>((*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<GLchar, Character>(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);
}
+5 -19
View File
@@ -35,25 +35,17 @@ void TextRenderer::Draw(RenderScene& scene)
auto textJob = std::dynamic_pointer_cast<TextJob>(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);