我正在绑一个计算得到的模型矩阵,该模型矩阵可以传递给着色器。但是我不知道如何计算矩阵的旋转部分。我有两个向量:一个是位置,一个是朝向方向,这是代码:
glm::mat4x4 CalcModelMatrix( const glm::vec3& position, const glm::vec3& orientation )
{
glm::mat4x4 model( 1.f );
// displacement
model = glm::translate( model, position );
// rotation
// ...
// scaling
model = glm::scale( model, glm::vec3( 1.f ) );
return model;
}
[我尝试使用std :: atan2计算组件明智的角度,然后逐个应用那些角度,然后使用glm :: lookAt和计算出的向上矢量应用这些角度,但我无法弄清楚。
使用欧拉,您会受苦。您需要使用四元数
http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions/
您需要根据旋转需求构建四元数,然后可以使用mat4 RotationMatrix = quaternion :: toMat4(quaternion)将其切换回mat4。以获得完整的模型矩阵