2014年5月1日 星期四

機器人手腳

   
    glutWireTeapot(0.3);//網狀
    glTranslatef(0.4,0,0);//球移出
    glutSolidSphere(0.2,30,30);

把程式碼包起來
glPushMatrix();//包起來 <右> 
  glTranslatef(-0.4,0.4,0);//球移出 
  glutSolidSphere(0.2,30,30);
glPopMatrix();



<右手>
glPushMatrix();//包起來 <右> 
    glTranslatef(0.4,0,0);//球移出 
    glutSolidSphere(0.2,30,30);
    
    glPushMatrix();//手臂<右> 
        glTranslatef(0.3,0,0);//移出 
          glScalef(2,0.8,0.8);//方塊方向 
          glutSolidCube(0.2);
      glPopMatrix();
    
    glPopMatrix();

<旋轉>
    glRotatef(angle1,0,0,1);


記得上面要宣告變數;
float angleX=0,angle1=0;
呼叫變數
if(key=='a') angleX+=3;
 if(key=='b') angleX-=3;
 if(key=='1') angle1+=3;

大腿小腿
//腳右 
    glPushMatrix();//大腿 
       glTranslatef(0.2,-0.2,0);//球移出 
       glRotatef(angle1,0,0,1);
       glTranslatef(0,-0.36/2,0);//移出 
      glPushMatrix();
          glScalef(0.2,0.36,0.2);//方塊方向 
          glutSolidCube(1);
      glPopMatrix(); 
       
      glPushMatrix();//小腿 
         glTranslatef(0,-0.18,0);//球移出 
         glRotatef(angle1,0,0,1);
         glTranslatef(0,-0.36/2,0);//移出 
         glScalef(0.2,0.36,0.2);//方塊方向 
         glutSolidCube(1);
       
      glPopMatrix();     
    glPopMatrix();
  


***本週程式碼***

#include <GL/glut.h>
float angleX=0,angle1=0,angle2;
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotatef(angleX, 0,1,0);
    
   // glutSolidTeapot(0.3); 
     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,0.8,0.8);//方塊方向 
          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,0.8,0.8);//方塊方向 
          glutSolidCube(0.2);
      glPopMatrix();
    glPopMatrix();
  
  //頭 
    glPushMatrix();//包起來 <上> 
       glTranslatef(0,0.4,0);//球移出 
       glutSolidSphere(0.2,30,30);
     
    glPopMatrix();
    
    //腳左 
    glPushMatrix();//大腿 
       glTranslatef(-0.2,-0.2,0);//球移出 
       glRotatef(angle1,0,0,1);
       glTranslatef(0,-0.36/2,0);//移出 
      glPushMatrix();
          glScalef(0.2,0.36,0.2);//方塊方向 
          glutSolidCube(1);
      glPopMatrix(); 
       
      glPushMatrix();//小腿 
         glTranslatef(0,-0.18,0);//球移出 
         glRotatef(angle1,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(angle1,0,0,1);
       glTranslatef(0,-0.36/2,0);//移出 
      glPushMatrix();
          glScalef(0.2,0.36,0.2);//方塊方向 
          glutSolidCube(1);
      glPopMatrix(); 
       
      glPushMatrix();//小腿 
         glTranslatef(0,-0.18,0);//球移出 
         glRotatef(angle1,0,0,1);
         glTranslatef(0,-0.36/2,0);//移出 
         glScalef(0.2,0.36,0.2);//方塊方向 
         glutSolidCube(1);
       
      glPopMatrix();     
    glPopMatrix();
  
  
        glPopMatrix();
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
     if(key=='a') angleX+=3;
     if(key=='b') angleX-=3;
     if(key=='1') angle1+=3;
     if(key=='2') angle2+=3;
     
     glutPostRedisplay();
        
}
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("week11");
     glutDisplayFunc(display);
     glutKeyboardFunc(keyboard);
     myLight();
     glutMainLoop();
}





沒有留言:

張貼留言