Using the FreeType lib to create text bitmaps to draw in OpenGL 3.x
Posted
by
Andy
on Stack Overflow
See other posts from Stack Overflow
or by Andy
Published on 2012-06-18T20:14:41Z
Indexed on
2012/06/18
21:16 UTC
Read the original article
Hit count: 364
At the moment I not too sure where my problem is. I can draw loaded images as textures no problem, however when I try to generate a bitmap with a char on it I just get a black box.
I am confident that the problem is when I generate and upload the texture.
Here is the method for that; the top section of the if statement just draws an texture of a image loaded from file (res/texture.jpg) and that draws perfectly. And the else part of the if statement will try to generate and upload a texture with the char (variable char enter) on.
Source Code, I will add shaders and more of the C++ if needed but they work fine for the image.
void uploadTexture()
{
if(enter=='/'){
// Draw the image.
GLenum imageFormat;
glimg::SingleImage image = glimg::loaders::stb::LoadFromFile("res/texture.jpg")->GetImage(0,0,0);
glimg::OpenGLPixelTransferParams params = glimg::GetUploadFormatType(image.GetFormat(), 0);
imageFormat = glimg::GetInternalFormat(image.GetFormat(),0);
glGenTextures(1,&textureBufferObject);
glBindTexture(GL_TEXTURE_2D, textureBufferObject);
glimg::Dimensions dimensions = image.GetDimensions();
cout << "Texture dimensions w "<< dimensions.width << endl;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, dimensions.width, dimensions.height, 0, params.format, params.type, image.GetImageData());
}else
{
// Draw the char useing the FreeType Lib
FT_Init_FreeType(&ft);
FT_New_Face(ft, "arial.ttf", 0, &face);
FT_Set_Pixel_Sizes(face, 0, 48);
FT_GlyphSlot g = face->glyph;
glGenTextures(1,&textureBufferObject);
glBindTexture(GL_TEXTURE_2D, textureBufferObject);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
FT_Load_Char(face, enter, FT_LOAD_RENDER);
FT_Bitmap theBitmap = g->bitmap;
int BitmapWidth = g->bitmap.width;
int BitmapHeight = g->bitmap.rows;
cout << "draw char - " << enter << endl;
cout << "g->bitmap.width - " << g->bitmap.width << endl;
cout << "g->bitmap.rows - " << g->bitmap.rows << endl;
int TextureWidth =roundUpToNextPowerOfTwo(g->bitmap.width);
int TextureHeight =roundUpToNextPowerOfTwo(g->bitmap.rows);
cout << "texture width x height - " << TextureWidth <<" x " << TextureHeight << endl;
GLubyte* TextureBuffer = new GLubyte[ TextureWidth * TextureWidth ];
for(int j = 0; j < TextureHeight; ++j)
{
for(int i = 0; i < TextureWidth; ++i)
{
TextureBuffer[ j*TextureWidth + i ] = (j >= BitmapHeight || i >= BitmapWidth ? 0 : g->bitmap.buffer[ j*BitmapWidth + i ]);
}
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, TextureWidth, TextureHeight, 0, GL_RGB8, GL_UNSIGNED_BYTE, TextureBuffer);
}
}
© Stack Overflow or respective owner