【问题标题】:2d OpenGL Rotation around center of Object围绕对象中心的 2d OpenGL 旋转
【发布时间】:2014-02-21 13:38:32
【问题描述】:

我的代码不能正常工作。

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();

问题是它没有绕中心旋转。它仅将对象的坐标转换到给定点,然后围绕坐标旋转。这是为什么呢?

【问题讨论】:

    标签: opengl rotation


    【解决方案1】:

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

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

    【讨论】:

    • @user3337420 不错。 :) 请将其标记为已接受的答案,以便其他人知道该问题已得到解答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多