2014年6月6日 星期五

Week 16 , 運鏡旋轉 (防止破圖)



#include <GL/glut.h>

float angleX =0, angle2=0, angle3=0, angle4=0, angle5=0,angle6=0,angle7=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)
{
 int i=1;
 if(key=='1') angleX+=5;//身體
 if(key=='2') angleX-=5;

 if(key=='q') angle2+=5;//左手臂
 if(key=='w') angle2-=5;
 if(key=='e') angle6+=5;//右手臂
 if(key=='r') angle6-=5;

 if(key=='a') angle3+=5;//左小手臂
 if(key=='s') angle3-=5;
 if(key=='d') angle7+=5;//右小手臂
 if(key=='f') angle7-=5;

 if(key=='7') angle4+=5;//左右大腿
 if(key=='8') angle4-=5;

 if(key=='9') angle5+=5;//左右小腿
 if(key=='0') angle5-=5;

 if(key==' ')//利用空白鍵改變拍攝方向
 {
  eyeX-=0.02;eyeY-=0.02;
 }
 else if(key=='0')
 {
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glOrtho(-2,2,-2,2,-10,10);//欲擴充之範圍以防止破圖
 glMatrixMode(GL_MODELVIEW);
}

 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();
 gluLookAt(eyeX, eyeY, eyeZ, objX, objY, objZ, upX, upY, upZ);

  glRotatef(angleX, 0,1,0);
  glutWireTeapot(0.25);
 
  glPushMatrix();
   glTranslatef(0,0.4,0);
   glutSolidSphere(0.25,30,30);
  glPopMatrix();
 
  glPushMatrix();//右肩
   glTranslatef(0.4,0,0);
   glutSolidSphere(0.12,30,30);
   glPushMatrix();//右手臂
    glRotatef(angle6,0,0,1);
    glTranslatef(0.18,0,0);
    glScalef(0.2,0.2,0.2);
    glutSolidCube(1);
    glPushMatrix();
     glTranslatef(0.95,0,0);
     glutSolidSphere(0.5,30,30);
     glPushMatrix();
      glRotatef(angle7,0,0,1);
      glTranslatef(0.9,0,0);
      glScalef(1,0.8,0.8);
      glutSolidCube(1);
     glPopMatrix();
    glPopMatrix();
   glPopMatrix();
  glPopMatrix();
 
  glPushMatrix();//左肩
   glTranslatef(-0.4,0,0);
   glutSolidSphere(0.12,30,30);
   glPushMatrix();//左手臂
    glRotatef(-angle2,0,0,1);
    glTranslatef(-0.18,0,0);
    glScalef(0.2,0.2,0.2);
    glutSolidCube(1);
    glPushMatrix();
     glTranslatef(-0.95,0,0);
     glutSolidSphere(0.5,30,30);
     glPushMatrix();
      glRotatef(-angle3,0,0,1);
      glTranslatef(-0.9,0,0);
      glScalef(1,0.8,0.8);
      glutSolidCube(1);
     glPopMatrix();
    glPopMatrix();
   glPopMatrix();
  glPopMatrix();
 
  glPushMatrix();//右邊大腿
   glTranslatef(0.12,-0.1,0);
   glRotatef(angle4,1,0,0);
   glTranslatef(0,-0.25,0);
   glPushMatrix();
    glScalef(0.18,0.3,0.2);
    glutSolidCube(1);
   glPopMatrix();
   glPushMatrix();//
    glTranslatef(0,-0.24,0);
    glutSolidSphere(0.1,30,30);
    glPushMatrix();
     glRotatef(angle5,1,0,0);
     glTranslatef(0,-0.24,0);
     glScalef(0.18,0.3,0.2);
     glutSolidCube(1);
    glPopMatrix();
   glPopMatrix();
  glPopMatrix();
 
  glPushMatrix();//左邊大腿
   glTranslatef(-0.1,-0.1,0);
   glRotatef(angle4,1,0,0);
   glTranslatef(0,-0.25,0);
   glPushMatrix();
    glScalef(0.18,0.3,0.2);
    glutSolidCube(1);
   glPopMatrix();
   glPushMatrix();//
    glTranslatef(0,-0.24,0);
    glutSolidSphere(0.1,30,30);
    glPushMatrix();
     glRotatef(angle5,1,0,0);
     glTranslatef(0,-0.24,0);
     glScalef(0.18,0.3,0.2);
     glutSolidCube(1);
    glPopMatrix();
   glPopMatrix();
  glPopMatrix();
 
 
 glPopMatrix();
 glutSwapBuffers();
}

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

 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);

 myLight();
 glutMainLoop();
}

沒有留言:

張貼留言