I am using freetype to display text, with the the help of this tutorial: http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01
But all i see is empty squares, I played around a lot with the shaders and came to a conclusion that that the alpha value of the squares was always 0.0 also for some reason the program crashes after first time of compiling edited text shaders
This is the code: Vertex Shader
#version 420
//vertexData.xy contains pos vertexData.zw contains uv coords
in vec4 vertexData;
out vec2 TexCoord;
void main(){
gl_Position = vec4(vertexData.x, vertexData.y, 0.0, 1.0);
TexCoord = vertexData.zw;
}
Fragment Shader
#version 420
in vec2 TexCoord;
out vec4 fragData;
uniform sampler2D tex;
uniform vec4 TextColor;
void main(){
//TextColor.rgb is the color texture(tex, TexCoord).r is texture alpha value
fragData = vec4(TextColor.rgb, texture(tex, TexCoord).r * TextColor.a);
}
FreeType
//Freetype init and face creation is in func Font::init()
void Font::renderText(const char *text, float x, float y, float sx, float sy, float rgba[4]){
//Attribute and Uniform Locations
GLint vDataLoc = ShaderResource::TextProgram->getAttributeLocation("vertexData");
GLint textColLoc = ShaderResource::TextProgram->getUniformLocation("TextColor");
GLint texSamplerLoc = ShaderResource::TextProgram->getUniformLocation("tex");
const char *p;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
FT_Set_Pixel_Sizes(face, 0, 48);
//iterate through text
for(p = text; *p; p++) {
if(FT_Load_Char(face, *p, FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
int width = to_nearest_pow2( g->bitmap.width );
int height = to_nearest_pow2( g->bitmap.rows );
glUniform4fv(textColLoc, 1, rgba);
glEnable(GL_TEXTURE_2D);
Texture Tex;
Tex = Texture();
Tex.bind();
Tex.setParameter(GL_TEXTURE_WRAP_S, GL_CLAMP);
Tex.setParameter(GL_TEXTURE_WRAP_T, GL_CLAMP);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//At first I thought I was passing wrong formats to glteximage2d so I played around with it using gl alpha, gl luminance alpha, gl luminance8 alpha8 etc...
//I believe the issue is that g->bitmap.buffer was not created properly i must not be loading that char correctly??
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
width,
height,
0,
GL_RED,
GL_UNSIGNED_BYTE,
g->bitmap.buffer
);
Tex.active(0);
glUniform1i(texSamplerLoc, 0);
//Then I create textbox and draw it using a vertex array obj and vbos using gl triangle strip etc...
Main.cpp Freetype Func call
//Disable Normal Shader Enable Text Shader
mainProgram->disable();
textProgram->use();
//Scale x & Scale y / window get screen width and height
float sx = 2.0 / window->getFrameBufferWidth();
float sy = 2.0 / window->getFrameBufferHeight();
//Text color
float color[4] = {0.0,0.0,0.0,1.0};
IHateComicSans.renderText("Hello World!", 0.0, 0.0, sx, sy, color);
textProgram->disable();
mainProgram->use();
After gltexImage2d glGetError returns GL_INVALID_OPERATION but only during the last iteration of the text
The problem was in my Texture class, fixed by just using opengl functions.