围绕对象中心的2d OpenGL旋转

时间:2014-02-21 13:38:33

标签: opengl rotation

我的代码无法正常工作。

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    // Here I translate the camera so that the point 0/0 will be at the 
    //bottom left of the screen
    gl.glTranslatef(CameraX, CameraY, CameraZ);
    // then I draw the object to rotate like many threads here said:
    gl.glPushMatrix();
    // translate to the middlepoint of object
    gl.glTranslatef(object.getPosX() + object.getWidth()/2, object.getPosY()               + object.getHeight()/2, 0);
    // rotate
    gl.glRotatef(30, 0, 0, 1);              
    object.draw(gl);
    gl.glPopMatrix();

问题在于它不绕中心旋转。它仅将对象的坐标转换为给定点,然后围绕坐标旋转。那是为什么?

1 个答案:

答案 0 :(得分:0)

那是因为在OpenGL中,glRotate没有指定方向,例如在Blender,但围绕原点旋转。你需要做的是:

translate(-pivot); // Make the pivot the origin
rotate(); // rotate around origin
translate(pivot); // translate back