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.

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.

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?

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

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.

How to do this ?, simple, for each point ML(k) (a point in the set) you do:

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 * (ML(k)-p) * -q + p

That should work.

链接地址: http://www.djcxy.com/p/81762.html

上一篇: 四元数正态化期间保持旋转

下一篇: OpenGL,GLM四元数旋转