1

我的对象围绕 0,0,0 旋转,但我希望它在 Y 轴上围绕自身旋转。我知道还有其他情况

在OpenGL中围绕自身旋转对象

glRotatef 没有按预期旋转

但我在旋转之前没有任何翻译,但它仍然不起作用。

我的编辑代码:

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

1 回答 1

2

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

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

于 2013-01-04T16:01:19.120 回答