2023年3月23日 星期四

yea 06

 WEEK06

            STEP06-1-1

                                        開新的專案  week06-1_TRT_robot2_teapot


改用上週程式碼  利用茶壺做robot
 glutSolidSphere(0.01,30,30);///step01 小球做中心
讓茶壺做旋轉
                

找一個點 作為茶壺轉動的點



程式碼 06-1

#include <GL/freeglut.h>
float angle=0; ///全域變數 angle 角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清畫面

    glutSolidSphere(0.01,30,30);///step01 小球做中心

    glPushMatrix();
        glTranslatef(0.25,0.25,0);///(3)
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(0.45,0,0);///(1)
        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();
    angle++;///角度++
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06");

glutDisplayFunc(display);
glutIdleFunc(display); ///重畫畫面
glutMainLoop();
}

step06-1-2

                        開新專案  week06-2_TRT_robot3_hierarchy 有階層的

做一個小方塊

程式碼:

glScalef(1,0.3,0.3);
        glutSolidCube(0.5);///大小為0.5的方塊



轉動的感覺怪怪的
調整位置在旋轉
往右移0.25 做旋轉



移動位置後做的旋轉



放在一個位置 讓手臂轉動


程式碼
#include <GL/freeglut.h>
float angle=0; ///全域變數 angle 角度
void myCube()///step02-1函式
{
    glPushMatrix();
        glScalef(1,0.3,0.3);
        glutSolidCube(0.5);///大小為0.5的方塊

    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清畫面

    glutSolidSphere(0.01,30,30);///step01 小球做中心

    glPushMatrix();
        glTranslatef(0.25,0,0);///(3)決定要把手放在什麼位置上
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(0.25,0,0);///(1)
        myCube();
        glPushMatrix();
            glTranslatef(0.25,0,0);///(3)決定要把手放在什麼位置上
            glRotatef(angle,0,0,1);///(2)
            glTranslatef(0.25,0,0);///(1)
            myCube();
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
    angle++;///角度++
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06");

glutDisplayFunc(display);
glutIdleFunc(display); ///重畫畫面
glutMainLoop();
}


做階層 有兩個手臂同時轉動



step06-2

兩隻手臂同時轉 但方向是不同的


程式碼
#include <GL/freeglut.h>
float angle=0; ///全域變數 angle 角度
void myCube()///step02-1函式
{
    glPushMatrix();
        glScalef(1,0.3,0.3);
        glutSolidCube(0.5);///大小為0.5的方塊

    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清畫面

    glutSolidSphere(0.01,30,30);///step01 小球做中心

    glPushMatrix();///右邊的
        glTranslatef(0.25,0,0);///(3)決定要把手放在什麼位置上
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(0.25,0,0);///(1)
        myCube();///上手臂
        glPushMatrix();
            glTranslatef(0.25,0,0);///(3)決定要把手放在什麼位置上
            glRotatef(angle,0,0,1);///(2)
            glTranslatef(0.25,0,0);///(1)
            myCube();///下手肘
        glPopMatrix();
    glPopMatrix();


    glPushMatrix();///左邊的
        glTranslatef(-0.25,0,0);///(3)決定要把手放在什麼位置上
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(-0.25,0,0);///(1)
        myCube();///上手臂
        glPushMatrix();
            glTranslatef(-0.25,0,0);///(3)決定要把手放在什麼位置上
            glRotatef(angle,0,0,1);///(2)
            glTranslatef(-0.25,0,0);///(1)
            myCube();///下手肘
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
    angle++;///角度++
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06");

glutDisplayFunc(display);
glutIdleFunc(display); ///重畫畫面
glutMainLoop();
}


把左邊的angle加負號 讓兩隻手方向相同


程式碼


#include <GL/freeglut.h>
float angle=0; ///全域變數 angle 角度
void myCube()///step02-1函式
{
    glPushMatrix();
        glScalef(1,0.3,0.3);
        glutSolidCube(0.5);///大小為0.5的方塊

    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清畫面

    glutSolidSphere(0.01,30,30);///step01 小球做中心

    glPushMatrix();///右邊的
        glTranslatef(0.25,0,0);///(3)決定要把手放在什麼位置上
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(0.25,0,0);///(1)
        myCube();///上手臂
        glPushMatrix();
            glTranslatef(0.25,0,0);///(3)決定要把手放在什麼位置上
            glRotatef(angle,0,0,1);///(2)
            glTranslatef(0.25,0,0);///(1)
            myCube();///下手肘
        glPopMatrix();
    glPopMatrix();


    glPushMatrix();///左邊的
        glTranslatef(-0.25,0,0);///(3)決定要把手放在什麼位置上
        glRotatef(-angle,0,0,1);///(2)
        glTranslatef(-0.25,0,0);///(1)
        myCube();///上手臂
        glPushMatrix();
            glTranslatef(-0.25,0,0);///(3)決定要把手放在什麼位置上
            glRotatef(-angle,0,0,1);///(2)
            glTranslatef(-0.25,0,0);///(1)
            myCube();///下手肘
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
    ///angle++;///角度++
}
void motion(int x, int y)
{///step03-1 新加的 mouse motion
    angle = x;
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06");

    glutMotionFunc(motion);///step03-1 新加的 mouse motion
glutDisplayFunc(display);
glutIdleFunc(display); ///重畫畫面
glutMainLoop();
}


練習期中考題目 



沒有留言:

張貼留言