2014年5月2日 星期五

機器人手臂

#include <GL/glut.h>

float angleX =0,angle2=0;
void keyboard(unsigned char key, int x, int y)
{
 if(key=='1') angleX+=5;
 if(key=='2') angleX-=5;
 if(key=='3') angle2+=5;
 if(key=='4') angle2-=5;
 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);
}

void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glutSolidTeapot(0.3);
 

 
  glPushMatrix();//右肩 
  glTranslatef(0.4,0,0);
  glutSolidSphere(0.2,30,30);
  glPushMatrix();//右手臂 
glRotatef(angle2,0,0,1);
glTranslatef(0,-0.5,0);
glScalef(0.2,1.5,0.2);
glutSolidCube(0.5);
  glPopMatrix();
    glPopMatrix();
   
    glPushMatrix();//左肩 
  glTranslatef(-0.4,0,0);
  glutSolidSphere(0.2,30,30);
  glPushMatrix();//左手臂 
glRotatef(angle2,0,0,1);
glTranslatef(0,-0.5,0);
glScalef(0.2,1.5,0.2);
glutSolidCube(0.5);
  glPopMatrix();
    glPopMatrix();
   
    glPushMatrix();
  glTranslatef(0,0.4,0);
  glutSolidSphere(0.2,30,30);
    glPopMatrix();
 
   
 


 glPopMatrix();
 glutSwapBuffers();
}

int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");

 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);

 myLight();
 glutMainLoop();
}

沒有留言:

張貼留言