2018年3月9日 星期五

Week02 璿元

Argc & Argv 是什麼?


1. 新增一個GLUT專案,將main.cpp內的程式碼全部刪除

2. 將以下程式碼KEY上去後就可以Build and Run了

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

int main(int argc, char ** argv)
{
    for(int i=0, i<argc, i++)
    {
         printf("argv[%d]:%s\n", i, argv[i]);
    }
}

  (a) argv是用來儲存命令字元內輸入的參數
  (b) argc是用來儲存我們輸入的參數有幾個



3. 將 freeglut-MinGW-3.0.0-1.mp → freeglut → bin → freeglut.dll 複製貼到剛剛建立的GLUT專案資料夾 → bin → Debug 中




5. 打開cmd(命令提示字元)


  (a) 找到剛剛建立的GLUT專案  bin  Debug 路徑,執行"電腦圖學.exe" 並在後面輸入一個字串,下方會將每個單字印出並換行


用程式碼畫出茶壺


完成圖

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

    void display()
   {
       glClear(GL_COLOR_BUFFER_BIT);
       // 5. 清畫面
       glutSolidTeapot(0.3);
       // 6. 大小為0.3的茶壺
       glutSwapBuffers();
       // 7. 交換 double buffers 來顯示畫出來的顏色
   }

   int main(int argc, char **argv)
   {
       glutInit(&argc, argv);
       // 1. 初始 glut 參數
       glutInitDisplayMode(GLUT_DOUBLE);
       // 2. 顯示模式:double buffers
       glutCreateWindow("A Teapot");
       // 3. 建立視窗(雙引號是視窗名稱)
       glutDisplayFunc(display);
       // 4. 顯示函式display()用來畫圖
       glutMainLoop();
       // 8. 主要的迴圈,用來控制程式
   }


改變茶壺的顏色

  方法一:用 glColor3f() 函式
        在函式 display() 中加入 glColor3f() 函式來改變茶壺顏色
        注意:glColor3f() 函式接受的三個參數都要是小數(0~1) 分別代表 R, G, B


  方法二:用 glColor3ub() 函式
        在函式 display() 中加入 glColor3ub() 函式來改變茶壺顏色
        注意:glColor3f() 函式接受的三個參數在(0~255) 分別代表 R, G, B


改變背景顏色


  用 glClearColor() 函式
        在函式 display() 中加入 glClearColor() 函式來改變背景顏色
        注意:glClearColor() 函式接受的三個參數都是小數(0~1) 分別代表 R, G, B, 不透明度,且要Key在清畫面( glClear(GL_BUFFER_BIT) )前


繪製彩色三角形




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

   void display()
   {
       glClearColor(191/255.0,251/255.0,203/255.0,1);
       glClear(GL_COLOR_BUFFER_BIT);

       glBegin(GL_POLYGON);
       //開始畫
           glColor3f(1, 0, 0);    glVertex2f(-1, -1);
           //設定頂點顏色       //設定頂點座標(接受範圍為 (-1~1) 分別代表x, y)
           glColor3f(0, 1, 0);    glVertex2f(1, -1);
           glColor3f(0, 0, 1);    glVertex2f(0, 1);
       glEnd();
       //結束畫

       glutSwapBuffers();
   }

   int main(int argc, char **argv)
   {
       glutInit(&argc, argv);
       glutInitDisplayMode(GLUT_DOUBLE);
       glutCreateWindow("A Triangle");
       glutDisplayFunc(display);
       glutMainLoop();
   }

沒有留言:

張貼留言