2014年5月22日 星期四

01160634_童琬尹, HW14, Week14



#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
IplImage * img[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
GLuint id[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int angleID=0;
FILE *fout=NULL, *fin=NULL;//要存檔的技巧1: 用 FILE * fout =NULL; 宣告
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("imgbody.png");
img[1]=cvLoadImage("imgHand1.png");
img[2]=cvLoadImage("imgLeg1.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(){
myPolygon(1);
}
void myHand(){
myPolygon(2);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,0.3,0);
myBody();
glPushMatrix();
glScalef(0.7,0.7,0.7);
glTranslatef(0.2,0.05,0);//右手
glRotatef(angle[1], 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPushMatrix();
glScalef(0.9,0.9,0.9);
glTranslatef(0,-0.25,0);//小右手
glRotatef(angle[2], 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glScalef(0.7,0.7,0.7);
glTranslatef(-0.15,0.1,0);//左手
glRotatef(-angle[3], 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPushMatrix();
glScalef(0.9,0.9,0.9);
glTranslatef(0,-0.25,0);//小左手
glRotatef(-angle[4], 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glScalef(0.7,0.7,0.7);
glTranslatef(-0.15,-0.4,0);//左腿
glRotatef(-angle[5], 0,0,1);
glTranslatef(0, -0.4, 0);
myLeg();
glPushMatrix();
glScalef(0.9,0.9,0.9);
glTranslatef(0,-0.4,0);//小左腿
glRotatef(-angle[6], 0,0,1);
glTranslatef(0, -0.4, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glScalef(0.7,0.7,0.7);
glTranslatef(0.15,-0.4,0);//右腿
glRotatef(angle[7], 0,0,1);
glTranslatef(0, -0.4, 0);
myLeg();
glPushMatrix();
glScalef(0.9,0.9,0.9);
glTranslatef(0,-0.4,0);//小右腿
glRotatef(angle[8], 0,0,1);
glTranslatef(0, -0.4, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void timer(int t);
//FILE * fout = NUll, *fin=NULL;
void keyboard(unsigned char key, int x, int y)
{
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;//右腳
if(key=='8') angleID=8;//小右腳

if(key=='x') printf("%f %f %f %f %f %f %f %f %f %f \n", angle[0], angle[1], angle[2], angle[3], angle[4], angle[5], angle[6],angle[7], angle[8], angle[9] );

 if(key=='s'){//要存檔的技巧
  for(int i=0;i<10;i++) printf("%f ", angle[i]);
  printf("\n");

   ///要存檔的技巧4: 用 if(fout==NULL)做保護, 讓檔案不要一直重開出錯, "開第一次就好了"
  if(fout == NULL) fout = fopen("my_first_file_to_open_yayaya.txt", "w+");///要存檔的技巧2: 開檔案, 要寫檔
  for(int i=0;i<10;i++) fprintf(fout,  "%f ", angle[i]);///要存檔的技巧3: printf() 改裝成 fprintf()
  fprintf(fout, "\n");///要存檔的技巧3: 改裝的技巧是, (函式名)前面加個 f, (參數)前面加個 fout
 }

  if(key=='r'){//要存檔的技巧
  ///要存檔的技巧4: 用 if(fout==NULL)做保護, 讓檔案不要一直重開出錯, "開第一次就好了"
  if(fin == NULL) fin = fopen("my_first_file_to_open_yayaya.txt", "r");///要存檔的技巧2: 開檔案, 要寫檔
  for(int i=0;i<10;i++) fscanf(fin,  "%f ", &angle[i]);///要存檔的技巧3: printf() 改裝成 fprintf()
 }
  if(key=='p'){
if(fin == NULL) fin = fopen("my_first_file_to_open_yayaya.txt", "r");
glutTimerFunc(1000,timer,0);
}


glutPostRedisplay();
}

void timer(int t)
{
glutTimerFunc(500, timer,0);
for(int i=0;i<10;i++) {
fscanf(fin, "%f ",&angle[i]);
}
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()
{
  myMP3.Load("123.mp3");
  myMP3.Play();
// printf("ya %d\n", myMP3);
//PlaySound("doors.wav", NULL, SND_ASYNC);
 
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week13");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();//不要忘了加哦!
glutMainLoop();

}

沒有留言:

張貼留言