目前我不太確定問題出在哪裏。我可以將加載的圖像作爲紋理繪製,沒有問題,但是當我嘗試生成帶有字符的位圖時,我只是得到一個黑盒子。使用FreeType lib創建文本位圖以在OpenGL 3.x中繪製
我相信問題在於當我生成並上傳紋理時。
以下是對此的方法; if語句的頂部部分只是繪製了從文件(res/texture.jpg)加載的圖像的紋理,並且完美地繪製了該紋理。 if語句的else部分將嘗試生成並上傳帶有char(變量char輸入)的紋理。
源代碼,我會添加着色器,如果需要更多的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);
}
}
只是爲了仔細檢查:您使用的是相同的代碼路徑,它使用GL_ALPHA紋理渲染「GL_RGB8」紋理,您想知道爲什麼沒有獲得任何顏色? – genpfault
我已經將它們更改爲GL_RGB8,但圖像仍然存在問題。我將在上面的帖子中進行編輯。 – Andrew