顯示具有 01160112_梁哲豪 標籤的文章。 顯示所有文章
顯示具有 01160112_梁哲豪 標籤的文章。 顯示所有文章

2014年5月30日 星期五

week15

#include <GL/glut.h>
float a=0; //內差的比重值
float angle=0;
float angleOld=0;
float angleNew=90;

void display()
{
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glRotatef(angle, 0, 0, 1);
  glutSolidTeapot(0.3);
 glPopMatrix();
 glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
 if(key=='r'){
  a+=0.1;
  angle = angleOld * (1-a) + angleNew * a; //內插公式
 }
 glutPostRedisplay();
}

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

 glutKeyboardFunc(keyboard);
 glutDisplayFunc(display);
 glutMainLoop();
}
#include <GL/glut.h>
float a=0; //內差的比重值
float angle[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleNew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle[0], 0, 0, 1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glPushMatrix();
        glTranslatef(0.5, 0,0);
        glRotatef(angle[1], 0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
if(key=='r'){
a+=0.1;
for(int i=0;i<=20;i++)
{
angle[i] = angleOld[i] * (1-a) + angleNew[i] * a; //內插公式
}
}
glutPostRedisplay();
}

int main()
{
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week15");
   
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
}

#include <GL/glut.h>
float a = 0; //內插法比重 

//有很多個角度都要做內插法怎麼辦? 利用陣列, 就可以存很多個角度了 
float angle[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //用來轉動的角度,由angleOld, angleNew計算出 
float angleOld[20] = {0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //舊的角度,用來做內插法 
float angleNew[20] = {90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //新的角度,用來做內插法 
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0], 0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0.5, 0,0);
glRotatef(angle[1], 0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void readNext()
{
a += 0.1; //比重一直增加,角度才會變化 
for( int i=0; i<=20; i++){ //全部打出來太遜了,  用for可以直接打20個,比葉問還厲害! 
angle[i] = angleOld[i] * (1-a) + angleNew[i] * a;
}
}
void timer(int t)
{
glutTimerFunc(100, timer, 0);
readNext();
glutPostRedisplay();

void keyboard(unsigned char key, int x, int y)
{
if (key == 'r'){
readNext();
}
else if (key== 'p'){
glutTimerFunc(0, timer, 0);
 }
 glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}

讀取檔案!!!!!!!!!!

void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID = 0;
if(key=='1') angleID = 1;
if(key=='2') angleID = 2;
if(key=='3') angleID = 3;
if(key=='s'){
if(fout==NULL) fout = fopen("this_is_my_file_for_animation.txt","w+");
for(int i=0; i<20; i++){
printf("%f ",angle[i]);
fprintf(fout, "%f ",angle[i]);  
}
}
printf("\n");
fprintf(fout, "\n");
if (key == 'r'){
readNext();
}
else if (key== 'p'){
glutTimerFunc(0, timer, 0);
}
glutPostRedisplay();
}

2014年5月16日 星期五

week13

#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0, angle0=0, angle1=0, angle2=0, angle3=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand0.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
    glEnable(GL_TEXTURE_2D);
    for(int i=0; i<4; i++){//技巧3: 迴圈
    img[i] = cvLoadImage( filename[i] );
    cvCvtColor(img[i], img[i], CV_BGR2RGB);
    glGenTextures(1, &id[i]);
    glBindTexture(GL_TEXTURE_2D, id[i]);//head
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img[i]->width, img[i]->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img[i]->imageData);
}
}
void myRect()//技巧1: 重覆的程式碼, 用函式拉出來
{//函式就是 黑盒子 幫你做事情!
    glPushMatrix();
        glScalef(0.2, 0.5, 0.5);
        glBegin(GL_POLYGON);
        glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
        glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
        glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
        glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
        glEnd();
    glPopMatrix();
}
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    glPushMatrix();
        glBindTexture(GL_TEXTURE_2D, id[0]);
        myRect();//body//技巧1: 用函式
        glPushMatrix();//left hand
            glTranslatef(-0.1, 0.15, 0);
            glRotatef(angleX, 0,0,1);
            glTranslatef(0, -0.25, 0);
            glBindTexture(GL_TEXTURE_2D, id[1]);
            myRect();//技巧1: 用函式
            glPushMatrix();
             glTranslatef(0,-0.25,0);
             glRotatef(angleX,0,0,1);
             glTranslatef(0,-0.25,0);
             myRect();
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();//right hand
            glTranslatef(0.1, 0.15, 0);
            glRotatef(-angleX, 0,0,1);
            glTranslatef(0, -0.25, 0);
            glBindTexture(GL_TEXTURE_2D, id[1]);
            myRect();//技巧1: 用函式
            glPushMatrix();
             glTranslatef(0,-0.25,0);
             glRotatef(-angleX,0,0,1);
             glTranslatef(0,-0.25,0);
             myRect();
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();//left leg
            glTranslatef( -0.05, -0.25, 0);
            glRotatef(180-angleX, 0,0,1);
            glTranslatef(0, 0.25, 0);
            glBindTexture(GL_TEXTURE_2D, id[2 ]);
            myRect();
            glPushMatrix();//left foot
                glTranslatef( 0, 0.25, 0);
                glRotatef(angleX, 0,0,1);
                glTranslatef(0, 0.15, 0);
                glBindTexture(GL_TEXTURE_2D, id[3]);
                myRect();
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();//right leg
            glTranslatef( 0.05, -0.25, 0);
            glRotatef(-(180-angleX), 0,0,1);
            glTranslatef(0, 0.25, 0);
            glBindTexture(GL_TEXTURE_2D, id[2]);
            myRect();
            glPushMatrix();//right foot
                glTranslatef( 0, 0.25, 0);
                glRotatef(-angleX, 0,0,1);
                glTranslatef(0, 0.15, 0);
                glBindTexture(GL_TEXTURE_2D, id[3]);
                myRect();
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
 
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') angleX+=5;
    if(key=='2') angleX-=5;
    glutPostRedisplay();//請電腦重新畫一次畫面
    }
    int main()
    {
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week13");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    myTexture();
 
    glutMainLoop();
}

