2014年5月22日 星期四

week14

#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <stdio.h>

IplImage * img[40];
GLuint id[40];
float angle1=0, angle2=0, angle3=0, angle4=0, angle5=0, angle6=0, angle7=0, angle8=0;
void myPrepareTexture(int i)
{
 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++){
  myPrepareTexture(i);
 }
}
void myPolygon(int num){
glPushMatrix();
glScalef(0.3, 0.8, 0.8);
glBindTexture(GL_TEXTURE_2D, idew[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();
 
  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);
myBody();
glPushMatrix();
glTranslatef(0.2,0,0);//右手
glRotatef(angle1, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();
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();
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 timer(int t);
FILE * fout = NULL, *fin=NULL;
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","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()
{
 glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
 glutCreateWindow("week12");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myTexture();
 glutMainLoop();
}


沒有留言:

張貼留言