c – OpenGL纹理格式的颜色错误

我正在尝试使用Free
Image加载图像,然后生成OpenGL纹理.它加载图像并生成纹理,但颜色存在问题.

这是原始图片:

这是结果:

纹理加载代码:

void TextureManager::LoadTexture(std::string id, std::string filePath){

Texture tex;
tex.TextureId = 0;

FIBITMAP* image = FreeImage_Load(FreeImage_GetFileType(filePath.c_str(), 0), filePath.c_str());


if (FreeImage_GetBPP(image) != 32) {

    image = FreeImage_ConvertTo32Bits(image);
}


FreeImage_FlipVertical(image);

tex.Width = FreeImage_GetWidth(image);
tex.Height = FreeImage_GetHeight(image);


glGenTextures(1, &tex.TextureId);
glBindTexture(GL_TEXTURE_2D, tex.TextureId);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,tex.Width, tex.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(image));

AddTextureToMap(id, tex);

FreeImage_Unload(image);
}

这是绘制它的代码:

glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, TextureManager.TextureMap[textureSelector].TextureId);

glBegin(GL_TRIANGLES);
glTexCoord2d(0, 0);
glVertex3f(-0.75, 0.75, 0);//top left
glTexCoord2d(1, 0);
glVertex3f(0.75, 0.75, 0);//top right
glTexCoord2d(0, 1);
glVertex3f(-0.75, -0.75, 0);//bottom left

glTexCoord2d(1, 0);
glVertex3f(0.75, 0.75, 0);//top right
glTexCoord2d(1, 1);
glVertex3f(0.75, -0.75, 0);//bottom right
glTexCoord2d(0, 1);
glVertex3f(-0.75, -0.75, 0);//bottom left
glEnd();
glFinish();

我很确定这是glTexImage2D中内部格式的问题,但我不确定在这种情况下我会使用哪种格式.我的问题是:它是格式,如果是,我应该使用什么;还是有另一个问题导致了这个问题?

最佳答案 尝试使用它

不同之处在于,许多图像格式使用反向字节顺序,您应该使用GL_BGR_EXT或GL_BGRA_EXT图像格式而不是GL_RGB或GL_RGBA

void TextureManager::LoadTexture(std::string id, std::string filePath){

Texture tex;
tex.TextureId = 0;

FIBITMAP* image = FreeImage_Load(FreeImage_GetFileType(filePath.c_str(), 0), filePath.c_str());


if (FreeImage_GetBPP(image) != 32) {

    image = FreeImage_ConvertTo32Bits(image);
}


FreeImage_FlipVertical(image);

tex.Width = FreeImage_GetWidth(image);
tex.Height = FreeImage_GetHeight(image);


glGenTextures(1, &tex.TextureId);
glBindTexture(GL_TEXTURE_2D, tex.TextureId);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,tex.Width, tex.Height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(image));

AddTextureToMap(id, tex);

FreeImage_Unload(image);
}
点赞