2017年10月19日 星期四

LBH的計圖學習筆記_week 06

一 、 控制鍵盤事件程式碼

/// 放全域
int rotateX=0,rotateY=0,rotateZ=0;
int oldX=0,oldY=0;

 /// 放主函式glutDisplayFunc後
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);

void keyboard(unsigned char key,int x,int y){  /// 參數依序是(按鍵字元x座標y座標)
      if(key=='1'){
        rotateX++;
      }
      if(key=='2'){
        rotateY++;
      }
      if(key=='3'){
        rotateZ++;
      }
      glutPostRedisplay(); /// 貼出公告,請電腦有空時,把畫面重新 re-display
}
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(rotateX,1,0,0);
        glRotatef(rotateY,0,1,0);
        glRotatef(rotateZ,0,0,1);
        drawmodel();
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){
    if(state==GLUT_DOWN){ /// 滑鼠按下去
        oldX=x;
        oldY=y;
    }
}
void motion(int x,int y){
    rotateY+=(-1*(x-oldX));
    rotateX+=(-1*(y-oldY));
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}


二 、 課堂練習  

1. 編譯執行Tranformation 程式(若卡在strdup(),修改complier設定,把c++ 11的選項取消掉,
    即可成功執行)




2. 開啟打光功能
    程式碼 :  (全域變數)
                   (1) GLfloat pos[]={0.0,0.0,-1.0,0.0};

                   (放在glutCreateWindow之後,glutMainLoop之前)
                   (2) glEnable(GL_DEPTH_TEST);
                   (3) glEnable(GL_LIGHT0); /// 打開打光功能
                   (4) glEnable(GL_LIGHTING);
                   (5) glLightfv(GL_LIGHT0,GL_POSITION,pos);

沒有留言:

張貼留言