Nulle
Nulle

Reputation: 1329

OpenGL: mat4x4 multiplied with vec4 yields tvec<float>

Consider the code:

glm::mat4x4 T = glm::mat4x4(1);
glm::vec4 vrpExpanded;
vrpExpanded.x = this->vrp.x;
vrpExpanded.y = this->vrp.y;
vrpExpanded.z = this->vrp.z;
vrpExpanded.w = 1;

this->vieworientationmatrix =  T * (-vrpExpanded);

Why does T*(-vrpExpanded) yield a vector? According to my knowledge of linear algebra this should yield a mat4x4.

Upvotes: 1

Views: 666

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473407

According to my knowledge of linear algebra this should yield a mat4x4.

Then that's the problem.

According to linear algebra, a matrix can be multipled by a scalar (which does element-wise multiplication) or by another matrix. But even then, a matrix * matrix multiplication only works if the number of rows in the first matrix equals the number of columns in the second. And the resulting matrix is one which has the number of columns in the first and the number of rows in the second.

So if you have an AxB matrix and you multiply it with a CxD matrix, this only works if B and C are equal. And the result is an AxD matrix.

Multiplying a matrix by a vector means to pretend the vector is a matrix. So if you have a 4x4 matrix and you right-multiply it with a 4-element vector, this will only make sense if you treat that vector as a 4x1 matrix (since you cannot multiply a 4x4 matrix by a 1x4 matrix). And the result of a 4x4 matrix * a 4x1 matrix is... a 4x1 matrix.

AKA: a vector.

GLM is doing exactly what you asked.

Upvotes: 6

Related Questions