Text rendering working ok.
This commit is contained in:
parent
eef93efc29
commit
4849d83fcf
10 changed files with 127 additions and 42 deletions
|
@ -72,23 +72,80 @@ void OpenGlPainter::renderTextLayer(const TextData& textData, DrawingContext* co
|
|||
initializeTextShader();
|
||||
}
|
||||
|
||||
auto first_char = textData.mContent[0];
|
||||
auto iter = mFontTextures.find(first_char);
|
||||
OpenGlFontTexture* working_texture{nullptr};
|
||||
if (iter == mFontTextures.end())
|
||||
for (auto c : textData.mContent)
|
||||
{
|
||||
auto glyph = context->getFontsManager()->getGlyph(first_char);
|
||||
auto new_texture = std::make_unique<OpenGlFontTexture>(nullptr);
|
||||
working_texture = new_texture.get();
|
||||
mFontTextures[first_char] = std::move(new_texture);
|
||||
}
|
||||
else
|
||||
{
|
||||
working_texture = iter->second.get();
|
||||
if (auto iter = mFontTextures.find(c); iter == mFontTextures.end())
|
||||
{
|
||||
auto glyph = context->getFontsManager()->getGlyph(textData.mFont.getFaceName(), textData.mFont.getSize(), c);
|
||||
auto new_texture = std::make_unique<OpenGlFontTexture>(glyph);
|
||||
mFontTextures[c] = std::move(new_texture);
|
||||
}
|
||||
}
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
const auto width = float(context->getSurface()->getWidth());
|
||||
const auto height = float(context->getSurface()->getHeight());
|
||||
|
||||
glm::mat4 projection = glm::ortho(0.0f, width, 0.0f, height);
|
||||
|
||||
unsigned int VAO, VBO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glUseProgram(mTextShaderProgram->getHandle());
|
||||
glUniform3f(glGetUniformLocation(mTextShaderProgram->getHandle(), "textColor"), 0.0, 0.0, 0.0);
|
||||
glUniformMatrix4fv(glGetUniformLocation(mTextShaderProgram->getHandle(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
|
||||
// iterate through all characters
|
||||
float x = textData.mLocation.GetX();
|
||||
const float y = textData.mLocation.GetY();
|
||||
|
||||
for (auto c : textData.mContent)
|
||||
{
|
||||
auto texture = mFontTextures[c].get();
|
||||
|
||||
float xpos = x + texture->getGlyph()->getBearingX();
|
||||
float ypos = y - (texture->getGlyph()->getHeight() - texture->getGlyph()->getBearingY());
|
||||
|
||||
float w = texture->getGlyph()->getWidth();
|
||||
float h = texture->getGlyph()->getHeight();
|
||||
// update VBO for each character
|
||||
float vertices[6][4] = {
|
||||
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||
{ xpos, ypos, 0.0f, 1.0f },
|
||||
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||
|
||||
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||
{ xpos + w, ypos + h, 1.0f, 0.0f }
|
||||
};
|
||||
|
||||
// render glyph texture over quad
|
||||
glBindTexture(GL_TEXTURE_2D, texture->getHandle());
|
||||
// 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)
|
||||
x += (texture->getGlyph()->getAdvanceX() >> 6); // bitshift by 6 to get value in pixels (2^6 = 64)
|
||||
}
|
||||
glBindVertexArray(0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void OpenGlPainter::renderMeshLayer(TriMesh* mesh, DrawingContext* context)
|
||||
|
@ -193,6 +250,8 @@ void OpenGlPainter::paint(DrawingContext* context)
|
|||
|
||||
glViewport(0, 0, width, height);
|
||||
//glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
|
||||
glOrtho(0, width, 0, height, -1.0, 1.0);
|
||||
|
||||
//glScissor(0, 0, width, height);
|
||||
//glMatrixMode(GL_PROJECTION);
|
||||
//glLoadIdentity();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue