2020年4月21日 星期二

week08

機器人手肘轉動

#include <GL/glut.h>
void arm()
{
    glPushMatrix(); ///備份矩陣
        glScalef(1, 0.3 ,0.3); ///x不變,y,z都變1/3
        glutSolidCube(0.5);
    glPopMatrix(); ///還原
}
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix(); ///右手
        glTranslatef(0.2,0,0); ///把下面正在以手肘為中心,讓轉動的手掛到上面去
        glRotatef(angle, 0,0,1); ///對z軸轉
        glTranslatef(0.25,0,0); ///把手肘(旋轉中心)放到畫面中心
        glColor3f(1,0,0); arm(); ///畫紅色的手

        glPushMatrix(); ///備份
            glTranslatef(0.25,0,0); ///把下面正在以手肘為中心,讓轉動的手掛到上面去
            glRotatef(angle, 0,0,1); ///對z軸轉
            glTranslatef(0.25,0,0); ///把手肘(旋轉中心)放到畫面中心
            glColor3f(1,1,0); arm();
        glPopMatrix(); ///還原矩陣
    glPopMatrix();
    glPushMatrix(); ///左手
        glTranslatef(-0.2,0,0); ///把下面正在以手肘為中心,讓轉動的手掛到上面去
        glRotatef(-angle, 0,0,1); ///對z軸轉
        glTranslatef(-0.25,0,0); ///把手肘(旋轉中心)放到畫面中心
        glColor3f(1,0,0); arm(); ///畫紅色的手

        glPushMatrix(); ///備份
            glTranslatef(-0.25,0,0); ///把下面正在以手肘為中心,讓轉動的手掛到上面去
            glRotatef(-angle, 0,0,1); ///對z軸轉
            glTranslatef(-0.25,0,0); ///把手肘(旋轉中心)放到畫面中心
            glColor3f(1,1,0); arm();
        glPopMatrix(); ///還原矩陣
    glPopMatrix();

    angle++;
    glutSwapBuffers();
}
int main(int argc, char*argv)
{
    glutInit(&argc, &argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week09");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}



沒有留言:

張貼留言