2022年3月31日 星期四

A

WEEK06


https://jsyeh.org/3dcg10/下載 data win32

windows.zip => 下載 \windows\Light Material.exe









(左上)右鍵,換模型

(左下)右鍵,換Meterial

(左下)右鍵,換模型
 


實作

freeglut裝好,改lib

Ctrl-F找 light

偷程式碼






#include <GL/glut.h>
const GLfloat light_ambient[] = {0.0f,0.0f,0.0f,1.0f};
const GLfloat light_diffuse[]={1.0f,1.0f,1.0f,1.0f};
const GLfloat light_specular[]={1.0f,1.0f,1.0f,1.0f};
const GLfloat light_position[]={2.0f,5.0f,-5.0f,0.0f};

const GLfloat mat_ambient[] = {0.7f,0.7f,0.7f,1.0f};
const GLfloat mat_diffuse[]={0.8f,0.8f,0.8f,1.0f};
const GLfloat mat_specular[]={1.0f,1.0f,1.0f,1.0f};
const GLfloat high_shininess[]={100.0f};
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,0);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week06 light");
    glutDisplayFunc(display);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();

}

了解打光的作法

#include <GL/glut.h>
#include <stdio.h>
float x=150,y=150,z=0,scale=1.0,angle=0.0;
    ///使茶壺在正中央
int oldX=0,oldY=0,now=1;///預設為移動
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

void display()
{
    glClearColor(0.5,0.5,0.5,1);///R G B A(為半透明,目前沒開)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備分矩陣
        glTranslatef((x-150)/150.0,-(y-150)/150.0,z);
        glRotatef(angle,0,1,0);///對y軸轉動
        glScalef(scale,scale,scale);///都縮放成scale倍
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    glutSwapBuffers();

}

void keyboard(unsigned char key,int mousex,int mousey)
{
    if(key=='1' || key=='w' || key=='W')now=1;///移動
    if(key=='2' || key=='e' || key=='E')now=2;///轉動
    if(key=='3' || key=='r' || key=='R')now=3;///縮放
}
void mouse(int botton,int state,int mousex,int mousey)
{
    oldX=mousex; oldY=mousey;
}

void motion(int mousex,int mousey)///用滑鼠移動茶壺
{
    if(now==1){///移動
        x+=(mousex-oldX); y+=(mousey-oldY);
    }
    else if(now==2){///轉動
        angle+=(mousex-oldX);///轉動
    }
    else if(now==3){///縮放
        if((mousex-oldX)>0)scale*=1.01;
        if((mousex-oldX)<0)scale*=0.99;
    }

    oldX=mousex; oldY=mousey;
    display();
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06-light");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    ///偷來的程式要放在glutCreateWindow 之後,才會有效

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    ///放在glutMainLoop之前
    glutMainLoop();///卡在這裡,之後的程式,都不會執行到
}












整合打光+上周交的

2022年3月29日 星期二

圖學筆記week06

 開啟glut專案



不用像往常一樣刪掉,按ctrl+F,搜尋light



就會跳出以下結果
把程式碼成下列:
///#include <GL/glut.h>
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };///這之前的程式都刪掉
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };


const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 light");

    glutDisplayFunc(display);
    ///引用來的程式,要放glutCreateWindow之後,才會有效
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
///放glutMainloop()之前
    glutMainLoop();///卡在這裡,之前的程式都不會執行到
}
///
變成下列結果

把上週keyboard_mouse_motion拿出來
///
#include <GL/glut.h>
#include <stdio.h>
float x=150, y=150, z=0, scale=1.0;
int oldX=0, oldY=0;
void display()
{
    glClearColor(0.5, 0.5, 0.5, 1);///R, G, B, A其中A半透明功能,目前沒開
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備份矩陣
        glTranslatef( (x-150)/150.0 , -(y-150)/150.0, z);
        glScalef(scale, scale, scale);
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
}
void keyboard(unsigned char key, int mouseX, int mouseY)
{

}
void mouse(int button, int state, int mouseX, int mouseY)
{///為了修飾瞬間移動的錯誤,改加了這條程式碼
    oldX =mouseX; oldY =mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(mouseX - oldX > 0) scale *=1.01;
    if(mouseX - oldX < 0) scale *=0.99;
    ///x += (mouseX-oldX); y += (mouseY - oldY);
    oldX = mouseX;  oldY = mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week05 keyboard");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}
///
改成下列程式,加入打光
///
#include <GL/glut.h>
#include <stdio.h>
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };


const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
float x=150, y=150, z=0, scale=1.0;
int oldX=0, oldY=0;
void display()
{
    glClearColor(0.5, 0.5, 0.5, 1);///R, G, B, A其中A半透明功能,目前沒開
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備份矩陣
        glTranslatef( (x-150)/150.0 , -(y-150)/150.0, z);
        glScalef(scale, scale, scale);
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
}
void keyboard(unsigned char key, int mouseX, int mouseY)
{

}
void mouse(int button, int state, int mouseX, int mouseY)
{///為了修飾瞬間移動的錯誤,改加了這條程式碼
    oldX =mouseX; oldY =mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(mouseX - oldX > 0) scale *=1.01;
    if(mouseX - oldX < 0) scale *=0.99;
    ///x += (mouseX-oldX); y += (mouseY - oldY);
    oldX = mouseX;  oldY = mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week05 keyboard");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    glutMainLoop();
}
///

