PDA

View Full Version : How Implement Euler angle in opengl



Usman Ali
03-23-2010, 04:51 AM
How i implement euler angle in opengl for yaw pitch roll angle movements with position (x y z).

I implement following code but i am not sure its work fine

glTranslatef(x,y,z);
glRotatef(yaw, 0.1f, 0.0f, 0.0f);
glRotatef(pitch, 0.0f, 0.1f, 0.0f);
glRotatef(roll, 0.1f, 0.0f, 0.1f);

Please tell me which code implement for this movement or first rotate or first translate.

_______________________________________
www.ashscholar.com (http://www.ashscholar.com)

elFarto
03-23-2010, 05:58 AM
The usual way to do this is:

glRotatef(yaw, 1.0f, 0.0f, 0.0f);
glRotatef(pitch, 0.0f, 1.0f, 0.0f);
glRotatef(roll, 0.0f, 0.0f, 1.0f);
glTranslatef(x,y,z);

This rotates the object around its center, then translates it out to its position.

Also you need to use 1.0f as the value of the vector, not 0.1f.

Regards
elFarto

Usman Ali
03-23-2010, 06:36 AM
I want to implement by using this Attitude Matrix this give me accurate result

A three-by-three matrix containing the direction cosines of the sensor’s X axis in column one, the direction cosines of the sensor’s Y axis in column two, and the direction cosines of the sensor’s Z axis in column three. The order of the Euler angle rotation sequence is yaw, pitch, and roll.

X Dir Cosines__________Y Dir Cosines__________Z Dir Cosines

CA*CE_______________CA*SE*SR - SA*CR_________CA*SE*CR + SA*SR
SA*CE_______________CA*SE*SR - SA*CR_________SA*SE*CR – CA*SR
–SE_________________CE*SR____________________CE*CR

where:
CA = Cos (yaw)
CE = Cos (pitch)
CR = Cos (roll)
SA = Sin (yaw)
SE = Sin (pitch)
SR = Sin (roll)

please tell how i implement in opengl.
_________________________________________________
www.ashscholar.com/blog (http://www.ashscholar.com/blog)

V-man
03-23-2010, 07:34 AM
Compute that 3x3 matrix, and fill the other parts with 0 to make it a 4x4 matrix.
You will get
[ x x x 0]
[ x x x 0]
[ x x x 0]
[ 0 0 0 1]

and then do
glTranslatef(x, y, z); //then translate
glMultMatrixf(mymatrix); //rotate first

Usman Ali
03-23-2010, 08:28 AM
az1 =yaw / 57.28835;
el1 = pitch / 57.28835;
ro1 = roll / 57.28835;
m[16];
// Attitude matix of current orientation

m[0] = Math::Cos(az1)*Math::Cos(el1); // CA*CE;
m[1] = Math::Cos(az1)*Math::Sin(el1)*Math::Sin(ro1) - Math::Sin(az1)*Math::Cos(ro1); // CA*SE*SR - SA*CR
m[2] = Math::Cos(az1)*Math::Sin(el1)*Math::Cos(ro1) + Math::Sin(az1)*Math::Cos(ro1); // CA*SE*CR + SA*SR
m[3] = 0;

m[4] = Math::Sin(az1)*Math::Cos(el1); // SA*CE
m[5] = Math::Cos(az1)*Math::Cos(ro1) + Math::Sin(az1)*Math::Sin(el1)*Math::Sin(ro1); // CA*CR + SA*SE*SR
m[6] = Math::Sin(az1)*Math::Sin(el1)*Math::Cos(ro1) - Math::Cos(az1)*Math::Sin(ro1); // SA*SE*CR – CA*SR
m[7] = 0;

m[8] = -1*Math::Sin(el1); // -SE
m[9] = Math::Cos(el1)*Math::Sin(ro1); // CE*SR
m[10] = Math::Cos(el1)*Math::Sin(ro1); // CE*CR
m[11] = 0;

m[12] = 0;
m[13] = 0;
m[14] = 0;
m[15] = 1;
glMultMatrixf(m);



glTranslatef(x,y,z);

I implement this code kindly tell me this is correct code.any change required.

_____________________________
www.ashscholar.com (http://www.ashscholar.com)

V-man
03-23-2010, 08:44 AM
I think you need to transpose your matrix because GL takes column major matrices, not row major.

Usman Ali
03-23-2010, 09:20 AM
m[0] = Math::Cos(az1)*Math::Cos(el1); // CA*CE;
m[1] = Math::Sin(az1)*Math::Cos(el1); // SA*CE
m[2] = -Math::Sin(el1); // -SE
m[3] = 0;

m[4] = Math::Cos(az1)*Math::Sin(el1)*Math::Sin(ro1) - Math::Sin(az1)*Math::Cos(ro1); // CA*SE*SR - SA*CR
m[5] = Math::Cos(az1)*Math::Cos(ro1) + Math::Sin(az1)*Math::Sin(el1)*Math::Sin(ro1); // CA*CR + SA*SE*SR
m[6] = Math::Cos(el1)*Math::Sin(ro1); // CE*SR
m[7] = 0;

m[8] = Math::Cos(az1)*Math::Sin(el1)*Math::Cos(ro1) + Math::Sin(az1)*Math::Cos(ro1); // CA*SE*CR + SA*SR
m[9] = Math::Sin(az1)*Math::Sin(el1)*Math::Cos(ro1) - Math::Cos(az1)*Math::Sin(ro1); // SA*SE*CR – CA*SR
m[10] = Math::Cos(el1)*Math::Sin(ro1); // CE*CR
m[11] = 0;

m[12] = 0;
m[13] = 0;
m[14] = 0;
m[15] = 1;
glMultMatrixf(m);


now its correct i transpose it.please check it.

if i use following code instead of gltranslate its correct?

m[12] = x;
m[13] = y;
m[14] = z;
m[15] = 1;

MaxH
03-23-2010, 09:49 AM
It would probably be faster if you tried this stuff out instead of asking people to check it.

Usman Ali
03-23-2010, 11:29 PM
I test but i think main purpose of forums is those person who have this problem in future they can find correct solution from my post.

solysk
03-31-2010, 10:05 PM
Usman. You have an error in your rotation matrix. I'm not sure if it will work with the opengl axis either. To fix your rotation matrix change the line:
m[10] = Math::Cos(el1)*Math::Sin(ro1); // CE*CR
to:
m[10] = Math::Cos(el1)*Math::Cos(ro1); // CE*CR

I would consider using the pitch roll yaw convention found at the bottom of this page:
http://mathworld.wolfram.com/EulerAngles.html

To implement this in opengl: (I think this is right?)
Matrix4 roll = Matrix4.CreateFromAxisAngle(new Vector3(0, 0, 1), psi);
Matrix4 yaw = Matrix4.CreateFromAxisAngle(new Vector3(0, 1, 0), phi);
Matrix4 pitch = Matrix4.CreateFromAxisAngle(new Vector3(1, 0, 0), theta);
Matrix4 modelView = yaw * pitch * roll;