Simple Vertex Color Problem when using Indices

Without any code at first let me describe the problem.

I recreated the basic lesson for a rotating cube. Lesson 4 Learning Webgl

The code has 4 vertices listed for each face for a total of 24 vertices. It uses indices to these 24 vertices 0-23 to do a drawElements(…) and it uses a color buffer array with 24 different colors.

So the end result of course is a rotating cube with each face assigned a different color.

What I did was work with the fact that a cube really only has 8 vertices not 24. So I made an array of 8 vertices. And I reworked the position index array to work with indices from 0-7 instead of what was given 0-23. I left the color buffer array alone since I put the same order to the face creation as in the original lesson.

When I completed the drawElements(…), I did have a perfect rotating cube. But the cube is not colored correctly. Each vertex has a single color so only the first eight of the color array elements (out of 24) were used. Basically The top half is one color and the bottom half is another color. And the faces of the cube do not have a unique color.

The question is : is there a way to assign color to vertex indices instead of to vertices?
I am suspecting that I could use gl_vertexID in the shaders, but I thought I’d get some opinions first?


No, the indices references the currently enabled vertex attribute arrays. Normally, you’d probably want to texture your model anyway, so this is usually of little concern…

Also, gl_vertexID is not available using OpenGL ES shaders. The ES shaders doesnt even have integer support.