7

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?

2
  • Maybe consider a texture lookup, where your index is a uv float, instead of an array lookup? Or can you cast/round the float to an int? Commented Jul 8, 2012 at 1:16
  • 1
    "which means it supports GLSL 1.2 at the latest" No it doesn't. GL ES 2.0 means that it supports the GLSL ES shading language, which is different from the desktop GLSL language. GLSL ES is based on GLSL 1.2, but they're not the same thing. Commented Jul 8, 2012 at 2:46

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

I get this error "Index expression has type 'float' but should have integral type" if I do what you said. Is it safe to do array[int(fIndex)] or will it run into precision errors?
I get into the same issue as weezor ("...should have integral type") (at least in some cases). It would be nice to clarify.
ES2 language spec says: "Array elements are accessed using an expression whose type is an integer." So yes, cast to int (lenient platforms do an implicit cast, so answer probably works on answerer's machine).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.