2023年3月16日 星期四

放假~

 WEEK05

STEP05-1

                    下載 data window 兩個壓縮檔 把window 資料夾解壓縮 把data的壓縮檔裡的data資                        料夾拉到window裡




                                               複習上禮拜的 在右邊轉動長高的藍色車子


轉動移動到右邊的藍色車子




複製上周的第一個程式 改變旋轉軸 紅色 綠色茶壺




程式碼

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

    glColor3f(1,0,0);///紅色
    glPushMatrix(); ///備份矩陣
        glRotatef(angle,0,0,1);///改成 0,0,1 z軸
        glTranslated(0.8,0,0);///移到右邊
        glutSolidTeapot( 0.3);
    glPopMatrix();///還原矩陣

    glColor3f(0,1,0);///綠色
    glPushMatrix(); ///備份矩陣
        glTranslated(0.8,0,0);///移到右邊
        glRotatef(angle*1.5,0,0,1);///改成 0,0,1 z軸
        glutSolidTeapot( 0.3);
    glPopMatrix();///還原矩陣

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

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

STEP05-2

                                                   opengl_TRT_demo 的互動程式 
按ToDraw 可以畫身體 畫手
可以按程式碼 移動它的位置 先放一個旋轉(在手前面 只影響手)
會對中心轉 會怪怪的 要改成移動 讓手臂的關節一道正中心
再把轉動加上去 手臂會再肚臍上轉動 但有點變態
最後再把移動加上去 讓旋轉的手臂 到肩膀上


程式碼:

myDrawObject(0);//畫身體
glPushMatrix();
glTranslatef( 0.17,0.38,0 ); //(3)最後 把手臂掛到肩膀上
glRotatef( angle , 0 ,0, 1 );//(2)轉動
glTranslatef( -0.17, -0.38, 0 );//(1)把關節放中心
myDrawObject(1);//畫手臂
glPopMatrix();

STEP05-3 

開新專案 week05-2_TRT_robot
要做出TRT關節轉動 先把WEEK05-1的程式拿來用
讓手臂到正中心可以做轉動









程式碼:.
///貼上week05-1_TRT_translate_rotate的程式
#include <GL/freeglut.h>
float angle=0; ///全域變數 angle 角度
void myCube()///step2 改函式
{
    glPushMatrix();///step1 簡化程式
        glScalef( 0.5,0.2,0.2 );///改長方形的大小
        glutSolidCube(1);///長方形
    glPopMatrix();

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

    glColor3f(1,1,1);///step4 白色的
    glutSolidCube(1);///step4 身體

    ///右上角
    glPushMatrix();///step2
        glTranslatef(0.5,0.5,0);///掛到右上角
        glRotatef( angle,0,0,1 );///step2 轉它
        glTranslated(0.25,0,0);///step3 往右移動 0.25 讓關節在正中心
        glColor3f(0,1,0);///step4 綠色的
        myCube();///STEP2 改函式


    glPopMatrix();///step2

    ///左上角
    glPushMatrix();///step2
        glTranslatef(-0.5,0.5,0);///掛到右上角
        glRotatef( angle,0,0,1 );///step2 轉它
        glTranslated(0.25,0,0);///step3 往右移動 0.25 讓關節在正中心
        glColor3f(0,1,0);///step4 綠色的
        myCube();///STEP2 改函式


    glPopMatrix();///step2

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

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



               

沒有留言:

張貼留言