macke_

06-27-2010, 04:05 PM

Hi!

I'm having some problems doing sequential rotations in OpenGL. My goal is to show the orientation of an aircraft using it's pitch/roll/yaw values. Since it's shown in a coordinate system I can't just move the view so I have to rotate the model.

The problem is that the following doesn't work (for this application) since after the first (i.e last call to glRotatef) the second rotation isn't around the "new" X axis:

glRotatef(roll, 1.0, 0.0, 0.0);

glRotatef(pitch, 0.0, 1.0, 0.0);

After looking around I have found some hints on solutions, but I can't seem to get the hang of it.

I'm using pyopengl (OpenGL Python bindings) and here are some off the things that I tested:

Using quaternions (which in my case produces the same result as sequential calls to glRotatef):

p = 0 * (math.pi/180) / 2.0;

y = aircraft_pitch * (math.pi/180) / 2.0;

r = aircraft_roll * (math.pi/180) / 2.0;

sinp = math.sin(p);

siny = math.sin(y);

sinr = math.sin(r);

cosp = math.cos(p);

cosy = math.cos(y);

cosr = math.cos(r);

x = sinr * cosp * cosy - cosr * sinp * siny;

y = cosr * sinp * cosy + sinr * cosp * siny;

z = cosr * cosp * siny - sinr * sinp * cosy;

w = cosr * cosp * cosy + sinr * sinp * siny;

T = [ 1 - 2*y*y - 2*z*z, 2*x*y - 2*w*z, 2*x*z + 2*w*y, 0,

2*x*y + 2*w*z, 1 - 2*x*x - 2*z*z, 2*y*z - 2*w*x, 0,

2*x*z - 2*w*y, 2*y*z + 2*w*x, 1 - 2*x*x - 2*y*y ,0,

0,0,0,0]

glMultMatrixf(T)

I've also tried to using the current ModelView matrix as input for the axis to do the rotation around (which causes the rotation to be slightly crooked):

mvm = glGetFloatv(GL_MODELVIEW_MATRIX)

norm = math.sqrt(mvm[0][0]*mvm[0][0] + mvm[0][1]*mvm[0][1] + mvm[0][2]*mvm[0][2])

glRotatef(aircraft_roll, mvm[0][0]/norm, mvm[0][1]/norm, mvm[0][2]/norm) #Roll

mvm = glGetFloatv(GL_MODELVIEW_MATRIX)

norm = math.sqrt(mvm[2][0]*mvm[2][0] + mvm[2][1]*mvm[2][1] + mvm[2][2]*mvm[2][2])

glRotatef(aircraft_pitch, mvm[2][0]/norm, mvm[2][1]/norm, mvm[2][2]/norm) # Pitch

I'm using the following setup:

glShadeModel(GL_SMOOTH)

glClearColor(0.0, 0.0, 0.0, 0.0)

glClearDepth(1.0)

glEnable(GL_DEPTH_TEST)

glDepthFunc(GL_LEQUAL)

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

glViewport(0, 0, width, height)

glMatrixMode(GL_PROJECTION)

glLoadIdentity()

gluPerspective(45, 1.0*width/height, 0.1, 100.0)

glMatrixMode(GL_MODELVIEW)

glLoadIdentity()

I would really appreciate some help here since I'm pretty stuck at the moment...

Thanks!

I'm having some problems doing sequential rotations in OpenGL. My goal is to show the orientation of an aircraft using it's pitch/roll/yaw values. Since it's shown in a coordinate system I can't just move the view so I have to rotate the model.

The problem is that the following doesn't work (for this application) since after the first (i.e last call to glRotatef) the second rotation isn't around the "new" X axis:

glRotatef(roll, 1.0, 0.0, 0.0);

glRotatef(pitch, 0.0, 1.0, 0.0);

After looking around I have found some hints on solutions, but I can't seem to get the hang of it.

I'm using pyopengl (OpenGL Python bindings) and here are some off the things that I tested:

Using quaternions (which in my case produces the same result as sequential calls to glRotatef):

p = 0 * (math.pi/180) / 2.0;

y = aircraft_pitch * (math.pi/180) / 2.0;

r = aircraft_roll * (math.pi/180) / 2.0;

sinp = math.sin(p);

siny = math.sin(y);

sinr = math.sin(r);

cosp = math.cos(p);

cosy = math.cos(y);

cosr = math.cos(r);

x = sinr * cosp * cosy - cosr * sinp * siny;

y = cosr * sinp * cosy + sinr * cosp * siny;

z = cosr * cosp * siny - sinr * sinp * cosy;

w = cosr * cosp * cosy + sinr * sinp * siny;

T = [ 1 - 2*y*y - 2*z*z, 2*x*y - 2*w*z, 2*x*z + 2*w*y, 0,

2*x*y + 2*w*z, 1 - 2*x*x - 2*z*z, 2*y*z - 2*w*x, 0,

2*x*z - 2*w*y, 2*y*z + 2*w*x, 1 - 2*x*x - 2*y*y ,0,

0,0,0,0]

glMultMatrixf(T)

I've also tried to using the current ModelView matrix as input for the axis to do the rotation around (which causes the rotation to be slightly crooked):

mvm = glGetFloatv(GL_MODELVIEW_MATRIX)

norm = math.sqrt(mvm[0][0]*mvm[0][0] + mvm[0][1]*mvm[0][1] + mvm[0][2]*mvm[0][2])

glRotatef(aircraft_roll, mvm[0][0]/norm, mvm[0][1]/norm, mvm[0][2]/norm) #Roll

mvm = glGetFloatv(GL_MODELVIEW_MATRIX)

norm = math.sqrt(mvm[2][0]*mvm[2][0] + mvm[2][1]*mvm[2][1] + mvm[2][2]*mvm[2][2])

glRotatef(aircraft_pitch, mvm[2][0]/norm, mvm[2][1]/norm, mvm[2][2]/norm) # Pitch

I'm using the following setup:

glShadeModel(GL_SMOOTH)

glClearColor(0.0, 0.0, 0.0, 0.0)

glClearDepth(1.0)

glEnable(GL_DEPTH_TEST)

glDepthFunc(GL_LEQUAL)

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

glViewport(0, 0, width, height)

glMatrixMode(GL_PROJECTION)

glLoadIdentity()

gluPerspective(45, 1.0*width/height, 0.1, 100.0)

glMatrixMode(GL_MODELVIEW)

glLoadIdentity()

I would really appreciate some help here since I'm pretty stuck at the moment...

Thanks!