2014年5月9日 星期五

無魂無體就像稻草人現在要把茶壺貼上木頭人

#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0, angle1=0;

void myTexture()
{
GLuint id;
IplImage * img = cvLoadImage("Body.jpg");
cvCvtColor(img, img, CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
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->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->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();
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//right hand
glTranslatef( 0.1, 0, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle1, 0,0,1);
glTranslatef(0, 0.15, 0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angleX), 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPushMatrix();//right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle1, 0,0,1);
glTranslatef(0, 0.15, 0);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();


glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3') angle1+=5;
if(key=='4') angle1-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();

glutMainLoop();
}

沒有留言:

張貼留言