打出下列程式碼,使其可以用鍵盤按鍵執行移動、縮放和轉動的指令:
///
#include <GL/glut.h>
#include <stdio.h>
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };


const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
float x=150, y=150, z=0, scale=1.0, angle=0.0;
int oldX=0, oldY=0, now=1;///now: 1移動, 2轉動, 3縮放
void display()
{
    glClearColor(0.5, 0.5, 0.5, 1);///R, G, B, A其中A半透明功能,目前沒開
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備份矩陣
        glTranslatef( (x-150)/150.0 , -(y-150)/150.0, z);
        glRotatef(angle, 0,1,0);///對Y軸轉動
        glScalef(scale, scale, scale);///縮放成scale倍
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
}
void keyboard(unsigned char key, int mouseX, int mouseY)
{
    if(key=='1' || key=='w' || key=='W') now=1;///移動
    if(key=='2' || key=='e' || key=='E') now=2;///轉動
    if(key=='3' || key=='r' || key=='R') now=3;///縮放
}
void mouse(int button, int state, int mouseX, int mouseY)
{///為了修飾瞬間移動的錯誤,改加了這條程式碼
    oldX =mouseX; oldY =mouseY;
}
void motion(int mouseX, int mouseY)
{
     if(now==1){///移動
        x += (mouseX - oldX);
        y += (mouseY - oldY);///移動
    }else if(now==2){///轉動
        angle += (mouseX - oldX);///轉動
    }else if(now==3){
        if( mouseX - oldX > 0) scale *= 1.01;///縮放
        if( mouseX - oldX < 0) scale *= 0.99;
    }
    oldX = mouseX;  oldY = mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week05 keyboard");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    glutMainLoop();
}
///




Ru的電腦圖學筆記>

 


💩Step1 主題是打光

    1.下載模型

        -下載 Windows.zip、Data.zip

        -將 Windows.zip解壓縮至 Windows 

        -將 Data.zip 裡的 Data 檔案丟到 Windows 資料夾

        -把裡面 Light Materal 打開:左上(左鍵旋轉、右鍵換模型)、左下(右鍵換Material)

    2.觀察模型



    3.偷程式碼時間

        -打開 codeblocks 建立新的 GLUT 檔案 

        -檔名:week06_light

        -點開 main.cpp 按 Ctrl+F 搜尋 light

💩Step2 自己做模型!

    1.將上衣步驟查到的程式碼下來,其他刪掉,保留下面兩個部分


    2.建立一個茶壺的模型

        -結合之前茶壺的程式,配合打光的程式碼,做出打光的茶壺

        -偷來的程式要放glutCreateWindow()之後、glutMainLoop()之前才會有效!

    3.完成後 Build&Run 可以看到一個暗暗的茶壺

        -也可以將 const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f }; 這行的第三項改成 -5.0f


💩Step3 製作可以縮放、旋轉的茶壺

    1.建立新的 GLUT 檔案,檔名:week06_light_mouse_motin_rotate

    2.將打光程式、呼叫函式植入上週的程式碼



    3.執行,可以用滑鼠左右拖曳,茶壺會放大縮小

    4.在程式碼中加入旋轉值 angel,glRotatef(angle,0,1,0); 對Y軸轉動,執行茶壺會旋轉


    5.新增新的 GLUT 檔案,檔名:week06_keyboard_mouse_motion_all

        -讓移動、轉動、縮放同時存在(按鍵盤w/W/1顯示移動,按e/E/2顯示轉動,按r/R/3顯示縮放)


完成!!💖💖💖💖💖💖💖


烏鴉寫筆記 week06

6-1. jsyeh.org/3dcg10 下載 data 以及 win32

        解壓縮後打開 win32 中的 Light Material . exe 

        左上視窗:(拖曳)旋轉、(右鍵)換模型

        左下視窗:(右鍵)換材質

        右邊的參數:glLightfv(...)的 fv 是 float vector (陣列)

                                GLfloat light_pos (陣列):代表光源位置



6-2. 打開範例GLUT程式後從其中傑出以下兩段程式碼後貼在茶壺GLUT專案



6-3. 將打光程式植入上週程式中

6-4. 在上周的程式(滑鼠拖曳移動和縮放)中加入鍵盤動作(W、E、R)和旋轉的程式碼


爆肝week06

Week06:打光

範例 http://jsyeh.org/3dcg10
1. 下載網頁中的data, win32

2. 將windows解壓縮, data.zip/data丟進解壓縮後的windows資料夾

 3.開啟Light Material.exe
    左上視窗點擊滑鼠右鍵:變更模型
    右邊視窗點擊滑鼠右鍵:變更調整參數
    左下視窗點擊滑鼠右鍵:變更模型的材質(Materials)

    右方函式glLightfv():
    fv = float vector (浮點數陣列)
    glLightfv(GL_LIGHT0GL_POSITIONlight_pos)
                     第0道光   位置                    陣列



