2017年10月26日 星期四

LBH的計圖學習筆記_week 07

一、 程式碼 (先開啟 glut 內建範例程式碼)

1. 改變長寬比 (視窗放大縮小不會影響比例)

   static void resize(int width, int height) {

      const float ar = (float) width / (float) height;   /// 計算長寬比

      glViewport(0, 0, width, height);

      glMatrixMode(GL_PROJECTION);

      glLoadIdentity();

      glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

      glMatrixMode(GL_MODELVIEW);

      glLoadIdentity() ;

   }

2. 用滑鼠改變光源 「x座標 」位置,全域變數光源位置的陣列 (GLfloat light_position[]) 前面 

    的 const 要刪掉,主函式 main() 加上 glutMotionFunc(motion);

    GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f }; /// 光的位置

    void motion(int x,int y){

       light_position[0]=(x-150)/150.0;

       light_position[1]=-1*(y-150)/150.0;

       glLightfv(GL_LIGHT0, GL_POSITION, light_position);

       glutPostRedisplay();

   }

   
   執行結果 :


沒有留言:

張貼留言