围绕世界0,0,0而不是对象中心旋转

时间:2013-01-04 15:54:27

标签: c++ opengl rotation scene

我的物体在0,0,0左右旋转,但我希望它在Y轴上绕自身旋转。 我知道还有其他案例

  

Rotating object around itself in OpenGL

     

glRotatef not rotating as expected

但是我在轮换之前没有任何翻译,但它仍无效。

我的已编辑代码:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotatef(_angle,0.0f,1.0f,0.0f);
glPushMatrix();

glTranslatef(0.0f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
    //Front
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    //Back
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);

    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);

    //Right
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);


    //Left
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);

    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);


    //Top
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);


    //Bottom
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);

    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);
glEnd();
glPopMatrix();

glutSwapBuffers();

我的原始代码:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glRotatef(angle,0.0f,1.0f,0.0f);  //The angle is a float that slowly increases until it reaches 360 Degrees, and then resets to 0.0
    /* This is a Cube */
glBegin(GL_TRIANGLES);
    //Front
    glVertex3f(-1.0f, -1.0f, -5.0f);
    glVertex3f(-1.0f, 1.0f, -5.0f);
    glVertex3f(1.0f, 1.0f, -5.0f);

    glVertex3f(-1.0f, -1.0f, -5.0f);
    glVertex3f(1.0f, -1.0f, -5.0f);
    glVertex3f(1.0f, 1.0f, -5.0f);

    //Back
    glVertex3f(-1.0f, -1.0f, -7.0f);
    glVertex3f(-1.0f, 1.0f, -7.0f);
    glVertex3f(1.0f, 1.0f, -7.0f);

    glVertex3f(-1.0f, -1.0f, -7.0f);
    glVertex3f(1.0f, -1.0f, -7.0f);
    glVertex3f(1.0f, 1.0f, -7.0f);

    //Right
    glVertex3f(1.0f, 1.0f, -5.0f);
    glVertex3f(1.0f, -1.0f, -5.0f);
    glVertex3f(1.0f, -1.0f, -7.0f);

    glVertex3f(1.0f, 1.0f, -5.0f);
    glVertex3f(1.0f, 1.0f, -7.0f);
    glVertex3f(1.0f, -1.0f, -5.0f);


    //Left
    glVertex3f(-1.0f, 1.0f, -5.0f);
    glVertex3f(-1.0f, -1.0f, -5.0f);
    glVertex3f(-1.0f, -1.0f, -7.0f);

    glVertex3f(-1.0f, 1.0f, -5.0f);
    glVertex3f(-1.0f, 1.0f, -7.0f);
    glVertex3f(-1.0f, -1.0f, -7.0f);


    //Top
    glVertex3f(-1.0f, 1.0f, -7.0f);
    glVertex3f(-1.0f, 1.0f, -5.0f);
    glVertex3f(1.0f, 1.0f, -5.0f);

    glVertex3f(-1.0f, 1.0f, -7.0f);
    glVertex3f(1.0f, 1.0f, -7.0f);
    glVertex3f(1.0f, 1.0f, -5.0f);


    //Bottom
    glVertex3f(-1.0f, -1.0f, -7.0f);
    glVertex3f(-1.0f, -1.0f, -5.0f);
    glVertex3f(1.0f, -1.0f, -5.0f);

    glVertex3f(-1.0f, -1.0f, -7.0f);
    glVertex3f(1.0f, -1.0f, -7.0f);
    glVertex3f(1.0f, -1.0f, -5.0f);
glEnd();

glutSwapBuffers();

1 个答案:

答案 0 :(得分:2)

确保物体的中心位于其局部坐标系的原点。

对于你的立方体,你的X / Y似乎居中,但你的Z中心看起来是-6。