opengl矩阵旋转四元数 [英] opengl matrix rotation quaternions
问题描述
我正在尝试使多维数据集绕x和y轴进行简单旋转:
Im trying to do a simple rotation of a cube about the x and y axis:
我想始终在x轴上旋转多维数据集x 并将多维数据集在y轴上旋转y,与x轴旋转无关
I want to always rotate the cube over the x axis by an amount x and rotate the cube over the yaxis by an amount y independent of the x axis rotation
首先我天真地做了:
glRotatef(x,1,0,0);
glRotatef(y,0,1,0);
然后
但是首先旋转x然后旋转y 我想独立于x访问旋转y.
but that first rotates over x then rotates over y i want to rotate over the y independently of the x access.
我开始研究四元数,所以我尝试了:
I started looking into quaternions, so i tried :
Quaternion Rotation1;
Rotation1.createFromAxisAngle(0,1, 0, globalRotateY);
Rotation1.normalize();
Quaternion Rotation2;
Rotation2.createFromAxisAngle(1,0, 0, globalRotateX);
Rotation2.normalize();
GLfloat Matrix[16];
Quaternion q=Rotation2 * Rotation1;
q.createMatrix(Matrix);
glMultMatrixf(Matrix);
这几乎完全可以完成连续2次glRotates的工作...所以我认为我错过了第2步.
that just does almost exactly what was accomplished doing 2 consecutive glRotates ...so i think im missing a step or 2.
四元数是走的路还是应该使用其他方式?并且,如果四元数是解决问题的方法,我可以添加哪些步骤来使多维数据集独立于每个轴旋转. 我认为其他人也有同样的问题: 在2个轴上旋转OpenGL场景
is quaternions the way to go or should i be using something different? AND if quaternions are the way to go what steps can i add to make the cube rotate independently of each axis. i think someone else has the same issue: Rotating OpenGL scene in 2 axes
推荐答案
我使用四元数使它能够正常工作:我确定还有其他方法,但是经过一些重新安装后,这对我来说非常合适.我在另一个论坛上发布了类似版本. http://www.opengl.org/discussion_boards /ubbthreads.php?ubb=showflat&Number=280859&#Post280859
I got this to work correctly using quaternions: Im sure there are other ways, but afeter some reseatch , this worked perfectly for me. I posted a similar version on another forum. http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=280859&#Post280859
首先创建变化角x/y的四元数表示 然后每帧将变化角度的四元数乘以一个累加的四元数,然后最后将该四元数转换为矩阵形式以乘以当前矩阵.这是循环的主要代码:
first create the quaternion representation of the angles of change x/y then each frame multiply the changing angles quaternions to an accumulating quaternion , then finally convert that quaternion to matrix form to multiply the current matrix. Here is the main code of the loop:
Quaternion3D Rotation1=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(-1.0f,0,0), DEGREES_TO_RADIANS(globalRotateX));
Quaternion3DNormalize(&Rotation1);
Quaternion3D Rotation2=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(0.0f,-1.0f,0), DEGREES_TO_RADIANS(globalRotateY));
Quaternion3DNormalize(&Rotation2);
Matrix3D Mat;
Matrix3DSetIdentity(Mat);
Quaternion3DMultiply(&QAccum, &Rotation1);
Quaternion3DMultiply(&QAccum, &Rotation2);
Matrix3DSetUsingQuaternion3D(Mat, QAccum);
globalRotateX=0;
globalRotateY=0;
glMultMatrixf(Mat);
然后绘制立方体
这篇关于opengl矩阵旋转四元数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!