toji / gl-matrix

Javascript Matrix and Vector library for High Performance WebGL apps
glmatrix.net
MIT License
5.38k stars 722 forks source link

Issue with mat4.translate . #344

Closed srikrishna0130 closed 5 years ago

srikrishna0130 commented 5 years ago

I think the mat4.translate function dosen't work as intended. I performed the following operations : mat4.translate(modelviewmatrix,modelviewmatrix,translate); and, mat4.scale(modelviewmatrix,modelviewmatrix,scale); mat4.translate(modelviewmatrix,modelviewmatrix,translate);

To achieve the translation observed in code 1 i needed to translate the second one by scale*translate in the code 2.

I then had a look at the source code ` if (a === out) {

out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];

out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];

out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];

out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; 

} `

To make the code work as intended i think this part needs a change to , ` if (a === out) {

out[12] = x + a[12];

out[13] = y + a[13];

out[14] = z + a[14];

out[15] = a[15];

}`

Sorry for any error i made...

stefnotch commented 5 years ago

Great question! I dusted off my rusty matrix mathematics and decided to take a stab at answering it.

Let's start off by making sure that we are on the same page here. A 4x4 transformation matrix looks like this: image

And, the order of operations in a single matrix is the following in gl-matrix:

In other words, if you translate a matrix by a vector, gl-matrix first scales and rotates the vector.

And this is exactly what is happening here image

I hope that I explained it in an understandable manner. If you have any further questions, feel free to ask.

stefnotch commented 5 years ago

Issue closed because gl-matrix is doing the "right" thing, at least according to the usual WebGL/OpenGL conventions.