[英]Using the FreeType lib to create text bitmaps to draw in OpenGL 3.x
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. 我可以将加载的图像绘制为纹理没有问题,但是当我尝试生成一个带有char的位图时,我只会得到一个黑框。
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. if语句的顶部仅绘制从文件(res / texture.jpg)加载的图像的纹理,并且可以完美绘制。 And the else part of the if statement will try to generate and upload a texture with the char (variable char enter) on. 而if语句的else部分将尝试生成并上传带有char(可变char enter)的纹理。
Source Code, I will add shaders and more of the C++ if needed but they work fine for the image. 源代码,如果需要的话,我将添加着色器和更多的C ++,但是它们对于图像工作正常。
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);
}
}
I'm not sure about the OpenGL part, but your algorithm to process FT bitmap is not correct. 我不确定OpenGL部分,但是您处理FT位图的算法不正确。 The number of bytes of each row in FT bitmap is bitmap->pitch
. FT位图中每一行的字节数为bitmap->pitch
。 The number of each pixel also depends on which render mode your are loading the character. 每个像素的数量还取决于您正在加载字符的渲染模式。 For example, if bitmap->pixel_mode
is FT_PIXEL_MODE_LCD
, each pixel is encoded as 3 bytes, in order of R, G, B and the values are actually the alpha mask value, while if the pixel mode is FT_PIXEL_MODE_GRAY
, each pixel is 1 byte and the values are the gray level. 例如,如果bitmap->pixel_mode
为FT_PIXEL_MODE_LCD
,则每个像素按R,G,B的顺序编码为3个字节,并且值实际上是alpha掩码值,而如果像素模式为FT_PIXEL_MODE_GRAY
,则每个像素为1个字节值是灰度级。
Take a look at http://freetype.sourceforge.net/freetype2/docs/reference/ft2-basic_types.html#FT_Bitmap 看看http://freetype.sourceforge.net/freetype2/docs/reference/ft2-basic_types.html#FT_Bitmap
The very first thing I'd do would be to look at the return codes that Freetype gives to pretty much ALL the FT_* functions. 我要做的第一件事就是查看Freetype提供给几乎所有FT_ *函数的返回码。
That's good practice anyway, but if you have a problem it should narrow it down substantially. 无论如何,这都是一个好习惯,但是如果您有问题,应该将其范围缩小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.