vertex shader with color attribute question


I am drawing a bunch of 2D objects using OpenGLES 2.0, so I have vertex and fragment shaders. Some of the objects have color data at each vertex, and some of the objects are all one color.

Do I need to switch shader programs whenever I switch from drawing a single-color object to a multi-color one, and vice-versa? Obviously the multi-color objects will need to use a shader which uses the color-per-vertex data as an attribute. But can I render the single-color objects using the same shader? I was thinking that a uniform variable could indicate whether to look at the color-per-vertex data or whether to use a global color.

So, for monochrome objects I would not want to pass any color-per-vertex data. But my question is – will there be problems if the colorattribute is not bound to valid data, even if I don’t use it?

Does that make sense? I’m wondering if it is possible to draw both types of objects without switching shaders, and without having to supply all that redundant color vertex data for the monochrome objects.


This is very easy. If the vertex shader reads an attribute that is not enabled (glDisableVertexAttribArray) then the shader reads the attribute from the “current vertex” instead of an array.

And you can set a constant attribute in the “current vertex” with glVertexAttrib.

So yes, you can use exactly the same shader and simply change the vertex array state. No need to write an ubershader with flow control via a uniform.

Awesome! Thanks very much