#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand0.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
 glEnable(GL_TEXTURE_2D);
 for(int i=0; i<4; i++){//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
 img[i] = cvLoadImage( filename[i] );
 cvCvtColor(img[i], img[i], CV_BGR2RGB);
 glGenTextures(1, &id[i]);
 glBindTexture(GL_TEXTURE_2D, id[i]);//head
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img[i]->width, img[i]->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img[i]->imageData);
}
}
void myRect()//技巧1: 重覆的程式碼, 用函式拉出來, 就不用一直copy啦
{//函式就是 黑盒子 幫你做事情!
 glPushMatrix();
  glScalef(0.2, 0.5, 0.5);
  glBegin(GL_POLYGON);
  glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
  glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
  glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
  glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
  glEnd();
 glPopMatrix();
}
void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glBindTexture(GL_TEXTURE_2D, id[0]);
  myRect();//body//技巧1: 用函式, 哥就是帥!
  glPushMatrix();//left hand 今天的技巧1: 大的手
   glTranslatef(-0.1, 0.15, 0);
   glRotatef(angle[0], 0,0,1);
   glTranslatef(0, -0.25, 0);
   glBindTexture(GL_TEXTURE_2D, id[1]);
   myRect();//技巧1: 用函式, 哥就是帥!
   glPushMatrix();// 今天的技巧2: 小的手
    glTranslatef(0, -0.25, 0);//(3) T: 掛在該掛的位子
    glRotatef(angle[1], 0,0,1);//(2) R:
    glTranslatef(0, -0.25, 0);//(1) T: 改變旋轉軸的位置
    myRect();
   glPopMatrix();
  glPopMatrix();
  glPushMatrix();//right hand
   glTranslatef(0.1, 0.15, 0);
   glRotatef(-angle[2], 0,0,1);
   glTranslatef(0, -0.25, 0);
   glBindTexture(GL_TEXTURE_2D, id[1]);
   myRect();//技巧1: 用函式, 哥就是帥!
   glPushMatrix();// 今天的技巧2: 小的手
    glTranslatef(0, -0.25, 0);//(3) T: 掛在該掛的位子
    glRotatef(-angle[3], 0,0,1);//(2) R:
    glTranslatef(0, -0.25, 0);//(1) T: 改變旋轉軸的位置
    myRect();
   glPopMatrix();
  glPopMatrix();
  glPushMatrix();//left leg
   glTranslatef( -0.05, -0.25, 0);
   glRotatef(180-angle[4], 0,0,1);
   glTranslatef(0, 0.25, 0);
   glBindTexture(GL_TEXTURE_2D, id[2]);
   myRect();
   glPushMatrix();//left foot
    glTranslatef( 0, 0.25, 0);
    glRotatef(angle[5], 0,0,1);
    glTranslatef(0, 0.15, 0);
    glBindTexture(GL_TEXTURE_2D, id[3]);
    myRect();
   glPopMatrix();
  glPopMatrix();
  glPushMatrix();//right leg
   glTranslatef( 0.05, -0.25, 0);
   glRotatef(-(180-angle[6]), 0,0,1);
   glTranslatef(0, 0.25, 0);
   glBindTexture(GL_TEXTURE_2D, id[2]);
   myRect();
   glPushMatrix();//left foot
    glTranslatef( 0, 0.25, 0);
    glRotatef(-angle[7], 0,0,1);
    glTranslatef(0, 0.15, 0);
    glBindTexture(GL_TEXTURE_2D, id[3]);
    myRect();
   glPopMatrix();  
  glPopMatrix();
 glPopMatrix();
 glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
 if(key=='0') angleID=0;
 if(key=='1') angleID=1;
 if(key=='2') angleID=2;
 if(key=='3') angleID=3;
 if(key=='4') angleID=4;
 if(key=='5') angleID=5;
 if(key=='6') angleID=6;
 if(key=='7') angleID=7;

 glutPostRedisplay();//請電腦重新畫一次畫面
}
int oldX=0, oldY=0;
void mouse(int button, int state, int x, int y)
{
 oldX=x; oldY=y;
}
void motion(int x, int y)
{
 angle[angleID]+=x-oldX;  oldX=x;
 glutPostRedisplay();
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 glutMouseFunc(mouse);
 glutMotionFunc(motion);
 myTexture();

 glutMainLoop();
}

