Resolution fixed and Centered text

This commit is contained in:
viktorljung
2016-01-19 11:45:34 +01:00
parent c4594524da
commit dbe56280a6
9 changed files with 80 additions and 30 deletions
+1
View File
@@ -27,6 +27,7 @@ public:
FT_Face Face; FT_Face Face;
int FontSize = 16;
~Font(); ~Font();
+1 -1
View File
@@ -25,7 +25,7 @@ private:
GLuint VAO, VBO; GLuint VAO, VBO;
void RenderText(std::string text, Font* font, GLfloat scale, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix); void RenderText(std::string text, Font* font, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix);
ShaderProgram* m_TextProgram; ShaderProgram* m_TextProgram;
+1 -1
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Text xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Text.xsd"> <Text xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Text.xsd">
<Content>Text</Content> <Content>Text</Content>
<Resource>Fonts/DroidSans.ttf</Resource> <Resource></Resource>
<Color R="1" G="1" B="1" A="1"/> <Color R="1" G="1" B="1" A="1"/>
<Visible>true</Visible> <Visible>true</Visible>
</Text> </Text>
+42 -10
View File
@@ -13,28 +13,58 @@
</c:Camera> </c:Camera>
<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="0.624614298" Y="0.345819205" Z="5.40656805"/> <Position X="3.57542324" Y="3.61114573" Z="9.16725922"/>
<Orientation X="0.0523599274" Y="-0.104719244" Z="8.8963592e-09"/> <Orientation X="-0.488691777" Y="0.575939536" Z="-3.55363916e-08"/>
</c:Transform> </c:Transform>
</Components> </Components>
<Children/> <Children>
<Entity>
<Components>
<c:Text>
<Content>Camera 1</Content>
<Resource>Fonts/DroidSans.ttf,24</Resource>
</c:Text>
<c:Transform>
<Position X="0.298732847" Y="0.296712071" Z="-1"/>
<Scale X="0.100000001" Y="0.100000001" Z="0.100000001"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity> </Entity>
<Entity> <Entity>
<Components> <Components>
<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="5" Y="1" Z="5"/> <Position X="7.20140934" Y="2.30552006" Z="8.67392826"/>
<Orientation X="-0.296705663" Y="0.767948866" Z="2.27869563e-07"/>
</c:Transform> </c:Transform>
</Components> </Components>
<Children/> <Children>
<Entity>
<Components>
<c:Text>
<Content>Camera 2</Content>
<Resource>Fonts/DroidSans.ttf,24</Resource>
</c:Text>
<c:Transform>
<Position X="0.286911726" Y="0.310270816" Z="-1"/>
<Scale X="0.100000001" Y="0.100000001" Z="0.100000001"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity> </Entity>
<Entity> <Entity>
<Components> <Components>
@@ -42,7 +72,7 @@
<Resource>Models/Core/UnitPlane.obj</Resource> <Resource>Models/Core/UnitPlane.obj</Resource>
</c:Model> </c:Model>
<c:Transform> <c:Transform>
<Position X="0" Y="-1" Z="0"/> <Position X="0" Y="-1" Z="0.600000024"/>
<Scale X="100" Y="1" Z="100"/> <Scale X="100" Y="1" Z="100"/>
</c:Transform> </c:Transform>
</Components> </Components>
@@ -60,10 +90,12 @@
<Entity> <Entity>
<Components> <Components>
<c:Text> <c:Text>
<Content>asdasdasdasdasd</Content> <Content>Welcome!</Content>
<Resource>Fonts/DroidSans.ttf</Resource> <Resource>Fonts/DroidSans.ttf,64</Resource>
</c:Text> </c:Text>
<c:Transform/> <c:Transform>
<Position X="0" Y="0" Z="0.700000048"/>
</c:Transform>
</Components> </Components>
<Children/> <Children/>
</Entity> </Entity>
+4 -6
View File
@@ -7,7 +7,6 @@ Font::Font(std::string path)
boost::char_separator<char> sep(","); boost::char_separator<char> sep(",");
tokenizer tok(path, sep); tokenizer tok(path, sep);
int fontSize = 16;
tokenizer::iterator it = tok.begin(); tokenizer::iterator it = tok.begin();
std::string filePath = ""; std::string filePath = "";
@@ -17,7 +16,7 @@ Font::Font(std::string path)
it++; it++;
if (it != tok.end()) { if (it != tok.end()) {
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&) {
LOG_ERROR("input string did not have a valid font resolution"); LOG_ERROR("input string did not have a valid font resolution");
} }
@@ -39,8 +38,8 @@ Font::Font(std::string path)
return; return;
} }
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");
@@ -88,14 +87,13 @@ Font::Font(std::string path)
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);
} }
+2 -2
View File
@@ -74,9 +74,9 @@ void PickingPass::Draw(RenderScene& scene)
m_EntityColors[std::make_tuple(pickInfo.Entity, pickInfo.World, pickInfo.Camera)] = glm::ivec2(pickColor[0], pickColor[1]); m_EntityColors[std::make_tuple(pickInfo.Entity, pickInfo.World, pickInfo.Camera)] = glm::ivec2(pickColor[0], pickColor[1]);
if (m_ColorCounter[0] > 255) { if (m_ColorCounter[0] > 255) {
m_ColorCounter[0] = 0; m_ColorCounter[0] = 0;
m_ColorCounter[1]++;; m_ColorCounter[1]++;
} else { } else {
m_ColorCounter[0]++;; m_ColorCounter[0]++;
} }
} }
@@ -8,6 +8,7 @@ PickingPassState::PickingPassState(GLuint frameBuffer)
GLERROR("---3"); GLERROR("---3");
Enable(GL_DEPTH_TEST); Enable(GL_DEPTH_TEST);
Enable(GL_CULL_FACE); Enable(GL_CULL_FACE);
Disable(GL_BLEND);
glm::vec4 clearColor = glm::vec4(0.f); glm::vec4 clearColor = glm::vec4(0.f);
//ClearColor(clearColor); //ClearColor(clearColor);
+1
View File
@@ -117,6 +117,7 @@ void Renderer::Draw(RenderFrame& frame)
m_TextRenderer->Draw(*scene); m_TextRenderer->Draw(*scene);
} }
m_ImGuiRenderPass->Draw(); m_ImGuiRenderPass->Draw();
glfwSwapBuffers(m_Window); glfwSwapBuffers(m_Window);
+27 -10
View File
@@ -34,21 +34,39 @@ void TextRenderer::Draw(RenderScene& scene)
for (auto &job : scene.TextJobs) { for (auto &job : scene.TextJobs) {
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, 0.01f, textJob->Color, textJob->Matrix, scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix()); RenderText(textJob->Content, textJob->Resource, textJob->Color, textJob->Matrix, scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix());
} }
} }
} }
void TextRenderer::RenderText(std::string text, Font* font, GLfloat scale, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix) void TextRenderer::RenderText(std::string text, Font* font, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix)
{ {
GLfloat x = 0; GLfloat penX = 0;
GLfloat y = 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++) {
Font::Character ch = font->m_Characters[*c];
stringWidth += (ch.Advance >> 6) * scale;
}
penX = -stringWidth/2.f;
// Activate corresponding render state // Activate corresponding render state
glEnable(GL_BLEND); glEnable(GL_BLEND);
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_TextProgram->Bind(); m_TextProgram->Bind();
@@ -59,13 +77,12 @@ void TextRenderer::RenderText(std::string text, Font* font, GLfloat scale, glm::
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO); glBindVertexArray(VAO);
// Iterate through all characters
std::string::const_iterator c; for (std::string::const_iterator c = text.begin(); c != text.end(); c++) {
for (c = text.begin(); c != text.end(); c++) {
Font::Character ch = font->m_Characters[*c]; Font::Character ch = font->m_Characters[*c];
GLfloat xpos = x + ch.Bearing.x * scale; GLfloat xpos = penX + ch.Bearing.x * scale;
GLfloat ypos = y - (ch.Size.y - ch.Bearing.y) * scale; GLfloat ypos = penY - (ch.Size.y - ch.Bearing.y) * scale;
GLfloat w = ch.Size.x * scale; GLfloat w = ch.Size.x * scale;
GLfloat h = ch.Size.y * scale; GLfloat h = ch.Size.y * scale;
@@ -89,7 +106,7 @@ void TextRenderer::RenderText(std::string text, Font* font, GLfloat scale, glm::
// Render quad // 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) // Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
x += (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);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);