2014年6月6日 星期五

HW-16 打光



#include <GL/glut.h>

float angleX =0,angle2=0,anglet=0,angleh=0;
float eyeX=0.3,eyeY=0.2,eyeZ=0.4, objX=0,objY=0,objZ=0, upX=0,upY=1,upZ=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;
  if(key=='5') anglet+=5;
  if(key=='6') anglet-=5;
  if(key=='7') angleh+=5;
  if(key=='8') angleh-=5;
  if(key==' '){
  eyeX-=0.02; eyeY-=0.02;
 }
  glutPostRedisplay();//請電腦重新畫一次畫面
}


void display()
{
  glEnable(GL_DEPTH_TEST);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
   gluLookAt(eyeX,eyeY,eyeZ, objX,objY,objZ, upX,upY ,upZ);        
    glRotatef(angleX, 0,1,0);
    glColor3f(0,0,0);
    glutSolidCube(0.7);
 

    glPushMatrix();//right shoulder
    glTranslatef(0.5,0,0);
    glColor3f(0.5,0.5,0.5);
     glutSolidSphere(0.17,30,30);
     glPushMatrix();//right arm
      glRotatef(angle2,0,0,1);
      glTranslatef(0.2,0,0);
      glScalef(0.3,0.25,0.2);
      glutSolidCube(1);
     glPopMatrix();
    glPopMatrix();
 
    glPushMatrix();//left shoulder
     glTranslatef(-0.5,0,0);
     glutSolidSphere(0.17,30,30);
     glColor3f(0,0,0);
     glPushMatrix();//left arm
      glRotatef(angle2,0,0,1);
      glTranslatef(-0.2,0,0);
      glScalef(0.3,0.25,0.2);
      glutSolidCube(1);
     glPopMatrix();
    glPopMatrix();

    glPushMatrix();//head
     glTranslatef(0,0.4,0);
     glutSolidCube(0.2);
     glColor3f(0,0,0);
     glPushMatrix();
       glRotatef(angleh,0,1,0);
       glTranslatef(0,0.3,0);
       glutSolidSphere(0.18,20,10);
     glPopMatrix();
    glPopMatrix();

  glPushMatrix();//right leg
   glTranslatef(0.15,-0.4,0);
   glutSolidSphere(0.12,30,30);
   glColor3f(0,0,0);
   glPushMatrix();
    glRotatef(anglet,1,0,0);
    glTranslatef(0.018,-0.2,0);
    glScalef(1,2,1);
    glutSolidCube(0.15);
   glPopMatrix();
  glPopMatrix();

    glPushMatrix();//left leg
   glTranslatef(-0.15,-0.4,0);
   glutSolidSphere(0.12,30,30);
   glColor3f(0,0,0);
   glPushMatrix();
       glRotatef(-anglet,1,0,0);
       glTranslatef(-0.018,-0.2,0);
       glScalef(1,2,1);
       glutSolidCube(0.15);
      glPopMatrix();
  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("week11");

  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);

 myLight();
 glutMainLoop();
}

沒有留言:

張貼留言