2014年5月23日 星期五




#include<stdio.h>
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <stdio.h>
//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;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int angleID=1;
int oldX,oldY;
void mouse(int botton, int state, int x, int y)
{
 oldX=x;
 oldY=y;
}
void motion(int x, int y)
{
 angle[angleID]+= x-oldX; oldX=x;
 glutPostRedisplay();
}

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(){
 glPushMatrix();
  glScalef(0.7, 0.7, 0.7);
  myPolygon(0);
 glPopMatrix();
}
void myLeg(){
 glPushMatrix();
  glScalef(0.5, 0.5, 0.5);
  myPolygon(1);
 glPopMatrix();
}
void myHand(){
 glPushMatrix();
  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();
        myBody();
     glPushMatrix();
         glTranslatef(0.1,0.1,0);//右手
         glRotatef(angle[1], 0,0,1);
      glutWireSphere(0.1,20,20);
         glTranslatef(0, -0.2, 0);
         myHand();
      glPushMatrix();
          glTranslatef(0,-0.2,0);//右手1
          glRotatef(angle[2], 0,0,1);
          glutWireSphere(0.1,20,20);
          glTranslatef(0, -0.2, 0);
          myHand();
     glPopMatrix();
     glPopMatrix();
     glPushMatrix();
         glTranslatef(-0.1,0.1,0);//左手
         glRotatef(-angle[3], 0,0,1);
      glutWireSphere(0.1,20,20);
         glTranslatef(0, -0.2, 0);
         myHand();
      glPushMatrix();
          glTranslatef(0,-0.2,0);//左手1
          glRotatef(-angle[4], 0,0,1);
       glutWireSphere(0.1,20,20);
          glTranslatef(0, -0.2, 0);
          myHand();
      glPopMatrix();
     glPopMatrix();
     glPushMatrix();
         glTranslatef(-0.1,-0.2,0);//左腿
         glRotatef(-angle[5], 0,0,1);
      glutWireSphere(0.1,20,20);
         glTranslatef(0, -0.2, 0);
         myLeg();
      glPushMatrix();
          glTranslatef(0,-0.2,0);//左腿1
          glRotatef(-angle[6], 0,0,1);
        glutWireSphere(0.1,20,20);
          glTranslatef(0, -0.2, 0);
       myLeg();
      glPopMatrix();
     glPopMatrix();
     glPushMatrix();
         glTranslatef(0.1,-0.2,0);//右腿
         glRotatef(-angle[7], 0,0,-1);
      glutWireSphere(0.1,20,20);
         glTranslatef(0, -0.2, 0);
         myLeg();
         glPushMatrix();
          glTranslatef(0,-0.2,0);//右腿1
          glRotatef(-angle[8], 0,0,-1);
       glutWireSphere(0.1,20,20);
          glTranslatef(0, -0.2, 0);
          myLeg();
      glPopMatrix();
     glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
FILE *fout=NULL, *fin=NULL;
void timer(int t)
{
     glutTimerFunc(1000,timer,0);
      if(fin==NULL) fin = fopen("file.txt","r");
     fscanf(fin,"%f %f %f %f %f %f %f\n",&angle[0], &angle[1],&angle[2],&angle[3],&angle[4],&angle[5],&angle[6],&angle[7]);
 printf("%f %f %f %f %f %f %f\n",angle[0], angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
 glutPostRedisplay();
}
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=='s'){
 if(fout==NULL) fout = fopen("file.txt","w+");
 printf("%f %f %f %f %f %f %f\n",angle[0], angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
 fprintf(fout,"%f %f %f %f %f %f %f\n",angle[0], angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
 }
 if(key=='r'){
 if(fin==NULL) fin = fopen("file.txt","r");
 fscanf(fin,"%f %f %f %f %f %f %f\n",&angle[0], &angle[1],&angle[2],&angle[3],&angle[4],&angle[5],&angle[6],&angle[7]);
 printf("%f %f %f %f %f %f %f\n",angle[0], angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
}
if(key=='p'){
glutTimerFunc(1000,timer,0);
}
 glutPostRedisplay();
}

int main()
{
 glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
 glutCreateWindow("week12");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 glutMouseFunc(mouse);
 glutMotionFunc(motion);
 myTexture();//不要忘了加哦!
 glutMainLoop();
}


沒有留言:

張貼留言