2023年5月17日 星期三

DTang---week14_切換關節

WEEK 14

1. 為避免如上周一樣當機,先將github安裝好並開啟桌面資料夾

2. 主題1:timer

    --glutTimerFunc(__等待時間__, timer, 0);


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){

    glutTimerFunc(500, timer, t+1); /// 設定下一個轉動timer

    angle += 90;

    glutPostRedisplay(); /// 重畫畫面

}

int main(int argc, char** argv){

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week14");

    glutDisplayFunc(display);

    glutTimerFunc(2000, timer, 0); /// 設定timer函式

    glutMainLoop();

}


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

week14-2_timer_play

加入鍵盤控制來啟動timer

先取消原本在main裡的timer

再來加入keyboard控制

void keyboard(unsigned char key, int x, int y){

    glutTimerFunc(0, timer, 0); /// 設定timer函式

}

...

glutKeyboardFunc(keyboard);

...

執行後可按下任意鍵盤啟動timer

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

week14-3_timer_alpha_interpolation


#include <GL/glut.h>

float angle = 0, newAngle = 0, oldAngle = 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){

    if(t<100)    glutTimerFunc(50, timer, t+1); /// 設定下一個轉動timer

    float alpha = t/100.0 ; ///alpha介於0.00~1.00之間

    angle = newAngle*alpha + (1-alpha)*oldAngle;

    glutPostRedisplay(); /// 重畫畫面

}

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;

    }

    glutPostRedisplay();

}

void keyboard(unsigned char key, int x, int y){

    glutTimerFunc(0, timer, 0); /// 設定timer函式

}

int main(int argc, char** argv){

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week14");

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutMainLoop();

}




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

Final Project

回到期末專案
step1 加入可控制顯示的程式碼,顯示目前選定的區塊

加入
int ID = 0; /// 0->頭、1->身、2->上手臂、3->下手臂
修改keyboard的程式碼
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();
}

在每個區塊的顯示程式碼上方加入兩行
if(ID == 0) glColor3f(1,0,0);
else glColor3f(1,1,1);

來改變顏色




再來要加入TRT功能

...

void display(){

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

    glScaled(0.2,0.2,0.2);

        if(body == NULL){

            head = glmReadOBJ("Model/head.obj");

            body = glmReadOBJ("Model/wholebody.obj");

            uparmR = glmReadOBJ("Model/uparmR.obj");

            lowarmR = glmReadOBJ("Model/armhandR.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(-1.166666, 0.466667, 0);

            glRotatef(angle,0,0,1);

            glTranslatef(1.166666, -0.466667, 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();

    glColor3f(0,1,0);

    glutSolidTeapot(0.02);

    glutSwapBuffers();

}

int oldX = 0; int oldY = 0;

void 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){

        oldX = x;

        oldY = y;

        angle = x;

    }

    display();

}

...




加入下手臂的TRT

glPushMatrix();

            glTranslatef(-1.166666, 0.466667, 0);

            glRotatef(angle,0,0,1);

            glTranslatef(1.166666, -0.466667, 0);


            if(ID == 2) glColor3f(1,0,0);

            else glColor3f(1,1,1);

            if(show[2]) glmDraw(uparmR, GLM_MATERIAL);


            glPushMatrix();

                glTranslatef(-1.933332, +0.133333, 0);

                glRotatef(angle,0,0,1);

                glTranslatef(1.933332, -0.133333, 0);


                if(ID == 3) glColor3f(1,0,0);

                else glColor3f(1,1,1);

                if(show[3]) glmDraw(lowarmR, GLM_MATERIAL);

            glPopMatrix();

        glPopMatrix();







沒有留言:

張貼留言