Glm translation matrix does not translate vector

I crossed a very simple error when I started using glm (in VS2010). I have this short code:

glm::mat4 translate = glm::translate(glm::mat4(1.f), glm::vec3(2.f, 0.f, 0.f)); glm::vec4 vector(1.f,1.f,1.f,0.f); glm::vec4 transformedVector = translate * vector; 

The result of the transformed Vector coincides with its original value (1.f, 1.f, 1.f, 0.f). I don’t know what is missing here. I tried the rotation matrix and it works fine, the point is correctly converted.

 glm::mat4 rotate = glm::rotate(glm::mat4(1.f), 90.f, glm::vec3(0.f, 0.f, 1.f)); glm::vec4 vector(1.f, 1.f, 1.f, 0.f); glm::vec4 transformedVector = rotate * vector; 

Ok, I figured out the problem. I would like to translate the vertex not into a vector, in this case I had to set the value of w to 1.

+7
c ++ glm-math
source share
1 answer

You forgot about projective coordinates. So the last component

 glm::vec4 vector 

should be 1. So the fix just does this:

 glm::mat4 translate = glm::translate(glm::mat4(1.f), glm::vec3(2.f, 0.f, 0.f)); glm::vec4 vector(1.f,1.f,1.f,1.f); glm::vec4 transformedVector = translate * vector; 

This is due to the way projective coordinates work, in essence, to get from projective coordinates (vec4) to normal coordinates (vec3), you divide the component by w. (What you cannot do if it is zero.)

The reason he works for rotation, but not for translations, is because in the projective space the rotations are β€œthe same” as in the normal space, but the translations are different.

I just noticed that you worked out your mistake, but I thought an explanation might help.

+19
source share

All Articles