If that is the case - that the type parameter specifies how many bytes per component - then what exactly do things like GL_UNSIGNED_BYTE_3_3_2 and GL_UNSIGNED_INT_8_8_8_8 mean???
It does both, depending on what the actual type is.
If the pixel transfer type is just a data type, then it specifies the data size per-component. If it has numbers in it, then the type specifies the size of the data per-pixel; the numbers specify the individual component sizes within that data type.
GL_UNSIGNED_INT_8_8_8_8
means that OpenGL will interpret each pixel as an unsigned integer. The first component will be the high 8 bits, the next will be the next 8 bits, and so forth.
However, where your problem is coming from is the fact that STB-image does not work with unsigned integers. Each pixel is written as 4 separate bytes, in RGBA order. Basically, it does this:
GLubyte arr[4] = {red, green, blue, alpha};
Now, that may sound like the same thing. But it isn't. The reason why has to do with endian issues.
When you do this in C/C++:
GLuint foo = 0;
foo |= (red << 24);
foo |= (green << 16);
foo |= (blue << 8);
foo |= (alpha << 0);
OpenGL's data types require GLuint
to be an unsigned integer exactly 32-bits in size. And assuming that red
, green
, blue
, and alpha
are all GLubyte
s (8-bit unsigned integers), C/C++ says that this will pack the red
bits into the high 8-bit byte, the green
into the next one, and so on. The C and C++ standards require this to work.
However, the C and C++ standards do not require this to work:
GLubyte *ptr = (GLubyte*)&foo;
ptr[0] == ((foo >> 24) & 0xFF);
That is, the first byte of memory pointed to by foo
does not have to be the red
component.
In little endian byte ordering, the low byte of a 32-bit integer is stored first, not last.
When OpenGL sees GL_UNSIGNED_INT
, that means it will interpret those four bytes exactly the way your CPU does. So GL_UNSIGNED_INT_8_8_8_8
will do the equivalent of foo
above. The first byte of memory it sees will be interpreted, on a little endian machine, as the low byte, not the high byte.
STB-image does not output GL_UNSIGNED_INT_8_8_8_8
. It treats each pixel as a 4-byte array, like arr
above. Therefore, you must tell OpenGL that this is how your data is stored. So you say that each component is one byte. Which is what GL_UNSIGNED_BYTE
does.