Convert transformation matrix to quaternion and vice versa

I try to save my rotation matrix as a quaternion, and then when I want to use it for transformation, translate it back. I use the glm library which provides mat4_cast and quat_cast for them. However, when I execute the following code:

glm::mat4 origTest = glm::lookAt(position, lookAtPt, up);
glm::quat quatTest = glm::quat_cast(origTest);
glm::mat4 mat4Test = glm::mat4_cast(quatTest);

I get different values ​​for origTest and mat4Test. Am I missing something? position, lookAtPt and higher - glm :: vec3.

+4
source share
1 answer

. . , , glm:: lookAt(), . , SRT . , , "lookAt", , (lookAtPt - position)

/* Mat4 to Quat */
glm::vec3 lookAtPt = direction;
glm::mat4 rotMatrix = glm::lookAt(glm::vec3(0), lookAtPt, up);
glm::quat rotation = glm::quat_cast(rotMatrix);

/* Quat to Mat4 */    
glm::mat4 identityMat = glm::mat4(1.0f);
glm::mat4 rotMatrix = glm::mat4_cast(rotation);   //rotation is glm::quat
glm::mat4 transMatrix = glm::translate(identityMat, position); 
glm::mat4 viewMatrix = rotMatrix * glm::inverse(transMatrix);
+6

Source: https://habr.com/ru/post/1538367/


All Articles