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?)
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.
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With