顯示具有 01161151_梁馨心 標籤的文章。 顯示所有文章
顯示具有 01161151_梁馨心 標籤的文章。 顯示所有文章

2014年5月30日 星期五

week15

1.交叉相乘喔!內插用鍵盤讓茶壺轉動

#include <GL/glut.h>
float a=0;//a is the alpha weight內插的比重 
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);//用現在的角度angle來轉動 
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();       
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='z')
{
a+=0.1;
angle=angleOld*(1-a)+angleNew*a;//內插公式 
}
glutPostRedisplay();//重劃一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
2.把浮點數變成陣列。
#include <GL/glut.h>
float a=0;//a is the alpha weight內插的比重 
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);//用現在的角度angle來轉動 
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=='z')
{
a+=0.1;
angle[0]=angleOld[0]*(1-a)+angleNew[0]*a;//內插公式 
angle[1]=angleOld[1]*(1-a)+angleNew[1]*a;
angle[2]=angleOld[2]*(1-a)+angleNew[2]*a;
angle[3]=angleOld[3]*(1-a)+angleNew[3]*a;
angle[4]=angleOld[4]*(1-a)+angleNew[4]*a;
angle[5]=angleOld[5]*(1-a)+angleNew[5]*a;
angle[6]=angleOld[6]*(1-a)+angleNew[6]*a;
angle[7]=angleOld[7]*(1-a)+angleNew[7]*a;
angle[8]=angleOld[8]*(1-a)+angleNew[8]*a;
angle[9]=angleOld[9]*(1-a)+angleNew[9]*a;
}
glutPostRedisplay();//重劃一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
3.簡化程式&加上Timer讓程式自動播放!
#include <GL/glut.h>
float a=0;//a is the alpha weight內插的比重 
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();
glTranslatef(angle[3], angle[4], angle[5]);
glPushMatrix();
glRotatef(angle[0], 0, 0, 1);//用現在的角度angle來轉動 
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0.5, 0, 0);
glRotatef(angle[1], 0, 0, 1);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();       
}
void readNext()
{
a+=0.1;
for (int i=0; i<20; i++)
{ angle[i]=angleOld[i]*(1-a)+angleNew[i]*a;//內插公式 
}
}
void timer(int t)
{
glutTimerFunc(10, timer, 0);//幾ms後開始, 0 ms後開始播放(一按按鍵就立即播放)
readNext();
glutPostRedisplay(); 
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='x')
{
readNext(); //按x就一下一下轉
}
else if(key=='y')
{
glutTimerFunc(0, timer, 0);//按下y就一次自己轉
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}





2014年5月23日 星期五

14週

~~以下動作都要裝OPENCV和glut的紙箱~~

來播wav格式的音檔喔!

Step.1-下載wav檔案,跟專案放在同一個資料夾。


Step.2-加入程式碼:
#include <windows.h>
#include <mmsystem.h>// Multiple Media
int main()
{
 PlaySound("wolf.wav", NULL, SND_SYNC);
}

Step.3-開小黑囉

若要開啟小黑視窗,在專案按右鍵專案選項內選Win32Console。

發現問題!!小黑出不來!!怎摸辦?


記得在程式碼多按一個空白鍵代表有編輯過小黑才出的來!
========================================

現在來播MP3檔案!!!!


Step.1-下載MP3檔案,跟專案放在同一個資料夾

Step.2-臉書下載CMP3_MCI.h檔案,跟專案放在同一個資料夾

Step.3-加上MP3檔案專屬的程式碼
標頭:
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
main函式:
myMP3.Load("song.mp3");
myMP3.Play();

發現問題!!閃退了!!怎摸辦?


加一段廢話程式碼就好了 ˊ 3 ˋ~
========================================

現在來讓聲音跟影片同步播喔!!!!
wav檔案的處理方式

Step.1-開新專案


Step.2-加入播放wav的程式碼
p.s記得要把音檔放進專案資料夾!!!
----------------------------------------
MP3檔案的處理方式
Step.1&2同wav檔

播放MP3的程式碼
========================================
現在來做動畫喔!!!!
Step.1-把上週程式碼抓過來
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
//float angleX =0, angle0=0, angle1=0, angle2=0, angle3=0;
float angle[8]={0, 0, 0, 0, 0, 0, 0, 0};//8個關節
int angleID=0; 
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.jpg", "imgHand0.jpg", "imgLeg0.jpg", "imgLeg1.jpg"};
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
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();
glTranslatef(0, -0.25, 0);//T
glRotatef(angle[1], 0, 0, 1);//R
glTranslatef(0, -0.25, 0);//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();
glTranslatef(0, -0.25, 0);//T
glRotatef(-angle[3], 0, 0, 1);//R
glTranslatef(0, -0.25, 0);//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();
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 motion(int x, int y)
{
angle[angleID]+= x-oldX; oldX=x;
glutPostRedisplay();//請電腦重新畫一次畫面
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();
glutMainLoop();
}
Step.2-準備好圖檔

Step.3-把圖檔放在專案資料夾:

Step.4-到opencv資料夾裡面把所有dill檔案都複製到專案資料夾

Step.5-加入重要的咒語在參數中
-lcv -lcxcore -lhighgui 
注意咒語後要有空格!!!
















2014年5月16日 星期五

