user4959035
user4959035

Reputation:

How to rotate object using the 3D graphics pipeline ( Direct3D/GL )?

I have some problems with trying to animate the rotation of mesh objects.

If to make the rotation process once all is fine. Meshes are rotated normally and the final image from the WebGL buffer looks pretty fine.

http://s22.postimg.org/nmzvt9zzl/311.png

But if to use the rotation in loop (with each new frame record) then the meshes are starting to look very weird, look the next screenshot:

http://s22.postimg.org/j2dpecga9/312.png

I won't provide here the programming code, because the issue is depend on incorrect 3D graphics handling.

I think some OpenGL/Direct3D developers may give an advice how to fix it, because this question relates to the 3D-programming subject and to the some specific GL or D3D function/method. Also I think the way of work with rotation is the same both in OpenGL and Direct3D because of linear algebra and affine affine transformations.

If you are really interested what I'm using, so the answer is WebGL.

Let me describe how do I use the rotation of object.

The simple rotation is made using the quaternions. Any mesh object I define has its quaternion property.

If to rotate the object then the method rotate() is doing the next:

// Some kind of pseudo-code
function rotateMesh( vector, angle ) {
    var tempQuatenion = math.convertRotationToQuaternion( vector, angle );
    this.quaternion = math.multiplyQuaternions( this.quaternion, tempQuatenion );
}

I use the next piece of code in Renderer class to handle the mesh translation and rotation:

// Some kind of pseudo-code
// for each mesh, which is added to scene
modelViewMatrix = new IdentityMatrix()
translateMatrixByVector( modelViewMatrix, mesh.position )
modelViewMatrix.multiplyByMatrix( mesh.quaternion.toMatrix() )

So... I want to ask you if the logic above is correct then I provide some sources of math functions which are used for quaternions, rotations, etc...

If the logic above is incorrect so I think it makes no sense to provide something else. Because it's required to fix the main logic.

Upvotes: 0

Views: 279

Answers (1)

user3529622
user3529622

Reputation:

Quaternion multiplication is not commutative, i.e., if A and B are quaternions then A * B != B * A. If you want to rotate quaternion A by quaternion B, you need to do A = B * A, so this:

    this.quaternion = math.multiplyQuaternions( this.quaternion, tempQuatenion );

should have its arguments reversed.

In addition, as mentioned by @ratchet-freak in comments, you should make sure your quaternions are always normalized, otherwise transformations other than rotation may happen.

Upvotes: 1

Related Questions