Question

Imagine that you've got one of these guys:

alt text
(source: codesampler.com)

Aka GL_TRIANGLE_STRIP. If you wanna color it using different colors, you could use:

glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray);
glEnableClientState(GL_COLOR_ARRAY);

Where each "item" in the color array matches a vertex point in the strip. But what if the colors just alternate between two different colors? Feels unnecessary to define a color for each vertex, if there's only two "real" different colors. So my question is if it's possible to define a colorArray with just two colors in it, and somehow make opengl alternate between those two when it's looping over the vertex array.

And yeah I'm a complete noob at opengl so maybe this is a stupid question...

Was it helpful?

Solution

You can do this by using OpenGL Color Index Mode. However, be aware that, in my experience, this actually is slower on most modern graphics card than just specifying each vertex color.

In color index mode, you send a separate color array, then each vertex specifies an index into the array, instead of a full rgb/rgba. It's more memory efficient, but not as optimized in most hardware drivers.

OTHER TIPS

You could use a vertex shader to do this by sending in a boolean flag for each vertex describing what color to be. Really the simplest/fastest thing is probably just to submit the color for each vertex.

If opengl had the ability to have multiple vertex streams and indices that might work (but also would probably not be worth it).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top