**!KANO!男子漢就要投直球**

關鍵是把關節弄成陣列,就不用設一大堆angle寫到瘋掉,有幾個關節就設多大~
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
//float angleX =0, angle0=0, angle1=0, angle2=0, angle3=0;
float angle[8]={0, 0, 0, 0, 0, 0, 0, 0};//8個關節
int angleID=0; 
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.jpg", "imgHand0.jpg", "imgLeg0.jpg", "imgLeg1.jpg"};
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
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();
glTranslatef(0, -0.25, 0);//T
glRotatef(angle[1], 0, 0, 1);//R
glTranslatef(0, -0.25, 0);//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();
glTranslatef(0, -0.25, 0);//T
glRotatef(-angle[3], 0, 0, 1);//R
glTranslatef(0, -0.25, 0);//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();
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 motion(int x, int y)
{
angle[angleID]+= x-oldX; oldX=x;
glutPostRedisplay();//請電腦重新畫一次畫面
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();
glutMainLoop();
}
========================================
決定用直球進行對決!!選定超帥姿勢↓
喬一下關節位置!
投手投出!


2014年5月2日 星期五

第11周

鍵盤"1"&"2"控制水平方向旋轉角度
#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);
 glPopMatrix();
 glutSwapBuffers();
}

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

 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);

 myLight();
 glutMainLoop();
}
增加球,讓球跟著茶壺轉~
#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);
 
  glTranslatef(0.5, 0, 0);
  glutSolidSphere(0.2, 30, 30);
  glPopMatrix();
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glTranslatef(-0.5, 0, 0);
  glutSolidSphere(0.2, 30, 30);
  glPopMatrix();
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glTranslatef(0, 0.5, 0);
  glutSolidSphere(0.2, 30, 30);
  glPopMatrix();
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glTranslatef(0, -0.5, 0);
  glutSolidSphere(0.2, 30, 30);
  glPopMatrix();
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glTranslatef(0.5, -0.5, 0);
  glutSolidSphere(0.2, 30, 30);
  glPopMatrix();
  glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
 
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
 
myLight();
glutMainLoop();
}
根目錄子目錄概念,階層式管理。
#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();//最外層再用一組Push& PopMatrix包起來做保護 
glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glutSolidTeapot(0.3);
 
  glTranslatef(0.5, 0, 0);
  glutSolidSphere(0.2, 30, 30);
  glPopMatrix();
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glTranslatef(-0.5, 0, 0);
  glutSolidSphere(0.2, 30, 30);
  glPopMatrix();
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glTranslatef(0, 0.5, 0);
  glutSolidSphere(0.2, 30, 30);
  glPopMatrix();
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glTranslatef(0, -0.5, 0);
  glutSolidSphere(0.2, 30, 30);
  glPopMatrix();
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glTranslatef(0.5, -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();
}
================================================================
#include <GL/glut.h>

float angleX =0, angle2=0, angle3=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') angle3+=5;
  if(key=='6') angle3-=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();//最外層再用一組Push& PopMatrix包起來做保護 
  glRotatef(angleX, 0,1,0);
  glutSolidTeapot(0.3);
  glPushMatrix();//右邊肩膀  
  glTranslatef(0.5, 0, 0);
  glutSolidSphere(0.2, 30, 30);
  glPushMatrix();//右手臂
  glRotatef(angle2, 0,0,1);
  glTranslatef(0.2, 0, 0);
  glScalef(0.4, 0.2, 0.2);
glutSolidCube(1);
glPopMatrix();  
  glPopMatrix();
 
  glPushMatrix();//左邊肩膀 
 
  glTranslatef(-0.5, 0, 0);
  glutSolidSphere(0.2, 30, 30);
  glPushMatrix();//右手臂
  glRotatef(angle2, 0,0,1);
  glTranslatef(-0.2, 0, 0);
  glScalef(0.4, 0.2, 0.2);
glutSolidCube(1);
  glPopMatrix();
  glPopMatrix();
  glPushMatrix();//頭 
  glTranslatef(0, 0.4, 0);
  glutSolidSphere(0.25, 30, 30);
  glPopMatrix();
   
  glPushMatrix();//下面  
  glTranslatef(0, -0.25, 0);
  glutSolidSphere(0.25, 30, 30);
  glPushMatrix();//左大腿 
  glRotatef(angle3, 1,0,0);
  glTranslatef(-0.1, -0.2, 0);
  glScalef(0.15, 0.3, 0.2);
  glutSolidCube(1);
  glPushMatrix();//左膝蓋 
  glRotatef(angle3, 1,0,0);
  glTranslatef(-0.1, -0.38, 0);
  glutSolidSphere(0.09,30,30);
  glPopMatrix();
  glPopMatrix();
  glPushMatrix();//右大腿 
  glRotatef(angle3, 1,0,0);
  glTranslatef(0.1, -0.2, 0);
  glScalef(0.15, 0.3, 0.2);
  glutSolidCube(1);
  glPopMatrix();
 
  glPushMatrix();//右膝蓋 
  glRotatef(angle2, 0,0,1);
  glTranslatef(0.1, -0.38, 0);
  glutSolidSphere(0.09,30,30);
  glPopMatrix();
  glPopMatrix();  
glPopMatrix();//階層式管理 
  glutSwapBuffers();
}

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