将四元数旋转转换为旋转矩阵?
基本上,给定一个四元数(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下代码基于四元数 (qw, qx, qy, qz),其中顺序基于 Boost 四元数:
首先,您必须标准化四元数:
然后您可以创建矩阵:
根据您的矩阵类,您可以在将其传递给 OpenGL 之前可能必须对其进行转置。
The following code is based on a quaternion (qw, qx, qy, qz), where the order is based on the Boost quaternions:
First you have to normalize the quaternion:
Then you can create your matrix:
Depending on your matrix class, you might have to transpose it before passing it to OpenGL.
一种非常容易可视化的方法是将四元数指定的旋转应用于基向量 (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...
您可能根本不需要处理旋转矩阵。这是一种比转换为矩阵并与向量相乘更快的方法:
[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:
[1] http://mollyrocket.com/forums/viewtopic.php?t=833&sid=3a84e00a70ccb046cfc87ac39881a3d0
使用 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)