minpu
minpu

Reputation: 61

Why does glm give a wrong result when transformating a 3D vector?

glm::vec4 x = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f);
glm::vec4 y = glm::vec4(0.0f, 1.0f, 0.0f, 0.0f);
glm::vec4 z = glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f);
glm::vec4 t(0.0f, 0.0f, 0.0f, 1.0f);

glm::mat4 rot(x, y, z, t);
glm::vec4 test = rot * glm::vec4(10.0f, 0.0f, 0.0f, 1.0f);

The "test" varible should be (0.0f, 0.0f, -10.0f, 1.0f) but the returned result is (0.0f, 0.0f, 10.0f, 1.0f). Is this a bug of glm?

Upvotes: 0

Views: 167

Answers (2)

odyss-jii
odyss-jii

Reputation: 2699

I suppose you are assuming that the four vectors x, y, z, and t are "row vectors", but it appears that they are "column vectors". Therefore, the matrix rot is most likely:

x  y  z  t
__________
0  0 -1  0
0  1  0  0 
1  0  0  0
0  0  0  1

Which when multiplied with column vector:

10
 0
 0
 1

Gives column vector:

 0
 0
10
 1

EDIT: Fixed inverted terminology

Upvotes: 1

Joseph Artsimovich
Joseph Artsimovich

Reputation: 1519

Wouldn't the actual result be (0, 0, 10, 2)? Essentially, matrix times a vector means a linear combination of matrix columns with coefficients from a vector. So, in your example

rot * glm::vec4(10.0f, 0.0f, 0.0f, 1.0f)

is equivalent to

10.0f * x + 0.0f * y + 0.0f * z + 1.0f * t

Upvotes: 0

Related Questions