Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use glVertexAttribPointer to assign a smaller vector to a larger one?

From section 5.8 of The OpenGL® ES Shading Language (v1.00, r17) [PDF] (emphasis mine):

The assignment operator stores the value of the rvalue-expression into the l-value and returns an r-value with the type and precision of the lvalue-expression. The lvalue-expression and rvalue-expression must have the same type. All desired type-conversions must be specified explicitly via a constructor.

So it sounds like doing something like this would not be legal:

vec3 my_vec3 = vec3(1, 2, 3);
vec4 my_vec4 = my_vec3;

And to make it legal the second line would have to be something like:

vec4 my_vec4 = vec4(my_vec3, 1); // add 4th component

I assumed that glVertexAttribPointer had similar requirements. That is, if you were assigning to a vec4 that the size parameter would have to be equal to 4.

Then I came across the GLES20TriangleRenderer sample for Android. Some relevant snippets:

attribute vec4 aPosition;

maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");

GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
        TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);

So aPosition is a vec4, but the call to glVertexAttribPointer that's used to set it has a size of 3. Is this code correct, is GLES20TriangleRenderer relying on unspecified behavior, or is there something else I'm missing?

like image 349
Laurence Gonsalves Avatar asked Oct 12 '11 23:10

Laurence Gonsalves


People also ask

What does glVertexAttribPointer do?

glVertexAttribPointer specifies the location and data format of the array of generic vertex attributes at index index to use when rendering.

What is vertex attribute?

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.

What is a vertex Open gl?

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.


1 Answers

The size of the attribute data passed to the shader does not have to match the size of the attribute in that shader. You can pass 2 values (from glVertexAttribPointer) to an attribute defined as a vec4; the leftover two values are zero, except for the W component which is 1. And similarly, you can pass 4 values to a vec2 attribute; the extra values are discarded.

So you can mix and match vertex attributes with uploaded values all you want.

like image 166
Nicol Bolas Avatar answered Sep 28 '22 16:09

Nicol Bolas