Color Error with OpenGL Texture Formats

I try to load an image using FreeImage and then create an OpenGL texture. It loads an image and generates a texture, but there is a problem with the colors.

Here's the original image: original

and here is the result: result

Texture download code:

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);
}

Here is the code that draws it:

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();

I'm sure this is a problem with the internal format in glTexImage2D, but I'm not sure which format I will use if this happens. My question is: is this a format, if so, what should I use; or is there another problem that causes this?

+4
source share
1 answer

Try using this instead

, , 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);
}
+1

Source: https://habr.com/ru/post/1611505/


All Articles