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);
glDrawElements(GL_QUADS, numIndices, GL_UNSIGNED_INT, 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:
(0,1) (0,0) (1,0)
and(1,1)
passed as varyings to the fragment shader. Then in the fragment shader I would computevec2 tmp = step(0.125, mod(texcoord.st, 0.25))
andfloat moo = mod(tmp.x + tmp.y, 2.0)
. The valuemoo
is then0
or1
depending on whether you need to render a black or a white pixel, so in the endgl_FragColor = vec4(moo, moo, moo, 1.0)
. If I'm correct, that's all you need. \$\endgroup\$ – sam hocevar Dec 12 '11 at 15:07