2017年10月19日 星期四

圖學筆記Week06.

主題:Lighting

主要使用程式碼:    
    GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
->pos為陣列宣告  這是一個打光位置的宣告。
以上程式碼是老師提供的範例中擷取出來--->[projection.c]
->打光前
---------------------------------------------------------------------------------
->打光後
-----------------------------------------------------------------------------------
ps. glEnable(GL_DEPTH_TEST); 此為深度測試,若沒有此行,打光會失敗。
------------------------------------------------------------------------------------

Mouse&Keyboard的旋轉移動

需要程式碼:
In main:
    glutMouseFunc(Mouse);
    glutMotionFunc(Motion);
    glutKeyboardFunc(Keyboard); 
增加凾式:
void Mouse(int button,int state,int x,int y)
void Motion(int x,int y)
void Keyboard(unsigned char key,int x,int y)

In Mousfunction:
if(state==GLUT_DOWN){oldX=x;oldY=y;} 設置擊點滑鼠時記錄作標。
In Motionfunction:
   rotateX+=(oldY-y);    rotate變數是要選轉的量,當滑鼠移動時記錄他的變量。
   rotateY+=(oldX-x);
   oldX=x;oldY=y;
   glutPostRedisplay();
In Keyboard:
    switch(key)
    {
    case '1':                     這是利用鍵盤按鍵改變旋轉的變量
        rotateX+=2;
        break;
    case '2':
        rotateY++;
        break;
    case '3':
        rotateZ++;
        break;
    }
    glutPostRedisplay();
In display:
    glPushMatrix();
    glRotatef(rotateX,1,0,0);
    glRotatef(rotateY,0,1,0);
    glRotatef(rotateZ,0,0,1);
    drawmodel();
    glPopMatrix();
-----------------------------------------------
以上即可旋轉


移動呢? 請嘗試看看吧!!

沒有留言:

張貼留言