Euler rotation ==> Quaternion rotation

Blitz3D Forums/Blitz3D Programming/Euler rotation ==> Quaternion rotation

hi!
i think you heard of my software render lib. my problem now is that i'm using euler rotation and i dont know how to use the real rotation - i mean quaternion rotation...

this example shows pretty good what i mean
Graphics3D 800, 600, 32, 2
SetBuffer BackBuffer()

;Camera
Cam = CreateCamera()
RotateEntity CreateLight(), 70, 20, 0

;Cubes
Cube1 = CreateCube()
PositionEntity Cube1, -2, 0, 5
Cube2 = CreateCube()
PositionEntity Cube2, 2, 0, 5

SetFont LoadFont("Arial", 30, True)
While Not KeyHit(1)
   ;Cube 1 rotation (Euler; WRONG!)
   rot = rot + 2
   RotateEntity Cube1, rot, rot, rot
   ;Cube 2 rotation (Quaternion; RIGHT!)
   TurnEntity Cube2, 2, 2, 2
   RenderWorld
   Text 40, 100, "Euler rotation:"
   Text 40, 140, "(Like in my software renderer)"
   Text 500, 100, "Quaternion rotation:"
   Text 500, 140, "(I need that)"
   Rect 400, 0, 1, 600
   Flip
Wend
End


this is my matrix lib so far. it is fully working, but the euler rotation is just bothering me ;D
;My matrix library

Global vglMat_aa#, vglMat_ab#, vglMat_ac#
Global vglMat_ba#, vglMat_bb#, vglMat_bc#
Global vglMat_ca#, vglMat_cb#, vglMat_cc#
Global vglMat_da#, vglMat_db#, vglMat_dc#

Function vglMatrixIdentity()
vglMat_aa# = 1: vglMat_ab# = 0: vglMat_ac# = 0
vglMat_ba# = 0: vglMat_bb# = 1: vglMat_bc# = 0
vglMat_ca# = 0: vglMat_cb# = 0: vglMat_cc# = 1
vglMat_da# = 0: vglMat_db# = 0: vglMat_dc# = 0
End Function

