Failing to map a simple unsigned byte rgb texture to a quad:

GL_UNPACK_ALIGNMENT specifies the alignment requirements for the start of each pixel row in memory. By default GL_UNPACK_ALIGNMENT is set to 4.
This means each row of the texture is supposed to have a length of 4*N bytes.

You specify a 2*2 texture with the data: 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0

With GL_UNPACK_ALIGNMENT set to 4 this is interpreted as

         column 1          column 2              alignment
row 1:   255, 0,   0,      255, 0,     0,        255, 0,
row 2:   0,   255, 0,      0,   undef, undef

So the texture is read as

          column 1   olumn 2  
row 1:    red,       red,
row 2:    green,     RGB(0, ?, ?)

You have to set glPixelStorei(GL_UNPACK_ALIGNMENT, 1); before glTextureSubImage2D, for reading a tight packed texture.

If you do not want to change GL_UNPACK_ALIGNMENT (the alignment remains set to 4), you must adjust the data as follows:

struct DummyRGB8Texture2d
{
    uint8_t data[8*2];
    int width;
    int height;
};

DummyRGB8Texture2d myTexture
{
    { 
        255, 0, 0, 255, 0, 0, // row 1
        0, 0,                 // 2 bytes alignment
        255, 0, 0, 255, 0, 0, // row 2
        0, 0                  // 2 bytes alignment
    },
    2u,
    2u
};

See further:

Leave a Comment