# How Implement Euler angle in opengl

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

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

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

CACE_______________CASESR - SACR_________CASECR + SASR
SA
CE_______________CASESR - SACR_________SASECR – CASR
–SE_________________CESR____________________CECR

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

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

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

I think you need to transpose your matrix because GL takes column major matrices, not row major.

m[0] = Math::Cos(az1)Math::Cos(el1); // CACE;
m[1] = Math::Sin(az1)Math::Cos(el1); // SACE
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); // CASESR - SACR
m[5] = Math::Cos(az1)Math::Cos(ro1) + Math::Sin(az1)Math::Sin(el1)Math::Sin(ro1); // CACR + SASESR
m[6] = Math::Cos(el1)Math::Sin(ro1); // CESR
m[7] = 0;

m[8] = Math::Cos(az1)Math::Sin(el1)Math::Cos(ro1) + Math::Sin(az1)Math::Cos(ro1); // CASECR + SASR
m[9] = Math::Sin(az1)Math::Sin(el1)Math::Cos(ro1) - Math::Cos(az1)Math::Sin(ro1); // SASECR – CASR
m[10] = Math::Cos(el1)Math::Sin(ro1); // CECR
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;

It would probably be faster if you tried this stuff out instead of asking people to check it.

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.

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); // CECR
to:
m[10] = Math::Cos(el1)Math::Cos(ro1); // CECR

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;