Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Happy monday.

I'm trying to use a GIF or a 8 bpp BMP image image as a source for an OpenGL texture.

The code is working well for other types of image (24, 32bits image)

I'm loading the file with CImage, but I'm having problems figuring the format and type parameters of the glTexImage2D function.

Either I get a full black texture, or I get the wrong colors.

// will create black texture.
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, imageWidth, image.GetHeight(), 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, bits );

// will create greyscale image.
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, imageWidth, image.GetHeight(), 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, bits );

// will create texture with wrong colors.
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, imageWidth, image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE_3_3_2, bits );


I also tried different other combinations, either it crashes, or give the same bad results.

This is the code I have right now:

CImage image;
image.Load(fileName);

// some image info.
int imageBPP = image.GetBPP();

GLuint  texture = 0;
glGenTextures(1, &texture);
glBindTexture( GL_TEXTURE_2D, texture );

BYTE* bits = static_cast<BYTE*>(image.GetBits());
// The bitmap is a bottom-up DIB, move to the start address of the image data
bits += (imageHeight-1) * imagePitch;

if ( imageBPP == 32 )
{
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, bits );
}
else if ( imageBPP == 24 )
{
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, bits );
}
else if (imageBPP==8)
{
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, image.GetWidth(), image.GetHeight(), 0, ????, ????, bits );
}
else
{
// ... error code.
}



Any Idea ?

Thanks

Max.

==========================
Following nv3 suggestion, I decided to convert the 8 bits image to a workable image.

Thanks.
Posted
Updated 31-Oct-13 5:55am
v2
Comments
nv3 28-Oct-13 10:30am    
You are passing GL_RGB as third argument and thereby telling OpenGL that your image has three color components. For an 8-bit bmp that would be quite unusual. I would try to use RL_RED instead.
Maximilien 28-Oct-13 10:46am    
If I change the 3rd argument to GL_RED, the texture is only using the red channel, the 3rd argument is the format of the texture, not the format of the loaded file. ???
nv3 28-Oct-13 12:03pm    
That could well be. In that case, GL_LUMINANCE, GL_UNSIGNED_BYTE should be the right settings. As you said that worked, but it delivered a grayscale image. Which is not surprising, as your input image is grayscale, isn't it?
Maximilien 28-Oct-13 21:40pm    
Nope, the image is a color image.
nv3 29-Oct-13 4:16am    
In that case it should be an 8-bit color palette image and your parameters should be GL_COLOR_INDEX, GL_UNSIGNED, just as you wrote in your first alternative. But that gave you a black texture. Have you tried to first convert your image to a regular 32 bpp RGB image and create the texture from there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900