OpenGL ES/Android -- Is there a built-in function to reset the scale, translation, rotation of a object?
My program draws an 开发者_StackOverflow中文版object, then translates, rotates, and scales it, then redraws it, etc etc. To set the translation I would do:
gl.glTranslatef(2,4,666);
then to clear it gl.glTranslatef(-2,-4,-666)
;
I'm wondering if there's a built in function to do so?
glPushMatrix() and glPopMatrix() are the normal ways to do this. Push before applying glTranslate, pop when done and it will revert the stack. You have to remember, OpenGL is a state based system that uses a stack. When you apply glTranslatef, you are adding the translate function to the stack, so anything drawn after it is placed on the stack will have that translation done to it. Calling
gl.glTranslatef(2,4,666);
and then
gl.glTranslatef(-2,-4,-666);
if I understand it correctly, will cause the scene to first move your object to (-2,-4,-666), then back (2,4,666). Because it is a stack, the last transformation you apply gets applied first, and the first is last. It helps to remember that little fact while you're setting up your scene. Just put a push before gl.glTranslatef(2,4,666);, and a pop after and you should be good.
glPushMatrix();
gl.glTranslatef(2,4,666);
//draw code here
glPopMatrix();
Just remember the whole stack thing and you should be able to think through any problem areas.
Just reset your matrix by using glLoadIdentity()
.
Another alternative (if you're having a lot of objects that needs their own matrix) is to make your object's drawings on a matrix copy. In order to accomplish that you need to use glPushMatrix()
and glPopMatrix()
.
glLoadIdentity()
精彩评论