GLM - 四元数和旋转

时间:2016-09-03 12:52:44

标签: c++ rotation glm-math

我尝试使用四元数来旋转程序中的对象。

但他们的问题是应用......轮换。 我试图以这种方式旋转矩阵:

 ModelMatrix = ModelMatrix * glm::toMat4(glm::quat(RotationW, RotationX, RotationY, RotationZ));

随机数,结果是一个巨大的缩放(为什么)和一点点旋转。

另外我们可以说:

quat3 = quat2 * quat1
Model1 = Model * glm::toMat4(glm::quat( quat3.w , quat3.x, quat3.y, quat3.z);

Model2 = Model * glm::toMat4(glm::quat( quat1.w , quat1.x, quat1.y, quat1.z);
Model2 = Model2 * glm::toMat4(glm::quat( quat2.w , quat2.x, quat2.y, quat2.z); 

Model2和Model1有相同的旋转吗?

我知道glm :: rotate(Model,RotationAngle,RotationAxis),但我更喜欢使用四元数

1 个答案:

答案 0 :(得分:3)

为了使四元数表示纯旋转,它必须是单位四元数。随机数通常没有此属性。您可能希望在生成四元数之前对它们进行规范化:

l = sqrt(RotationW * RotationW + RotationX * RotationX
         + RotationY * RotationY + RotationZ * RotationZ);
RotationW /= l;
RotationX /= l;
...

当然,仅使用四元数将它们转换为矩阵没有任何意义。直接计算矩阵将更有效。

对于四元数产品,您的顺序错误:

q3 = q2 * q1
=> M * mat(q3) = M * mat(q2) * mat(q1)