電腦圖學 2023-05-17 Week14
1. 主題: 切換關節2. 主題: 切換移動、旋轉
3. 主題: 計時器、內插動作
安裝Git GitBash
-cd desktop
-git clone https://github.com/Owoyayou/2023graphicsa
-cd 2023graphicsa
-start .
GLUT專案
先把freeglut丟到桌面設定好
程式:timer
freeglut再裝一次
glutTimerFunc
CodeBlocks:File - New - Project
Week14-1_timer
#include <GL/glut.h> float angle=0; void display(){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(angle,0,0,1); glutSolidTeapot(0.3); glPopMatrix(); glutSwapBuffers(); } void timer(int t){///timer函式 做對應動作 glutTimerFunc(500,timer,t+1);///設定下一個鬧鐘 angle += 90;///增加90度 glutPostRedisplay();///重畫畫面 } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("week14"); glutDisplayFunc(display); glutTimerFunc(2000,timer,0)///step1設定timer函式 glutMainLoop(); }
循環轉90度Week14-2_timer_play///用鍵盤播放#include <GL/glut.h> float angle=0; void display(){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(angle,0,0,1); glutSolidTeapot(0.3); glPopMatrix(); glutSwapBuffers(); } void timer(int t) ///timer函式 做對應動作 { glutTimerFunc(500,timer,t+1);///設定下一個鬧鐘 angle += 90;///增加90度 glutPostRedisplay();///重畫畫面 } void keyboard(unsigned char key,int x,int y) { glutTimerFunc(0,timer,0); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("week14"); glutDisplayFunc(display); glutKeyboardFunc(keyboard);///step1-2 ///glutTimerFunc(2000,timer,0)///step1設定timer函式 glutMainLoop(); }按一下鍵盤就會開始一直旋轉90度增加mouse()負責處理按下去、放開對應動作motion()負責即時更新角度timer()裡想要用100格的內插,所以t小於100十持續設定下一次timer用alpha算出角度值///week14-3_timer_alpha_interpolation #include <GL/glut.h> float angle=0,oldAngle=0,newAngle=0;///宣告變數 void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(angle,0,0,1); glutSolidTeapot(0.3); glPopMatrix(); glutSwapBuffers(); } void timer(int t) ///timer函式 做對應動作 { if(t<100)glutTimerFunc(50,timer,t+1);///在100之內,設定下一個鬧鐘 float alpha = t/100.0;///alpha介於0.00~1.00之間 angle += 90;///增加90度 glutPostRedisplay();///重畫畫面 } void motion(int x,int y) { angle=0; glutPostRedisplay(); } void mouse(int button,int state,int x,int y) { if(state==GLUT_DOWN) oldAngle=x;///按下去 if(state==GLUT_UP) newAngle=x;///放開來 glutPostRedisplay(); } void keyboard(unsigned char key,int x,int y) { glutTimerFunc(0,timer,0); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("week14"); glutDisplayFunc(display); glutMouseFunc(mouse);///按下去表示起點 glutMotionFunc(motion);///當mouse在motion時,即時更新畫面 glutKeyboardFunc(keyboard); glutMainLoop(); }回到Final_Project > 使用CodeBlocks開啟Final_Project.cbp更新內容int show[4]={1,1,1,1};int ID=0;///0:頭 1:身體 2:上手臂 3:下手臂if(ID==0) glColor3f(1,0,0);///選定 設紅色else glColor3f(1,1,1);///沒選定 設白色///week12_5_TRT_keyboard #include <stdio.h> #include <GL/glut.h> #include "glm.h" GLMmodel * head = NULL; GLMmodel * body = NULL; GLMmodel * uparmR = NULL;///這邊改成我自己的檔案名稱,回家用 GLMmodel * lowarmR = NULL;///++ int show[4]={1,1,1,1}; int ID=0;///0:頭 1:身體 2:上手臂 3:下手臂 ///week14 void keyboard(){ if(key=='0') ID=0; //show[0]=!show[0]; if(key=='1') ID=1; //show[1]=!show[1]; if(key=='2') ID=2; //show[2]=!show[2]; if(key=='3') ID=3; //show[3]=!show[3]; glutPostRedisplay(); } FILE*fout=NULL; FILE*fin=NULL; float teapotX=0,teapotY=0; float angle=0,angle2=0,angle3=0; void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glScalef(0.2,0.2,0.2);///week13 if(body==NULL){ head=glmReadOBJ("model/head.obj"); body=glmReadOBJ("model/body.obj"); uparmR=glmReadOBJ("model/uparmR.obj"); lowarmR=glmReadOBJ("model/lowarmR.obj"); } if(ID==0) glColor3f(1,0,0);///選定 設紅色 else glColor3f(1,1,1);///沒選定 設白色 if(show[0]) glmDraw(head,GLM_MATERIAL); if(ID==1) glColor3f(1,0,0);///選定 設紅色 else glColor3f(1,1,1);///沒選定 設白色 if(show[1]) glmDraw(body,GLM_MATERIAL); glPushMatrix(); glTranslatef(teapotX,teapotY,0); if(ID==2) glColor3f(1,0,0);///選定 設紅色 else glColor3f(1,1,1);///沒選定 設白色 if(show[2])glmDraw(uparmR,GLM_MATERIAL); glPopMatrix(); if(ID==3) glColor3f(1,0,0);///選定 設紅色 else glColor3f(1,1,1);///沒選定 設白色 if(show[3]) glmDraw(lowarmR,GLM_MATERIAL); glPopMatrix(); glutSwapBuffers(); } int oldX=0,oldY=0; void motion(int x,int y){ teapotX+=(x-oldX)/150.0; teapotY+=(x-oldY)/150.0; oldX=x; oldY=y; printf("glTranslatef(%f,%f,0);\n",teapotX,teapotY); glutPostRedisplay(); oldX=x; oldY=y; anlge=x;; display(); } void mouse(int button,int state,int x,int y){ if(state==GLUT_DOWN){ teapotX=(x-150)/150.0; teapotY=(150-y)/150.0; if(fout==NULL)fout=fopen("file4.txt","w"); fprintf(fout,"%f %f\n",teapotX,teapotY); } display(); } //全選 > Edit > Comment //void keyboard(unsigned char key,int x,int y){///step2-2 keyboard函式 // if(fin==NULL){///step2-2 如果檔案還沒 fopen(), 就開它 // fclose(fout);///前面mouse會開fout指標, 所以要關掉 // fin=fopen("file4.txt","r");///step2-2 沒開檔,就開 // } // fscanf(fin,"%f %f",&teapotX,&teapotY);///step2-2 真的讀檔 // display();///step2-2 重畫畫面 //} int main(int argc,char**argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH); glutCreateWindow("week12"); glutDisplayFunc(display); glutMouseFunc(mouse); glutMouseFunc(motion);///week13 glutKeyboardFunc(keyboard); glutMainLoop(); }按0123分別可以顯示出不同部位#include <stdio.h>#include <GL/glut.h>#include "glm.h"GLMmodel * head = NULL;GLMmodel * body = NULL;GLMmodel * uparmR = NULL;GLMmodel * lowarmR = NULL;int show[4]={1,1,1,1};///用show[i]決定要不要顯示int ID=3;///0:頭 1:身體 2:上手臂 3:下手臂void keyboard (unsigned char key, int x, int y){if (key=='0') ID = 0;if (key=='1') ID = 1;if (key=='2') ID = 2;if (key=='3') ID = 3;glutPostRedisplay();}FILE * fout = NULL; ///一開始,檔案沒有開, NULLFILE * fin = NULL; ///要讀檔用的指標, 一開始也是 NULLfloat teapotX=0, teapotY=0;float angle=0, angle2=0, angle3=0;void display(){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glPushMatrix();glScalef(0.2, 0.2, 0.2);///week13-2-2if(body==NULL){head=glmReadOBJ("model/head.obj");body=glmReadOBJ("model/body.obj");uparmR=glmReadOBJ("model/uparmR.obj");lowarmR=glmReadOBJ("model/lowarmR.obj");///glmUnitize(body);///week13-2-1這行之後會改}if(ID==0) glColor3f(1,0,0);///選定的,設紅色else glColor3f(1,1,1);if (show[0]) glmDraw(head, GLM_MATERIAL);if(ID==1) glColor3f(1,0,0);///選定的,設紅色else glColor3f(1,1,1);if (show[1]) glmDraw(body, GLM_MATERIAL);glPushMatrix();glTranslatef(-1.200000, +0.453333, 0);glRotatef(angle, 0, 0, 1);///glTranslatef(teapotX, teapotY, 0);glTranslatef(1.200000, -0.453333, 0);if(ID==2) glColor3f(1,0,0);///選定的,設紅色else glColor3f(1,1,1);if (show[2]) glmDraw(uparmR, GLM_MATERIAL);glPushMatrix();glTranslatef(-1.946666, +0.180001, 0);//glTranslatef(teapotX, teapotY, 0);glRotatef(angle, 0, 0, 1);glTranslatef(1.946666, -0.180001, 0);if(ID==3) glColor3f(1,0,0);///選定的,設紅色else glColor3f(1,1,1);if (show[3]) glmDraw(lowarmR, GLM_MATERIAL);glPopMatrix();glPopMatrix();glPopMatrix();glColor3f(0,1,0);///放小茶壺在中心點當參考glutSolidTeapot(0.02);///放小茶壺在中心點當參考glutSwapBuffers();}int oldX=0, oldY=0;///week13-03-2void motion(int x, int y){teapotX += (x -oldX)/150.0;teapotY -= (y -oldY)/150.0;oldX=x;oldY=y;angle = x;printf("glTranslatef(%f, %f, 0);\n",teapotX,teapotY);glutPostRedisplay();}void mouse(int button, int state, int x, int y){if(state==GLUT_DOWN){teapotX = (x-150)/150.0;///teapotX = (x-150)/150.0;teapotY = (150-y)/150.0;///teapotY = (150-y)/150.0;angle = x;if(fout==NULL) fout = fopen("file4.txt", "w"); ///step2-2 沒開檔,就開fprintf(fout, "%f %f\n", teapotX, teapotY); ///step2-2 要再存座標}display();}///void keyboard(unsigned char key, int x, int y) ///step02-2 keyboard函式///{///if(fin==NULL){ ///step2-2 如果檔案還沒 fopen(), 就開它///fclose(fout); ///前面mouse會開fout指標, 所以要關掉///fin = fopen("file4.txt", "r"); ///step2-2 沒開檔,就開/// }///fscanf(fin, "%f %f", &teapotX, &teapotY); ///step2-2 讀檔///display(); ///step2-2 重畫畫面///}int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);glutCreateWindow("week14");glutDisplayFunc(display);glutMouseFunc(mouse);glutMotionFunc(motion);glutKeyboardFunc(keyboard); ///step2-2 (開檔、讀檔)glutMainLoop();}
沒有留言:
張貼留言