2014年5月9日 星期五

week12

#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0;

void myTexture()
{
 GLuint id;
 IplImage * img = cvLoadImage("body.jpg");
 cvCvtColor(img, img, CV_BGR2RGB);
 glEnable(GL_TEXTURE_2D);
 glGenTextures(1, &id);
 glBindTexture(GL_TEXTURE_2D, id);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}
void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glPushMatrix();//body
   glScalef(0.2, 0.5, 0.5);
   glBegin(GL_POLYGON);
    glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
    glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
    glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
    glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
   glEnd();
  glPopMatrix();
  glPushMatrix();//left hand
   glTranslatef(-0.1, 0, 0);
   glRotatef(angleX, 0,0,1);
   glTranslatef(0, 0.25, 0);
   glScalef(0.2, 0.5, 0.5);
   glBegin(GL_POLYGON);
    glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
    glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
    glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
    glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
   glEnd();
  glPopMatrix();
  glPushMatrix();//right hand
   glTranslatef( 0.1, 0, 0);
   glRotatef(-angleX, 0,0,1);
   glTranslatef(0, 0.25, 0);
   glScalef(0.2, 0.5, 0.5);
   glBegin(GL_POLYGON);
    glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
    glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
    glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
    glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
   glEnd();
  glPopMatrix();
  glPushMatrix();//left leg
   glTranslatef( -0.05, -0.25, 0);
   glRotatef(180-angleX, 0,0,1);
   glTranslatef(0, 0.25, 0);
   glScalef(0.2, 0.5, 0.5);
   glBegin(GL_POLYGON);
    glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
    glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
    glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
    glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
   glEnd();
  glPopMatrix();
  glPushMatrix();//right leg
   glTranslatef( 0.05, -0.25, 0);
   glRotatef(-(180-angleX), 0,0,1);
   glTranslatef(0, 0.25, 0);
   glScalef(0.2, 0.5, 0.5);
   glBegin(GL_POLYGON);
    glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
    glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
    glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
    glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
   glEnd();
  glPopMatrix();
 glPopMatrix();
 glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
 if(key=='1') angleX+=5;
 if(key=='2') angleX-=5;
 glutPostRedisplay();//請電腦重新畫一次畫面
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myTexture();

 glutMainLoop();
}
                                                                                                                                              
簡化後!!!!!!!!!!

#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0;

void myTexture()
{
GLuint id;
IplImage * img = cvLoadImage("imgBody.png");
cvCvtColor(img, img, CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}
void myRect()//技巧1: 重覆的程式碼, 用函式拉出來, 就不用一直copy啦
{//函式就是 黑盒子 幫你做事情!
glPushMatrix();
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//right hand
glTranslatef( 0.1, 0, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(30, 0,0,1);
glTranslatef(0, 0.15, 0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angleX), 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();

glutMainLoop();
}

2014年5月2日 星期五

week11

#include <GL/glut.h>

float angleX =0;
void keyboard(unsigned char key, int x, int y)
{
 if(key=='1') angleX+=5;
 if(key=='2') angleX-=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.5,0,0);
  glutSolidSphere(0.2,30,30);
  glPopMatrix();

  glPushMatrix();
  glTranslatef(-0.5,0,0);
  glutSolidSphere(0.2,30,30);
  glPopMatrix();

  glPushMatrix();
  glTranslatef(0,0.5,0);
  glutSolidSphere(0.2,30,30);
  glPopMatrix();

  glPushMatrix();
  glTranslatef(0,-0.5,0);
  glutSolidSphere(0.2,30,30);
  glPopMatrix();

 glPopMatrix();
 glutSwapBuffers();
}

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

 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);

 myLight();
 glutMainLoop();

}
glPushMatrix();//右肩 
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();//右臂 
glRotatef(angleX, 0,0,1);
glTranslatef(0.2,0,0);
glScalef(3,1,1);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左肩 
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();//左臂 
glRotatef(angleX, 0,0,-1);
glTranslatef(-0.2,0,0);
glScalef(3,1,1);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();

                 glPushMatrix();//腹 
glTranslatef(0,-0.4,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();//右腿 
glRotatef(angleX, 1,0,0);
glTranslatef(0.2,-0.3,0);
glScalef(1,2,1);
glutSolidCube(0.2);
glPopMatrix();
glPushMatrix();//左腿 
glRotatef(angleX, -1,0,0);
glTranslatef(-0.2,-0.3,0);
glScalef(1,2,1);
glutSolidCube(0.2);
glPopMatrix();
                glPopMatrix();