Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provide an integer for an array index in OpenGL ES 2.0?

Tags:

glsl

opengl-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?

like image 636
Elliott Avatar asked Jul 08 '12 01:07

Elliott


People also ask

What is a VAO in OpenGL?

A Vertex Array Object (VAO) is an OpenGL Object that stores all of the state needed to supply vertex data (with one minor exception noted below). It stores the format of the vertex data as well as the Buffer Objects (see below) providing the vertex data arrays.

How many bytes is a vec4?

A value of type float requires 4 bytes worth of storage. Any float value is stored in 4 consecutive bytes of memory. A sequence of 4 floats, in GLSL parlance a vec4, is exactly that: a sequence of four floating-point values. Therefore, a vec4 takes up 16 bytes, 4 floats times the size of a float.

What are vertex attributes in OpenGL?

A vertex attribute is an input variable to a shader that is supplied with per-vertex data. In OpenGL core profile, they are specified as in variables in a vertex shader and are backed by a GL_ARRAY_BUFFER . These variable can contain, for example, positions, normals or texture coordinates.


1 Answers

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

like image 60
Nicol Bolas Avatar answered Sep 27 '22 18:09

Nicol Bolas