- 方法:(內插法)
- 動作存檔(只存關鍵影格)
- #include <GL/glut.h>
- #include <stdio.h>
- FILE * fout=NULL;
- FILE * fin=NULL;
- int angle[10]={0,0,0,0,0, 0,0,0,0,0};
- int angleID=0;
- int oldX, oldY;
- void display();
- void motion(int x, int y);
- void mouse(int button, int state, int x, int y);
- void keyboard(unsigned char key, int x, int y);
- void saveAll();
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1,1,1);
- glutSolidTeapot(0.2);
- glPushMatrix();
- glTranslatef(0.2, 0,0 );
- glRotatef(angle[0], 0,0,1);
- glTranslatef(0.2, 0,0 );
- glColor3f(1,0,0);
- glutSolidTeapot(0.2);
- glPushMatrix();
- glTranslatef(0.2, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(0.2, 0,0);
- glColor3f(1,1,0);
- glutSolidTeapot(0.2);
- glPopMatrix();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(-0.2, 0,0 );
- glRotatef(angle[2], 0,0,1);
- glTranslatef(-0.2, 0,0 );
- glColor3f(0,1,0);
- glutSolidTeapot(0.2);
- glPushMatrix();
- glTranslatef(-0.2, 0,0);
- glRotatef(angle[3], 0,0,1);
- glTranslatef(-0.2, 0,0);
- glColor3f(0,0,1);
- glutSolidTeapot(0.2);
- glPopMatrix();
- glPopMatrix();
- glutSwapBuffers();
- }
- void mouse(int button, int state, int x, int y)
- {
- oldX=x; oldY=y;
- }
- void saveAll()///(自己寫的函式)全部存檔
- {
- if(fout==NULL) fout=fopen("motion.txt", "w+");
- for(int i=0; i<10; i++){
- printf( "%d ", angle[i]);
- fprintf(fout, "%d ", angle[i]);
- }
- printf( "\n");
- fprintf(fout, "\n");
- }
- void motion(int x, int y)
- {
- angle[angleID] += (x-oldX);
- oldX=x;
- ///不要一直saveAll() saveAll();
- glutPostRedisplay();
- }
- void readAll()
- {
- if(fin==NULL) fin=fopen("motion.txt", "r");
- for(int i=0; i<10; i++){
- fscanf(fin, "%d", &angle[i]);
- }
- }
- 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=='s') saveAll();///調好動作,才存檔
- if(key=='r'){
- readAll();
- glutPostRedisplay();
- }
- }
- int main(int argc, char**argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week16");
- glutDisplayFunc(display);
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- }

- Alpha補間內插(自動播放)
- #include <GL/glut.h>
- #include <stdio.h>
- FILE * fout=NULL;
- FILE * fin=NULL;
- int angle[10]={0,0,0,0,0, 0,0,0,0,0};
- int angleID=0;
- int oldX, oldY;
- int newAngle[10],oldAngle[10];
- void display();
- void motion(int x, int y);
- void mouse(int button, int state, int x, int y);
- void keyboard(unsigned char key, int x, int y);
- void saveAll();
- void timer(int t)
- {
- glutTimerFunc(100,timer,t+1);//100ms 之後開啟timer,timer(t+1)
- if(t==0){
- if(fin==NULL) fin=fopen("motion.txt", "r");
- for(int i=0; i<10; i++){
- fscanf(fin, "%d", &newAngle[i]);
- }
- }
- if(t%10==0){
- for(int i=0;i<10;i++){
- oldAngle[i]=newAngle[i];
- fscanf(fin,"%d",&newAngle[i]);
- }
- }
- float Alpha = (t%10)/10.0;///一秒十畫
- for(int i=0;i<10; i++){
- angle[i]=Alpha*newAngle[i]+(1-Alpha)*oldAngle[i];
- printf("%d",angle[i]);///debug用
- }
- printf("%.2f\n",Alpha);///debug用
- glutPostRedisplay();
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(1,1,1);
- glutSolidTeapot(0.2);
- glPushMatrix();
- glTranslatef(0.2, 0,0 );
- glRotatef(angle[0], 0,0,1);
- glTranslatef(0.2, 0,0 );
- glColor3f(1,0,0);
- glutSolidTeapot(0.2);
- glPushMatrix();
- glTranslatef(0.2, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(0.2, 0,0);
- glColor3f(1,1,0);
- glutSolidTeapot(0.2);
- glPopMatrix();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(-0.2, 0,0 );
- glRotatef(angle[2], 0,0,1);
- glTranslatef(-0.2, 0,0 );
- glColor3f(0,1,0);
- glutSolidTeapot(0.2);
- glPushMatrix();
- glTranslatef(-0.2, 0,0);
- glRotatef(angle[3], 0,0,1);
- glTranslatef(-0.2, 0,0);
- glColor3f(0,0,1);
- glutSolidTeapot(0.2);
- glPopMatrix();
- glPopMatrix();
- glutSwapBuffers();
- }
- void mouse(int button, int state, int x, int y)
- {
- oldX=x; oldY=y;
- }
- void saveAll()///(自己寫的函式)全部存檔
- {
- if(fout==NULL) fout=fopen("motion.txt", "w+");
- for(int i=0; i<10; i++){
- printf( "%d ", angle[i]);
- fprintf(fout, "%d ", angle[i]);
- }
- printf( "\n");
- fprintf(fout, "\n");
- }
- void motion(int x, int y)
- {
- angle[angleID] += (x-oldX);
- oldX=x;
- ///不要一直saveAll() saveAll();
- glutPostRedisplay();
- }
- void readAll()
- {
- if(fin==NULL) fin=fopen("motion.txt", "r");
- for(int i=0; i<10; i++){
- fscanf(fin, "%d", &angle[i]);
- }
- }
- 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=='s') saveAll();///調好動作,才存檔
- if(key=='r'){
- readAll();
- glutPostRedisplay();
- }
- if(key=='p'){
- glutTimerFunc(0,timer,0);//0ms 馬上開啟timer,timer(0)
- }
- }
- int main(int argc, char**argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week16");
- glutDisplayFunc(display);
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- }

