2014年5月29日 星期四

Week14



#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
void timer(int t);
FILE * fout =NULL,*fin=NULL;

//IplImage *img1, *img2, *img3, *img4;
//GLuint id1, id2, id3, id4;
IplImage * img[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
GLuint id[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
float angle1=0, angle2=0, angle3=0, angle4=0, angle5=0, angle6=0, angle7=0, angle8=0;
void myPrepareTexture(int i)//用參數來配, 找對應正確的變數
{//技巧3: 可以把重覆的東西, 用函式來簡化
 cvCvtColor(img[i], img[i], CV_BGR2RGB);
 glEnable(GL_TEXTURE_2D);
 glGenTextures(1, &id[i]);
 glBindTexture(GL_TEXTURE_2D, id[i]);
 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 myTexture()
{
 img[0]=cvLoadImage("1.png");
 img[1]=cvLoadImage("2.png");
 img[2]=cvLoadImage("3.png");
 for(int i=0;i<3;i++){//技巧2: 可以把重覆的東西, 用迴圈來簡化
  myPrepareTexture(i);
 }
}
void myPolygon(int num){//技巧3: 可以把重覆的東西, 用函式來簡化
 glPushMatrix();
  glScalef(0.3, 0.8, 0.8);
  glBindTexture(GL_TEXTURE_2D, id[num]);
  glBegin(GL_POLYGON);
   glTexCoord2f(0, 0);   glVertex3f( -0.5,  0.5, 0);
   glTexCoord2f(1, 0);   glVertex3f(  0.5,  0.5, 0);
   glTexCoord2f(1, 1);   glVertex3f(  0.5, -0.5, 0);
   glTexCoord2f(0, 1);   glVertex3f( -0.5, -0.5, 0);
  glEnd();
 glPopMatrix();
}
void myBody(){
 myPolygon(0);
}
void myLeg(){
 glPushMatrix();
  glScalef(0.5, 0.5, 0.5);
  myPolygon(1);
 glPopMatrix();
}
void myHand(){
 glPushMatrix();// 今天的技巧2: 可以利用 scale把後面的東西都變小
  //T R S: Translate, Rotate, Scale, 要放在 glPushMatrix()/glPopMatrix()
  glScalef(0.5, 0.5, 0.5);
  myPolygon(2);
 glPopMatrix();
}
void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glScalef(0.5, 0.5, 0.5);//今天的技巧1: 可以利用 scale把後面的東西都變小
  myBody();
  glPushMatrix();//今天的技巧3: 大的在外面
   glTranslatef(0.2,0,0);//右手
   glRotatef(angle1, 0,0,1);
   glTranslatef(0, -0.2, 0);
   myHand();
   glPushMatrix();//今天的技巧3: 小的在裡面
    glTranslatef(0,-0.2,0);//小右手
    glRotatef(angle2, 0,0,1);
    glTranslatef(0, -0.2, 0);
    myHand();
   glPopMatrix();
  glPopMatrix();
  glPushMatrix();
   glTranslatef(-0.2,0,0);//左手
   glRotatef(-angle3, 0,0,1);
   glTranslatef(0, -0.2, 0);
   myHand();
   glPushMatrix();//今天的技巧3: 小的在裡面
    glTranslatef(0,-0.2,0);//小右手
    glRotatef(-angle4, 0,0,1);
    glTranslatef(0, -0.2, 0);
    myHand();
   glPopMatrix();  
  glPopMatrix();
  glPushMatrix();
   glTranslatef(0.1,-0.3,0);//右腿
   glRotatef(angle5, 0,0,1);
   glTranslatef(0, -0.2, 0);
   myLeg();
   //請把小腿及腳掌做出來
   glPushMatrix();
    glTranslatef(0,-0.2,0);//右腿
    glRotatef(angle6, 0,0,1);
    glTranslatef(0, -0.2, 0);
    myLeg();  
   glPopMatrix();
  glPopMatrix();
  glPushMatrix();
   glTranslatef(-0.1,-0.3,0);//左腿
   glRotatef(-angle7, 0,0,1);
   glTranslatef(0, -0.2, 0);
   myLeg();
   //請把小腿及腳掌做出來
   glPushMatrix();
    glTranslatef(0,-0.2,0);//右腿
    glRotatef(-angle8, 0,0,1);
    glTranslatef(0, -0.2, 0);
    myLeg();  
   glPopMatrix();  
  glPopMatrix();
 glPopMatrix();
 glutSwapBuffers();
}



void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angle1+=3;
if(key=='2') angle1-=3;
if(key=='3') angle2+=3;
if(key=='4') angle2-=3;
if(key=='5') angle3+=3;
if(key=='6') angle3-=3;
if(key=='7') angle4+=3;
if(key=='8') angle4-=3;

if(key=='s'){//存檔
if(fout==NULL)fout=fopen("my_output_file_for_animation.txt","w+");
fprintf(fout,"%f %f %f %f %f %f %f %f \n",angle1,angle2,angle3,angle4,angle5,angle6,angle7,angle8);
}
if(key=='r'){//讀黨
if(fin==NULL)fin=fopen("my_output_file_for_animation.txt","r");
fscanf(fin,"%f %f %f %f %f %f %f %f\n",&angle1,&angle2,&angle3,&angle4,&angle5,&angle6,&angle7,&angle8);
printf("%f %f %f %f %f %f %f %f\n",angle1,angle2,angle3,angle4,angle5,angle6,angle7,angle8);
}

if(key=='p'){
if(fin==NULL)fin=fopen("my_output_file_for_animation.txt","r");
glutTimerFunc(1000,timer ,0);
}
glutPostRedisplay();
}
void timer(int t)
{

glutTimerFunc(1000,timer ,0);
fscanf(fin,"%f %f %f %f %f %f %f %f\n",&angle1,&angle2,&angle3,&angle4,&angle5,&angle6,&angle7,&angle8);
printf("%f %f %f %f %f %f %f %f\n",angle1,angle2,angle3,angle4,angle5,angle6,angle7,angle8);


glutPostRedisplay();
}

int main()
{

   
myMP3.Load("CCCCEEEE.mp3");
myMP3.Play();
  
    //PlaySound("Pup.wav",NULL,SND_ASYNC);


 glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
 glutCreateWindow("week12");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myTexture();//不要忘了加哦!
 glutMainLoop();
  return 0;
}

沒有留言:

張貼留言