What is the best way to specify the colors of different squares while drawing a chess board?

opengl

Solution

If you really want to draw a quad for each field, duplicating the vertices is the way to go. There are no problems with different vertices having the same coordinates. The GL's rasterization rules will make sure that there are a) no gaps at such shared edges and b) there is also no overdraw, so you will be fine.

However, you can also draw the whole field as one quad and use texturing. All you would need is a 2x2 sized texture with the black and white colors and can use the `GL_NEAREST` filtering mode so get a nice and sharp checkerboard pattern. With that approach, you can also dynamically change the number of fields without changing the texture at all, just by using the `GL_REPEAT` mode and only changing the texcoords.

In modern shader based GL, you can also procedurally generate the checkerboard pattern directly in the fragment shader.

Problem

What is the best way to specify the colors of different squares while drawing a chess board? Suppose I want a 2 by 2 board with colors like this: ``` *-----*-----* |black|white| *-----*-----* |white|black| *-----*-----* ``` I can now have 9 vertices and draw the board with GL_QUADS primitive. As I understand filling a square with some color means specifying a color of each vertex with that color. But filling every square with a different color means duplicating 5 vertices ``` *-----**----* |black|white| **-----**----** |white|black| *-----**----* ``` Is it the simplest way to do this? And is it actually allowed in OpenGL to have vertices with equal coordinates and different colors?

Original source