2014 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2014年6月11日 星期三
2014年5月29日 星期四
Week15
#include<GL/glut.h>
float a=0;
float angle[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleNew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void timer (int t)
{
}
void keyboard(unsigned char key, int x,int y)
{
if(key=='s'){
}else if(key=='r'){
a+=0.1;
angle[0]=angleOld[0]*(1-a)+angleNew[0]*a;
glutPostRedisplay();
}else if(key=='p'){
}
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
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;
}
2014年5月15日 星期四
Week13
0. 講課前, 先考大家幾個問題
0.1. Q: 請問我們課程最後要做出什麼? (大家要有方向!) A: 會跳舞的機器人
0.2. Q: 請問上週教什麼? A: T-R-T 可以對任意軸轉動
0.3. Q: 請問接下來要教什麼?
Week13 調關節、寫檔案、讀檔案、播動畫(timer計時器)
Week14 聲音、特效
Week15 運鏡、節奏
Week16 (buffer...)
Week17 期末作品展示/票選最佳作品!!!!!
0.1. Q: 請問我們課程最後要做出什麼? (大家要有方向!) A: 會跳舞的機器人
0.2. Q: 請問上週教什麼? A: T-R-T 可以對任意軸轉動
0.3. Q: 請問接下來要教什麼?
Week13 調關節、寫檔案、讀檔案、播動畫(timer計時器)
Week14 聲音、特效
Week15 運鏡、節奏
Week16 (buffer...)
Week17 期末作品展示/票選最佳作品!!!!!
-----------------------------------------------------------------------------------------------
目標:會跳舞的機器人
#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 angleX=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("body.png");
img[1]=cvLoadImage("hand.png");
img[2]=cvLoadImage("leg.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();
myBody();
glPushMatrix();
glTranslatef(0.2,0,0);//右手
glRotatef(angleX, 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);//左手
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,-0.3,0);//左腿
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.4, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week13");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();//不要忘了加哦!
glutMainLoop();
}
------------------------------------------------------------------------------------------------------------
#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 angleX=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("body.png");
img[1]=cvLoadImage("hand.png");
img[2]=cvLoadImage("leg.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(angleX, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();//今天的技巧3: 小的在裡面
glTranslatef(0,-0.2,0);//小右手
glRotatef(angleX, 0,0,1);
glTranslatef(0, 0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);//左手
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();//今天的技巧3: 小的在裡面
glTranslatef(0,-0.2,0);//小右手
glRotatef(angleX, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.2,-0.3,0);//右腿
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,-0.3,0);//左腿
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week13");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();//不要忘了加哦!
glutMainLoop();
}
------------------------------------------------------------------------------------------------------------
#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 angleA=0,angleX=0,angleY=0,angleZ=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("body.png");
img[1]=cvLoadImage("hand.png");
img[2]=cvLoadImage("leg.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();
glScalef(0.5,0.5,0.5);
myBody();
glPushMatrix();
glTranslatef(0.2,0,0);//右手
glRotatef(angleA, 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPushMatrix();
glTranslatef(0,-0.25,0);//small右手
glRotatef(angleZ, 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);//左手
glRotatef(-angleA, 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPushMatrix();
glTranslatef(0,-0.25,0);//small左手
glRotatef(angleZ, 0,0,-1);
glTranslatef(0, -0.4, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,-0.3,0);//左腿
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.4, 0);
myLeg();
glPushMatrix();
glTranslatef(0,-0.3,0);//小左腿
glRotatef(-angleY, 0,0,1);
glTranslatef(0, -0.4, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.2,-0.3,0);//右腿
glRotatef(-angleX, 0,0,-1);
glTranslatef(0, -0.4, 0);
myLeg();
glPushMatrix();
glTranslatef(0,-0.3,0);//小右腿
glRotatef(-angleY, 0,0,-1);
glTranslatef(0, -0.4, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleA+=3;
if(key=='3') angleA-=3;
if(key=='a') angleX+=3;
if(key=='d') angleX-=3;
if(key=='w') angleY+=3;
if(key=='s') angleY-=3;
if(key=='q') angleZ+=3;
if(key=='e') angleZ-=3;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week13");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();//不要忘了加哦!
glutMainLoop();
}
-----------------------------------------------------用滑鼠移動-----------------------------------------------------
#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 angle[10]={0,0,0,0,0,0,0,0,0,0};
int angleID = 1;
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("body.png");
img[1]=cvLoadImage("hand.png");
img[2]=cvLoadImage("leg.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();
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.8,0.8,0.8);
myBody();
glPushMatrix();
glTranslatef(0.15,0.25,0);//右手
glRotatef(angle[1], 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();
glTranslatef(0,-0.2,0);//右小手
glRotatef(angle[2], 0,0,1);
glutSolidSphere(0.09,30,30);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.15,0.25,0);//左手
glRotatef(angle[3], 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();
glTranslatef(0,-0.2,0);//左小手
glRotatef(angle[4], 0,0,1);
glutSolidSphere(0.09,30,30);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.15,-0.35,0);//左腿
glRotatef(angle[5], 0,0,1);
glTranslatef(0, -0.2, 0);
myLeg();
glPushMatrix();
glTranslatef(0,-0.2,0);//左小腿
glRotatef(angle[6], 0,0,1);
glutSolidSphere(0.09,30,30);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,-0.35,0);//右腿
glRotatef(angle[7], 0,0,1);
glTranslatef(0, -0.2, 0);
myLeg();
glPushMatrix();
glTranslatef(0,-0.23,0);//右小腿
glRotatef(angle[8], 0,0,1);
glutSolidSphere(0.09,30,30);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
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;
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()
{
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week13");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();//不要忘了加哦!
glutMainLoop();
}
訂閱:
文章 (Atom)






