2022年5月16日 星期一

RH的學習筆記٩(ˊᗜˋ*)وweek13

 複習T-R-T

1.創一個新的week13_rect_TRT的專案,再輸入基本的10行程式碼

```c
#include <GL/glut.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);



    glutSwapBuffers();

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week13_rect_TRT");



    glutDisplayFunc(display);
    glutMainLoop();

}
```
接著輸入新教的程式rect在display()函式裡面
glRectf(0.5,0.5,-0.5,-0.5); ///四邊形(Rectangle)在小黑視窗的座標

2.按剛剛的程式碼把TRT寫上去,並將還不知道數值的程式註解掉

```c
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.5,0.5,-0.5,-0.5);     ///白色的四邊形(Rectangle)
    glPushMatrix();
        ///glTranslatef(x,y,z);
        ///glRotatef(angle,0,0,1);
        ///glTranslatef(x2,y2,z2);
        glColor3f(1,0,0);  ///紅色
        glRectf(0.5,0.5,1.0,0.3);
    glPopMatrix();

    glutSwapBuffers();
}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week13_rect_TRT");

    glutDisplayFunc(display);
    glutMainLoop();
}
```

3.把紅色四邊形座標0.5,0.5移到0,0



display()函式增加glTranslatef(-0.5,-0.4,0);


新增一個變數angle並修改Rotatef的參數,使紅色四邊形轉動45度
float angle=45;
glRotatef(angle,0,0,1);

4.為了能用滑鼠控制手臂移動,新增一個變數oldX,寫mouse、motion兩個函式,最後在main()裡面寫glutMouseFunc(mouse);、glutMotionFunc(motion);

```c
float oldX=0;
void mouse(int button,int  state,int x,int y)///mouse按下去
{
    oldX=x;
}
void motion(int x,int y)
{
    angle+=(x-oldX);
    oldX=x;
    glutPostRedisplay();///重畫畫面 Re display
}

glutMouseFunc(mouse);
glutMotionFunc(motion);
```

5.新開一個專案week_rect_TRT_TRT,調整白色四邊形的身體參數使其變修長;但卻手卻與身體分開了,所以又要修改Translatef()的參數調整手臂位置

程式碼如下:
glRectf(0.3,0.5,-0.3,-0.2);
glTranslatef(0.3,0.5,0);

6.新增下手肘後把float angle值改成0,接著修改display()裡各部位的參數(身體、上臂、下手肘)

程式碼如下:

```c

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);

    glRectf(0.3,0.5,-0.3,-0.2);     ///讓身體短一點,結果手分開了

    glPushMatrix(); ///掛的位置改了

        glTranslatef(0.3,0.5,0);///用這個調整手臂

        glRotatef(angle,0,0,1);///旋轉

        glTranslatef(-0.3,-0.4,0);///先把旋轉中心放中間

        glColor3f(1,0,0);

        glRectf(0.3,0.5,0.8,0.3);///上手臂

        glPushMatrix();

           ///3.把下手肘掛在關節上

           ///2.旋轉

           ///1.把下手肘的旋轉中心放正中心

            glColor3f(0,1,0);///綠色

            glRectf(0.8,0.5,1.1,0.3);

        glPopMatrix();

    glPopMatrix();


    glutSwapBuffers();

}

```

7.先將紅色上臂的選轉Rotatef()註解掉,並寫出綠色下手肘的TRT

程式碼如下:
```c
    glTranslatef(0.8,0.4,0);///3.把下手肘掛在關節上
    glRotatef(angle,0,0,1);///2.旋轉
    glTranslatef(-0.8,-0.4,0);///1.把下手肘的旋轉中心放正中心
```

8.複製現在的PushMatrix、PopMatrix做出另一隻手,並把x座標的值改成負的,以改變位置(全部關節一起動)

display()裡的程式碼如下:
```c
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5,-0.3,-0.2);     ///讓身體短一點,結果手分開了

    glPushMatrix(); ///右半邊
        glTranslatef(0.3,0.5,0);///用這個調整手臂
        glRotatef(angle,0,0,1);///旋轉
        glTranslatef(-0.3,-0.4,0);///先把旋轉中心放中間
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.8,0.3);///上手臂
        glPushMatrix();
            glTranslatef(0.8,0.4,0);///3.把下手肘掛在關節上
            glRotatef(angle,0,0,1);///2.旋轉
            glTranslatef(-0.8,-0.4,0);///1.把下手肘的旋轉中心放正中心
            glColor3f(0,1,0);///綠色
            glRectf(0.8,0.5,1.1,0.3);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix(); ///左半邊
        glTranslatef(-0.3,0.5,0);///用這個調整手臂
        glRotatef(angle,0,0,1);///旋轉
        glTranslatef(+0.3,-0.4,0);///先把旋轉中心放中間
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.8,0.3);///左上手臂
        glPushMatrix();
            glTranslatef(-0.8,0.4,0);///3.把下手肘掛在關節上
            glRotatef(angle,0,0,1);///2.旋轉
            glTranslatef(+0.8,-0.4,0);///1.把左下手肘的旋轉中心放正中心
            glColor3f(0,1,0);///綠色
            glRectf(-0.8,0.5,-1.1,0.3);///再畫左下手肘
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
```

9.利用數字鍵決定哪一個關節轉動

首先先float一個angle[20]、int angleID,接著int一個keyboard()函式,再來修改motion裡面的angle,最後把每個關節對應各個angleID
**
如果angleID=0等於angle[0],也因此angle[angleID]等於底幾個關節
angleID=1    angle[1]
angleID=2    angle[2]
angleID=3    angle[3]
程式碼如下:
```c
#include <GL/glut.h>
float angle[20],oldX=0;
int angleID=0;  ///0:第0個關節,1:第1個關節,2:第2個關節
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0')angleID=0;
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
}///用按鍵來決定motion()裡改的angle[i]是哪一個
void mouse(int button,int  state,int x,int y)///mouse按下去
{
    oldX=x;
}
void motion(int x,int y)
{
    angle[angleID]+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5,-0.3,-0.2);     ///讓身體短一點,結果手分開了

    glPushMatrix(); ///右半邊
        glTranslatef(0.3,0.5,0);///用這個調整手臂
        glRotatef(angle[0],0,0,1);///旋轉
        glTranslatef(-0.3,-0.4,0);///先把旋轉中心放中間
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.8,0.3);///上手臂
        glPushMatrix();
            glTranslatef(0.8,0.4,0);///3.把下手肘掛在關節上
            glRotatef(angle[1],0,0,1);///2.旋轉
            glTranslatef(-0.8,-0.4,0);///1.把下手肘的旋轉中心放正中心
            glColor3f(0,1,0);///綠色
            glRectf(0.8,0.5,1.1,0.3);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix(); ///左半邊
        glTranslatef(-0.3,0.5,0);///用這個調整手臂
        glRotatef(angle[2],0,0,1);///旋轉
        glTranslatef(+0.3,-0.4,0);///先把旋轉中心放中間
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.8,0.3);///左上手臂
        glPushMatrix();
            glTranslatef(-0.8,0.4,0);///3.把下手肘掛在關節上
            glRotatef(angle[3],0,0,1);///2.旋轉
            glTranslatef(+0.8,-0.4,0);///1.把左下手肘的旋轉中心放正中心
            glColor3f(0,1,0);///綠色
            glRectf(-0.8,0.5,-1.1,0.3);///再畫左下手肘
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char**argv)
{

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_many_TRT");

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();

}
```



沒有留言:

張貼留言