顯示具有 40447026S_王詠俞 標籤的文章。 顯示所有文章
顯示具有 40447026S_王詠俞 標籤的文章。 顯示所有文章

2017年10月6日 星期五

ClassNote_Week04 移動 & 旋轉 & 縮放 & 滑鼠移動

1. 移動

。以下程式碼為例
glPushMatrix();
 glTranslated(-2.4,1.2,-6);  /// 移動(X,Y,Z)  d表double
 glRotated(60,1,0,0);
 glRotated(a,0,0,1);
 glScalef(1,1,1);
 glutSolidSphere(1,slices,stacks);
glPopMatrix();

2. 旋轉

。以下程式碼為例
glPushMatrix();
 glTranslated(-2.4,1.2,-6);
 /// 旋轉(度數,X,Y,Z)  X,Y,Z軸表向量
 glRotated(60,1,0,0);  /// 沿著x軸轉動60度
 glRotated(a,0,0,1);    /// 沿著z軸每秒轉動90度
 glScalef(1,1,1);
 glutSolidSphere(1,slices,stacks);
glPopMatrix();
依照右手座標系統旋轉

3. 縮放

。以下程式碼為例
glPushMatrix();
 glTranslated(-2.4,1.2,-6);
 glRotated(60,1,0,0);
 glRotated(a,0,0,1);
 glScalef(1,1,1);  /// 縮放比率(X,Y,Z)
 glutSolidSphere(1,slices,stacks);
glPopMatrix();

4. 用滑鼠移動

。以下程式碼為例
#include <GL/glut.h>
#include <stdio.h>
float dx=0.0,dy=0.0,dz=0.0;
int oldX=0,oldY=0,oldZ=0;

void display(){
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
 glTranslated(dx,dy,dz);
 glutSolidTeapot(0.3);
 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){
 dx += (x-oldX)/150.0;
 dy += -(y-oldY)/150.0;         /// Y座標上下顛倒所以要加負號
 oldX = x;
 oldY = y;
 glutPostRedisplay();             /// 畫面刷新
}

int main(int argc, char *argv[]){
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week 04");
 glutDisplayFunc(display);
 glutMouseFunc(mouse);
 glutMotionFunc(motion);
 glutMainLoop();
 return EXIT_SUCCESS;
}

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檔案的執行結果~

2017年9月22日 星期五

ClassNote_Week02 色彩 & 點、線、面

1. 繪圖主程式

int main(int argc, char *argv[]){
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("GLUT Shapes");  /// 建立3D視窗("視窗名")
 glutDisplayFunc(teapot)  /// 設定display函式
 glutMainLoop();
 return EXIT_SUCCESS;
}

2. 色彩

。以茶壺為例
void teapot(void){
 glClearColor(1,0,0,0);  /// 設定預設顏色(R,G,B,A)  A:半透明度
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  ///初始化
 glColor3f(1,1,0);          /// 設定物件色彩(R,G,B)  3f表3個0-1之間float
 glutSolidTeapot(0.3);   /// 繪製物件-茶壺(大小)
 glutSwapBuffers();      /// 將緩衝區繪圖結果切換至畫面上顯示  雙緩衝區繪圖
}

3. 點、線、面

。以畫三角形為例
void triangle(void){
 glClearColor(1,1,1,0);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glBegin(GL_POINTS);          /// 點
// glBegin(GL_LINE_LOOP);  /// 線
 glBegin(GL_POLYGON);      /// 面
 glColor3f(1,0,0);
 glVertex3f(-0.5,1,0);  /// 設定頂點(X,Y,Z)
 glColor3f(0,1,0);
 glVertex3f(-1,-1,0);
 glVertex3f(0,-1,0);
 glEnd();  /// 結束繪圖
 glBegin(GL_POLYGON);
 glColor3f(1,0,0);
 glVertex3f(0.5,1,0);
 glColor3f(0,0,1);
 glVertex3f(0,-1,0);
 glVertex3f(1,-1,0);
 glEnd();
 glutSwapBuffers();
}

2017年9月15日 星期五

ClassNote_Week01 openGL專案 & glut專案

1. openGL_01

。設定compiler
(1) Moodle >上課軟體 >下載  codeblocks 16.01mingw
(2) 安裝 codeblocks-16.01mingw-setup.exe
(3) 打開 CodeBlocks

(4) Settings >Compiler >Global compiler settings >Toolchain executables
      >Compiler's installation directory >Auto-detect >OK

(5) 完成預設工作!
。openGL專案
(1) File >New >Project >OpenGL project >Go

(2) 設定project名字 >Go
(3) 完成專案創建,編譯並執行

2. glut_01

。設定compiler
(1) Moodle >上課軟體 >下載 freeglut 3.0.0 MinGW
(2) 解壓縮後找到 freeglut資料夾 >lib
(3) 複製一份 freeglut.a 並改名為 libglut32.a
。glut專案
(1) File >New >Project >GLUT project >Go

(2) 設定project名字 >Go
(3) 設定GLUT的路徑 >Go

(4) 完成專案創建,編譯並執行