打光Light
開啟OpenGL新專案,按下Ctrl+f搜尋light
複製light陣列宣告與函式呼叫,並加上之前的茶壺程式碼

```
#include <GL/glut.h>
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
///以上是複製的程式碼(陣列宣告)
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week06_light");

    glutDisplayFunc(display);
    ///以下是複製的程式碼(函式呼叫)
    ///複製的程式碼要放在glutCreateWindow()之後才有效
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();ˇ一
}
```

或是將複製的陣列宣告與函式呼叫植入上週的程式碼


```

#include <stdio.h>///印出用
#include <GL/glut.h>
///打光陣列
/*這裡放陣列宣告*/

float x=150, y=150, z=0, scale=1.0;///座標設定為150, 150在視窗正中央
int oldX=0, oldY=0;
void display()
{
    glClearColor(0.5, 0.5, 0.5, 1);///灰色背景R,G,B,A 其中A為半透明Alpha值
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備分矩陣
        glTranslatef((x-150)/150.0, -(y-150)/150.0, z);
        glScalef(scale, scale, scale);///x,y,z縮放為 scale倍,維持原有比例
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
}
void mouse(int button, int state, int mouseX, int mouseY)
{///重設圖案位置,避免瞬間移動的問題
    oldX = mouseX; oldY = mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(mouseX-oldX >0) scale *=1.01;
    if(mouseX-oldX <0) scale *=0.99;///+-百分之一

    oldX = mouseX;
    oldY = mouseY;

    display();
}
void keyboard( unsigned char key, int mouseX, int mouseY )
{
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week05 keyboard");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);///鍵盤事件函式
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

/*這裡放函式呼叫*/
    glutMainLoop();
}
```


續上週:旋轉

加上新的變數(angle)與glRoataef()函式
```
#include <stdio.h>///印出用
#include <GL/glut.h>
///打光陣列
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

float x=150, y=150, z=0, scale=1.0, angle=0.0;///座標設定為150, 150在視窗正中央
int oldX=0, oldY=0;
void display()
{
    glClearColor(0.5, 0.5, 0.5, 1);///灰色背景R,G,B,A 其中A為半透明Alpha值
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備分矩陣
        glTranslatef((x-150)/150.0, -(y-150)/150.0, z);
        glRotatef(angle, 0, 1, 0);///對Y軸轉動
        glScalef(scale, scale, scale);///x,y,z縮放為 scale倍,維持原有比例
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
}
void mouse(int button, int state, int mouseX, int mouseY)
{///重設圖案位置,避免瞬間移動的問題
    oldX = mouseX; oldY = mouseY;
}
void motion(int mouseX, int mouseY)
{
    
    angle += (mouseX-oldX);///轉動
    
    ///if(mouseX-oldX >0) scale *=1.01;///縮放
    ///if(mouseX-oldX <0) scale *=0.99;///+-百分之一

    ///oldX = mouseX;///移動
    ///oldY = mouseY;

    display();
}
void keyboard( unsigned char key, int mouseX, int mouseY )
{
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week05 keyboard");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);///鍵盤事件函式
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    ///打光函式
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();
}
```


Keybaord()函式輸入切換功能

```
#include <stdio.h>///印出用
#include <GL/glut.h>
///打光陣列
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

float x=150, y=150, z=0, scale=1.0, angle=0.0;///座標設定為150, 150在視窗正中央
int oldX=0, oldY=0, now=1;///設定now變數切換功能
void display()
{
    glClearColor(0.5, 0.5, 0.5, 1);///灰色背景R,G,B,A 其中A為半透明Alpha值
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備分矩陣
        glTranslatef((x-150)/150.0, -(y-150)/150.0, z);
        glRotatef(angle, 0, 1, 0);///對Y軸轉動
        glScalef(scale, scale, scale);///x,y,z縮放為 scale倍,維持原有比例
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
}
void keyboard( unsigned char key, int mouseX, int mouseY )
{
    if(key=='1' || key=='w' || key=='W') now=1;///移動
    if(key=='2' || key=='e' || key=='E') now=2;///轉動
    if(key=='3' || key=='r' || key=='R') now=3;///縮放
}
void mouse(int button, int state, int mouseX, int mouseY)
{///重設圖案位置,避免瞬間移動的問題
    oldX = mouseX; oldY = mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(now==1)
    {
        x += (mouseX-oldX);///移動
        y += (mouseY-oldY);
    }
    else if(now==2)
    {
        angle += (mouseX-oldX);///轉動
    }
    else if(now==3)
    {
        if(mouseX-oldX >0) scale *=1.01;///縮放
        if(mouseX-oldX <0) scale *=0.99;///+-百分之一
    }

    display();
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week05 keyboard");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);///鍵盤事件函式
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    ///打光函式
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();
}
```

VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...