我想旋转一个立方体,所以首先我计算四元数,将其转换为旋转矩阵,然后将它与顶点相乘,是吗?
我用glm.hpp做了,但是当旋转轴不是(0,1,0)或(1,0,1)或(0,0,1)时,立方体的旋转非常奇怪。 / p>
代码是这样的:
glm::vec3 rotationAxis = glm::normalize((glm::cross(glm::vec3(0,1,1), glm::vec3(1,1,0))));
GLfloat cosAngle = glm::dot(glm::normalize(glm::vec3(0, 1, 1)), glm::normalize(glm::vec3(1, 1, 0)));
GLfloat s = glm::sqrt((1 + cosAngle) * 2);
GLfloat invs = 1 / s;
glm::quat rotationQuat = glm::quat(
s * 0.5f,
rotationAxis.x * invs,
rotationAxis.y * invs,
rotationAxis.z * invs);
glm::mat4 matTemp = glm::mat4_cast(rotationQuat);
ArcballMatrix = matTemp*ArcballMatrix;
并且顶点着色器是这样的:
gl_Position = projection * view * ArcballMatrix * vec4(vertexPosition, 1.0f);
我错过了什么吗?