PDA

View Full Version : Strange texture behavior when rendering a glyph with FreeType.



Leo2018
02-23-2018, 05:35 AM
I'm attempting to render a glyph with OpenGL and FreeType.

OpenGL part of the program


int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ;
int w, h;
glfwGetFramebufferSize(window, &w, &h);
glViewport(0, 0, w, h);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

GLuint shaderProg = loadShader();
glUseProgram(shaderProg);
GLuint uniform_tex = glGetUniformLocation(shaderProg, "tex");
GLuint uniform_color = glGetUniformLocation(shaderProg, "color");
GLfloat color[4] = { 1, 1, 1, 1 };
glUniform4fv(uniform_color, 1, color);

FT_Library library;
FT_Face face;
FT_Init_FreeType(&library);
FT_New_Face(library, "C:\\Windows\\Fonts\\Arial.ttf", 0, &face);
FT_Set_Pixel_Sizes(face, 0, 64); // rendering depends on what value I pass to this function
FT_Load_Char(face, 117, FT_LOAD_RENDER);

GLuint tex;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(uniform_tex, 0);
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);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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);

float vertices[] = {
0, 1, 0, 0,
0, 0, 0, 1,
1, 1, 1, 0,
1, 0, 1, 1
};
GLuint vbo, vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);

while (!glfwWindowShouldClose(window))
{
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}

Vertex shader

#version 330 core

layout (location = 0) in vec4 coord;
out vec2 texpos;

void main(void) {
texpos = coord.zw;
gl_Position = vec4(coord.xy, 0, 1);
}

Fragment shader

#version 330 core

in vec2 texpos;
uniform sampler2D tex;
uniform vec4 color;

out vec4 fragColor;

void main(void) {
fragColor = vec4(color.rgb, texture2D(tex, texpos).r);
}


This rendering is produced when text height is 64
2676

This rendering is produced when text height is 256
2677

For some other text sizes like 255, 256 or 512 it works. But for 1024 or 64 it again becomes like on the first picture.

Can someone explain this behavior?
Thanks!

GClements
02-23-2018, 11:25 AM
glTexImage2D(...);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);


Those calls are in the wrong order. You need to call glPixelStore() before uploading the texture data. Pixel storage modes affect the process of reading and writing rectangular blocks of pixels to or from textures and framebuffers. They aren't properties of a texture object.

I can't guarantee that's related to your problem, but incorrect alignment is a common cause of "skewed" textures.

Leo2018
02-24-2018, 12:18 AM
yes! this solved the problem. Thank you!