FreeType will only render for a couple of seconds [closed]
up vote
0
down vote
favorite
So I have Been trying to implement Freetype to my 2D Engine and the text actually renders but only for one second or less and then it dissappears, already made sure that the render function is still been called, have no idea what could be wrong. Also it only renders the text on the Update function, when I try to render on Render it just wont pop up (this is driving me crazy, the functions are being called one after the other and nothing is modified).
Example:
TextRenderer* Text;
int main() {
//OpenGL stuff
Text = new Text(1920, 1080);
Text->Load("fonts/arial.ttf", 24);
while(window.isOpen) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwPollEvents();
Update();
Render();
glfwSwapBuffers(m_window);
}
}
void Update() {
//Works for half a second
Text->RenderText("Hello World", 200, 300, 1.0f);
}
void Render() {
//Just wont work
Text->RenderText("Hello World", 200, 300, 1.0f);
}
TextRenderer.cpp:
TextRenderer::TextRenderer(GLuint width, GLuint height)
{
// Load and configure shader
this->TextShader = Resources::LoadShader("shaders/text.vs", "shaders/text.frag", "text");
this->TextShader.SetMatrix4("projection", glm::ortho(0.0f, static_cast<GLfloat>(width), static_cast<GLfloat>(height), 0.0f), GL_TRUE);
this->TextShader.SetInteger("text", 0);
// Configure VAO/VBO for texture quads
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->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);
}
void TextRenderer::Load(std::string font, GLuint fontSize)
{
// First clear the previously loaded Characters
this->Characters.clear();
// Then initialize and load the FreeType library
FT_Library ft;
if (FT_Init_FreeType(&ft)) // All functions return a value different than 0 whenever an error occurred
std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
// Load font as face
FT_Face face;
if (FT_New_Face(ft, font.c_str(), 0, &face))
std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
// Set size to load glyphs as
FT_Set_Pixel_Sizes(face, 0, fontSize);
// Disable byte-alignment restriction
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Then for the first 128 ASCII characters, pre-load/compile their characters and store them
for (GLubyte c = 0; c < 128; c++) // lol see what I did there
{
// Load character glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
continue;
}
// Generate texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// 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
};
Characters.insert(std::pair<GLchar, Character>(c, character));
}
glBindTexture(GL_TEXTURE_2D, 0);
// Destroy FreeType once we're finished
FT_Done_Face(face);
FT_Done_FreeType(ft);
}
void TextRenderer::RenderText(std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color)
{
// Activate corresponding render state
this->TextShader.Use();
this->TextShader.SetVector3f("textColor", color);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(this->VAO);
// Iterate through all characters
std::string::const_iterator c;
for (c = text.begin(); c != text.end(); c++)
{
Character ch = Characters[*c];
GLfloat xpos = x + ch.Bearing.x * scale;
GLfloat ypos = y + (this->Characters['H'].Bearing.y - ch.Bearing.y) * scale;
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, 1.0 },
{ xpos + w, ypos, 1.0, 0.0 },
{ xpos, ypos, 0.0, 0.0 },
{ xpos, ypos + h, 0.0, 1.0 },
{ xpos + w, ypos + h, 1.0, 1.0 },
{ xpos + w, ypos, 1.0, 0.0 }
};
// Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
// Update content of VBO memory
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Render quad
glDrawArrays(GL_TRIANGLES, 0, 6);
// Now advance cursors for next glyph
x += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (1/64th times 2^6 = 64)
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
c++ opengl glsl freetype
closed as off-topic by Some programmer dude, Rabbid76, Gerhard Barnard, genpfault, Baum mit Augen Nov 25 at 21:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Some programmer dude, Rabbid76, Gerhard Barnard, genpfault, Baum mit Augen
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 10 more comments
up vote
0
down vote
favorite
So I have Been trying to implement Freetype to my 2D Engine and the text actually renders but only for one second or less and then it dissappears, already made sure that the render function is still been called, have no idea what could be wrong. Also it only renders the text on the Update function, when I try to render on Render it just wont pop up (this is driving me crazy, the functions are being called one after the other and nothing is modified).
Example:
TextRenderer* Text;
int main() {
//OpenGL stuff
Text = new Text(1920, 1080);
Text->Load("fonts/arial.ttf", 24);
while(window.isOpen) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwPollEvents();
Update();
Render();
glfwSwapBuffers(m_window);
}
}
void Update() {
//Works for half a second
Text->RenderText("Hello World", 200, 300, 1.0f);
}
void Render() {
//Just wont work
Text->RenderText("Hello World", 200, 300, 1.0f);
}
TextRenderer.cpp:
TextRenderer::TextRenderer(GLuint width, GLuint height)
{
// Load and configure shader
this->TextShader = Resources::LoadShader("shaders/text.vs", "shaders/text.frag", "text");
this->TextShader.SetMatrix4("projection", glm::ortho(0.0f, static_cast<GLfloat>(width), static_cast<GLfloat>(height), 0.0f), GL_TRUE);
this->TextShader.SetInteger("text", 0);
// Configure VAO/VBO for texture quads
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->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);
}
void TextRenderer::Load(std::string font, GLuint fontSize)
{
// First clear the previously loaded Characters
this->Characters.clear();
// Then initialize and load the FreeType library
FT_Library ft;
if (FT_Init_FreeType(&ft)) // All functions return a value different than 0 whenever an error occurred
std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
// Load font as face
FT_Face face;
if (FT_New_Face(ft, font.c_str(), 0, &face))
std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
// Set size to load glyphs as
FT_Set_Pixel_Sizes(face, 0, fontSize);
// Disable byte-alignment restriction
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Then for the first 128 ASCII characters, pre-load/compile their characters and store them
for (GLubyte c = 0; c < 128; c++) // lol see what I did there
{
// Load character glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
continue;
}
// Generate texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// 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
};
Characters.insert(std::pair<GLchar, Character>(c, character));
}
glBindTexture(GL_TEXTURE_2D, 0);
// Destroy FreeType once we're finished
FT_Done_Face(face);
FT_Done_FreeType(ft);
}
void TextRenderer::RenderText(std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color)
{
// Activate corresponding render state
this->TextShader.Use();
this->TextShader.SetVector3f("textColor", color);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(this->VAO);
// Iterate through all characters
std::string::const_iterator c;
for (c = text.begin(); c != text.end(); c++)
{
Character ch = Characters[*c];
GLfloat xpos = x + ch.Bearing.x * scale;
GLfloat ypos = y + (this->Characters['H'].Bearing.y - ch.Bearing.y) * scale;
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, 1.0 },
{ xpos + w, ypos, 1.0, 0.0 },
{ xpos, ypos, 0.0, 0.0 },
{ xpos, ypos + h, 0.0, 1.0 },
{ xpos + w, ypos + h, 1.0, 1.0 },
{ xpos + w, ypos, 1.0, 0.0 }
};
// Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
// Update content of VBO memory
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Render quad
glDrawArrays(GL_TRIANGLES, 0, 6);
// Now advance cursors for next glyph
x += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (1/64th times 2^6 = 64)
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
c++ opengl glsl freetype
closed as off-topic by Some programmer dude, Rabbid76, Gerhard Barnard, genpfault, Baum mit Augen Nov 25 at 21:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Some programmer dude, Rabbid76, Gerhard Barnard, genpfault, Baum mit Augen
If this question can be reworded to fit the rules in the help center, please edit the question.
2
That sounds like your erasing and not redrawing the text. So probably not an error in the text-rendering (since it do render your text even if it only shows for a short time).
– Some programmer dude
Nov 15 at 1:34
@Someprogrammerdude That sounds probable, where should I look then?
– Aether
Nov 15 at 1:36
1
The code that callsTextRenderer::RenderText
could be a good start.
– Some programmer dude
Nov 15 at 1:37
1
In your main render function, do you erase everything before drawing again? Will theTextRenderer
objects you want to draw always be valid in that code? There could quite literally be thousands of reasons of this happening, but since you haven't provided us with a Minimal, Complete, and Verifiable Example it's really impossible to tell you anything more.
– Some programmer dude
Nov 15 at 1:50
1
@AdaRaider Thank you so much, please if you have any questions about the engine, just ask!
– Aether
Nov 15 at 3:42
|
show 10 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So I have Been trying to implement Freetype to my 2D Engine and the text actually renders but only for one second or less and then it dissappears, already made sure that the render function is still been called, have no idea what could be wrong. Also it only renders the text on the Update function, when I try to render on Render it just wont pop up (this is driving me crazy, the functions are being called one after the other and nothing is modified).
Example:
TextRenderer* Text;
int main() {
//OpenGL stuff
Text = new Text(1920, 1080);
Text->Load("fonts/arial.ttf", 24);
while(window.isOpen) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwPollEvents();
Update();
Render();
glfwSwapBuffers(m_window);
}
}
void Update() {
//Works for half a second
Text->RenderText("Hello World", 200, 300, 1.0f);
}
void Render() {
//Just wont work
Text->RenderText("Hello World", 200, 300, 1.0f);
}
TextRenderer.cpp:
TextRenderer::TextRenderer(GLuint width, GLuint height)
{
// Load and configure shader
this->TextShader = Resources::LoadShader("shaders/text.vs", "shaders/text.frag", "text");
this->TextShader.SetMatrix4("projection", glm::ortho(0.0f, static_cast<GLfloat>(width), static_cast<GLfloat>(height), 0.0f), GL_TRUE);
this->TextShader.SetInteger("text", 0);
// Configure VAO/VBO for texture quads
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->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);
}
void TextRenderer::Load(std::string font, GLuint fontSize)
{
// First clear the previously loaded Characters
this->Characters.clear();
// Then initialize and load the FreeType library
FT_Library ft;
if (FT_Init_FreeType(&ft)) // All functions return a value different than 0 whenever an error occurred
std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
// Load font as face
FT_Face face;
if (FT_New_Face(ft, font.c_str(), 0, &face))
std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
// Set size to load glyphs as
FT_Set_Pixel_Sizes(face, 0, fontSize);
// Disable byte-alignment restriction
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Then for the first 128 ASCII characters, pre-load/compile their characters and store them
for (GLubyte c = 0; c < 128; c++) // lol see what I did there
{
// Load character glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
continue;
}
// Generate texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// 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
};
Characters.insert(std::pair<GLchar, Character>(c, character));
}
glBindTexture(GL_TEXTURE_2D, 0);
// Destroy FreeType once we're finished
FT_Done_Face(face);
FT_Done_FreeType(ft);
}
void TextRenderer::RenderText(std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color)
{
// Activate corresponding render state
this->TextShader.Use();
this->TextShader.SetVector3f("textColor", color);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(this->VAO);
// Iterate through all characters
std::string::const_iterator c;
for (c = text.begin(); c != text.end(); c++)
{
Character ch = Characters[*c];
GLfloat xpos = x + ch.Bearing.x * scale;
GLfloat ypos = y + (this->Characters['H'].Bearing.y - ch.Bearing.y) * scale;
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, 1.0 },
{ xpos + w, ypos, 1.0, 0.0 },
{ xpos, ypos, 0.0, 0.0 },
{ xpos, ypos + h, 0.0, 1.0 },
{ xpos + w, ypos + h, 1.0, 1.0 },
{ xpos + w, ypos, 1.0, 0.0 }
};
// Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
// Update content of VBO memory
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Render quad
glDrawArrays(GL_TRIANGLES, 0, 6);
// Now advance cursors for next glyph
x += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (1/64th times 2^6 = 64)
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
c++ opengl glsl freetype
So I have Been trying to implement Freetype to my 2D Engine and the text actually renders but only for one second or less and then it dissappears, already made sure that the render function is still been called, have no idea what could be wrong. Also it only renders the text on the Update function, when I try to render on Render it just wont pop up (this is driving me crazy, the functions are being called one after the other and nothing is modified).
Example:
TextRenderer* Text;
int main() {
//OpenGL stuff
Text = new Text(1920, 1080);
Text->Load("fonts/arial.ttf", 24);
while(window.isOpen) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwPollEvents();
Update();
Render();
glfwSwapBuffers(m_window);
}
}
void Update() {
//Works for half a second
Text->RenderText("Hello World", 200, 300, 1.0f);
}
void Render() {
//Just wont work
Text->RenderText("Hello World", 200, 300, 1.0f);
}
TextRenderer.cpp:
TextRenderer::TextRenderer(GLuint width, GLuint height)
{
// Load and configure shader
this->TextShader = Resources::LoadShader("shaders/text.vs", "shaders/text.frag", "text");
this->TextShader.SetMatrix4("projection", glm::ortho(0.0f, static_cast<GLfloat>(width), static_cast<GLfloat>(height), 0.0f), GL_TRUE);
this->TextShader.SetInteger("text", 0);
// Configure VAO/VBO for texture quads
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->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);
}
void TextRenderer::Load(std::string font, GLuint fontSize)
{
// First clear the previously loaded Characters
this->Characters.clear();
// Then initialize and load the FreeType library
FT_Library ft;
if (FT_Init_FreeType(&ft)) // All functions return a value different than 0 whenever an error occurred
std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
// Load font as face
FT_Face face;
if (FT_New_Face(ft, font.c_str(), 0, &face))
std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
// Set size to load glyphs as
FT_Set_Pixel_Sizes(face, 0, fontSize);
// Disable byte-alignment restriction
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Then for the first 128 ASCII characters, pre-load/compile their characters and store them
for (GLubyte c = 0; c < 128; c++) // lol see what I did there
{
// Load character glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
continue;
}
// Generate texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// 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
};
Characters.insert(std::pair<GLchar, Character>(c, character));
}
glBindTexture(GL_TEXTURE_2D, 0);
// Destroy FreeType once we're finished
FT_Done_Face(face);
FT_Done_FreeType(ft);
}
void TextRenderer::RenderText(std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color)
{
// Activate corresponding render state
this->TextShader.Use();
this->TextShader.SetVector3f("textColor", color);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(this->VAO);
// Iterate through all characters
std::string::const_iterator c;
for (c = text.begin(); c != text.end(); c++)
{
Character ch = Characters[*c];
GLfloat xpos = x + ch.Bearing.x * scale;
GLfloat ypos = y + (this->Characters['H'].Bearing.y - ch.Bearing.y) * scale;
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, 1.0 },
{ xpos + w, ypos, 1.0, 0.0 },
{ xpos, ypos, 0.0, 0.0 },
{ xpos, ypos + h, 0.0, 1.0 },
{ xpos + w, ypos + h, 1.0, 1.0 },
{ xpos + w, ypos, 1.0, 0.0 }
};
// Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
// Update content of VBO memory
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Render quad
glDrawArrays(GL_TRIANGLES, 0, 6);
// Now advance cursors for next glyph
x += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (1/64th times 2^6 = 64)
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
c++ opengl glsl freetype
c++ opengl glsl freetype
edited Nov 15 at 1:58
asked Nov 15 at 1:27
Aether
295
295
closed as off-topic by Some programmer dude, Rabbid76, Gerhard Barnard, genpfault, Baum mit Augen Nov 25 at 21:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Some programmer dude, Rabbid76, Gerhard Barnard, genpfault, Baum mit Augen
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Some programmer dude, Rabbid76, Gerhard Barnard, genpfault, Baum mit Augen Nov 25 at 21:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Some programmer dude, Rabbid76, Gerhard Barnard, genpfault, Baum mit Augen
If this question can be reworded to fit the rules in the help center, please edit the question.
2
That sounds like your erasing and not redrawing the text. So probably not an error in the text-rendering (since it do render your text even if it only shows for a short time).
– Some programmer dude
Nov 15 at 1:34
@Someprogrammerdude That sounds probable, where should I look then?
– Aether
Nov 15 at 1:36
1
The code that callsTextRenderer::RenderText
could be a good start.
– Some programmer dude
Nov 15 at 1:37
1
In your main render function, do you erase everything before drawing again? Will theTextRenderer
objects you want to draw always be valid in that code? There could quite literally be thousands of reasons of this happening, but since you haven't provided us with a Minimal, Complete, and Verifiable Example it's really impossible to tell you anything more.
– Some programmer dude
Nov 15 at 1:50
1
@AdaRaider Thank you so much, please if you have any questions about the engine, just ask!
– Aether
Nov 15 at 3:42
|
show 10 more comments
2
That sounds like your erasing and not redrawing the text. So probably not an error in the text-rendering (since it do render your text even if it only shows for a short time).
– Some programmer dude
Nov 15 at 1:34
@Someprogrammerdude That sounds probable, where should I look then?
– Aether
Nov 15 at 1:36
1
The code that callsTextRenderer::RenderText
could be a good start.
– Some programmer dude
Nov 15 at 1:37
1
In your main render function, do you erase everything before drawing again? Will theTextRenderer
objects you want to draw always be valid in that code? There could quite literally be thousands of reasons of this happening, but since you haven't provided us with a Minimal, Complete, and Verifiable Example it's really impossible to tell you anything more.
– Some programmer dude
Nov 15 at 1:50
1
@AdaRaider Thank you so much, please if you have any questions about the engine, just ask!
– Aether
Nov 15 at 3:42
2
2
That sounds like your erasing and not redrawing the text. So probably not an error in the text-rendering (since it do render your text even if it only shows for a short time).
– Some programmer dude
Nov 15 at 1:34
That sounds like your erasing and not redrawing the text. So probably not an error in the text-rendering (since it do render your text even if it only shows for a short time).
– Some programmer dude
Nov 15 at 1:34
@Someprogrammerdude That sounds probable, where should I look then?
– Aether
Nov 15 at 1:36
@Someprogrammerdude That sounds probable, where should I look then?
– Aether
Nov 15 at 1:36
1
1
The code that calls
TextRenderer::RenderText
could be a good start.– Some programmer dude
Nov 15 at 1:37
The code that calls
TextRenderer::RenderText
could be a good start.– Some programmer dude
Nov 15 at 1:37
1
1
In your main render function, do you erase everything before drawing again? Will the
TextRenderer
objects you want to draw always be valid in that code? There could quite literally be thousands of reasons of this happening, but since you haven't provided us with a Minimal, Complete, and Verifiable Example it's really impossible to tell you anything more.– Some programmer dude
Nov 15 at 1:50
In your main render function, do you erase everything before drawing again? Will the
TextRenderer
objects you want to draw always be valid in that code? There could quite literally be thousands of reasons of this happening, but since you haven't provided us with a Minimal, Complete, and Verifiable Example it's really impossible to tell you anything more.– Some programmer dude
Nov 15 at 1:50
1
1
@AdaRaider Thank you so much, please if you have any questions about the engine, just ask!
– Aether
Nov 15 at 3:42
@AdaRaider Thank you so much, please if you have any questions about the engine, just ask!
– Aether
Nov 15 at 3:42
|
show 10 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
@Aether I got your engine runnning and while I don't yet have the answer as to why this is occuring (haven't had a thorough look yet) I can tell you that the issue does not occur if you don't update the camera. My gut feeling on this is that you have moved the camera or the text so that it is no longer in view but I'm hoping to add more to this when I can.
void Sandbox::Init() {
AETHER_CLIENT_TRACE("Initialized LifeSandbox");
GameManager::GameSpeed = 1;
Resources::LoadTexture("textures/default.png", "default");
Resources::LoadTexture("textures/emoji.png", "emoji");
Resources::LoadShader("shaders/spriteVertexShader.bin", "shaders/spriteFragmentShader.bin", "sprite");
Renderer = new Renderer2D(Resources::GetShader("sprite"));
Text->Load("fonts/arial.TTF", 24);
MainCamera = new Camera();
}
void Sandbox::Update()
{
//MainCamera->Update(iVec2(0.0f, 0.0f));
}
void Sandbox::Render()
{
//Works fine
Text->RenderText("Press ENTER to start", 0, 0, 1.0, iVec3(2.0f));
}
Been looking to the Camera code, think it has to do with the view2D uniform, the text shader doesnt't have that uniform and is really only that what gets modified when the Update is called. I will try adding that Uniform to the Shader and will Update with the results.
– Aether
Nov 15 at 13:55
Awesome, I didn't get a chance to dig any deeper but good luck!
– AdaRaider
Nov 15 at 14:54
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
@Aether I got your engine runnning and while I don't yet have the answer as to why this is occuring (haven't had a thorough look yet) I can tell you that the issue does not occur if you don't update the camera. My gut feeling on this is that you have moved the camera or the text so that it is no longer in view but I'm hoping to add more to this when I can.
void Sandbox::Init() {
AETHER_CLIENT_TRACE("Initialized LifeSandbox");
GameManager::GameSpeed = 1;
Resources::LoadTexture("textures/default.png", "default");
Resources::LoadTexture("textures/emoji.png", "emoji");
Resources::LoadShader("shaders/spriteVertexShader.bin", "shaders/spriteFragmentShader.bin", "sprite");
Renderer = new Renderer2D(Resources::GetShader("sprite"));
Text->Load("fonts/arial.TTF", 24);
MainCamera = new Camera();
}
void Sandbox::Update()
{
//MainCamera->Update(iVec2(0.0f, 0.0f));
}
void Sandbox::Render()
{
//Works fine
Text->RenderText("Press ENTER to start", 0, 0, 1.0, iVec3(2.0f));
}
Been looking to the Camera code, think it has to do with the view2D uniform, the text shader doesnt't have that uniform and is really only that what gets modified when the Update is called. I will try adding that Uniform to the Shader and will Update with the results.
– Aether
Nov 15 at 13:55
Awesome, I didn't get a chance to dig any deeper but good luck!
– AdaRaider
Nov 15 at 14:54
add a comment |
up vote
1
down vote
accepted
@Aether I got your engine runnning and while I don't yet have the answer as to why this is occuring (haven't had a thorough look yet) I can tell you that the issue does not occur if you don't update the camera. My gut feeling on this is that you have moved the camera or the text so that it is no longer in view but I'm hoping to add more to this when I can.
void Sandbox::Init() {
AETHER_CLIENT_TRACE("Initialized LifeSandbox");
GameManager::GameSpeed = 1;
Resources::LoadTexture("textures/default.png", "default");
Resources::LoadTexture("textures/emoji.png", "emoji");
Resources::LoadShader("shaders/spriteVertexShader.bin", "shaders/spriteFragmentShader.bin", "sprite");
Renderer = new Renderer2D(Resources::GetShader("sprite"));
Text->Load("fonts/arial.TTF", 24);
MainCamera = new Camera();
}
void Sandbox::Update()
{
//MainCamera->Update(iVec2(0.0f, 0.0f));
}
void Sandbox::Render()
{
//Works fine
Text->RenderText("Press ENTER to start", 0, 0, 1.0, iVec3(2.0f));
}
Been looking to the Camera code, think it has to do with the view2D uniform, the text shader doesnt't have that uniform and is really only that what gets modified when the Update is called. I will try adding that Uniform to the Shader and will Update with the results.
– Aether
Nov 15 at 13:55
Awesome, I didn't get a chance to dig any deeper but good luck!
– AdaRaider
Nov 15 at 14:54
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
@Aether I got your engine runnning and while I don't yet have the answer as to why this is occuring (haven't had a thorough look yet) I can tell you that the issue does not occur if you don't update the camera. My gut feeling on this is that you have moved the camera or the text so that it is no longer in view but I'm hoping to add more to this when I can.
void Sandbox::Init() {
AETHER_CLIENT_TRACE("Initialized LifeSandbox");
GameManager::GameSpeed = 1;
Resources::LoadTexture("textures/default.png", "default");
Resources::LoadTexture("textures/emoji.png", "emoji");
Resources::LoadShader("shaders/spriteVertexShader.bin", "shaders/spriteFragmentShader.bin", "sprite");
Renderer = new Renderer2D(Resources::GetShader("sprite"));
Text->Load("fonts/arial.TTF", 24);
MainCamera = new Camera();
}
void Sandbox::Update()
{
//MainCamera->Update(iVec2(0.0f, 0.0f));
}
void Sandbox::Render()
{
//Works fine
Text->RenderText("Press ENTER to start", 0, 0, 1.0, iVec3(2.0f));
}
@Aether I got your engine runnning and while I don't yet have the answer as to why this is occuring (haven't had a thorough look yet) I can tell you that the issue does not occur if you don't update the camera. My gut feeling on this is that you have moved the camera or the text so that it is no longer in view but I'm hoping to add more to this when I can.
void Sandbox::Init() {
AETHER_CLIENT_TRACE("Initialized LifeSandbox");
GameManager::GameSpeed = 1;
Resources::LoadTexture("textures/default.png", "default");
Resources::LoadTexture("textures/emoji.png", "emoji");
Resources::LoadShader("shaders/spriteVertexShader.bin", "shaders/spriteFragmentShader.bin", "sprite");
Renderer = new Renderer2D(Resources::GetShader("sprite"));
Text->Load("fonts/arial.TTF", 24);
MainCamera = new Camera();
}
void Sandbox::Update()
{
//MainCamera->Update(iVec2(0.0f, 0.0f));
}
void Sandbox::Render()
{
//Works fine
Text->RenderText("Press ENTER to start", 0, 0, 1.0, iVec3(2.0f));
}
edited Nov 15 at 7:06
answered Nov 15 at 6:47
AdaRaider
3989
3989
Been looking to the Camera code, think it has to do with the view2D uniform, the text shader doesnt't have that uniform and is really only that what gets modified when the Update is called. I will try adding that Uniform to the Shader and will Update with the results.
– Aether
Nov 15 at 13:55
Awesome, I didn't get a chance to dig any deeper but good luck!
– AdaRaider
Nov 15 at 14:54
add a comment |
Been looking to the Camera code, think it has to do with the view2D uniform, the text shader doesnt't have that uniform and is really only that what gets modified when the Update is called. I will try adding that Uniform to the Shader and will Update with the results.
– Aether
Nov 15 at 13:55
Awesome, I didn't get a chance to dig any deeper but good luck!
– AdaRaider
Nov 15 at 14:54
Been looking to the Camera code, think it has to do with the view2D uniform, the text shader doesnt't have that uniform and is really only that what gets modified when the Update is called. I will try adding that Uniform to the Shader and will Update with the results.
– Aether
Nov 15 at 13:55
Been looking to the Camera code, think it has to do with the view2D uniform, the text shader doesnt't have that uniform and is really only that what gets modified when the Update is called. I will try adding that Uniform to the Shader and will Update with the results.
– Aether
Nov 15 at 13:55
Awesome, I didn't get a chance to dig any deeper but good luck!
– AdaRaider
Nov 15 at 14:54
Awesome, I didn't get a chance to dig any deeper but good luck!
– AdaRaider
Nov 15 at 14:54
add a comment |
2
That sounds like your erasing and not redrawing the text. So probably not an error in the text-rendering (since it do render your text even if it only shows for a short time).
– Some programmer dude
Nov 15 at 1:34
@Someprogrammerdude That sounds probable, where should I look then?
– Aether
Nov 15 at 1:36
1
The code that calls
TextRenderer::RenderText
could be a good start.– Some programmer dude
Nov 15 at 1:37
1
In your main render function, do you erase everything before drawing again? Will the
TextRenderer
objects you want to draw always be valid in that code? There could quite literally be thousands of reasons of this happening, but since you haven't provided us with a Minimal, Complete, and Verifiable Example it's really impossible to tell you anything more.– Some programmer dude
Nov 15 at 1:50
1
@AdaRaider Thank you so much, please if you have any questions about the engine, just ask!
– Aether
Nov 15 at 3:42