# Chessboard colors with VBO

I am trying to draw a chessboard pattern using VBO. Geometrywise, I have it implemented and working nicely. However, I have come to the point where I want to color up the board and I have realized that I cannot do it because vertices should be black and white at the same time, depending which square they are drawing. So, looks like I cannot create a color array the same way I create a vertex array.

Any ideas how I can sort this out? Is there anyway to assign two colors to the same vertex, and select one or another depening on the actual square being rendered?

This is the code I am using so far, which is generating a nice "white to black" diagonal pattern instead of the chessboards-like.

glGenBuffersARB(1, &VBOfloorVerticesAndNormals);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBOfloorVerticesAndNormals);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, numVertices*sizeof(float), vertices, GL_STATIC_DRAW_ARB);

glGenBuffersARB(1, &VBOcolors);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBOcolors);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, numColors*sizeof(float), colors, GL_STATIC_DRAW_ARB);

glGenBuffersARB(1, &VBOfloorIndex);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, VBOfloorIndex);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, numIndices*sizeof(int), indices, GL_STATIC_DRAW_ARB);

glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBOfloorVerticesAndNormals);
glVertexPointer(3, GL_FLOAT, 0, 0);

glEnableClientState(GL_COLOR_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBOcolors);
glColorPointer(4, GL_FLOAT, 0, 0);

glIndexPointer(GL_UNSIGNED_SHORT, 0, 0);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);


And this is the pattern I get:

• Are you going to use a fragment shader for the rendering later on? – sam hocevar Dec 12 '11 at 14:12
• No, but what if I would? Now I am curious about your answer :) – Dan Dec 12 '11 at 14:29
• I would only render one large quad, with four vertices and four texture coordinates (0,1) (0,0) (1,0) and (1,1) passed as varyings to the fragment shader. Then in the fragment shader I would compute vec2 tmp = step(0.125, mod(texcoord.st, 0.25)) and float moo = mod(tmp.x + tmp.y, 2.0). The value moo is then 0 or 1 depending on whether you need to render a black or a white pixel, so in the end gl_FragColor = vec4(moo, moo, moo, 1.0). If I'm correct, that's all you need. – sam hocevar Dec 12 '11 at 15:07

You should have duplicate vertices on corners. That is a common practice.

In the end you will have 2 vertice arrays, one for black and one for white cells. If my math serves me well - 64 vertices in each array.

• That's what I was thinking... A VBO that draws the black squares and another one that draws the white squares. I didn't know that this was a common practice thought :) – Dan Dec 12 '11 at 13:54
• You need 79 vertices to display the 32 black squares or the 32 white squares of a chessboard: 4 vertices for each of the 32 quads = 128, minus 7×7 = 49 for each vertex that belongs diagonally to two quads, equals 79. – sam hocevar Dec 12 '11 at 14:59
• Also, it is a real waste to send two VBOs. Just send the 81 vertices once and for all, and play with the index buffer to select which ones to draw in each call. – sam hocevar Dec 12 '11 at 15:00

I can think of a few ways of making a chessboard pattern.

1. Texture. The easiest method. Make a 2x2 texture with wrap uv, point sampling mode and fill a quad with that, repeating the texture several times.
2. 64 separate 4 vertex quads (i.e, duplicated vertices, like @Krom mentioned)
3. Use flat shading, where the color of the polygon is taken from the first (or last) vertex of the polygon. Tricky, and you either need old enough or new enough opengl to do it.
• Thanks Jari. I am not interested in using textures at this point, I will go for solution 2, as @Krom suggested. – Dan Dec 12 '11 at 14:06