2023年5月17日 星期三

Orangutan - 第十四週 - 切換關節、移動、旋轉

 

=========================================================================

FOURTEENTH


一、轉動(自動、鍵盤)


1.先將Final_Project從雲端下載下來

2.開一個新的GLUT專案並命名為week14-1_timer

3.將程式碼全部刪除並修改為以下程式碼
#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) { glutTimerFunc(500,timer,t+1); angle+=50; glutPostRedisplay(); } int main(int argc,char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("week14"); glutDisplayFunc(display); glutTimerFunc(2000,timer,0); glutMainLoop(); }

3.執行後就會看到茶壺像時鐘一樣旋轉

4.開一個新的GLUT專案並命名為week14-2_timer_play

5.將week14-1程式碼拿來用,並將紅框中的keyboard函式程式碼加上去,記得main函式要呼叫keyboard
void keyboard(unsigned char key,int x,int y) { glutTimerFunc(0,timer,0); }

6.執行後按空白鍵就可以旋轉

7.利用Excel來說明轉動、alpha值


二、轉動(滑鼠、鍵盤)


1.開一個新的GLUT專案並命名為week14-3_timer_alpha_interpolation

2.把week14-2的程式碼拿來用並修改,再加上紅框中的程式碼
void motion(int x,int y) { angle=x; glutPostRedisplay(); } void mouse(int button,int state,int x,int y) { if(state==GLUT_DOWN) oldAngle = x; if(state==GLUT_UP) newAngle = x; }
main函式中的
glutMouseFunc(mouse);
glutMotionFunc(motion);

3.執行之後先使用滑鼠來選擇要從哪邊開始轉,選完兩個角度之後就可以按下空白鍵並觀察它是怎麼轉動的

4.最後save everything,並用Git Bash推送上雲端

三、Final Project


1.開啟Final_Project專案


2.將紅框中的程式碼加上或修改

外面做宣告的
int ID=0;
keyboard函式中的
    if(key=='0') ID = 0;
    if(key=='1') ID = 1;
    if(key=='2') ID = 2;
    if(key=='3') ID = 3;
display函式中的
        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);

3.執行後點選數字鍵0-3就會顯示紅色的部位

4.將display函式中修改,並將綠點補上當成中心點裡用滑鼠加上座標找出手臂的旋轉位子
display函式中的
//glTranslatef(-teapotX,-teapotY,0);
//glRotatef(angle,0,0,1);
glTranslatef(-teapotX,-teapotY,0);
glColor3f(0,1,0);///中心點綠茶壺 glutSolidTeapot(0.02);

5.將紅框中的程式碼補上
display函式中的
glTranslatef(-1.119999, 0.400000, 0); glRotatef(angle,0,0,1); glTranslatef(1.119999, -0.400000, 0);
motion函式中的
angle = x;

6.執行後就可以看到手臂在正確的位子上可以做旋轉

7.將原來下手臂的程式碼放到上方程式碼的裡面,並使用前面用過的方法將手臂旋轉中心找到
display函式中的
glPushMatrix();
    glTranslatef(-1.853332, 0.060000, 0);
    glRotatef(angle,0,0,1);
    glTranslatef(1.853332, -0.060000, 0);
    if(ID==3) glColor3f(1,0,0);
    else glColor3f(1,1,1);
    if(show[3]) glmDraw(lowarmR , GLM_MATERIAL);
glPopMatrix();

8.執行完之後使用滑鼠移動,就會看到上下手臂一起做轉動






































沒有留言:

張貼留言