http://www.youtube.com/watch?v=7Ia61CtMIRA
2014 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2014年6月12日 星期四
2014年6月6日 星期五
week16
*利用作業七來修改程式碼*
1. gluLookAt()做視角
程式碼:
float eyex=0.3,eyey=0.2,eyez=0.4,objx=0,objy=0,objz=0,upx=0,upy=1,upz=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyex,eyey,eyez,objx,objy,objz,upx,upy,upz);
glRotatef(angleX, 0,1,0);
glPushMatrix();//右肩
glTranslatef(0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//右臂
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//右手
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0, 0.55, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//左臂
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//左手
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左大腿
glTranslatef(-0.1, -0.15, 0);
glRotatef(angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(-0.01, -0.3, 0);//左腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix();//右大腿
glTranslatef(0.1, -0.15, 0);
glRotatef(-angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(0, -0.3, 0);//右腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix(); //身體
glTranslatef(0, 0.1, 0);
glScalef(0.4, 0.6, 0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
2. 使用空白鍵做運鏡
程式碼:
float angleX =0, angle2 = 0, angle3 = 0;
float eyex=0.3,eyey=0.2,eyez=0.4,objx=0,objy=0,objz=0,upx=0,upy=1,upz=0; *將宣告往前提*
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;//全身的轉動
if(key=='2') angleX-=5;//全身的轉動
if(key=='3') angle2+=5;//手的轉動
if(key=='4') angle2-=5;//手的轉動
if(key=='5') angle3+=5;//腳的轉動
if(key=='6') angle3-=5;//腳的轉動
if(key=='0') //控制運鏡轉動
{
eyex-=0.02;
eyey-=0.02;
}
glutPostRedisplay();
}
3. 解決破圖
4. 邊旋轉邊放大,背景變色
#include <GL/glut.h>
#include <math.h>
float angleX =0, angle2 = 0, angle3 = 0;
float eyex=0.3,eyey=1.2,eyez=0.4,objx=0,objy=0,objz=0,upx=0,upy=1,upz=0;
float thesize=1;
void timerRotateShow(int t)
{
if(t%10==0) glClearColor(rand()%255/255.0,rand()%255/255.0,rand()%255/255.0,rand()%255/255.0);
glutTimerFunc(30,timerRotateShow,t+1);
eyex=cos(t/100.0*3.1415);
eyez=sin(t/100.0*3.1415);
eyey-=0.005;
thesize+=0.05;
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;//全身的轉動
if(key=='2') angleX-=5;//全身的轉動
if(key=='3') angle2+=5;//手的轉動
if(key=='4') angle2-=5;//手的轉動
if(key=='5') angle3+=5;//腳的轉動
if(key=='6') angle3-=5;//腳的轉動
if(key=='0')
{
eyex-=0.02;
eyey-=0.02;
}
else if(key=='a')
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);
}
else if(key=='z')
{
glutTimerFunc(0,timerRotateShow,0);
}
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyex,eyey,eyez,objx,objy,objz,upx,upy,upz);
glScalef(thesize,thesize,thesize);
glRotatef(angleX, 0,1,0);
glPushMatrix();//右肩
glTranslatef(0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//右臂
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//右手
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0, 0.55, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//左臂
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//左手
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左大腿
glTranslatef(-0.1, -0.15, 0);
glRotatef(angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(-0.01, -0.3, 0);//左腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix();//右大腿
glTranslatef(0.1, -0.15, 0);
glRotatef(-angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(0, -0.3, 0);//右腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix(); //身體
glTranslatef(0, 0.1, 0);
glScalef(0.4, 0.6, 0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
}
1. gluLookAt()做視角
程式碼:
float eyex=0.3,eyey=0.2,eyez=0.4,objx=0,objy=0,objz=0,upx=0,upy=1,upz=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyex,eyey,eyez,objx,objy,objz,upx,upy,upz);
glRotatef(angleX, 0,1,0);
glPushMatrix();//右肩
glTranslatef(0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//右臂
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//右手
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0, 0.55, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//左臂
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//左手
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左大腿
glTranslatef(-0.1, -0.15, 0);
glRotatef(angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(-0.01, -0.3, 0);//左腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix();//右大腿
glTranslatef(0.1, -0.15, 0);
glRotatef(-angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(0, -0.3, 0);//右腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix(); //身體
glTranslatef(0, 0.1, 0);
glScalef(0.4, 0.6, 0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
2. 使用空白鍵做運鏡
程式碼:
float angleX =0, angle2 = 0, angle3 = 0;
float eyex=0.3,eyey=0.2,eyez=0.4,objx=0,objy=0,objz=0,upx=0,upy=1,upz=0; *將宣告往前提*
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;//全身的轉動
if(key=='2') angleX-=5;//全身的轉動
if(key=='3') angle2+=5;//手的轉動
if(key=='4') angle2-=5;//手的轉動
if(key=='5') angle3+=5;//腳的轉動
if(key=='6') angle3-=5;//腳的轉動
if(key=='0') //控制運鏡轉動
{
eyex-=0.02;
eyey-=0.02;
}
glutPostRedisplay();
}
3. 解決破圖
程式碼:
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;//全身的轉動
if(key=='2') angleX-=5;//全身的轉動
if(key=='3') angle2+=5;//手的轉動
if(key=='4') angle2-=5;//手的轉動
if(key=='5') angle3+=5;//腳的轉動
if(key=='6') angle3-=5;//腳的轉動
if(key=='0')
{
eyex-=0.02;
eyey-=0.02;
}
else if(key=='a')
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);
}
glutPostRedisplay();
}
4. 邊旋轉邊放大,背景變色
程式碼:
#include <stdlib.h>#include <GL/glut.h>
#include <math.h>
float angleX =0, angle2 = 0, angle3 = 0;
float eyex=0.3,eyey=1.2,eyez=0.4,objx=0,objy=0,objz=0,upx=0,upy=1,upz=0;
float thesize=1;
void timerRotateShow(int t)
{
if(t%10==0) glClearColor(rand()%255/255.0,rand()%255/255.0,rand()%255/255.0,rand()%255/255.0);
glutTimerFunc(30,timerRotateShow,t+1);
eyex=cos(t/100.0*3.1415);
eyez=sin(t/100.0*3.1415);
eyey-=0.005;
thesize+=0.05;
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;//全身的轉動
if(key=='2') angleX-=5;//全身的轉動
if(key=='3') angle2+=5;//手的轉動
if(key=='4') angle2-=5;//手的轉動
if(key=='5') angle3+=5;//腳的轉動
if(key=='6') angle3-=5;//腳的轉動
if(key=='0')
{
eyex-=0.02;
eyey-=0.02;
}
else if(key=='a')
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);
}
else if(key=='z')
{
glutTimerFunc(0,timerRotateShow,0);
}
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyex,eyey,eyez,objx,objy,objz,upx,upy,upz);
glScalef(thesize,thesize,thesize);
glRotatef(angleX, 0,1,0);
glPushMatrix();//右肩
glTranslatef(0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//右臂
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//右手
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0, 0.55, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.25, 0.3, 0);
glutSolidSphere(0.1, 30, 30);
glPushMatrix();//左臂
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.2, 0, 0);
glScalef(0.5, 0.1, 0.1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//左手
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.5, 0, 0);
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左大腿
glTranslatef(-0.1, -0.15, 0);
glRotatef(angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(-0.01, -0.3, 0);//左腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix();//右大腿
glTranslatef(0.1, -0.15, 0);
glRotatef(-angle3, 1, 0, 0);
glTranslatef(0, -0.3, 0);
glPushMatrix();
glScalef(0.13, 0.6, 0.13);
glutSolidCube(1);
glPopMatrix();
glTranslatef(0, -0.3, 0);//右腳掌
glutSolidSphere(0.1, 30, 30);
glPopMatrix();
glPushMatrix(); //身體
glTranslatef(0, 0.1, 0);
glScalef(0.4, 0.6, 0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
}
2014年5月30日 星期五
week15
1.內插法
程式碼:
#include<GL/glut.h>
float a=0;
float angle=0;
float angleOld=0;
float angleNew=90;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='z')
{
a+=0.1;
angle=angleOld*(1-a)+angleNew*a;
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
程式碼:
#include<GL/glut.h>
float a=0;
float angle=0;
float angleOld=0;
float angleNew=90;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='z')
{
a+=0.1;
angle=angleOld*(1-a)+angleNew*a;
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
2.改用矩陣方式儲存
修改程式部分;
#include<GL/glut.h>
float a=0;
float angle[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,90,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();
glPushMatrix();
glTranslatef(0.5,0,0);
glRotatef(angle[1],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='z')
{
a+=0.1;
angle[0]=angleOld[0]*(1-a)+angleNew[0]*a;
angle[1]=angleOld[1]*(1-a)+angleNew[1]*a;
angle[2]=angleOld[2]*(1-a)+angleNew[2]*a;
angle[3]=angleOld[3]*(1-a)+angleNew[3]*a;
angle[4]=angleOld[4]*(1-a)+angleNew[4]*a;
angle[5]=angleOld[5]*(1-a)+angleNew[5]*a;
angle[6]=angleOld[6]*(1-a)+angleNew[6]*a;
angle[7]=angleOld[7]*(1-a)+angleNew[7]*a;
angle[8]=angleOld[8]*(1-a)+angleNew[8]*a;
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
3.自動轉動
程式碼:
#include<GL/glut.h>
float a=0;
float angle[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,90,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();
glTranslatef(angle[3],angle[4],angle[5]);
glPushMatrix();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0.5,0,0);
glRotatef(angle[1],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void readNext()
{
a+=0.1;
for(int i=0;i<20;i++)
{
angle[i]=angleOld[i]*(1-a)+angleNew[i]*a;
}
}
void timer(int t)
{
glutTimerFunc(10,timer,0);
readNext();
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='z')//手動轉動
{
readNext();
}
else if(key=='p') //自動轉動
{
glutTimerFunc(0,timer,0);
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
4.加入滑鼠控制、存檔、讀檔
程式碼:
#include<stdio.h>
#include<GL/glut.h>
float a=0;
float angle[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,90,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};
FILE *fout=NULL, *fin=NULL;
int angleID=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(angle[3],angle[4],angle[5]);
glPushMatrix();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0.5,0,0);
glRotatef(angle[1],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void readNext()
{
if(fin==NULL) fin=fopen("this_if_my_file_to_animation.txt","r");
a+=0.05;
for(int i=0;i<20;i++)
{
angle[i]=angleOld[i]*(1-a)+angleNew[i]*a;
}
if(a>=1.0)
{
a=0.0;
for(int i=0;i<20;i++)
{
angleOld[i]=angleNew[i];
fscanf(fin,"%f ",&angleNew[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();
}
void timer(int t)
{
glutTimerFunc(10,timer,0);
readNext();
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='s')
{
if(fout==NULL) fout=fopen("this_if_my_file_to_animation.txt","w+");
for(int i=0;i<20;i++)
{
printf("%f",angle[i]);
fprintf(fout,"%f ",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
}
if(key=='z')
{
readNext();
}
else if(key=='p')
{
glutTimerFunc(0,timer,0);
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
2014年5月23日 星期五
WEEK14
1. 播放音樂 (歡呼聲)
3. 將音樂以MP3檔方式播放
程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
for(int i=0; i<4; i++){//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
img[i] = cvLoadImage( filename[i] );
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);//head
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 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();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angle[0], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(angle[1],0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angle[4], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix() ; //left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle[5], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angle[6]), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix(); //right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle[7], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=NULL;
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID=0;
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=='a')
{
if(fout==NULL) fout=fopen("this_if_my_file_to_animation.txt","w+");
printf("%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]);
fprintf(fout,"%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]);
}
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();
glutMainLoop();
}
6.自動轉動
程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
for(int i=0; i<4; i++){//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
img[i] = cvLoadImage( filename[i] );
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);//head
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 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();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angle[0], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(angle[1],0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angle[4], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix() ; //left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle[5], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angle[6]), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix(); //right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle[7], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=NULL, *fin=NULL;
void timer(int t)
{
glutTimerFunc(1000,timer,0);
if(fin==NULL) fin=fopen("this_if_my_file_to_animation.txt","r");
fscanf(fin,"%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]);
printf("%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]);
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID=0;
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=='a')
{
if(fout==NULL) fout=fopen("this_if_my_file_to_animation.txt","w+");
printf("%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]);
fprintf(fout,"%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]);
}
if(key=='s')
{
if(fin==NULL) fin=fopen("this_if_my_file_to_animation.txt","r");
fscanf(fin,"%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]);
printf("%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]);
}
if(key=='p')
{
glutTimerFunc(1000,timer,0);
}
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();
glutMainLoop();
}
程式碼:
#include <windows.h>
#include <mmsystem.h>
int main()
{
PlaySound("cheer.wav",NULL,SND_SYNC);
}
2. 開新專案加上音樂播放
新增程式碼:
#include <windows.h>
#include <mmsystem.h>
int main()
{
PlaySound("cheer.wav",NULL,SND_ASYNC); //SND_SYNC→要等同步 SND_ASYNC→不等同步
}
*要將音樂檔複製到資料夾中!!
3. 將音樂以MP3檔方式播放
新增程式碼:
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
int main()
{
myMP3.Load("Hero.mp3");
myMP3.Play();
}
4. 增加存檔
#include <stdio.h>
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
for(int i=0; i<4; i++){//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
img[i] = cvLoadImage( filename[i] );
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);//head
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 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();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angle[0], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(angle[1],0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angle[4], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix() ; //left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle[5], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angle[6]), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix(); //right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle[7], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=NULL;
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID=0;
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=='a')
{
if(fout==NULL) fout=fopen("this_if_my_file_to_animation.txt","w+");
printf("%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]);
fprintf(fout,"%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]);
}
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();
glutMainLoop();
}
5. 讀檔轉動
程式碼:
FILE *fout=NULL, *fin=NULL;
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID=0;
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=='a')
{
if(fout==NULL) fout=fopen("this_if_my_file_to_animation.txt","w+");
printf("%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]);
fprintf(fout,"%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]);
}
if(key=='s')
{
if(fin==NULL) fin=fopen("this_if_my_file_to_animation.txt","r");
fscanf(fin,"%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]);
printf("%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]);
}
glutPostRedisplay();//請電腦重新畫一次畫面
}
6.自動轉動
程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
for(int i=0; i<4; i++){//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
img[i] = cvLoadImage( filename[i] );
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);//head
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 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();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angle[0], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(angle[1],0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angle[4], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix() ; //left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle[5], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angle[6]), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix(); //right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle[7], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=NULL, *fin=NULL;
void timer(int t)
{
glutTimerFunc(1000,timer,0);
if(fin==NULL) fin=fopen("this_if_my_file_to_animation.txt","r");
fscanf(fin,"%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]);
printf("%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]);
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID=0;
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=='a')
{
if(fout==NULL) fout=fopen("this_if_my_file_to_animation.txt","w+");
printf("%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]);
fprintf(fout,"%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]);
}
if(key=='s')
{
if(fin==NULL) fin=fopen("this_if_my_file_to_animation.txt","r");
fscanf(fin,"%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]);
printf("%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]);
}
if(key=='p')
{
glutTimerFunc(1000,timer,0);
}
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();
glutMainLoop();
}
2014年5月16日 星期五
week13
1. 複習上週:
程式碼:
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0, angle0=0, angle1=0, angle2=0, angle3=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand0.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
for(int i=0; i<4; i++){//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
img[i] = cvLoadImage( filename[i] );
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);//head
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 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();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(30, 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angleX), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();
glutMainLoop();
}
2. 增加左右手臂與腳
變更程式碼:
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(angleX,0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(-angleX,0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix() ; //left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angleX), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix(); //right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
3. 增加滑鼠與鍵盤控制關節轉動
程式碼:
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand0.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
for(int i=0; i<4; i++){//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
img[i] = cvLoadImage( filename[i] );
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);//head
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 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();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angle[0], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(angle[1],0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();
glTranslatef(0,-0.25,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(0,-0.25,0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angle[4], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix() ; //left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle[5], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angle[6]), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix(); //right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle[7], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID=0;
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;
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();
glutMainLoop();
}
訂閱:
文章 (Atom)

















