Search code examples
c++opengltexture-mapping

Creating checkerboard image for texture


Please help me understand what the following is doing. Specifically, what is variable 'c', and what is the third array dimension for (isn't an image a 2-dimensional pixel rectangle)? I'll post a link to this code if context is needed, but the context in general is mapping this checkerboard pattern to a rotating cube.

GLubyte image[TextureSize][TextureSize][3];
GLubyte image2[TextureSize][TextureSize][3];

// Create a checkerboard pattern
for ( int i = 0; i < 64; i++ ) {
    for ( int j = 0; j < 64; j++ ) {
        GLubyte c = (((i & 0x8) == 0) ^ ((j & 0x8)  == 0)) * 255;
        image[i][j][0]  = c;
        image[i][j][1]  = c;
        image[i][j][2]  = c;
        image2[i][j][0] = c;
        image2[i][j][1] = 0;
        image2[i][j][2] = c;
    }
}

Solution

  • An image is 2 spatial dimensions and color - so 3 dimensions in a way.
    The last [] is the red,green,blue pixel values

    This is just using the 'c' array syntax to do the calculations into the memory for you.

    The layout in memory is just [row1][col1][red], [row1][col1][green], [row1][col1][blue], [row1][col2][red], [row1][col2][green], [row1][col2][blue] ........

    So if c is either 0 or 255 then

    // sets all red,green,blue to same value = black (c=0) or white (c=255)
    image[i][j][0]  = c;
    image[i][j][1]  = c;
    image[i][j][2]  = c;
    
    // sets red and blue on but green off = purple
    image[i][j][0]  = c;
    image[i][j][1]  = 0;
    image[i][j][2]  = c;