In some WebGL application, let's assume that we have a GLSL vertex shader which starts like this:
attribute vec4 foo1;
attribute vec4 foo2;
attribute vec4 foo3;
attribute vec4 foo4;
and some corresponding Javascript code for binding a data structure for those attributes:
var buf = gl.createBuffer(), loc;
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([…]));
loc = gl.getAttribLocation(program, 'foo1');
gl.enableVertexArray(loc);
gl.vertexAttribPointer(loc, 4, gl.FLOAT, false, 16, 0);
loc = gl.getAttribLocation(program, 'foo2');
gl.enableVertexArray(loc);
gl.vertexAttribPointer(loc, 4, gl.FLOAT, false, 16, 4);
loc = gl.getAttribLocation(program, 'foo3');
gl.enableVertexArray(loc);
gl.vertexAttribPointer(loc, 4, gl.FLOAT, false, 16, 8);
loc = gl.getAttribLocation(program, 'foo4');
gl.enableVertexArray(loc);
gl.vertexAttribPointer(loc, 4, gl.FLOAT, false, 16, 12);
Now, according to the GL ES 2.0 specs, a vertex shader attribute can be defined as either a float
, vec2
, vec3
, vec4
, mat2
, mat3
or mat4
.
So if I change the vertex shader code to define just one mat4
attribute, like so...
attribute mat4 foo;
... the question is what is the corresponding JS code to bind some pointers to a mat4
attribute?
I have found the question mat3 attribute in WebGL, but the answer is not explicit enough. Reading the answers and some other documentation, it seems that the correct solution is along the lines of:
loc = gl.getAttribLocation(program, 'foo');
gl.enableVertexArray(loc);
gl.vertexAttribPointer(loc , 4, gl.FLOAT, false, 16, 0);
gl.vertexAttribPointer(loc+1, 4, gl.FLOAT, false, 16, 4);
gl.vertexAttribPointer(loc+2, 4, gl.FLOAT, false, 16, 8);
gl.vertexAttribPointer(loc+3, 4, gl.FLOAT, false, 16, 12);
Am I right in assuming that the locations of the 4 vec4
components of a mat4
are always adjacent and in increasing order? Is this documented somewhere?
Besides these locations counting towards the MAX_VERTEX_ATTRIBS
limit (normally 16 in WebGL), is there any other good practice to be aware of?
You're correct. From the spec section 2.10.4
stride and offsets in WebGL are in bytes so I suspect you wanted
Let's check