Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for a vertex attribute to be an array in GLSL-ES 2.0?

In GLSL-ES it's possible to have arrays. For example, the GLSL ES Specification gives the following example of a uniform variable that's an array:

uniform vec4 lightPosition[4];

Is it possible to have vertex attributes that are arrays? In other words, is the following legal according to the spec?

attribute vec4 foo[3];  // three vec4s per vertex

Is the answer (either yes or no) explicitly mentioned anywhere in the GLSL ES Specification? (I can't find it, but I haven't read every line of the spec.)

Also, if it is legal, how does one initialize such an attribute using the OpenGL ES 2.0 API? (Assuming glVertexAttribPointer would be used, what is the layout of the vertices/array-elements/vector-elements?)

like image 569
Laurence Gonsalves Avatar asked Nov 22 '11 00:11

Laurence Gonsalves


People also ask

What is a vertex array 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.

What is a vertex GLSL?

Vertex shaders manipulate coordinates in a 3D space and are called once per vertex. The purpose of the vertex shader is to set up the gl_Position variable — this is a special, global, and built-in GLSL variable. gl_Position is used to store the position of the current vertex.

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.

How do I create an array in GLSL?

To initialize an array you can use a constructor. Like in object orientation GLSL has a constructor for arrays. The syntax is as follows. int a[4] = int[](4, 2, 0, 5, 1); float a[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1); int[] c = int[3](1, 2, 3); int[] d = int[](5, 7, 3, 4, 5, 6);


1 Answers

The GLSL ES 2.0 specification states that attributes cannot be declared as arrays.

In desktop GL, you can have attribute arrays. When the attribute is assigned an attribute index (either with glBindAttribLocation or automatically by the shader being linked), it will get consecutive attributes, starting with the one you requested if you used glBindAttribLocation. So if foo was given the location 5, foo[0] would be 5, foo[1] would be 6, and foo[2] would be 7.

If there is some ES 2.0 extension to allow attribute arrays, it would likely work like this.

like image 113
Nicol Bolas Avatar answered Oct 26 '22 09:10

Nicol Bolas