2020年4月7日 星期二

第六週做3D茶壺讓他轉動然後打光ㄉ卡小

Teams教學time!

搜尋Teams

登入帳號後

加入團隊
豪耶!成功



進入課程

複習上次內容打開codeblocks
然後這樣那樣這樣那樣
搭拉~
圖片根本放不進來為什麼辣!(沒事,我解決ㄌ哈)
總之就是弄ㄍ打光ㄉ3D茶壺出來辣!
氣鼠ㄟ!

然後用滑鼠移動茶壺ㄉ位置辣!
還是沒有辦法放圖片辣!
之後讓鍵盤按下E或W可以轉動或移動
豪耶!
就醬,因為圖片一直放不上來ㄏ
氣鼠 :)

來讓茶壺自轉ㄅ
自轉ㄉ時候泥還是可以轉他、移動ㄜ
故意轉跟他不同方向,跟他尬起來辣!

直接給你程式碼辣!

#include <GL/glut.h>
///打光第0步:宣告陣列,裡面有光的屬性值
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 3.0f, -5.0f, 0.0f };///光源位置

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

int type=1;///TODO2 1:rotate 2:translate
float teapotx=0,teapoty=0;///TODO1
float teapotroty=0;///TODO2 轉動ㄉ變數
int oldx=0,oldy=0;///TODO1
void mouse(int button,int state,int x,int y){///TODO1 mouse要移動茶壺 step1
    oldx=x;///TODO1
    oldy=y;///TODO1
}
void motion(int x,int y){///TODO1 step2 motion 動
    if(type==1){
        teapotroty+= x-oldx;
    }
    else if(type==2){
        teapotx+= x-oldx;///TODO1
        teapoty+= y-oldy;///TODO1
    }
    oldx=x;///TODO1
    oldy=y;///TODO1
    glutPostRedisplay();///TODO1 更新畫面
}
void keyboard(unsigned char key,int x,int y){
    if(key=='e' || key=='E') type=1;///MAYA 轉動
    if(key=='w' || key=='W') type=2;///MAYA 移動
}

static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///TODO2 備份矩陣
        glTranslatef(teapotx/150.0,-teapoty/150.0,0);
        glRotatef(teapotroty,0,-1,0);///X方向ㄉ動 vs. Y軸
        glutSolidTeapot(0.3);///TODO2
    glPopMatrix();///TODO2 備份矩陣
    glutSwapBuffers();
    teapotroty++;///TODO3 每次多轉一度
}


int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(300,300);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("Weeeeeeeeeeeeeek06");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);///TODO
    glutMotionFunc(motion);///TODO1
    glutKeyboardFunc(keyboard);///TODO2
    glutIdleFunc(display);///TODO3 閒ㄉ時候重畫,沒有閒時間辣!

    ///打光需要3Dㄉdepth test
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    ///打光1:要開啟光線,原來範例就寫好了
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    ///打光2:要設定光的設定值(前面有陣列,這裡拿來用)
    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    ///打光2.2:要把物體也設定好它的matreial
    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();

    return EXIT_SUCCESS;

}

解釋轉動、移動

先到 點我辣!這個網站
然後下載datawin32glut32.dll

(5+9:好啊好啊)
將windows解壓縮,glut32.dll跟data的資料放進去
點擊Transformation開啟檔案




沒有留言:

張貼留言