OpenGL default value for unbuffered vertex attribute when using layout specifiers

I suppose this will be one of those things that is "undefined", but I cannot find a specific answer from google.

Say in my vertex shader:

layout(location = 0) in vec3 vPosition; layout(location = 1) in vec3 vNormal; layout(location = 2) in vec4 vColour; 

But there is nothing buffered for location 2 with glEnableVertexAttribArray () or glVertexAttribPointer (). Can I expect the meaning to be something special?

I assumed for vec4 that it would be along the lines {0,0,0,0}, {0,0,0,1} or {1,1,1,1}, but in mine it is {0,0,1 ,one}.

When I previously used glBindAttribLocation () to specify locations, it was by default {1,1,1,1} on 4 different machines using 3 different operating systems (ubuntu 12.04, windows 7 and ubuntu 10.04).

Can we assume that the value will be {0,0,1,1} on the machines? or is it just a coincidence?

+7
attributes shader opengl vertex-buffer
source share
2 answers

Can one expect value to be something special?

Yes, that is of particular importance.

Assuming you have correctly disabled an array of attributes (i.e.: glDisableVertexAttribArray at that attribute index), the value you get is the vertex attribute in the context , as the glVertexAttrib set of functions set. This is a global context state not stored in the VAO.

By default, they all start with (0, 0, 0, 1). However, if you provide a specific attribute using an array in this attribute index, the context value is effectively destroyed. Therefore, you must reset the context value if you want to use it.

+5
source share

They are undefined here, but this may not be a problem. There, the GL state consists of the values CURRENT_VERTEX_ATTRIB . Initially, they are (0,0,0,1). You can explicitly set attribute values ​​for those attributes in which the array is not included using the glVertexAttrib() function family.

The only thing to worry about is what happens to the current values ​​when the attribute array is actually turned on during drawing. To quote Spec (version 3.3) , section 2.8.3. Vertex arrays - drawing command:

If the array corresponding to the general attribute required by the vertex shader is not activated, then the corresponding element is taken from the current common one (see section 2.7).

If the array corresponding to the common attribute required by the vertex shader, the corresponding current common attribute value is undefined after execution of DrawArraysOneInstance .

So you just need to specify a useful value after you have drawn the array allowed for this particular attribute.

UPDATE

This behavior has really changed since OpenGL 4.2 :

If the array corresponding to the general attribute required by the vertex shader is not included, then the corresponding element is taken from the current common attribute (see section 2.7). Otherwise, if the array is included, the corresponding current total attribute value does not affect the execution of DrawArraysOneInstance.

So, now the call to glDraw*() will never change the currently set attribute values.

+1
source share

All Articles