You are very close. I will implement it from the very beginning, just for the case.
Allows you to have a simple triangle model with vertices:
var triangleVertices = [ 0.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0, -1.0, 0.0 ];
Now we create mat4, which represents all the transformations made on this model. No transformation is represented by an identity matrix, each of which is 0, and 1 is diagonal.
var identityMatrix = [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ]; var modelMatrix = identityMatrix.slice(0);
We are ready to carry out translations, rotation or scaling. The best way to apply it to a model is to create another mat4 that will represent our desired transformation, and then multiply the model matrix by the transformation matrix. Thus, we can make several transformations in the desired order with just a few operations with matrices, and now we do not need to worry about vertices.
So, let's create a translation matrix that will be the easiest. You start with the identity matrix, and you should only change the mat [12, 13, 14], which is x, y, z.
translate it into - let's say [0, 0, -5] - using this function
var translate = [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,-5.0, 1.0 ];
The transformation matrix is ββdone, and we want to multiply it by our model matrix. Animation matrices are a transformation, any transformation. The function for matrix multiplication that I have done right now. It should work, I hope :) A mathematical source from the wiki.
var mat4Multiply = (function () { function ABProduct(i, j, a, b) { var k, sum = 0; for (k = 0; k < 4; k++) { sum += a[i * 4 + k] * b[j + k * 4]; } return sum; } function mat4Multiply(a, b) { var i, j, product = []; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { product[i * 4 + j] = ABProduct(i, j, a, b); } } return product; } return mat4Multiply; })();
Just to demonstrate how to do multiplication, short code:
modelMatrix = mat4Multiply(modelMatrix, translate); // but now I change my mind and I want to move model again by another 3 in Z translate = [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 3.0, 1.0 ]; modelMatrix = mat4Multiply(modelMatrix, translate); // after this content of the modelMatrix is: // [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -2, 1]
Now we need to do only the final vertex transformation. As I said, translation is the simplest conversion, so I will only perform the translation function, this is a related wiki article .
Note
Do not confuse this picture: 
This was the view used in OpenGL, but later we decided to use a transposed version of the matrix, which is a simple operation in the following figure:

END NOTE
Translation Function:
function vec3TranslateMat4(vec, mat4) { vec[0] += mat4[12]; vec[1] += mat4[13]; vec[2] += mat4[14]; return vec; }
To translate the vertices of a triangle, you should only do:
var i, vertex; for(i=0;i<triangleVertices.length;i+=3) { vertex = triangleVertices.slice(i, i+3); vertex = vec3TranslateMat4(vertex, modelMatrix) triangleVertices[i] = vertex[0]; triangleVertices[i+1] = vertex[1]; triangleVertices[i+2] = vertex[2]; }
Once we have applied the transformation matrix, we must again set the model matrix to unity. After that, we can do new transformations, but on modified vertices.
And we are done. Personaly I use gl matrix library , which already contains all the operations for matrices and vectors that I need.
Transformations with and without matrices
All transformations could be done without matrices. But the matrix allows us to store the current location in one variable without the need to immediately convert the vertices. The vertex transformations are long and long.
Another good reason is that you can use only a few basic functions, mat4Multiply and vec3TransformMat4 (I did not show you this one). See how rotation and scale are integrated into the matrix (the left side of the image, the right side is the camera).
