2018年3月30日 星期五

Week05_邱顯傑

T.R.T轉動
(1)再次觀察Transformation.exe

在我們上週觀察的軟體中,這次觀察另一種R.T的差別


↑↑↑↑右鍵如圖上紅框

先旋轉再移動與先移動再旋轉的差別很重要!!



(1)↑↑↑↑用上面的圖來說明差別(視頻公轉)

↑↑↑↑整個托盤的大桌子要去轉動



------------------------------------------------------------------------------------------------------------------



(2)↑↑↑↑另一個是先轉動的差別說明(視頻自轉)











(2)接下來用程式來觀察

我們用glut一開始的程式碼來做改造


    glPushMatrix();
        glTranslated(-2.4,1.2,-6);///把下面動的球,移動到左上方
        glRotated(a,0,0,1);///轉動
        glutSolidSphere(1,slices,stacks);
    glPopMatrix();

上圖可以看到先轉動後,圓形是只有自己對自己做旋轉(自轉),在把物體放入對應位置上


-------------------------------------------------------------------------------------------------------------------



    glPushMatrix();
        glRotated(a,0,0,1);///轉動   樓下的....
        glTranslated(-2.4,1.2,-6);///移動到左上方
        glutSolidSphere(1,slices,stacks);
    glPopMatrix();

上圖則是你先把物體放到對應的位置上後,再去做旋轉,就會有公轉效果









(3)自己來實做出TRT轉動囉



  step1:  基本上使用簡單的轉動,但卻有疑慮,轉動一直在(0,0)開始轉
不能使用茶壺握把轉動,所以要做修改




step2: 我們更改原本轉動的圖形,讓旋轉中心移動到我們要的茶壺柄上



#include <GL/glut.h>
float angle=0;///step1: 讓茶壼自動轉動的角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///step1:備份矩陣
    glRotatef(angle, 0,0,1);///step1:轉動
    glTranslatef(0.45,-0.10,0);  ///step2:讓茶壺柄移動到正中央
    glutSolidTeapot(0.3);       ///      就可以柄來轉動
    glPopMatrix();///step1:還原矩陣
    glutSwapBuffers();
}
void motion(int x, int y)
{
    angle=x;///step1: 改變角度
    glutPostRedisplay();///step1: 請 重畫畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week05 TRT");

    glutDisplayFunc(display);
    glutMotionFunc(motion);///step1:
    glutMainLoop();
}


最後就是學習關節相關的程式碼如何打(step3)


以紅色為中心,綠色當關節使用,T.R.T的概念來了
    glPushMatrix();
        glColor3f(1,0,0); glutSolidTeapot(0.3);
        ///step3: 中央紅色茶壺

        glTranslatef(0.4,0.13,0);  ///(3)T(最後掛上去) step3:
        glRotatef(angle, 0,0,1);///(2)R(再) step1:轉動
        glTranslatef(0.45,-0.10,0);///(1)T(先)  step2:讓茶壺柄移動到正中央
        glColor3f(0,1,0); glutSolidTeapot(0.3);///step3:綠色
    glPopMatrix();



簡單就能輕易地做出兩個關節只要複製貼上

    glPushMatrix();
        glColor3f(1,0,0); glutSolidTeapot(0.3);
       
        glPushMatrix();
            glTranslatef(0.4,0.13,0);  ///(3)T(最後掛上去) step3:
            glRotatef(angle, 0,0,1);///(2)R(再) step1:轉動
            glTranslatef(0.45,-0.10,0);///(1)T(先)  step2:讓茶壺柄移動到正中央
            glColor3f(0,1,0); glutSolidTeapot(0.3);///step3:綠色
            glPushMatrix();
                glTranslatef(0.4,0.13,0);  ///(3)T(最後掛上去) step3:
                glRotatef(angle, 0,0,1);///(2)R(再) step1:轉動
                glTranslatef(0.45,-0.10,0);///(1)T(先)  step2:讓茶壺柄移動到正中央
                glColor3f(0,1,0); glutSolidTeapot(0.3);///step3:綠色
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();


P.S如果要做出左邊的同樣程式碼只要把Translate的X軸改成負號即可









沒有留言:

張貼留言