Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL glColorPointer repeat colors?

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...

like image 451
quano Avatar asked Aug 21 '09 18:08

quano


2 Answers

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.

like image 61
Reed Copsey Avatar answered Oct 23 '22 16:10

Reed Copsey


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).

like image 3
fuzzy-waffle Avatar answered Oct 23 '22 15:10

fuzzy-waffle