Provide an integer for an array index in OpenGL ES 2.0

glslopengl-es

I am developing an OpenGL application for the iPhone. In my vertex shader, I need a way to change the color of a large number of (but not all) of my vertices, at once, so I settled on color indexing. This will allow me to leave the VBO static, and modify a single uniform variable rather than looping through each vertex and modifying color information between each frame.

My plan is to create a uniform with a color array, add an integer containing an index in the attributes. Here is my vertex shader:

uniform mat4 u_mvp_matrix;
uniform vec4 u_color_array[];

attribute vec4 a_position;
attribute int a_colorIndex;

varying lowp vec4 v_color;

void main()
{
    v_color = u_color_array[a_colorIndex];

    gl_Position = u_mvp_matrix * a_position;
}

This raises an error:

int can't be an in in the vertex shader

I did some research. The iPhone supports OpenGL ES 2.0 at the latest, which means it supports GLSL 1.2 at the latest, and apparently integers are only supported in GLSL 1.3 and later. I tried changing a_colorIndex to a float. I didn't expect it to work, and it didn't.

How can I specify a color index for each vertex?

Best Answer

Specify the attribute as a float. You can use floats as indices into arrays.

Related Topic