2019年5月2日 星期四

學費進的來 學生出不去 銘傳發大財

電腦圖學
(1)小考T-R-T轉動
(2)主題:階層轉動,骨架
(3)主題:鍵盤按鍵
下載freeglut 以及老師給的程式碼在120.125.89.81


#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(255,255,255);///白色
    glutSolidTeapot( 0.3 );///身體
    ///右邊的手臂
    glPushMatrix();
        glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
        glRotatef(angle, 0,0,1);///(2)旋轉
        glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸

        glColor3f(255,0,0);///紅色
        glutSolidTeapot( 0.2 );///手臂
    glPopMatrix();

    glutSwapBuffers();
    angle++;
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week11 TRT");
    glutIdleFunc(display);///Now!!!!
    glutDisplayFunc(display);

    glutMainLoop();
}


--------------------------------------------------------
第二個 跟手臂一樣有關節會動
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(255,255,255);///白色
    glutSolidTeapot( 0.3 );///身體
    ///右邊的手臂
    glPushMatrix();
    glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
    glRotatef(angle, 0,0,1);///(2)旋轉
    glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂

    glPushMatrix();
    glTranslatef(0.2, 0,0 );
    glRotatef(angle, 0,0,1);
    glTranslatef(0.28, 0,0);

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂
    glPopMatrix();
    glPopMatrix();



   glPushMatrix();
    glTranslatef(-0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
    glRotatef(angle, 0,0,1);///(2)旋轉
    glTranslatef(-0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂

    glPushMatrix();
    glTranslatef(-0.2, 0,0 );
    glRotatef(angle, 0,0,1);
    glTranslatef(-0.28, 0,0);

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂
    glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
    angle++;
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week11 06160565");
    glutIdleFunc(display);///Now!!!!
    glutDisplayFunc(display);

    glutMainLoop();
}

--------------------------------------------------------------
第三個 用滑鼠移動關節
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(255,255,255);///白色
    glutSolidTeapot( 0.3 );///身體
    ///右邊的手臂
    glPushMatrix();
    glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
    glRotatef(angle, 0,0,1);///(2)旋轉
    glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂

    glPushMatrix();
    glTranslatef(0.2, 0,0 );
    glRotatef(angle, 0,0,1);
    glTranslatef(0.28, 0,0);

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂
    glPopMatrix();
    glPopMatrix();



   glPushMatrix();
    glTranslatef(-0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
    glRotatef(angle, 0,0,1);///(2)旋轉
    glTranslatef(-0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂

    glPushMatrix();
    glTranslatef(-0.2, 0,0 );
    glRotatef(angle, 0,0,1);
    glTranslatef(-0.28, 0,0);

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂
    glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
    ///angle++;
}
    void motion(int x,int y)
    {
        angle=x;
        display();
    }
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week11 06160565");
    glutIdleFunc(display);///Now!!!!
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}
-----------------------------------
第四個 透過按鍵1 2 3 4 設定動哪個關節
#include <GL/glut.h>
float angle1,angle2,angle3,angle4;
int joinID=1;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(255,255,255);///白色
    glutSolidTeapot( 0.3 );///身體
    ///右邊的手臂
    glPushMatrix();
    glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
    glRotatef(angle1, 0,0,1);///(2)旋轉
    glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂

    glPushMatrix();
    glTranslatef(0.2, 0,0 );
    glRotatef(angle2, 0,0,1);
    glTranslatef(0.28, 0,0);

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂
    glPopMatrix();
    glPopMatrix();



   glPushMatrix();
    glTranslatef(-0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
    glRotatef(angle3, 0,0,1);///(2)旋轉
    glTranslatef(-0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂

    glPushMatrix();
    glTranslatef(-0.2, 0,0 );
    glRotatef(angle4, 0,0,1);
    glTranslatef(-0.28, 0,0);

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂
    glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
    ///angle++;
}
    void motion(int x,int y)
    {
        if(joinID==1) angle1=x;
        if(joinID==2) angle2=x;
        if(joinID==3) angle3=x;
        if(joinID==4) angle4=x;
        display();
    }
    void keyboard(unsigned char key,int x,int y)
    {
        if(key=='1') joinID=1;
        if(key=='2') joinID=2;
        if(key=='3') joinID=3;
        if(key=='4') joinID=4;
        display();
    }
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week11 06160565");
    glutIdleFunc(display);///Now!!!!
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

而這次特別加上glutKeyboardFunc(keyboard);
讓程式可以偵測到我們按了數字鍵幾號
並設定出 按1 2 3 4各動哪些關節
------------------------------------------
第五個 因為如果我們有20個關節要動 打20個if實在太纍贅了 所以使用矩陣
就不需要打這麼多程式碼 看起來也更整潔乾淨了
#include <GL/glut.h>
float angle[20];
int joinID=1;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(255,255,255);///白色
    glutSolidTeapot( 0.3 );///身體
    ///右邊的手臂
    glPushMatrix();
    glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
    glRotatef(angle[1], 0,0,1);///(2)旋轉
    glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂

    glPushMatrix();
    glTranslatef(0.2, 0,0 );
    glRotatef(angle[2], 0,0,1);
    glTranslatef(0.28, 0,0);

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂
    glPopMatrix();
    glPopMatrix();



   glPushMatrix();
    glTranslatef(-0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
    glRotatef(angle[3], 0,0,1);///(2)旋轉
    glTranslatef(-0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂

    glPushMatrix();
    glTranslatef(-0.2, 0,0 );
    glRotatef(angle[4], 0,0,1);
    glTranslatef(-0.28, 0,0);

    glColor3f(255,0,0);///紅色
    glutSolidTeapot( 0.2 );///手臂
    glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
    ///angle++;
}
    void motion(int x,int y)
    {
        angle[joinID]=x;
        ///if(joinID==1) angle1=x;
        ///if(joinID==2) angle2=x;
        ///if(joinID==3) angle3=x;
        ///if(joinID==4) angle4=x;
        display();
    }
    void keyboard(unsigned char key,int x,int y)
    {
        if(key=='1') joinID=1;
        if(key=='2') joinID=2;
        if(key=='3') joinID=3;
        if(key=='4') joinID=4;
        display();
    }
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week11 06160565");
    glutIdleFunc(display);///Now!!!!
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

因此上述有些程式碼便可以刪除 就不需要那麼佔版面了


沒有留言:

張貼留言