2017年9月28日 星期四

ClassNote_Week03 程式技巧講解 & 3D模型

1. 程式技巧講解

#include <GL/glut.h>
#include <stdio.h>

//Drawing funciton
void draw(void){
  FILE *f;
  float w, h, c;
  float r, g, b;
  float i, j, x, y;

  //Background color
  glClearColor(0,0,0,1);
  glClear(GL_COLOR_BUFFER_BIT);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///在main中glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
///可能會導致GL_DEPTH變動,因此一定要寫
GL_DEPTH_BUFFER_BIT以免出問題!

  //Read pixel file
  f = fopen("minion.txt", "r");
  //Read width, height, channel
  fscanf(f, "%f %f %f", &w, &h, &c);

  //Set point size
  glPointSize(10.0f);
///將每一個像素放大十倍,這樣即使畫面放大也不會破圖

  //Begin drawing
  glBegin(GL_POINTS);
  for(i=0; i<w; i++){
    for(j=0; j<h; j++){
      //Read r, g, b
      fscanf(f, "%f %f %f", &r, &g, &b);

      //Geometric transformation
      x = -(i-(w/2.0))/(w/2.0);
      y = (j-(h/2.0))/(h/2.0);

      //Draw(in python, read as bgr)
      glColor3f(b/255.0, g/255.0, r/255.0);
      glVertex3f(y, x, 0);
    }
  }

  //End drawing
  glEnd();
  fclose(f);

  //Draw order
  glutSwapBuffers();
}
✤注意:如果在display函式中開檔讀檔,則每次loop執行都會浪費時間
   所以盡量不要在display中開檔讀檔!
執行結果~

2. 3D模型

3D模型檔案可在老師的網站>data下載
可以使用3D Exploration開啟各種3D模型檔案

利用3D Exploration轉譯成cpp檔案後就可以使用CodeBlocks的GLUT專案開啟
    如果編譯時出現問題,把有問題的地方註解掉就好了!
經過轉換為cpp檔案的執行結果~

沒有留言:

張貼留言