我需要在局部坐标系中旋转对象,就像你可以在3dmax\maya中旋转它一样…
我当前的代码是:
ModelMatrix = glm::mat4(1.0f);
TransformMatrix = glm::mat4(1.0f);
ScaleMatrix = glm::mat4(1.0f);
RotateMatrix = glm::mat4(1.0f);
ScaleMatrix = glm::scale(ScaleMatrix, glm::vec3(scalex, scalez, scaley));
TransformMatrix = glm::translate(TransformMatrix, glm::vec3(x, z, y));
RotateMatrix = glm::rotate(RotateMatrix, anglex, glm::vec3(1, 0, 0));
RotateMatrix= glm::rotate(RotateMatrix, angley, glm::vec3(0, 0, 1));
RotateMatrix = glm::rotate(RotateMatrix, anglez, glm::vec3(0, 1, 0));
ModelMatrix = TransformMatrix * ScaleMatrix* RotateMatrix;
MVP = Projection * View * ModelMatrix ;anglex,y,z-来自键盘。
现在只有最后一个维度是局部的(我的例子是glm::vec3(0,1,0) Z轴)在这个IMAGE中,我显示了我需要的(2)和我已经得到的(3)……如果我改变"anglez“,它总是像滚动一样工作。但是anglex和angley在世界坐标系中。
我的第二个尝试-使用四元数:
quat MyQuaternion= glm::quat(cos(glm::radians(xangle / 2)), 0, sin(glm::radians(xangle / 2)), 0);
quat MyQuaternion2 = glm::quat(cos(glm::radians(yangle/ 2)), sin(glm::radians(yangle / 2)), 0, 0);
quat MyQuaternion3 = glm::quat(cos(glm::radians(zangle / 2)), 0,0,sin(glm::radians(zangle / 2)));
glm::mat4 RotationMatrix = toMat4(MyQuaternion*MyQuaternion2*MyQuaternion3);但我也得到了同样的结果
发布于 2015-10-08 22:30:01
您应该修改整个ModelMatrix,而不是角度。将ModelMatrix初始化为单位矩阵。然后,当您处理键盘输入时:
if(rotate about x-axis)
ModelMatrix = glm::rotate(ModelMatrix, angle, glm::vec3(1, 0, 0));
if(rotate about y-axis)
ModelMatrix = glm::rotate(ModelMatrix, angle, glm::vec3(0, 1, 0));
if(rotate about z-axis)
ModelMatrix = glm::rotate(ModelMatrix, angle, glm::vec3(0, 0, 1));
if(any rotation happened)
MVP = Projection * View * ModelMatrix ;您可以在任何级别执行此修改。MVP级别、ModelMatrix级别(如此处所示)或RotateMatrix级别。
https://stackoverflow.com/questions/33018407
复制相似问题