Search code examples
opengltexel

Texel data in case of GL_LUMINANCE when glTexImage2D is called


Usually a texel is an RGBA value. What data does a texel represent in the following code:

const int TEXELS_W = 2, TEXELS_H = 2;
GLubyte texels[] = {
    100, 200,  0,  0,
    200,  250,  0,  0
};
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(
    GL_TEXTURE_2D,
    0,                  // mipmap reduction level
    GL_LUMINANCE,
    TEXELS_W,
    TEXELS_H,
    0,                  // border (must be 0)
    GL_LUMINANCE,
    GL_UNSIGNED_BYTE,
    texels);

Solution

  • GLubyte texels[] = {
        100, 200,  0,  0,
        200,  250,  0,  0
    };
    

    OpenGL will only read 4 of these values. Because GL_UNPACK_ALIGNMENT defaults to 4, OpenGL expects each row of pixel data to be aligned to 4 bytes. So the two 0's in each row are just padding, because the person who wrote this code didn't know how to change the alignment.

    So OpenGL will read 100, 200 as the first row, then skip to the next 4 byte boundary and read 200, 250 as the second row.