Function vglMatrixTranslate(x#, y#, z#)
vglMat_da# = vglMat_da# + x#
vglMat_db# = vglMat_db# + y#
vglMat_dc# = vglMat_dc# + z#
End Function

This Function is what is euler-rotation And Not quaternion-rotation ===>


You must know, in the function below is
matrix rotation And matrix multiply all in one!


Function vglMatrixRotate(x#, y#, z#)
If x# <> 0 Then
	vglMat2_ab# = vglMat_ab#
	vglMat2_bb# = vglMat_bb#
	vglMat2_cb# = vglMat_cb#
	vglMat2_db# = vglMat_db#
	vglMat_ab# = vglMat_ab# * Cos(x#) + vglMat_ac# * -Sin(x#)
	vglMat_ac# = vglMat2_ab# * Sin(x#) + vglMat_ac# * Cos(x#)
	vglMat_bb# = vglMat_bb# * Cos(x#) + vglMat_bc# * -Sin(x#)
	vglMat_bc# = vglMat2_bb# * Sin(x#) + vglMat_bc# * Cos(x#)
	vglMat_cb# = vglMat_cb# * Cos(x#) + vglMat_cc# * -Sin(x#)
	vglMat_cc# = vglMat2_cb# * Sin(x#) + vglMat_cc# * Cos(x#)
	vglMat_db# = vglMat_db# * Cos(x#) + vglMat_dc# * -Sin(x#)
	vglMat_dc# = vglMat2_db# * Sin(x#) + vglMat_dc# * Cos(x#)
EndIf
If y# <> 0 Then
	vglMat2_aa# = vglMat_aa#
	vglMat2_ba# = vglMat_ba#
	vglMat2_ca# = vglMat_ca#
	vglMat2_da# = vglMat_da#
	vglMat_aa# = vglMat_aa# * Cos(y#) + vglMat_ac# * Sin(y#)
	vglMat_ac# = vglMat2_aa# * -Sin(y#) + vglMat_ac# * Cos(y#)
	vglMat_ba# = vglMat_ba# * Cos(y#) + vglMat_bc# * Sin(y#)
	vglMat_bc# = vglMat2_ba# * -Sin(y#) + vglMat_bc# * Cos(y#)
	vglMat_ca# = vglMat_ca# * Cos(y#) + vglMat_cc# * Sin(y#)
	vglMat_cc# = vglMat2_ca# * -Sin(y#) + vglMat_cc# * Cos(y#)
	vglMat_da# = vglMat_da# * Cos(y#) + vglMat_dc# * Sin(y#)
	vglMat_dc# = vglMat2_da# * -Sin(y#) + vglMat_dc# * Cos(y#)
EndIf
If z# <> 0 Then
	vglMat2_aa# = vglMat_aa#
	vglMat2_ba# = vglMat_ba#
	vglMat2_ca# = vglMat_ca#
	vglMat2_da# = vglMat_da#
	vglMat_aa# = vglMat_aa# * Cos(z#) + vglMat_ab# * -Sin(z#)
	vglMat_ab# = vglMat2_aa# * Sin(z#) + vglMat_ab# * Cos(z#)
	vglMat_ba# = vglMat_ba# * Cos(z#) + vglMat_bb# * -Sin(z#)
	vglMat_bb# = vglMat2_ba# * Sin(z#) + vglMat_bb# * Cos(z#)
	vglMat_ca# = vglMat_ca# * Cos(z#) + vglMat_cb# * -Sin(z#)
	vglMat_cb# = vglMat2_ca# * Sin(z#) + vglMat_cb# * Cos(z#)
	vglMat_da# = vglMat_da# * Cos(z#) + vglMat_db# * -Sin(z#)
	vglMat_db# = vglMat2_da# * Sin(z#) + vglMat_db# * Cos(z#)
EndIf
End Function

Function vglMatrixScale(x#, y#, z#)
vglMat_aa# = vglMat_aa# * x#
vglMat_ab# = vglMat_ab# * y#
vglMat_ac# = vglMat_ac# * z#
vglMat_ba# = vglMat_ba# * x#
vglMat_bb# = vglMat_bb# * y#
vglMat_bc# = vglMat_bc# * z#
vglMat_ca# = vglMat_ca# * x#
vglMat_cb# = vglMat_cb# * y#
vglMat_cc# = vglMat_cc# * z#
vglMat_da# = vglMat_da# * x#
vglMat_db# = vglMat_db# * y#
vglMat_dc# = vglMat_dc# * z#
End Function


thank you for any help :)

I'm not sure if it helps, but for my saveanimmesh routine, i needed to convert euler angles to quaternions. After a while, I learnt that Blitz has it's own system for quats, and with the help of a routine by LeadWerks, I could convert from p/y/r to w/x/y/z:
Function RotationToString$(iPitch#, iYaw#, iRoll#)

	;this code was written by LeadWerks
	;http://www.blitzbasic.com/Community/posts.php?topic=51579
	sp# = Sin(iYaw   / 2)
	cp# = Cos(iYaw   / 2)
	sy# = Sin(iRoll  / 2)
	cy# = Cos(iRoll  / 2)
	sr# = Sin(iPitch / 2)
	cr# = Cos(iPitch / 2)
	
	w# = + (cr * cp * cy - sr * sp * sy)
	x# = - (sr * cp * cy - cr * sp * sy)
	y# = + (cr * sp * cy + sr * cp * sy)
	z# = - (sr * sp * cy + cr * cp * sy)

	Return w + "," + x + "," + y + "," + z + ";;"
	
End Function


hmh. i dont know what that is, but i tried to implement this in a few ways, it might work. but it aint...
sorry

;this code was written by LeadWerks

That should read: This code was copied from other people's code by Leadwerks.

Hehe.

hm. whatever
there must be a very simple formula which converts euler to quaternion angles.

But quaternions need 4 parameters, right ? That is what this function does, it converts the 3 parameter pitch/yaw/roll rotation to a 4 parameter quaternion.
The problem you're having it a bit above my head. I understand what the trouble is, but I'm not sure how to solve it.
It might be related to the 'gimbal lock'. I've had some problems with that when reading angles from a rotated object, and I found some code that solves that. But that is the reverse of your problem.
After a search on gimbal lock, I found this:
http://www.dscho.co.uk/blitz/tutorials/quaternions.shtml
(there is a link for quat.bb) I hope it helps.

this code there simply rotates a cube. what i wanted to know is how to do this with simple vectors and matrices...

Have you seen this page? First one that came up on Google. Seems to explain how to do Euler to Quat.

hm. it seems like i have to do this the hard way - search till my fingers bleed.
thank you guys anyway:)

Doesn't that article tell you what you need, then?

I think he require a code for copy&paste :/

i did not ask for a someone who gets me tons of code...
i'm rather looking for something that can help me out. i think this article offers a lot of good stuff.