2014年5月1日 星期四

hw11


#include <Gl/glut.h>
float teapotX=0,teapotY=0,angle1=0,angle2=0;
void keyboard(unsigned char key, int x, int y)
{

    if(key=='a' || key=='A') teapotX+=3;
    if(key=='b' || key=='B') teapotX-=3;
    if(key=='1') angle1+=3;
    if(key=='2') angle1-=3;
    if(key=='3') angle2+=3;
    if(key=='4') angle2-=3;


    glutPostRedisplay();
}

void display()
{
 glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(teapotX,0,1,0);
        glutWireTeapot(0.3);
       
        glPushMatrix();//右手 
            glTranslatef(0.4,0,0);
            glutSolidSphere(0.2,30,30);
            glPushMatrix();
              glRotatef(angle1,0,0,1);
              glTranslatef(0.3,0,0);
              glScalef(2,1,1);
              glutSolidCube(0.2);
            glPopMatrix();
       glPopMatrix();
        
       
       glPushMatrix();//左手 
         glTranslatef(-0.4,0,0);
         glutSolidSphere(0.2,30,30);
         glPushMatrix();
             glRotatef(angle1,0,0,1);  
             glTranslatef(-0.3,0,0);
             glScalef(2,1,1);
             glutSolidCube(0.2);
         glPopMatrix();
       glPopMatrix();
       
       
       glPushMatrix();//右腳 
            glTranslatef(0.2,-0.2,0);
            glRotatef(angle2,1,1,0);
            glTranslatef(0,-0.36/2,0);
            glPushMatrix(); 
              glScalef(0.2,0.36,0.2);
              glutSolidCube(1);
            glPopMatrix();
            glPushMatrix();
              glTranslatef(0,-0.18,0);
              glRotatef(angle2,0,0,1);
              glTranslatef(0,-0.36/2,0);
              glScalef(0.2,0.36,0.2);
              glutSolidCube(1);
            glPopMatrix();
       glPopMatrix();
       
       
       glPushMatrix();//左腳 
            glTranslatef(-0.2,-0.2,0);
            glRotatef(angle2,-1,1,0);
            glTranslatef(0,-0.36/2,0);
            glPushMatrix(); 
              glScalef(0.2,0.36,0.2);
              glutSolidCube(1);
            glPopMatrix();
            glPushMatrix();
              glTranslatef(0,-0.18,0);
              glRotatef(angle2,0,0,1);
              glTranslatef(0,-0.36/2,0);
              glScalef(0.2,0.36,0.2);
              glutSolidCube(1);
            glPopMatrix();
       glPopMatrix();
        
        
        glPushMatrix();//頭 
         glTranslatef(0,0.4,0);
         glutSolidSphere(0.2,30,30);
        glPopMatrix();
        
        
        
        
        
      
       
    glPopMatrix();

    glutSwapBuffers();
}
void myLight()
{
    const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING); 
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
int main()
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week10");
   
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    myLight();

    glutMainLoop();
}


沒有留言:

張貼留言