简体   繁体   English

OpenGL,GLM四元数旋转

[英]OpenGL, GLM Quaternion Rotations

I'm updating an old OpenGL project and I'm switching all the (deprecated) glMatrix() functions for matrices and quaternions, and I'm having trouble getting the rotation working. 我正在更新一个旧的OpenGL项目,并切换了所有(不推荐使用的) glMatrix()函数用于矩阵和四元数,并且在使旋转起作用时遇到了麻烦。

My drawing looks like this: 我的图看起来像这样:

//these two are supposedly working
mat4 mProjection = perspective(FOV, aspectRatio, near, far);
mat4 mView = lookAt(cameraPosition, cameraCenter, headsUp);

mat4 mModel = mat4(1.0f);
mat4 mMVP = mProjection * mView * mModel;

What I'm trying to do now is to apply rotation to an object around a specific point (like the object's center). 我现在想要做的是将旋转应用于围绕特定点(如对象中心)的对象。 I tried: 我试过了:

mat4 mModelRotation = rotate(mModel, object->RotationY(), vec3(0.0, 1.0, 0.0)); //RotationY being an angle in degrees
mat4 mMVP = mProjection * mView * mModel * mModelRotation;

But this causes the object to rotate around one of it's edges, not it's center. 但这会导致对象绕其边缘之一而不是中心旋转。

I'd like to know how can I apply Quaternions to rotate the object around any point I pass as parameter for example. 我想知道如何应用四元数来围绕对象传递的任何点(例如,作为参数传递)旋转对象。

I'm unexperienced with matrices, since I avoided them because I could use the glMatrix() functions before, so I don't understand much about the relation between the spatial position and them, and trying to update them to Quaternions is looking even more complicated. 我对矩阵没有经验,因为我以前可以使用glMatrix()函数,所以避免了它们,因此我对空间位置和它们之间的关系了解得不多,而尝试将它们更新为四元数看起来就更多了复杂。

I've read about the logic of Quaternions and how to use them, technically, but I don't understand where their values comes from. 我已经从技术上了解了四元数的逻辑以及如何使用它们,但是我不知道它们的值从何而来。

For example: 例如:

//axis is a unit vector
local_rotation.w  = cosf( fAngle/2)
local_rotation.x = axis.x * sinf( fAngle/2 )
local_rotation.y = axis.y * sinf( fAngle/2 )
local_rotation.z = axis.z * sinf( fAngle/2 )
total = local_rotation * total

I read this, and I have no clue what these values are. 我读了这篇文章,不知道这些值是什么。 Axis is a unit vector... of what? 轴是单位矢量...是什么? fAngle I assume it's the angle I want to rotate, but since Quaternions use an arbitrary axis, how do I get the value for each of the XYZ axis, and how do I specify it in the Quaternion? fAngle我假定是我要旋转的角度,但是由于四元数使用任意轴,因此如何获取每个XYZ轴的值,以及如何在四元数中指定它?

So, I'm looking for any practical example/tutorial of a Quaternion, so I can understand what's going on. 因此,我正在寻找四元数的任何实际示例/教程,以便我了解发生了什么。

The only information I have when I want to rotate an object is the axis I want to rotate (x, y OR z, not all of them, but a combination of them in the final result), and a value in degrees. 当我要旋转对象时,我唯一拥有的信息就是我想旋转的轴(x,y或z,不是全部,而是最终结果中的组合),以及度数。 I'm not much of a math person, so any tutorial that doesn't use shortcuts is highly appreciated. 我不是一个数学家,所以任何不使用快捷方式的教程都将受到高度赞赏。

OK, let say that you have a model ML (set of points of a model) and a point P to rotate that ML . 好的,假设您有一个模型ML (模型的点集)和一个要旋转ML的点P

All the rotations are referred at to the origin, so you need to move the set of points ML taking the point P as the origin, make the rotation of all the points and then move it back. 所有旋转均以原点为参考,因此您需要移动以点P为原点的点集ML ,使所有点旋转,然后再移回原点。

How to do this ?, simple, for each point ML(k) (a point in the set) you do: 简单地,对于每个点ML(k) (集合中的一个点),您都可以这样做:

ML(k)-P --> with this you move the points, using the P point as origin

then rotate: 然后旋转:

ROT * (ML(k)-P)

and finally, you move it back: 最后,将其移回:

ROT(ML(k)-P) + P

As quaternions, you replace the matrix mult by q and -q 作为四元数,您用q-q替换矩阵倍数

q * (ML(k)-p) * -q + p

That should work. 那应该工作。

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

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