Alireza Farahani
Alireza Farahani

Reputation: 2539

OpenGL - Confused with transformations based on former transformations

I've i've written a simple program by openGL ES 1.1, on android which draws 3D objects with ability to navigate through the space.

I want to move left, right, in, out and rotate left, right, up, down like first person camera mode in 3D games. (this means I want to rotate camera/eye itself) here's my drawing part in onDrawFrame(GL10 gl):

    gl.glLoadIdentity();
    gl.glRotatef(lfRt, 0.0f, 1.0f, 0.0f);
    gl.glRotatef(upDn, 1.0f, 0.0f, 0.0f);
    gl.glTranslatef(X, 0.0f, 0.0f);
    gl.glTranslatef(0.0f, Y, 0.0f);
    gl.glTranslatef(0.0f, 0.0f, Z);

lfRt and other variables determines the point we must be there. (To describe more, every moveIn() call pluses the z with some constant. others are with same logic).

Now here is my confusion. If I put the translations first, move methods work fine (in, left, etc.) but rotations are wrong. (whole world rotates around origin) On other hand, if I put rotations first, camera rotates fine but move methods go incorrect. for example moveIn() always move in same line/axis. but I want to move through not moving on z-axis

What should I do?

Upvotes: 0

Views: 111

Answers (1)

Michael IV
Michael IV

Reputation: 11436

You have to apply the rotation first and then do the translation in this scenario.Remember, matrix operations are non-commutative.Here is a simple example for FPS camera .This thread is also helpful.

Upvotes: 1

Related Questions