Strange color shift after loading a GL_RGB texture
Asked Answered
C

1

6

Here's a primitive 5x2 texture, defined in RGB and RGBA formats.

//                       1               2               3                     4               5
unsigned char rgb[]  = { 0, 0, 0,        0, 0, 0,        200, 200, 200,        0, 0, 0,        200, 200, 200,
                         0, 0, 0,        0, 0, 0,        200, 200, 200,        0, 0, 0,        200, 200, 200,       };
unsigned char rgba[] = { 0, 0, 0, 255,   0, 0, 0, 255,   200, 200, 200, 255,   0, 0, 0, 255,   200, 200, 200, 255,
                         0, 0, 0, 255,   0, 0, 0, 255,   200, 200, 200, 255,   0, 0, 0, 255,   200, 200, 200, 255,  };

Loading the RGBA version works as expected:

glTexImage2D(GL_TEXTURE_2D,
             0,       // mipmap level
             GL_RGB,  // dest format
             5,       // width
             2,       // height
             0,       // deprecated
             GL_RGBA, // source format
             GL_UNSIGNED_BYTE,
             rgba);

enter image description here

Loading the RGB texture results in a weird color shift:

glTexImage2D(GL_TEXTURE_2D,
             0,       // mipmap level
             GL_RGB,  // dest format
             5,       // width
             2,       // height
             0,       // deprecated
             GL_RGB,  // source format
             GL_UNSIGNED_BYTE,
             rgb);

enter image description here

The difference in the code between the first and the second screenshot are just those two arguments to glTexImage2D.

What am I doing wrong?

Environment

  • ATI Radeon HD5450, OpenGL 4.2
  • Windows 7, 64-Bit
  • Visual Studio 2010
Chauvin answered 6/6, 2012 at 16:56 Comment(4)
Did you set the pack alignment? Default is 4 bytes, so the rgba array will be OK, but it won't work with the rgb array.Gob
No. What arguments to glPixelStorei would fix this issue?Chauvin
See here -- glPixelStorei(GL_UNPACK_ALIGNMENT, 1) should fix it.Gob
Thanks so much! Add an official answer, if you like. I'll gladly vote you up.Chauvin
G
11

See here -- glPixelStorei(GL_UNPACK_ALIGNMENT, 1) should fix it.

Gob answered 6/6, 2012 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.