From 12dd1d21de0f6edd0868f0dd60d6ea40493ecbe3 Mon Sep 17 00:00:00 2001 From: FakeShemp Date: Wed, 9 Mar 2016 12:11:26 +0100 Subject: [PATCH 1/5] Text now handles color, alpha and icon parsing --- include/Engine/Rendering/TextPass.h | 1 + src/Engine/Rendering/TextPass.cpp | 98 +++++++++++++++++++++++++++-- 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/include/Engine/Rendering/TextPass.h b/include/Engine/Rendering/TextPass.h index 8fbb3df0..bb11e5b7 100644 --- a/include/Engine/Rendering/TextPass.h +++ b/include/Engine/Rendering/TextPass.h @@ -23,6 +23,7 @@ public: private: void renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix); + std::string parseColors(std::string text, std::map& colorChanges, glm::vec4 originalColor); Font* font; GLuint VAO, VBO; diff --git a/src/Engine/Rendering/TextPass.cpp b/src/Engine/Rendering/TextPass.cpp index 9583fcfd..71a1c405 100644 --- a/src/Engine/Rendering/TextPass.cpp +++ b/src/Engine/Rendering/TextPass.cpp @@ -16,7 +16,7 @@ void TextPass::Initialize() glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); - + m_TextProgram = ResourceManager::Load("#TextProgram"); m_TextProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/Text.vert.glsl"))); m_TextProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/Text.frag.glsl"))); @@ -46,6 +46,83 @@ void TextPass::Draw(RenderScene& scene, FrameBuffer& frameBuffer) delete state; } +std::string TextPass::parseColors(std::string text, std::map& colorChanges, glm::vec4 originalColor) +{ + std::string parsedString = text; + glm::vec4 newColor = originalColor; + bool colorChange = false; + + for (std::string::const_iterator c = parsedString.begin(); c != parsedString.end(); c++) { + if (*c == char(92)) { // Backlash + if (*(c + 1) == char('C')) { // C for Color + bool hasCorrectFormat = true; + + for (std::string::const_iterator colorC = c + 2; colorC != c + 8; colorC++) { + if (*colorC < '0' || *colorC > 'F') { + hasCorrectFormat = false; + break; + } + } + + if (hasCorrectFormat == true) { + colorChange = true; + + std::array hexToInt = { + std::stoi(std::string(c + 2, c + 4), 0, 16), + std::stoi(std::string(c + 4, c + 6), 0, 16), + std::stoi(std::string(c + 6, c + 8), 0, 16) + }; + + newColor = glm::vec4( + float(hexToInt[0]) / 255.f, + float(hexToInt[1]) / 255.f, + float(hexToInt[2]) / 255.f, + newColor.a); + + parsedString.erase(c, (c + 8)); + } + } + + if (*(c + 1) == char('A')) { // A for Alpha + bool hasCorrectFormat = true; + + for (std::string::const_iterator colorC = c + 2; colorC != c + 4; colorC++) { + if (*colorC < '0' || *colorC > 'F') { + hasCorrectFormat = false; + break; + } + + if (hasCorrectFormat == true) { + colorChange = true; + + int hexToInt = std::stoi(std::string(c + 2, c + 4), 0, 16); + + newColor = glm::vec4( + newColor.r, + newColor.g, + newColor.b, + float(hexToInt) / 255.f); + + parsedString.erase(c, (c + 4)); + } + } + } + + if (*(c + 1) > '0' && *(c + 1) < '9') { // Icons + parsedString.replace(c, (c + 2), 1, (*(c + 1) - 48)); + } + + if (colorChange) { + colorChanges[c - parsedString.begin()] = newColor; + } + } + } + + + + return parsedString; +} + void TextPass::renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix) { GLfloat penX = 0; @@ -54,10 +131,13 @@ void TextPass::renderText(std::string text, Font* font, TextJob::AlignmentEnum a 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; - } + std::map colorChanges; + std::string parsedText = parseColors(text, colorChanges, color); + + for (std::string::const_iterator c = parsedText.begin(); c != parsedText.end(); c++) { + Font::Character ch = font->m_Characters[*c]; + stringWidth += (ch.Advance >> 6) * scale; + } if(alignment == TextJob::AlignmentEnum::Center) { penX = -stringWidth/2.f; @@ -78,7 +158,13 @@ void TextPass::renderText(std::string text, Font* font, TextJob::AlignmentEnum a glBindVertexArray(VAO); - for (std::string::const_iterator c = text.begin(); c != text.end(); c++) { + for (std::string::const_iterator c = parsedText.begin(); c != parsedText.end(); c++) { + + auto it = colorChanges.find(c - parsedText.begin()); + if (it != colorChanges.end()) { + glUniform4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "textColor"), 1, glm::value_ptr(it->second)); + } + Font::Character ch = font->m_Characters[*c]; GLfloat xpos = penX + ch.Bearing.x * scale; From ba6d2b758dfdac3554069d6eea668548fcef5bc7 Mon Sep 17 00:00:00 2001 From: FakeShemp Date: Wed, 9 Mar 2016 12:34:09 +0100 Subject: [PATCH 2/5] Add killstreak icon --- src/Engine/Rendering/TextPass.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engine/Rendering/TextPass.cpp b/src/Engine/Rendering/TextPass.cpp index 71a1c405..a38fe4d1 100644 --- a/src/Engine/Rendering/TextPass.cpp +++ b/src/Engine/Rendering/TextPass.cpp @@ -108,7 +108,7 @@ std::string TextPass::parseColors(std::string text, std::map& co } } - if (*(c + 1) > '0' && *(c + 1) < '9') { // Icons + if (*(c + 1) >= '1' && *(c + 1) <= '9') { // Icons parsedString.replace(c, (c + 2), 1, (*(c + 1) - 48)); } From 2e5dfc609a114ac0eafc235289d92c514af46f15 Mon Sep 17 00:00:00 2001 From: FakeShemp Date: Wed, 9 Mar 2016 19:51:57 +0100 Subject: [PATCH 3/5] support headshot icons --- src/Engine/Rendering/TextPass.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Engine/Rendering/TextPass.cpp b/src/Engine/Rendering/TextPass.cpp index a38fe4d1..705f0ab6 100644 --- a/src/Engine/Rendering/TextPass.cpp +++ b/src/Engine/Rendering/TextPass.cpp @@ -108,8 +108,12 @@ std::string TextPass::parseColors(std::string text, std::map& co } } - if (*(c + 1) >= '1' && *(c + 1) <= '9') { // Icons - parsedString.replace(c, (c + 2), 1, (*(c + 1) - 48)); + if ((*(c + 1) >= '1' && *(c + 1) <= '9') || *(c + 1) <= 'B' || *(c + 1) <= 'E' || *(c + 1) <= 'F') { // Icons + if (*(c + 1) >= '1' && *(c + 1) <= '9') { + parsedString.replace(c, (c + 2), 1, (*(c + 1) - 48)); + } else { + parsedString.replace(c, (c + 2), 1, (*(c + 1) - 55)); + } } if (colorChange) { From 42af029a265241a30d2a3e4e808a03066263e995 Mon Sep 17 00:00:00 2001 From: FakeShemp Date: Wed, 9 Mar 2016 21:25:51 +0100 Subject: [PATCH 4/5] fix baggu --- src/Engine/Rendering/TextPass.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engine/Rendering/TextPass.cpp b/src/Engine/Rendering/TextPass.cpp index 705f0ab6..fb177ed6 100644 --- a/src/Engine/Rendering/TextPass.cpp +++ b/src/Engine/Rendering/TextPass.cpp @@ -108,7 +108,7 @@ std::string TextPass::parseColors(std::string text, std::map& co } } - if ((*(c + 1) >= '1' && *(c + 1) <= '9') || *(c + 1) <= 'B' || *(c + 1) <= 'E' || *(c + 1) <= 'F') { // Icons + if ((*(c + 1) >= '1' && *(c + 1) <= '9') || *(c + 1) == 'B' || *(c + 1) == 'E' || *(c + 1) == 'F') { // Icons if (*(c + 1) >= '1' && *(c + 1) <= '9') { parsedString.replace(c, (c + 2), 1, (*(c + 1) - 48)); } else { From 981dc3467e8be2e21309ed5af81c8683bdcbd235 Mon Sep 17 00:00:00 2001 From: FakeShemp Date: Wed, 9 Mar 2016 22:11:17 +0100 Subject: [PATCH 5/5] Baggu fixed again +Bonus document formatted --- src/Engine/Rendering/TextPass.cpp | 253 ++++++++++++++++-------------- 1 file changed, 131 insertions(+), 122 deletions(-) diff --git a/src/Engine/Rendering/TextPass.cpp b/src/Engine/Rendering/TextPass.cpp index fb177ed6..cb21e01d 100644 --- a/src/Engine/Rendering/TextPass.cpp +++ b/src/Engine/Rendering/TextPass.cpp @@ -7,23 +7,23 @@ TextPass::TextPass() void TextPass::Initialize() { - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - glBindVertexArray(VAO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - - m_TextProgram = ResourceManager::Load("#TextProgram"); - m_TextProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/Text.vert.glsl"))); - m_TextProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/Text.frag.glsl"))); - m_TextProgram->Compile(); - m_TextProgram->BindFragDataLocation(0, "sceneColor"); - m_TextProgram->BindFragDataLocation(1, "bloomColor"); - m_TextProgram->Link(); + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + glBindVertexArray(VAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + m_TextProgram = ResourceManager::Load("#TextProgram"); + m_TextProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/Text.vert.glsl"))); + m_TextProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/Text.frag.glsl"))); + m_TextProgram->Compile(); + m_TextProgram->BindFragDataLocation(0, "sceneColor"); + m_TextProgram->BindFragDataLocation(1, "bloomColor"); + m_TextProgram->Link(); } void TextPass::Update() @@ -33,17 +33,17 @@ void TextPass::Update() void TextPass::Draw(RenderScene& scene, FrameBuffer& frameBuffer) { - GLERROR("Derp1"); - TextPassState* state = new TextPassState(frameBuffer.GetHandle()); - for (auto &job : scene.Jobs.Text) { - auto textJob = std::dynamic_pointer_cast(job); - if (textJob) { + GLERROR("Derp1"); + TextPassState* state = new TextPassState(frameBuffer.GetHandle()); + for (auto &job : scene.Jobs.Text) { + 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()); - } - } - GLERROR("Derp2"); - delete state; + renderText(textJob->Content, textJob->Resource, textJob->Alignment, textJob->Color, textJob->Matrix, scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix()); + } + } + GLERROR("Derp2"); + delete state; } std::string TextPass::parseColors(std::string text, std::map& colorChanges, glm::vec4 originalColor) @@ -54,70 +54,77 @@ std::string TextPass::parseColors(std::string text, std::map& co for (std::string::const_iterator c = parsedString.begin(); c != parsedString.end(); c++) { if (*c == char(92)) { // Backlash - if (*(c + 1) == char('C')) { // C for Color - bool hasCorrectFormat = true; + if ((c + 1) != parsedString.end()) { + if (*(c + 1) == char('C')) { // C for Color + if ((c + 7) != parsedString.end()) { + bool hasCorrectFormat = true; - for (std::string::const_iterator colorC = c + 2; colorC != c + 8; colorC++) { - if (*colorC < '0' || *colorC > 'F') { - hasCorrectFormat = false; - break; + for (std::string::const_iterator colorC = c + 2; colorC != c + 8; colorC++) { + if (*colorC < '0' || *colorC > 'F') { + hasCorrectFormat = false; + break; + } + } + + if (hasCorrectFormat == true) { + colorChange = true; + + std::array hexToInt = { + std::stoi(std::string(c + 2, c + 4), 0, 16), + std::stoi(std::string(c + 4, c + 6), 0, 16), + std::stoi(std::string(c + 6, c + 8), 0, 16) + }; + + newColor = glm::vec4( + float(hexToInt[0]) / 255.f, + float(hexToInt[1]) / 255.f, + float(hexToInt[2]) / 255.f, + newColor.a); + + parsedString.erase(c, (c + 8)); + } } } - if (hasCorrectFormat == true) { - colorChange = true; + if (*(c + 1) == char('A')) { // A for Alpha + if ((c + 3) != parsedString.end()) { + bool hasCorrectFormat = true; - std::array hexToInt = { - std::stoi(std::string(c + 2, c + 4), 0, 16), - std::stoi(std::string(c + 4, c + 6), 0, 16), - std::stoi(std::string(c + 6, c + 8), 0, 16) - }; + for (std::string::const_iterator colorC = c + 2; colorC != c + 4; colorC++) { + if (*colorC < '0' || *colorC > 'F') { + hasCorrectFormat = false; + break; + } + } - newColor = glm::vec4( - float(hexToInt[0]) / 255.f, - float(hexToInt[1]) / 255.f, - float(hexToInt[2]) / 255.f, - newColor.a); + if (hasCorrectFormat == true) { + colorChange = true; - parsedString.erase(c, (c + 8)); - } - } + int hexToInt = std::stoi(std::string(c + 2, c + 4), 0, 16); - if (*(c + 1) == char('A')) { // A for Alpha - bool hasCorrectFormat = true; + newColor = glm::vec4( + newColor.r, + newColor.g, + newColor.b, + float(hexToInt) / 255.f); - for (std::string::const_iterator colorC = c + 2; colorC != c + 4; colorC++) { - if (*colorC < '0' || *colorC > 'F') { - hasCorrectFormat = false; - break; - } - - if (hasCorrectFormat == true) { - colorChange = true; - - int hexToInt = std::stoi(std::string(c + 2, c + 4), 0, 16); - - newColor = glm::vec4( - newColor.r, - newColor.g, - newColor.b, - float(hexToInt) / 255.f); - - parsedString.erase(c, (c + 4)); + parsedString.erase(c, (c + 4)); + } } } - } - if ((*(c + 1) >= '1' && *(c + 1) <= '9') || *(c + 1) == 'B' || *(c + 1) == 'E' || *(c + 1) == 'F') { // Icons - if (*(c + 1) >= '1' && *(c + 1) <= '9') { - parsedString.replace(c, (c + 2), 1, (*(c + 1) - 48)); - } else { - parsedString.replace(c, (c + 2), 1, (*(c + 1) - 55)); + if ((*(c + 1) >= '1' && *(c + 1) <= '9') || *(c + 1) == 'B' || *(c + 1) == 'E' || *(c + 1) == 'F') { // Icons + if (*(c + 1) >= '1' && *(c + 1) <= '9') { + parsedString.replace(c, (c + 2), 1, (*(c + 1) - 48)); + } + else { + parsedString.replace(c, (c + 2), 1, (*(c + 1) - 55)); + } } - } - if (colorChange) { - colorChanges[c - parsedString.begin()] = newColor; + if (colorChange) { + colorChanges[c - parsedString.begin()] = newColor; + } } } } @@ -129,11 +136,11 @@ std::string TextPass::parseColors(std::string text, std::map& co void TextPass::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; - GLfloat scale = 1.0/font->FontSize; + GLfloat penX = 0; + GLfloat penY = 0; + GLfloat scale = 1.0 / font->FontSize; - GLfloat stringWidth = 0.f; + GLfloat stringWidth = 0.f; std::map colorChanges; std::string parsedText = parseColors(text, colorChanges, color); @@ -143,61 +150,63 @@ void TextPass::renderText(std::string text, Font* font, TextJob::AlignmentEnum a stringWidth += (ch.Advance >> 6) * scale; } - if(alignment == TextJob::AlignmentEnum::Center) { - penX = -stringWidth/2.f; - } else if (alignment == TextJob::AlignmentEnum::Right) { - penX = -stringWidth; - } else { - penX = 0; - } - - - - m_TextProgram->Bind(); - glUniform4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "textColor"), 1, glm::value_ptr(color)); - glUniformMatrix4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); - glUniformMatrix4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewMatrix)); - glUniformMatrix4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix)); - glActiveTexture(GL_TEXTURE0); - glBindVertexArray(VAO); + if (alignment == TextJob::AlignmentEnum::Center) { + penX = -stringWidth / 2.f; + } + else if (alignment == TextJob::AlignmentEnum::Right) { + penX = -stringWidth; + } + else { + penX = 0; + } - for (std::string::const_iterator c = parsedText.begin(); c != parsedText.end(); c++) { + + m_TextProgram->Bind(); + glUniform4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "textColor"), 1, glm::value_ptr(color)); + glUniformMatrix4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewMatrix)); + glUniformMatrix4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix)); + glActiveTexture(GL_TEXTURE0); + glBindVertexArray(VAO); + + + for (std::string::const_iterator c = parsedText.begin(); c != parsedText.end(); c++) { auto it = colorChanges.find(c - parsedText.begin()); if (it != colorChanges.end()) { glUniform4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "textColor"), 1, glm::value_ptr(it->second)); } - Font::Character ch = font->m_Characters[*c]; + Font::Character ch = font->m_Characters[*c]; - GLfloat xpos = penX + ch.Bearing.x * scale; - GLfloat ypos = penY - (ch.Size.y - ch.Bearing.y) * scale; + GLfloat xpos = penX + ch.Bearing.x * scale; + GLfloat ypos = penY - (ch.Size.y - ch.Bearing.y) * scale; - GLfloat w = ch.Size.x * scale; - GLfloat h = ch.Size.y * scale; + GLfloat w = ch.Size.x * scale; + GLfloat h = ch.Size.y * scale; - GLfloat vertices[6][4] = { - { xpos, ypos + h, 0.0, 0.0 }, - { xpos, ypos, 0.0, 1.0 }, - { xpos + w, ypos, 1.0, 1.0 }, + GLfloat vertices[6][4] = { + { xpos, ypos + h, 0.0, 0.0 }, + { xpos, ypos, 0.0, 1.0 }, + { xpos + w, ypos, 1.0, 1.0 }, - { xpos, ypos + h, 0.0, 0.0 }, - { xpos + w, ypos, 1.0, 1.0 }, - { xpos + w, ypos + h, 1.0, 0.0 } - }; + { xpos, ypos + h, 0.0, 0.0 }, + { xpos + w, ypos, 1.0, 1.0 }, + { xpos + w, ypos + h, 1.0, 0.0 } + }; - glBindTexture(GL_TEXTURE_2D, ch.TextureID); + glBindTexture(GL_TEXTURE_2D, ch.TextureID); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glDrawArrays(GL_TRIANGLES, 0, 6); - penX += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64) - } - glBindVertexArray(0); - glBindTexture(GL_TEXTURE_2D, 0); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glDrawArrays(GL_TRIANGLES, 0, 6); + penX += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64) + } + glBindVertexArray(0); + glBindTexture(GL_TEXTURE_2D, 0); - GLERROR("Text rendering Error"); + GLERROR("Text rendering Error"); }