Jenicek
Jenicek

Reputation: 21

OpenGL ES rotation in fixed coordinate system

I'm having real trouble finding out how to rotate an object arround two axes without changing axes orientation. I need only local rotation, first arround X axis and then arround Y axis(only example, it doesn't matter how many transformations arround which axes) without transforming the whole coordinate system, only the object. The problem is that if I'm using glRotatef arround X axis, the axes are rotated also and that's what I don't want. I've red bunch of articles about it but it seems I'm still missing something. Thanks for every help.

To have some sample code here, it's something like this

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(rotX, 1.0f, 0.0f, 0.0f);
glRotatef(rotY, 0.0f, 1.0f, 0.0f);
drawObject();

but this transforms the coordinate system also.

Upvotes: 2

Views: 1778

Answers (2)

Kyle
Kyle

Reputation: 51

Create a global matrix. Add xAngle and yAngle to the matrix when rotating.

Matrix.rotateM(matrix, 0, xAngleADD, matrix[1], matrix[5], matrix[9]);
Matrix.rotateM(matrix, 0, yAngleADD, matrix[0], matrix[4], matrix[8]);
gl.glMultMatrixf(matrix, 0);

Upvotes: 1

Thomas
Thomas

Reputation: 181745

You probably need to restore your modelview matrix after drawing the object. You can do this using the built-in matrix stack of OpenGL. The common pattern looks like this:

// Set up global coordinate system:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// ... add world and view transformations here ...

// Draw your object:
glPushMatrix(); // save the current matrix on the stack
glRotatef(rotX, 1.0f, 0.0f, 0.0f);
glRotatef(rotY, 0.0f, 1.0f, 0.0f);
drawObject();
glPopMatrix(); // restore the previously saved matrix

// Repeat the above for other objects

Upvotes: 0

Related Questions