Reputation: 85
I'm having some issues with glRotateF. The problem is the following:
i'm drawing 3 lines that represent a Cartesian coordinate system, they represent the point of view of a camera.
This camera can rotate its point of view. When it rotates, it sends a message with the angles and the axis that had rotated.
My app gets this message and sends it to a method, this method gets the axis that was rotated and its angle and sends it to this method:
So far everything is working, the problem starts after this.
if I send a message to rotate the angle X on any angle and angle Z on any angle too, it just rotates the z axis.
In Debugging I noticed that first it rotated the X angle in the given angle, but when it rotates the Z in the angle, it goes back to the original position and then it rotates Z, losing the X's rotation. Like this example:
Initial position:
rotate X on 90º:
rotate Z on 90º (What should be):
rotate Z on 90º (What is really happening):
What i want is rotate x and then z without lose x rotation.
This is how i call to rotate an axis:
openGl.rotateX((float) x);
openGl.rotateZ((float) z);
The method rotateX and rotateZ
public void rotateX(float grau) {
mCubeRotation = grau;
eixoX = 1.0f;
eixoY = 0.0f;
eixoZ = 0.0f;
surfaceView.requestRender(); // line to call onDrawFrame
}
public void rotateZ(float grau) {
mCubeRotation = grau;
eixoX = 0.0f;
eixoY = 0.0f;
eixoZ = 1.0f;
surfaceView.requestRender(); // line to call onDrawFrame
}
This is the code of rotation:
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -8.0f);
// When i rotate x eixoX equal 1 and the other axis is 0.
// When i rotate z eixoZ equal 1 and the other axis is 0.
gl.glRotatef(mCubeRotation, eixoX, eixoY, eixoZ);
mCube.draw(gl);
gl.glLoadIdentity();
lock = false;
}
if someone help me, I will appreciate.
Upvotes: 0
Views: 259
Reputation: 12229
Your rotate methods are explicitly clobbering the global eixo*
variables set by the "other" rotate method, and your draw method is resetting the rendering state matrix via glLoadIdentity
, so that is why you only ever get a single rotation.
In general you can't do what you're trying to do with three simple variables and a single call to glRotatef
on top of an identity matrix; rotation doesn't concatenate that neatly unless your rotations are trivial because you need to order them correctly (applying two rotations around world-space X then Y is not the same thing as two rotations around world-space Y and then X - ordering matters). This is especially true across frames where you generally want to start from an arbitrary starting rotation. In general you want to apply a series of glRotatef
calls on top of an existing matrix to generate the rotation incrementally.
Slightly off topic - if you have any choice here I would really suggest switching to use OpenGL ES 2.x and maintaining the complete translation matrix locally in your application. The OpenGL ES 1.1 API is old and relatively inefficient, and it's push/pop matrix stack assumes a very specific way of writing a scene graph rendering engine; OpenGL ES 2.x and shaders is the way forward and available on all modern devices.
Upvotes: 0