bytebucket

04-21-2011, 02:12 PM

I'm trying to debug something in a GL app and am thinking maybe the problem is an issue with fundamentals.

The initial view orientation is looking down the negative z axis with positive x to the right and positive y up.

I'd like to rotate the view as follows. I'd like to rotate about the X axis first, then about the (original) Y axis, then about the (original) Z axis.

How should I achieve this in OpenGL?

Currently, I'm manually building up a rotation matrix in an array and then using glMultMatrixd. The problem I'm having is that for certain angles, the view doesn't end up where I'd expect it.

Here's the code being used to manually construct the rotation matrix. In the below r.x, r.y, and r.z are the rotations about X, Y, and Z in degrees, DTR is PI/180, and T is a representation of the matrix as a 2D array accessed using T[row][col]. T has already been initialized to identity before the below code executes.

double ca,sa,cb,sb,cg,sg;

sa = r.z*DTR;

ca = cos(sa);

sa = sin(sa);

sb = r.y*DTR;

cb = cos(sb);

sb = sin(sb);

sg = r.x*DTR;

cg = cos(sg);

sg = sin(sg);

T[0][0]= ca*cb;

T[0][1]=ca*sb*sg-sa*cg;

T[0][2]=ca*sb*cg+sa*sg;

T[1][0]= sa*cb;

T[1][1]=sa*sb*sg+ca*cg;

T[1][2]=sa*sb*cg-ca*sg;

T[2][0]= -sb;

T[2][1]=cb*sg;

T[2][2]=cb*cg;

// ...

double m[16];

int c = 0;

for (int i=0;i<4;i++)

for (int j=0;j<4;j++)

m[c++] = WorldToView.T[j][i];

glMultMatrixd( m );

What would the recommended way be to achieve the desired rotations in OpenGL, other than manually building the matrix as I'm doing now?

As an example of the problem which prompted this: If I attempt to rotate X,Y,Z = { 90, 0, 180 } using the described rotation it works fine. If I instead attempt X,Y,Z = { 90, 0, 90 } I end up with something which doesn't look correct. What I end up with appears to be reachable using X,Y,Z = { -90, -90, 0 }.

Note that I get equivalent results using the below code.

glRotated(r.z,0,0,1);

glRotated(r.y,0,1,0);

glRotated(r.x,1,0,0);

The initial view orientation is looking down the negative z axis with positive x to the right and positive y up.

I'd like to rotate the view as follows. I'd like to rotate about the X axis first, then about the (original) Y axis, then about the (original) Z axis.

How should I achieve this in OpenGL?

Currently, I'm manually building up a rotation matrix in an array and then using glMultMatrixd. The problem I'm having is that for certain angles, the view doesn't end up where I'd expect it.

Here's the code being used to manually construct the rotation matrix. In the below r.x, r.y, and r.z are the rotations about X, Y, and Z in degrees, DTR is PI/180, and T is a representation of the matrix as a 2D array accessed using T[row][col]. T has already been initialized to identity before the below code executes.

double ca,sa,cb,sb,cg,sg;

sa = r.z*DTR;

ca = cos(sa);

sa = sin(sa);

sb = r.y*DTR;

cb = cos(sb);

sb = sin(sb);

sg = r.x*DTR;

cg = cos(sg);

sg = sin(sg);

T[0][0]= ca*cb;

T[0][1]=ca*sb*sg-sa*cg;

T[0][2]=ca*sb*cg+sa*sg;

T[1][0]= sa*cb;

T[1][1]=sa*sb*sg+ca*cg;

T[1][2]=sa*sb*cg-ca*sg;

T[2][0]= -sb;

T[2][1]=cb*sg;

T[2][2]=cb*cg;

// ...

double m[16];

int c = 0;

for (int i=0;i<4;i++)

for (int j=0;j<4;j++)

m[c++] = WorldToView.T[j][i];

glMultMatrixd( m );

What would the recommended way be to achieve the desired rotations in OpenGL, other than manually building the matrix as I'm doing now?

As an example of the problem which prompted this: If I attempt to rotate X,Y,Z = { 90, 0, 180 } using the described rotation it works fine. If I instead attempt X,Y,Z = { 90, 0, 90 } I end up with something which doesn't look correct. What I end up with appears to be reachable using X,Y,Z = { -90, -90, 0 }.

Note that I get equivalent results using the below code.

glRotated(r.z,0,0,1);

glRotated(r.y,0,1,0);

glRotated(r.x,1,0,0);