本文介绍了将四元数旋转转换为旋转矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

  boost :: math :: quaternion< float>四元; 
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;

然后您可以创建您的矩阵:

<$ (
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之前进行转置。


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...

解决方案

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.

这篇关于将四元数旋转转换为旋转矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 05:23