2022年5月16日 星期一

⚝ YI-ZHEN的筆記 week13

 

  • 用方塊做TRT
1. 建立新專案
2. 貼上基礎的程式,並加上畫四邊形的程式。

#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glRectf(0.5, 0.5, -0.5, -0.5); ///畫四邊形
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500); ///視窗大小
    glutCreateWindow("Week13_rect_TRT");

    glutDisplayFunc(display); ///這次不放Idle

    glutMainLoop();
}

3. 延續剛才的程式 加入TRT程式,將還未知的東西先註解掉,並增加身體和手臂

#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); ///身體
    glPushMatrix();
        ///glTranslatef(x, y, z); ///要掛在0.5 0.5
        ///glRotatef(angle, 0, 0, 1);
        ///glTranslatef(x, y, z);
        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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week13_rect_TRT");

    glutDisplayFunc(display); ///不放Idle

    glutMainLoop();
}

4. 調整位置
5. 移動紅色手臂讓它到中心位置,根據位置來推算,要移動(-0.5, -0.4)才能到中心

#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); ///身體
    glPushMatrix();
        ///glTranslatef(x, y, z); ///要掛在0.5 0.5
        ///glRotatef(angle, 0, 0, 1);
        glTranslatef(-0.5, -0.4, 0);
        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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week13_rect_TRT");

    glutDisplayFunc(display); ///不放Idle

    glutMainLoop();
}

6. 讓它旋轉45度

#include <GL/glut.h>
float angle=45;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1, 1, 1); ///白色的
    glRectf(0.5, 0.5, -0.5, -0.5); ///身體
    glPushMatrix();
        ///glTranslatef(x, y, z); ///要掛在0.5 0.5
        glRotatef(angle, 0, 0, 1);
        glTranslatef(-0.5, -0.4, 0);
        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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week13_rect_TRT");

    glutDisplayFunc(display); ///不放Idle

    glutMainLoop();
}
7. 把機器人的手移回原位,建立mouse及motion函式使紅色手臂可以利用滑鼠來旋轉
(滑鼠按下去左右拖曳就旋轉)

#include <GL/glut.h>
float angle=45, oldX=0;
void mouse(int button, int state, int x, int y)
{
    oldX= x;
}
void motion(int x, int y)
{
    angle += (x-oldX);
    oldX= x;
    glutPostRedisplay(); ///重畫畫面
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1, 1, 1); ///白色的
    glRectf(0.5, 0.5, -0.5, -0.5); ///身體
    glPushMatrix();
        glTranslatef(0.5, 0.4, 0); ///要掛在0.5 0.4
        glRotatef(angle, 0, 0, 1);
        glTranslatef(-0.5, -0.4, 0);
        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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week13_rect_TRT");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display); ///不放Idle

    glutMainLoop();
}
  • 機器人右邊 TRT
1. 建立新專案
2. 先將機器人瘦身,再把手臂拉回身體旁邊
3. 做下手肘延伸指紅色手臂旁邊

#include <GL/glut.h>
float angle=0, oldX=0; ///角度歸零
void mouse(int button, int state, int x, int y)
{
    oldX= x;
}
void motion(int x, int y)
{
    angle += (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.4, 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();
}
int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week13_rect_TRT");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display); ///不放Idle

    glutMainLoop();
}

4. 開始做 TRT (先把手臂的旋轉註解掉,以免混亂)
    4-1 先把手肘旋轉中心移動到中間
    4-2 進行旋轉
    4-3 最後在把手肘拉回關節上

#include <GL/glut.h>
float angle=0, oldX=0; ///角度歸零
void mouse(int button, int state, int x, int y)
{
    oldX= x;
}
void motion(int x, int y)
{
    angle += (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, 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();
}
int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week13_rect_TRT");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display); ///不放Idle

    glutMainLoop();
}

  • 機器人 TRT
1. 建立新專案
2. 將畫右手的程式碼複製並下方
3. 把x值 負改正,正改負(就會得到左半邊的手)

#include <GL/glut.h>
float angle=0, oldX=0; ///角度歸零
void mouse(int button, int state, int x, int y)
{
    oldX= x;
}
void motion(int x, int y)
{
    angle += (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.4, 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.4, 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();
}
int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week13_rect_TRT");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display); ///不放Idle

    glutMainLoop();
}

4. 要使機器人的關節可以分別轉動,要加上keyboard函式,
    讓它可以根據鍵盤案下的按鍵決定要動哪個關節

#include <GL/glut.h>
float angle[20], oldX=0; ///角度歸零
int angleID=0; ///0為第0個關節...以此類推
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;
}
void mouse(int button, int state, int x, int y)
{
    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.4, 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.4, 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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week13_rect_TRT");

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display); ///不放Idle

    glutMainLoop();
}



沒有留言:

張貼留言