简体   繁体   English

给定一个方向和一个 position 如何用 GLM 计算旋转矩阵

[英]Given a direction and a position how to calculate rotation matrix with GLM

I am tying get a model matrix calculated that can be passed on to shaders.我想得到一个可以传递给着色器的 model 矩阵。 But i cannot figure out how to calculate the rotation portion of the matrix.但我不知道如何计算矩阵的旋转部分。 I have two vectors: one a position and a facing direction one, here is the code:我有两个向量:一个是 position 和一个面向方向的向量,代码如下:

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;
}

I tried using std::atan2 to calculate component wise angles and apply those one after an other and glm::lookAt with a calculated up vector but I can't figure it out.我尝试使用 std::atan2 来计算组件角度,并一个接一个地应用这些角度,并使用 glm::lookAt 计算出向上的向量,但我无法弄清楚。

With euler you'll suffer.使用欧拉,您将受苦。 You neeed to use quaternions你需要使用四元数

http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions/ http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions/

You need to build your quaternion according to your rotation needs and then you can switch that back to mat4 with mat4 RotationMatrix = quaternion::toMat4(quaternion);您需要根据旋转需要构建四元数,然后可以使用 mat4 RotationMatrix = quaternion::toMat4(quaternion); 将其切换回 mat4 to achieve the full model matrix实现全model矩阵

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM