0

我无法围绕自身旋转房间。它的中心在 (5,5,0) 所以我想如果我用 glTranslatef(5,5,5) 翻译房间和房间里的物体然后 glRotatef(rotateroom,0,0,1) 然后绘制项目并使用 glTranslate(-5,-5,0) 它会将房间和其中的所有东西旋转大约 5,5,0 但它似乎仍在旋转 (0,0,0) 我不太确定是什么我做错了。我在这里先向您的帮助表示感谢。

void drawside(){
    int i,j;
    /*for (j = 0; j <= 8; j++) {
       glBegin(GL_LINE_STRIP);
       for (i = 0; i <= 30; i++)
          glEvalCoord2f((GLfloat)i/30.0, (GLfloat)j/8.0);
       glEnd();
       glBegin(GL_LINE_STRIP);
       for (i = 0; i <= 30; i++)
          glEvalCoord2f((GLfloat)j/8.0, (GLfloat)i/30.0);
       glEnd();
    }*/
    glEvalMesh2(GL_FILL, 0, 20, 0, 20);
}
void drawRoom(){
    //floor
    glBegin(GL_POLYGON);
    glColor3f(1,1,1);
    glNormal3f(0,0,0);
    glVertex3f(0,0,0);
    glNormal3f(0,10,0);
    glVertex3f(0,10,0);
    glNormal3f(10,10,0);
    glVertex3f(10,10,0);
    glNormal3f(10,0,0);
    glVertex3f(10,0,0);
    glEnd(
    );
    //wall
    glBegin(GL_POLYGON);
    glColor3f(0,0,1);
    glNormal3f(0,10,0);
    glVertex3f(0,10,0);
    glNormal3f(0,10,10);
    glVertex3f(0,10,10);
    glNormal3f(10,10,10);
    glVertex3f(10,10,10);
    glNormal3f(10,10,0);
    glVertex3f(10,10,0);
    glEnd();
    //wall2
    glBegin(GL_POLYGON);
    glColor3f(0,1,0);
    glNormal3f(10,10,0);
    glVertex3f(10,10,0);
    glNormal3f(10,10,0);
    glVertex3f(10,10,10);
    glNormal3f(10,0,10);
    glVertex3f(10,0,10);
    glNormal3f(10,0,0);
    glVertex3f(10,0,0);
    glEnd();
}
void drawObjects(){
    glColor3f(1,0,1);
    glTranslatef(2,2,0);
    if(conefill == 1)
        glShadeModel(GL_FLAT);
    else
        glShadeModel(GL_SMOOTH);
    glutSolidCone(cone,5,10,2);
    glTranslatef(5,5,0);
    glColor3f(1,0,0);
    if(spherefill == 1)
        glShadeModel(GL_FLAT);
    else 
        glShadeModel(GL_SMOOTH);
    glutSolidSphere(sphere,20,20);

}

void display(){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(viewx,viewy, viewz,viewx +lx, 5, viewz + ly,0.0f, 0.0f,  1.0f);
    //gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    GLfloat light_position[] = {-1.0,5.0,5.0,0.0};
    //GLfloat light_direction[] = {-5,-5,-5};
    //GLfloat amb[] = {0,0,0,1};
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    //glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,light_direction);
    //glLightfv(GL_LIGHT0, GL_AMBIENT,amb);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef(5,5,0);
    glRotatef(rotateroom,0,0,1);
    glPushMatrix(); 
    drawRoom();
    glPopMatrix();
    glColor3f(0.0, 0.0, 1.0);
    glPushMatrix();
    glTranslatef(6,3,1);
    glPushMatrix ();
    drawside();
    glPopMatrix();
    glPushMatrix();
    //glTranslatef(5,0,0);
    glRotatef(90,1,0,0);
    //glTranslatef(-5,0,0);
    glTranslatef(0,.5,.5);
    drawside();
    glPopMatrix();
    glPushMatrix();
    //glTranslatef(5,0,0);
    glRotatef(180,1,0,0);
    //glTranslatef(-5,0,0);
    glTranslatef(0,1,0);
    drawside();
    glPopMatrix();
    glPushMatrix();
    glRotatef(90,0,0,1);
    glTranslatef(-.5,.5,0);
    drawside();
    glPopMatrix();
    glPushMatrix();
    glRotatef(270,0,0,1);
    glTranslatef(.5,.5,0);
    drawside();
    glPopMatrix();
    glPopMatrix();
    drawObjects();
    glTranslatef(-5,-5,0);
    glPopMatrix();
    glutSwapBuffers();
    glFlush();
}
4

1 回答 1

1

您只需要反转指令的顺序:

glRotatef(rotateroom,0,0,1);
glTranslatef(5,5,0);

每当你变换一个点时,你必须颠倒你想要应用的变换序列。这与变换矩阵相乘的方式有关。

于 2012-11-25T19:13:11.237 回答