- 增加更多關節,整體上下移動
- #include <GL/glut.h>
- #include <stdio.h>
- FILE * fout=NULL;
- FILE * fin=NULL;
- int angle[20]={};///增加關節
- int angleID=0;
- int oldX, oldY;
- void display();
- void motion(int x, int y);
- void mouse(int button, int state, int x, int y);
- void keyboard(unsigned char key, int x, int y);
- void saveAll();
- int oldAngle[20]={};///增加關節
- int newAngle[20]={};///增加關節
- void timer(int t)
- {
- glutTimerFunc(100, timer, t+1);
- if(t==0){
- if(fin==NULL) fin=fopen("motion.txt", "r");
- for(int i=0; i<20; i++){///增加關節
- fscanf(fin, "%d", &newAngle[i]);
- }
- }
- if(t%10==0){
- for(int i=0; i<20; i++){///增加關節
- oldAngle[i] = newAngle[i];
- fscanf(fin, "%d", &newAngle[i]);
- }
- }
- float Alpha = (t%10)/10.0;
- for(int i=0; i<20; i++){///增加關節
- angle[i] = Alpha*newAngle[i]+(1-Alpha)*oldAngle[i];
- printf("%d ", angle[i]);///debug用
- }
- printf(" %.2f\n", Alpha);///debug用
- glutPostRedisplay();
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();///保護一下
- glTranslatef( angle[11]/200.0, angle[12]/200.0, 0);
- ///增加關節, 整數變成 float,才不會跳
- glColor3f(1,1,1);
- glutSolidTeapot(0.2);
- glPushMatrix();
- glTranslatef(0.2, 0,0 );
- glRotatef(angle[0], 0,0,1);
- glTranslatef(0.2, 0,0 );
- glColor3f(1,0,0);
- glutSolidTeapot(0.2);
- glPushMatrix();
- glTranslatef(0.2, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(0.2, 0,0);
- glColor3f(1,1,0);
- glutSolidTeapot(0.2);
- glPopMatrix();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(-0.2, 0,0 );
- glRotatef(angle[2], 0,0,1);
- glTranslatef(-0.2, 0,0 );
- glColor3f(0,1,0);
- glutSolidTeapot(0.2);
- glPushMatrix();
- glTranslatef(-0.2, 0,0);
- glRotatef(angle[3], 0,0,1);
- glTranslatef(-0.2, 0,0);
- glColor3f(0,0,1);
- glutSolidTeapot(0.2);
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();///保護一下
- glutSwapBuffers();
- }
- void mouse(int button, int state, int x, int y)
- {
- oldX=x; oldY=y;
- }
- void saveAll()
- {
- if(fout==NULL) fout=fopen("motion.txt", "w+");
- for(int i=0; i<20; i++){///增加關節
- printf( "%d ", angle[i]);
- fprintf(fout, "%d ", angle[i]);
- }
- printf( "\n");
- fprintf(fout, "\n");
- }
- void motion(int x, int y)
- {
- angle[angleID] += (x-oldX);
- if(angleID==11) angle[angleID+1] += (oldY-y);///增加關節
- ///如果是移動的關節,一次動2個 angieID, angleID+1
- oldX=x; oldY=y;///增加關節
- glutPostRedisplay();
- }
- void readAll()
- {
- if(fin==NULL) fin=fopen("motion.txt", "r");
- for(int i=0; i<20; i++){//增加關節
- fscanf(fin, "%d", &angle[i]);
- }
- }
- 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=='a') angleID=11;///移動的關節 ///增加關節
- if(key=='s') saveAll();
- if(key=='r'){
- readAll();
- glutPostRedisplay();
- }
- if(key=='p'){
- glutTimerFunc(0, timer, 0);
- }
- }
- int main(int argc, char**argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week16");
- glutDisplayFunc(display);
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- }

2020電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2020年6月10日 星期三
(*^ω^*) 喵~Week16
➽動畫內插
訂閱:
張貼留言 (Atom)

沒有留言:
張貼留言