電腦圖學_week16
2021/06/07(一)
今日的上課內容:
- 複習15週程式
- 角度改成多個角度
- 轉動特定關節角度
- 滑鼠控制轉動角度,鍵盤控制特定關節
- 可以存檔的轉動特定角度
- 會邊讀動作和邊做動作
今日的程式碼:
- ///主題: 按按鈕轉動特定角度
- #include <stdio.h> ///printf() fprintf() fscanf() fopen() step 01外掛
- #include <GL/glut.h>
- float angle[20] = {},diff = 2;///上週程式,會增加.會減少!!!
- int angleID=0; ///現在有20個angle,利用angle[0],angle[1]...angle[angleID]
- void timer(int t)///上周程式,會增加.會減少!!!
- {
- glutTimerFunc(30, timer,t+1);///上週程式,設定新的timer
- angle[angleID] += diff;///上週程式,會增加.會減少!!!
- if(angle[angleID]>90) diff =-2;///上週程式,會增加.會減少!!!
- if(angle[angleID]<0) diff = +2;///上週程式,會增加.會減少!!!
- glutPostRedisplay();///上週教display()
- }
- void keyboard(unsigned char key,int x,int y) ///keyboard()需要
- {
- if(key=='0') angleID=0; ///之後timer()會改變關節 angle[0]
- if(key=='1') angleID=1; ///之後timer()會改變關節 angle[1]
- if(key=='2') angleID=2; ///之後timer()會改變關節 angle[2]
- if(key=='3') angleID=3; ///之後timer()會改變關節 angle[3]
- }
- void display()
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();///Step05-2
- glutSolidTeapot( 0.3 );///身體
- glPushMatrix();///Step05-2
- glTranslatef(-0.3,0,0);
- glRotatef(angle[0], 0,0,1);
- glTranslatef(-0.3,0,0);
- glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
- glPushMatrix();
- glTranslatef(-0.3, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(-0.3, 0,0);
- glutSolidTeapot( 0.3 );///左手肘
- glPopMatrix();
- glPopMatrix();///Step05-2
- glPushMatrix();///Step05-2
- glTranslatef(+0.3,0,0);
- glRotatef(-angle[2], 0,0,1);
- glTranslatef(+0.3,0,0);
- glutSolidTeapot( 0.3 );///右手臂
- glPushMatrix();///step05-3
- glTranslatef(+0.3,0,0);
- glRotatef(-angle[3], 0,0,1);
- glTranslatef(+0.3,0,0);
- glutSolidTeapot( 0.3 );///右手肘
- glPopMatrix();///Step05-3
- glPopMatrix();///Step05-2
- glPopMatrix();///Step05-2
- glutSwapBuffers();
- }
- int main(int argc , char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
- glutCreateWindow("week15 many angles");
- glutKeyboardFunc(keyboard);
- glutDisplayFunc(display);
- glutTimerFunc(0, timer, 0);
- glutMainLoop();
- }///先不放貼圖,先不放打光
- ///主題: 用滑鼠操作角度,按按鈕選取特定關節
- #include <stdio.h> ///printf() fprintf() fscanf() fopen() step 01外掛
- #include <GL/glut.h>
- float angle[20] = {},diff = 2;///上週程式,會增加.會減少!!!
- int angleID=0; ///現在有20個angle,利用angle[0],angle[1]...angle[angleID]
- void timer(int t)///上周程式,會增加.會減少!!!
- ///timer()什麼都不做
- {
- ///glutTimerFunc(30, timer,t+1);///上週程式,設定新的timer
- ///angle[angleID] += diff;///上週程式,會增加.會減少!!!
- ///if(angle[angleID]>90) diff =-2;///上週程式,會增加.會減少!!!
- ///if(angle[angleID]<0) diff = +2;///上週程式,會增加.會減少!!!
- ///glutPostRedisplay();///上週教display()
- }
- ///想要用mouse() 及 motion() 來操作關節!!!(期末作品)第7週教學
- 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();///上週display()
- }
- void keyboard(unsigned char key,int x,int y) ///keyboard()需要
- {
- if(key=='0') angleID=0; ///之後timer()會改變關節 angle[0]
- if(key=='1') angleID=1; ///之後timer()會改變關節 angle[1]
- if(key=='2') angleID=2; ///之後timer()會改變關節 angle[2]
- if(key=='3') angleID=3; ///之後timer()會改變關節 angle[3]
- }
- void display()
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();///Step05-2
- glutSolidTeapot( 0.3 );///身體
- glPushMatrix();///Step05-2
- glTranslatef(-0.3,0,0);
- glRotatef(angle[0], 0,0,1);
- glTranslatef(-0.3,0,0);
- glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
- glPushMatrix();
- glTranslatef(-0.3, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(-0.3, 0,0);
- glutSolidTeapot( 0.3 );///左手肘
- glPopMatrix();
- glPopMatrix();///Step05-2
- glPushMatrix();///Step05-2
- glTranslatef(+0.3,0,0);
- glRotatef(-angle[2], 0,0,1);
- glTranslatef(+0.3,0,0);
- glutSolidTeapot( 0.3 );///右手臂
- glPushMatrix();///step05-3
- glTranslatef(+0.3,0,0);
- glRotatef(-angle[3], 0,0,1);
- glTranslatef(+0.3,0,0);
- glutSolidTeapot( 0.3 );///右手肘
- glPopMatrix();///Step05-3
- glPopMatrix();///Step05-2
- glPopMatrix();///Step05-2
- glutSwapBuffers();
- }
- int main(int argc , char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
- glutCreateWindow("week15 many angles");
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutKeyboardFunc(keyboard);
- glutDisplayFunc(display);
- glutTimerFunc(0, timer, 0);
- glutMainLoop();
- }///先不放貼圖,先不放打光
- ///主題: 滑鼠控制角度,鍵盤控制特定關節還可以儲存角度
- #include <stdio.h> ///printf() fprintf() fscanf() fopen() step 01外掛
- #include <GL/glut.h>
- float angle[20] = {},diff = 2;///上週程式,會增加.會減少!!!
- int angleID=0; ///現在有20個angle,利用angle[0],angle[1]...angle[angleID]
- void timer(int t)///上周程式,會增加.會減少!!!
- ///timer()什麼都不做
- {
- ///glutTimerFunc(30, timer,t+1);///上週程式,設定新的timer
- ///angle[angleID] += diff;///上週程式,會增加.會減少!!!
- ///if(angle[angleID]>90) diff =-2;///上週程式,會增加.會減少!!!
- ///if(angle[angleID]<0) diff = +2;///上週程式,會增加.會減少!!!
- ///glutPostRedisplay();///上週教display()
- }
- ///想要用mouse() 及 motion() 來操作關節!!!(期末作品)第7週教學
- 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();///上週display()
- }
- #include <stdio.h>///step03:上週教的第0步 才能用fopen() printf() fprintf()
- FILE * fout = NULL;///step03:上週教的第1步
- void keyboard(unsigned char key,int x,int y) ///keyboard()需要
- {
- if(key=='0') angleID=0; ///之後timer()會改變關節 angle[0]
- if(key=='1') angleID=1; ///之後timer()會改變關節 angle[1]
- if(key=='2') angleID=2; ///之後timer()會改變關節 angle[2]
- if(key=='3') angleID=3; ///之後timer()會改變關節 angle[3]
- if(key=='s') {///想要存檔 save
- if(fout == NULL) fout = fopen("angle.txt","w+");///step03 若NULL fopen()
- for(int i=0; i<20;i++) printf("%.2f ", angle[i] );///step03 for 迴圈印陣列
- printf("\n");
- for(int i=0; i<20;i++) fprintf(fout, "%.2f ", angle[i] );///step03 for 迴圈印陣列
- fprintf(fout,"\n");
- }
- }
- void display()
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();///Step05-2
- glutSolidTeapot( 0.3 );///身體
- glPushMatrix();///Step05-2
- glTranslatef(-0.3,0,0);
- glRotatef(angle[0], 0,0,1);
- glTranslatef(-0.3,0,0);
- glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
- glPushMatrix();
- glTranslatef(-0.3, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(-0.3, 0,0);
- glutSolidTeapot( 0.3 );///左手肘
- glPopMatrix();
- glPopMatrix();///Step05-2
- glPushMatrix();///Step05-2
- glTranslatef(+0.3,0,0);
- glRotatef(-angle[2], 0,0,1);
- glTranslatef(+0.3,0,0);
- glutSolidTeapot( 0.3 );///右手臂
- glPushMatrix();///step05-3
- glTranslatef(+0.3,0,0);
- glRotatef(-angle[3], 0,0,1);
- glTranslatef(+0.3,0,0);
- glutSolidTeapot( 0.3 );///右手肘
- glPopMatrix();///Step05-3
- glPopMatrix();///Step05-2
- glPopMatrix();///Step05-2
- glutSwapBuffers();
- }
- int main(int argc , char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
- glutCreateWindow("week15 many angles");
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutKeyboardFunc(keyboard);
- glutDisplayFunc(display);
- glutTimerFunc(0, timer, 0);
- glutMainLoop();
- }///先不放貼圖,先不放打光
- ///主題: 邊轉動邊存檔關閉後重開可以讀取動作
- #include <stdio.h> ///printf() fprintf() fscanf() fopen() step 01外掛
- #include <GL/glut.h>
- float angle[20] = {},diff = 2;///上週程式,會增加.會減少!!!
- int angleID=0; ///現在有20個angle,利用angle[0],angle[1]...angle[angleID]
- void timer(int t)///上周程式,會增加.會減少!!!
- ///timer()什麼都不做
- {
- ///glutTimerFunc(30, timer,t+1);///上週程式,設定新的timer
- ///angle[angleID] += diff;///上週程式,會增加.會減少!!!
- ///if(angle[angleID]>90) diff =-2;///上週程式,會增加.會減少!!!
- ///if(angle[angleID]<0) diff = +2;///上週程式,會增加.會減少!!!
- ///glutPostRedisplay();///上週教display()
- }
- ///想要用mouse() 及 motion() 來操作關節!!!(期末作品)第7週教學
- int oldX=0,oldY=0;
- #include <stdio.h>///step03:上週教的第0步 才能用fopen() printf() fprintf()
- FILE * fout = NULL;///step03:上週教的第1步
- FILE * fin = NULL;
- ///搬家搬到樓上
- 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();///上週display()
- ///現在想要邊motion 邊存檔
- if(fout == NULL) fout = fopen("angle.txt","w+");///step03 若NULL fopen()
- for(int i=0; i<20;i++) printf("%.2f ", angle[i] );///step03 for 迴圈印陣列
- printf("\n");
- for(int i=0; i<20;i++) fprintf(fout, "%.2f ", angle[i] );///step03 for 迴圈印陣列
- fprintf(fout,"\n");
- }
- void keyboard(unsigned char key,int x,int y) ///keyboard()需要
- {
- if(key=='0') angleID=0; ///之後timer()會改變關節 angle[0]
- if(key=='1') angleID=1; ///之後timer()會改變關節 angle[1]
- if(key=='2') angleID=2; ///之後timer()會改變關節 angle[2]
- if(key=='3') angleID=3; ///之後timer()會改變關節 angle[3]
- if(key=='s') {///想要存檔 save
- if(fout == NULL) fout = fopen("angle.txt","w+");///step03 若NULL fopen()
- for(int i=0; i<20;i++) printf("%.2f ", angle[i] );///step03 for 迴圈印陣列
- printf("\n");
- for(int i=0; i<20;i++) fprintf(fout, "%.2f ", angle[i] );///step03 for 迴圈印陣列
- fprintf(fout,"\n");
- }
- if(key=='r'){///step04: read 想讀進來
- if(fin == NULL) fin = fopen("angle.txt","r");///NULL, fopen()
- for (int i=0;i<20;i++) fscanf(fin,"%f", &angle[i]); ///step04
- glutPostRedisplay();///step04 重畫畫面
- }
- }
- void display()
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();///Step05-2
- glutSolidTeapot( 0.3 );///身體
- glPushMatrix();///Step05-2
- glTranslatef(-0.3,0,0);
- glRotatef(angle[0], 0,0,1);
- glTranslatef(-0.3,0,0);
- glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
- glPushMatrix();
- glTranslatef(-0.3, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(-0.3, 0,0);
- glutSolidTeapot( 0.3 );///左手肘
- glPopMatrix();
- glPopMatrix();///Step05-2
- glPushMatrix();///Step05-2
- glTranslatef(+0.3,0,0);
- glRotatef(-angle[2], 0,0,1);
- glTranslatef(+0.3,0,0);
- glutSolidTeapot( 0.3 );///右手臂
- glPushMatrix();///step05-3
- glTranslatef(+0.3,0,0);
- glRotatef(-angle[3], 0,0,1);
- glTranslatef(+0.3,0,0);
- glutSolidTeapot( 0.3 );///右手肘
- glPopMatrix();///Step05-3
- glPopMatrix();///Step05-2
- glPopMatrix();///Step05-2
- glutSwapBuffers();
- }
- int main(int argc , char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
- glutCreateWindow("week15 many angles");
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutKeyboardFunc(keyboard);
- glutDisplayFunc(display);
- glutTimerFunc(0, timer, 0);
- glutMainLoop();
- }///先不放貼圖,先不放打光
- ///主題: 會邊讀動作和儲存,按r可以自動放動作
- #include <stdio.h> ///printf() fprintf() fscanf() fopen() step 01外掛
- #include <GL/glut.h>
- float angle[20] = {},diff = 2;///上週程式,會增加.會減少!!!
- int angleID=0; ///現在有20個angle,利用angle[0],angle[1]...angle[angleID]
- void timer(int t)///上周程式,會增加.會減少!!!
- ///timer()什麼都不做
- {
- ///glutTimerFunc(30, timer,t+1);///上週程式,設定新的timer
- ///angle[angleID] += diff;///上週程式,會增加.會減少!!!
- ///if(angle[angleID]>90) diff =-2;///上週程式,會增加.會減少!!!
- ///if(angle[angleID]<0) diff = +2;///上週程式,會增加.會減少!!!
- ///glutPostRedisplay();///上週教display()
- }
- ///想要用mouse() 及 motion() 來操作關節!!!(期末作品)第7週教學
- int oldX=0,oldY=0;
- #include <stdio.h>///step03:上週教的第0步 才能用fopen() printf() fprintf()
- FILE * fout = NULL;///step03:上週教的第1步
- FILE * fin = NULL;
- ///搬家搬到樓上
- 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();///上週display()
- ///現在想要邊motion 邊存檔
- if(fout == NULL) fout = fopen("angle.txt","w+");///step03 若NULL fopen()
- for(int i=0; i<20;i++) printf("%.2f ", angle[i] );///step03 for 迴圈印陣列
- printf("\n");
- for(int i=0; i<20;i++) fprintf(fout, "%.2f ", angle[i] );///step03 for 迴圈印陣列
- fprintf(fout,"\n");
- }
- void keyboard(unsigned char key,int x,int y) ///keyboard()需要
- {
- if(key=='0') angleID=0; ///之後timer()會改變關節 angle[0]
- if(key=='1') angleID=1; ///之後timer()會改變關節 angle[1]
- if(key=='2') angleID=2; ///之後timer()會改變關節 angle[2]
- if(key=='3') angleID=3; ///之後timer()會改變關節 angle[3]
- if(key=='s') {///想要存檔 save
- if(fout == NULL) fout = fopen("angle.txt","w+");///step03 若NULL fopen()
- for(int i=0; i<20;i++) printf("%.2f ", angle[i] );///step03 for 迴圈印陣列
- printf("\n");
- for(int i=0; i<20;i++) fprintf(fout, "%.2f ", angle[i] );///step03 for 迴圈印陣列
- fprintf(fout,"\n");
- }
- if(key=='r'){///step04: read 想讀進來
- if(fin == NULL) fin = fopen("angle.txt","r");///NULL, fopen()
- for (int i=0;i<20;i++) fscanf(fin,"%f", &angle[i]); ///step04
- glutPostRedisplay();///step04 重畫畫面
- }
- }
- void display()
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();///Step05-2
- glutSolidTeapot( 0.3 );///身體
- glPushMatrix();///Step05-2
- glTranslatef(-0.3,0,0);
- glRotatef(angle[0], 0,0,1);
- glTranslatef(-0.3,0,0);
- glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
- glPushMatrix();
- glTranslatef(-0.3, 0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(-0.3, 0,0);
- glutSolidTeapot( 0.3 );///左手肘
- glPopMatrix();
- glPopMatrix();///Step05-2
- glPushMatrix();///Step05-2
- glTranslatef(+0.3,0,0);
- glRotatef(-angle[2], 0,0,1);
- glTranslatef(+0.3,0,0);
- glutSolidTeapot( 0.3 );///右手臂
- glPushMatrix();///step05-3
- glTranslatef(+0.3,0,0);
- glRotatef(-angle[3], 0,0,1);
- glTranslatef(+0.3,0,0);
- glutSolidTeapot( 0.3 );///右手肘
- glPopMatrix();///Step05-3
- glPopMatrix();///Step05-2
- glPopMatrix();///Step05-2
- glutSwapBuffers();
- }
- int main(int argc , char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
- glutCreateWindow("week15 many angles");
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutKeyboardFunc(keyboard);
- glutDisplayFunc(display);
- glutTimerFunc(0, timer, 0);
- glutMainLoop();
- }///先不放貼圖,先不放打光
今日成果:
↑邊讀動作邊存檔的,按r可以自動讀取動作


沒有留言:
張貼留言