Reputation: 713
I'm coming across a really weird bug lately. In my vertex shader I have three attributes:
attribute vec3 vPosition;
attribute vec3 vNormal;
attribute vec3 vColor;
I initialize the buffers associated to all of them the same way, and everything looks fine. However, when, in my rendering cycle I bind the attributes to the buffers, the 1st and 3rd attributes work fine, while the 2nd one gives me a weird error. This bugs me as I use the same code for all 3 attributes.
This is my binding code:
gl.bindBuffer(gl.ARRAY_BUFFER, this.vBuffer);
gl.vertexAttribPointer(gl.getAttribLocation(program, "vPosition"), 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(gl.getAttribLocation(program, "vPosition"));
gl.bindBuffer(gl.ARRAY_BUFFER, this.nBuffer);
gl.vertexAttribPointer(gl.getAttribLocation(program, "vNormal"), 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(gl.getAttribLocation(program, "vNormal"));
gl.bindBuffer(gl.ARRAY_BUFFER, this.cBuffer);
gl.vertexAttribPointer(gl.getAttribLocation(program, "vColor"), 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(gl.getAttribLocation(program, "vColor"));
and this is the error code I get (I get it twice, once for each line calling getAttribLocation(program, "vNormal")
:
Error: WebGL warning: vertexAttribPointer: -1 is not a valid `index`. This value probably comes from a getAttribLocation() call, where this return value -1 means that the passed name didn't correspond to an active attribute in the specified program.
This seems to suggest I'm doing something wrong with getAttribLocation
, but I pass it the actual name for the variable I have in the shader, just like I do for the other two. Is there anything really obvious that I'm missing? How can I solve this?
Upvotes: 1
Views: 1257
Reputation: 211268
getAttribLocation
get's the attribute index of an active attribute. An attribute is a program resource.
The WebGL 1.0 Specification for 5.14.10 Uniforms and attributes - getAttribLocation
points to OpenGL ES 2.0 Specification - 2.10.4 Shader Variables, where is specified:
A generic attribute variable is considered active if it is determined by the compiler and linker that the attribute may be accessed when the shader is executed. Attribute variables that are declared in a vertex shader but never used are not considered active. [...]
This means, that an attribute variable, that is not "used" in the vertex shader program doesn't become active. So the variable is not associated to an attribute index and getAttribLocation
returns -1, when the index of the variable is queried.
Upvotes: 1