2017年3月14日 星期二

Week04_嗯對,這是賴小沫的

 1_作業互評,判斷什麼是好的作品

透過互評的方式,看看別人作品的創意及用心
利用老師傳送的hw2資料夾
 
 解壓縮hw2後,開啟list.html

list.html選出最好的三個作品
 
將最好的三個作品圖檔上傳至Moodle


 2_畫正圓形

新增一個新的專案:透過路徑→File/New/Project...

新增一個GLUT專案:將下拉式選單往下尋找GLUT project

建立專案名稱並設定存放位置,預防找不到檔案的風險

因為不需要修改任何設定,故直接按下 Finish 即可

完成上述步驟,即可得到內含有一個程式碼的專案

將GLUT專案中的程式碼增減如下:
  
#include <GL/glut.h>
#include <math.h>

void display(void)
{
    glBegin(GL_POLYGON);
    for(float angle=0; angle <= 3.1415926*2; angle+=0.01){
        glVertex2f( cos(angle), sin(angle) );  //利用三角函數來畫圓
    }
    glEnd();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");    //視窗名稱

    glutDisplayFunc(display);    //執行display(void)函式

    glutMainLoop();

    return EXIT_SUCCESS;
}

執行:按下 Build&Run(F9)即可開始執行,執行後會看到一個白色的正圓形
 

 3_加上Mouse函式

繼續使用上面的程式碼,增加Mouse函式為可以透過滑鼠點擊的方式,更改圓形的顏色

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

void display(void)
{
    glBegin(GL_POLYGON);
    for(float angle=0; angle <= 3.1415926*2; angle+=0.01){
        glVertex2f( cos(angle), sin(angle) );  //利用三角函數來畫圓
    }
    glEnd();
}

void mouse(int button, int state, int x, int y)
{
    printf("%d %d %d %d\n",button, state, x, y);
        //按滑鼠的哪一個鍵(左0,中1,右2)   按下/放開   x座標   y座標
    glColor3f(1,x/300.0,y/300.0);  //數值介於0~1之間,故除以300.0才能產生小數
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");    //視窗名稱

    glutDisplayFunc(display);    //執行display(void)函式
    glutMouseFunc(mouse);   //執行mouse(void)函式

    glutMainLoop();

    return EXIT_SUCCESS;
}

 

 4_加上Motion函式

延續使用前一個程式碼,加入Motion函式,執行後便可用滑鼠點擊拖曳Drag動起來,變色

void motion(int x,int y)  //拖曳
{
    glColor3f(1,x/300.0,y/300.0);
    glutPostRedisplay();  //告訴電腦,有空的時候要Re-display
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");    //視窗名稱

    glutDisplayFunc(display);    //執行display(void)函式
    glutMouseFunc(mouse);   //執行mouse(void)函式
    glutMotionFunc(motion);  //執行motion(void)函式
   
    glutMainLoop();

    return EXIT_SUCCESS;
}


 5_會動的小精靈嘴巴

繼續使用剛剛的程式碼,在Motion函式中加入嘴巴角度的變化設定,如此一來便可藉由拖曳改變嘴巴大小

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

float mouth=0;

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLE_FAN);  //三角形依扇形的方式展開
    glVertex2f(0,0);
    for(float angle=0+mouth;angle<3.1415926*2-mouth;angle+=3.1415926/100){
        glVertex2f(cos(angle),sin(angle));
    }
    glEnd();
    glutSwapBuffers();
}

void mouse(int button,int state,int x,int y)  //滑鼠點擊
{
    //printf("%d %d %d %d\n",button,state,x,y);
    glColor3f(1,x/300.0,y/300.0);
}

void motion(int x,int y)  //拖曳
{
    mouth=x/300.0;
    glColor3f(1,x/300.0,y/300.0);
    glutPostRedisplay();  //告訴電腦,有空的時候要Re-display
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04160223_This is a round.");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

沒有留言:

張貼留言