简体   繁体   中英

OpenGL texture color separation

I'm using CImg within an OpenGL environment. When I try to add a texture, each color seems to separate out into separate tile within the texture. For example, when I attempt to use a texture that looks like this:

it comes out in my program looking like this:

The texture that I'm loading is a 512x512 .bmp file, saved in Gimp with 24 bit color depth, RBG. I've tried some of the other formats that Gimp allows, but those just cause errors with CImg.

This is how I load the texture:

glBindTexture(GL_TEXTURE_2D, 13);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

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

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

CImg<unsigned char> src("MilkyWay.bmp");
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, src.width(), src.height(), 
    0, GL_RGB, GL_UNSIGNED_BYTE, src);

and this is how I display the texture:

glEnable (GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, 13);
glBegin (GL_QUADS);         
    glTexCoord2f (0.0, 1.0);
    glVertex3f (-120.0, 120.0, 0.0);
    glTexCoord2f (1.0, 1.0);
    glVertex3f (120.0, 120.0, 0.0);
    glTexCoord2f (1.0, 0.0);
    glVertex3f (120.0, -120.0, 0.0);
    glTexCoord2f (0.0, 0.0);
    glVertex3f (-120.0, -120.0, 0.0);
glEnd ();

CImg stores channels non-interleaved (or 'planar'), but GL is expecting the opposite. The result is what you see.

You could rearrange the data (or have CImg do it for you), but as the channels also tend to be interleaved in the source format, having the image library de-interleave them only for some intermediate step to re-interleave before handing off to GL would be inefficient. I would instead recommend using a different method of loading your image data, such that it is directly usable.

There are many libraries which will do this, though BMP is a pretty easy format to read directly.

根据此处,您还可以使用:

src.permute_axes("cxyz");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM