将四元数旋转转换为旋转矩阵?

发布于 2024-08-07 10:24:25 字数 112 浏览 6 评论 0原文

基本上,给定一个四元数(qx,qy,qz,qw)...我如何将其转换为OpenGL旋转矩阵?我也对哪个矩阵行是“向上”、“向右”、“向前”等感兴趣......我有一个四元数的相机旋转,我需要在向量中......

Basically, given a quaterion (qx, qy, qz, qw)... How can i convert that to an OpenGL rotation matrix? I'm also interested in which matrix row is "Up", "Right", "Forward" etc... I have a camera rotation in quaternion that I need in vectors...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神回复 2024-08-14 10:24:25

以下代码基于四元数 (qw, qx, qy, qz),其中顺序基于 Boost 四元数:

boost::math::quaternion<float> quaternion;
float qw = quaternion.R_component_1();
float qx = quaternion.R_component_2();
float qy = quaternion.R_component_3();
float qz = quaternion.R_component_4();

首先,您必须标准化四元数:

const float n = 1.0f/sqrt(qx*qx+qy*qy+qz*qz+qw*qw);
qx *= n;
qy *= n;
qz *= n;
qw *= n;

然后您可以创建矩阵:

Matrix<float, 4>(
    1.0f - 2.0f*qy*qy - 2.0f*qz*qz, 2.0f*qx*qy - 2.0f*qz*qw, 2.0f*qx*qz + 2.0f*qy*qw, 0.0f,
    2.0f*qx*qy + 2.0f*qz*qw, 1.0f - 2.0f*qx*qx - 2.0f*qz*qz, 2.0f*qy*qz - 2.0f*qx*qw, 0.0f,
    2.0f*qx*qz - 2.0f*qy*qw, 2.0f*qy*qz + 2.0f*qx*qw, 1.0f - 2.0f*qx*qx - 2.0f*qy*qy, 0.0f,
    0.0f, 0.0f, 0.0f, 1.0f);

根据您的矩阵类,您可以在将其传递给 OpenGL 之前可能必须对其进行转置。

The following code is based on a quaternion (qw, qx, qy, qz), where the order is based on the Boost quaternions:

boost::math::quaternion<float> quaternion;
float qw = quaternion.R_component_1();
float qx = quaternion.R_component_2();
float qy = quaternion.R_component_3();
float qz = quaternion.R_component_4();

First you have to normalize the quaternion:

const float n = 1.0f/sqrt(qx*qx+qy*qy+qz*qz+qw*qw);
qx *= n;
qy *= n;
qz *= n;
qw *= n;

Then you can create your matrix:

Matrix<float, 4>(
    1.0f - 2.0f*qy*qy - 2.0f*qz*qz, 2.0f*qx*qy - 2.0f*qz*qw, 2.0f*qx*qz + 2.0f*qy*qw, 0.0f,
    2.0f*qx*qy + 2.0f*qz*qw, 1.0f - 2.0f*qx*qx - 2.0f*qz*qz, 2.0f*qy*qz - 2.0f*qx*qw, 0.0f,
    2.0f*qx*qz - 2.0f*qy*qw, 2.0f*qy*qz + 2.0f*qx*qw, 1.0f - 2.0f*qx*qx - 2.0f*qy*qy, 0.0f,
    0.0f, 0.0f, 0.0f, 1.0f);

Depending on your matrix class, you might have to transpose it before passing it to OpenGL.

柏林苍穹下 2024-08-14 10:24:25

一种非常容易可视化的方法是将四元数指定的旋转应用于基向量 (1,0,0)、(0,1,0) 和 (0,0,1) 。旋转后的值
给出旋转系统中相对于原始系统的基向量。使用这些
向量来形成旋转矩阵的行。得到的矩阵及其转置,
表示原系统与系统之间的正变换和逆变换
旋转系统。

我不熟悉OpenGL使用的约定,所以也许其他人可以回答
你问题的那部分...

One way to do it, which is pretty easy to visualize, is to apply the rotation specified by your quaternion to the basis vectors (1,0,0), (0,1,0), and (0,0,1). The rotated values
give the basis vectors in the rotated system relative to the original system. Use these
vectors to form the rows of the rotation matrix. The resulting matrix, and its transpose,
represent the forward and inverse transformations between the original system and the
rotated system.

I'm not familiar with the conventions used by OpenGL, so maybe someone else can answer
that part of your question...

寻找一个思念的角度 2024-08-14 10:24:25

您可能根本不需要处理旋转矩阵。这是一种比转换为矩阵并与向量相乘更快的方法:

  // move vector to camera position co (before or after rotation depending on the goal)
  v -= co;

  // rotate vector v by quaternion q; see info [1]
  vec3 t = 2 * cross(q.xyz, v);
  v = v + q.w * t + cross(q.xyz, t);

[1]

You might not have to deal with a rotation matrix at all. Here is a way that appears to be faster than converting to a matrix and multiplying a vector with it:

  // move vector to camera position co (before or after rotation depending on the goal)
  v -= co;

  // rotate vector v by quaternion q; see info [1]
  vec3 t = 2 * cross(q.xyz, v);
  v = v + q.w * t + cross(q.xyz, t);

[1] http://mollyrocket.com/forums/viewtopic.php?t=833&sid=3a84e00a70ccb046cfc87ac39881a3d0

会傲 2024-08-14 10:24:25

使用 glm,您可以简单地使用转换运算符。
因此,要将matrix4转换为四元数,只需编写

glm::mat4_cast(quaternion_name)

using glm, you can simply use a casting operator.
so to convert from a matrix4 to quaternion, simply write

glm::mat4_cast(quaternion_name)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文