# OpenGL WebGL - How to send an array of matrices to the vertex shader?

## Recommended Posts

I want to send an array of matrices (to be more specific: a dynamically changing between draw calls Float32Array of 4x4 Float32Arrays) to a vertex shader with multiple model matrices (size of this.modelMatrices depends on how many objects are moving, so there may be only one model matrix and just in the next frame there may be 30 of them).

When I just call:
gl.uniformMatrix4fv(program.mMatrixUniform, false, this.modelMatrices);
where this.modelMatrices is an Float32Array, and program.mMatrixUniform is declared like this:
program.mMatrixUniform = gl.getUniformLocation(program, 'uMMatrix');
it throws:
gl.INVALID_VALUE was caused by call to: uniformMatrix4fv.

In OpenGL uniformMatrix4fv there is an additional count attribute, which allows to specify the amount of matrices sent while in WebGL there isn't. Also, the question is, does dynamically changing this.modelMatrices causes a problem too?

What is a proper way of doing it in WebGL?

##### Share on other sites

depends on how many objects are moving, so there may be only one model matrix and just in the next frame there may be 30 of them).

You need to define in shader the static length of the uniform array of matricies, and since that, the array of floats you pass to uniform setting function cannot be shorter than that (but can be longer since some updates). There is also inablitity to specify offset in the array, so it will always fill the uniform from the begining of array (this is so silly its hard to believe, but I just cannot find the offset accepting functions in specs!)

But there are more ways to set matricies to shaders, you can simply create a vec4 array, and set that, and use it to construct the matricies or just use them as rows the raw way in shader, doting 4d vectors with them.

##### Share on other sites

@JohnnyCode

I like the second method, but I'm not sure what would be more computational expensive - creating matrices from an array of vec4 on GPU or sending on each draw call filled Float32Array with let's say 32 matrices? Also I don't need to offset anything since I found a clever way of managing model matrices, so it is not a problem for now.

##### Share on other sites

I don't remember if I succeeded in sending Matrix4 arrays.

My engine is sending Vec4 arrays for now.

The reason behind this is that Transformation matrix can be 3x4 which spare you 1 Vec4 per matrix.

Considering you have a limit of uniforms in the vertex shader, this is mostly welcome!

##### Share on other sites

In your shader code you need to create the array of elements you want to use

uniform mat4[4] uMMatrix;


In reality, webgl just created four seperate uniforms with the names uMMatrix[0], uMMatrix[1], uMMatrix[2],and uMMatrix[3]. You need to bind them seperately.

program.mMatrixUniform[0] = gl.getUniformLocation(program, 'uMMatrix[0]');
gl.uniformMatrix4fv(program.mMatrixUniform[0], false, this.modelMatrices[0]);

There is more discussion on the subject here

##### Share on other sites

Sending an array of floats and saving uniform vectors slots sounds more promising to me than sending ready matrices and binding every one of them separately, so I think I'd rather go that way.

BTW Is there a way to define size of an array in the vertex shader basing on MAX_VERTEX_UNIFORM_VECTORS value, so it would be more 'cross-hardware'?

##### Share on other sites

BTW Is there a way to define size of an array in the vertex shader basing on MAX_VERTEX_UNIFORM_VECTORS value, so it would be more 'cross-hardware'?

I would like to correct you here that you should establish as many vectors in a compiled shader as you need, not a single bit more.

If amount you expect shrinks or grows it will bogus up your API calls. (no count or offset parameters in webgl specs uniform setters)

So establish as many vec4a as you need, not more, and check with gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS)) to see if the current setup actualy even supports you least needed amount. If the MVUV provided in outer API of yours is smaller than your needs, you need to fallback on your own responsibility and pick lesser shaders - if even.

##### Share on other sites

@JohnnyCode

I'm a little bit confused now.

GLSL specification states - "If an array is indexed with an expression that is not an integral constant expression, or if an array is passed as an argument to a function, then its size must be declared before any such use."

So unless I'd make a long long if-else ladder (which is strongly dissuaded on older GLSL) which would look like this:

else if (matrixIndex == 1)
return mat4(rawMatrices[12], rawMatrices[13], ..., rawMatrices[23], 0, 0, 0, 1);
else if (matrixIndex == 2)
return mat4(rawMatrices[24], rawMatrices[25], ..., rawMatrices[35], 0, 0, 0, 1);
...
else if (matrixIndex == 63)
return mat4(rawMatrices[756], rawMatrices[757], ..., rawMatrices[767], 0, 0, 0, 1); 

I'm bound to declare it's size first, and because of this I can't really see any advantageous solution which would establish as many floats as I need (because of the fact that the amount of needed floats would dynamically change at the runtime).

Edited by Winged

##### Share on other sites

(because of the fact that the amount of needed floats would dynamically change at the runtime).

Ah I see what you ment. You want to maximize the array , as much as it can get.

Well if you want to declare an array size of unifrom declaration of a shader to be maximal on a system, you may call gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS)) and put this value to your shader code string, before you compile it (and also knowinng this value for your very aplication) .

Wheather this constant (MAX_VERTEX_UNIFORM_VECTORS) is available in GLSL code compiler I do not know .

But bare in mind that once shader gets compiled, the uniform array keeps its size, so change at the runtime will not be possible unless you recompile, but I do not see why would someone need to change an unifrom array length through runtime? (just fill the unsed rest always,  even bogus,  you cannot specify count in webgl so always pass conforming array of floats)

You can index vec4 array with an expresion in vertex shader I am sure.

like :

uniform vec4 u_vecs[256];

....

int ind = 6 ; // sixth matrix

mat4 themat=mat4(u_vecs[ind*4],u_vecs[ind*4+1],u_vecs[ind*4+2],u_vecs[ind*4+3]);

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627678
• Total Posts
2978605
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 12
• 12
• 10
• 12
• 22