目前我不太确定我的问题出在哪里。我可以将加载的图像绘制为纹理没问题,但是当我尝试生成一个带有字符的位图时,我只会得到一个黑框。
我确信问题出在我生成和上传纹理时。
这是实现的方法; if 语句的顶部部分只是绘制从文件 (res/texture.jpg) 加载的图像的纹理,并且绘制得非常完美。 if 语句的 else 部分将尝试生成并上传带有 char(变量 char enter)的纹理。
源代码,如果需要,我将添加着色器和更多 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);
}
}
我是一名优秀的程序员,十分优秀!