调用 glTexImage2D 时 GL_LUMINANCE 情况下的 Texel 数据

Texel data in case of GL_LUMINANCE when glTexImage2D is called

通常纹素是一个RGBA值。下面代码中一个texel代表什么数据:

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

GL_LUMINANCE:

Each element is a single luminance value. The GL converts it to floating point, then assembles it into an RGBA element by replicating the luminance value three times for red, green, and blue and attaching 1 for alpha. Each component is then multiplied by the signed scale factor GL_c_SCALE, added to the signed bias GL_c_BIAS, and clamped to the range [0,1] (see glPixelTransfer).

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

OpenGL 只会读取其中的 4 个值。因为 GL_UNPACK_ALIGNMENT 默认为 4,OpenGL 期望 each row of pixel data to be aligned to 4 bytes。所以每一行的两个0只是填充,因为写这段代码的人不知道如何改变对齐方式。

因此 OpenGL 将读取 100, 200 作为第一行,然后跳到下一个 4 字节边界并读取 200, 250 作为第二行。