顯示具有 09160694_鍾沚霖 標籤的文章。 顯示所有文章
顯示具有 09160694_鍾沚霖 標籤的文章。 顯示所有文章

2022年4月12日 星期二

爆肝week08

 

Week07:模型

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

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

 3.開啟Light Material.exe改檔名
 4.glm.c改檔名為glm.cpp並加入專案
    開啟以查看程式碼
    

#期中考複習

```
glPushMatrix();//備份矩陣
    glTranslatef(x, y, z);//移動
    glRotatef(angle, x, y, z);//旋轉
    glScalef(x, y, z);//縮放
glPopMatrix();//還原矩陣
```

# 使用上次的打光程式碼

2022年3月29日 星期二

爆肝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();
}
```

2022年3月22日 星期二

爆肝Week05

Week05:移動、旋轉、縮放、矩陣

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

2. 將windows解壓縮, data.zip/data丟進解壓縮後的windows資料夾
 3.開啟Translate.exe
    按Alt+S可以交換位移(glTranslatef)與旋轉(glRotatef)
    順序不同,效果也不同

鍵盤事件glutKeyboardFunc()
1.使用先前的茶壺程式碼,加上keyboard函式並利用printf()印出執行的動作
```
#include <stdio.h>///印出用
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    glColor3f(1, 1, 0);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void keyboard( unsigned char key, int x, int y )
{
    printf("你按下了%c在 %d %d座標\n", key, x, y);
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week05 keyboard");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);///鍵盤事件函式
    glutMainLoop();
}

```
2-1.加上先前教過的Motion與Mouse函式
```
#include <stdio.h>///印出用
#include <GL/glut.h>
float x=0, y=0, z=0;
int oldX=0, oldY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef((x-150)/150.0, -(y-150)/150.0, z);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button, int state, int mouseX, int mouseY)
{
}
void motion(int mouseX, int mouseY)
{
}
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();
}
```


2-2.在Motion拖曳函式中加上參式與重設座標的公式
```
void motion(int mouseX, int mouseY)
{
    x +=(mouseX-oldX);
    y +=(mouseY-oldY);

    oldX = mouseX;
    oldY = mouseY;

    display();
}
```

2-3.在 mouse函式中設置錨點,去除圖形瞬移的錯誤
```
#include <stdio.h>///印出用
#include <GL/glut.h>
float x=150, y=150, z=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);
        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)
{
    x +=(mouseX-oldX);
    y +=(mouseY-oldY);

    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();
}
```

縮放函式glScalef()

3.加上縮放函式,在motion函式中改寫公式
```
#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();
}
```

未完待續...

2022年3月15日 星期二

爆肝Week04

旋轉Rotatef()

1.修改之前的茶壺程式碼

```

#include <GL/glut.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);

    glPushMatrix();///備份矩陣

        glRotatef(90, 0, 0, 1);

        glColor3f(1, 1, 0);

        glutSolidTeapot(0.3);

    glPopMatrix();///還原矩陣

    /*備份與還原間要縮排*/

    glutSwapBuffers();

}


int main(int argc, char** argv)

{

    glutInit( &argc, argv);

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow("week_02");


    glutDisplayFunc(display);

    glutMainLoop();

}

```


利用滑鼠事件glutMotionFunc()旋轉

```

#include <GL/glut.h>

float angle=0;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);

    glPushMatrix();///備份矩陣

        glRotatef(angle, 0, 0, 1);

        glColor3f(1, 1, 0);

        glutSolidTeapot(0.3);

    glPopMatrix();///還原矩陣

    /*備份與還原間要縮排*/

    glutSwapBuffers();

}

void Motion(int x, int y)

{

    angle=x;

    display();

}

int main(int argc, char** argv)

{

    glutInit( &argc, argv);

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow("week_04");


    glutDisplayFunc(display);ouse

    glutMotionFunc(Motion);

    glutMainLoop();

}

```

    只使用Motion函式會在每次旋轉時重製角度
可以利用Mousec函式在每一次拖曳結束後固定角度
```

#include <GL/glut.h>

float angle=0, oldX =0;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);

    glPushMatrix();///備份矩陣

        glRotatef(angle, 0, 0, 1);

        glColor3f(1, 1, 0);

        glutSolidTeapot(0.3);

    glPopMatrix();///還原矩陣

    /*備份與還原間要縮排*/

    glutSwapBuffers();

}

void Motion(int x, int y)

{

    angle +=(x-oldX);

    oldX =x;

    display();///重製畫面

}

void MouseClick(int button, int state, int x, int y)

{

    oldX=x;///定錨

}

int main(int argc, char** argv)

{

    glutInit( &argc, argv);

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow("week_04");


    glutDisplayFunc(display);

    glutMotionFunc(Motion);

    glutMouseFunc(MouseClick);

    glutMainLoop();

}

```

***glutMouseFunc()與glutMotionFunc()的差別***

--glutMouseFunc()適用於滑鼠點擊,按下與放開個為不同事件
--glutMotionFunc()適用於滑鼠拖曳

2022年3月8日 星期二

爆肝Week03

 範例 http://jsyeh.org/3dcg10

1. 下載網頁中的data, win32

2. 將windows解壓縮, data.zip/data丟進解壓縮後的windows資料夾
 3. 執行Transformation.exe
    對視窗右上角點擊右鍵:換模型
    拖曳下方綠色數值:旋轉、移動、大小


移動 Translattef()
使用上週寫出茶壺的程式碼
1.在座標上畫出圖形
```
#include <GL/glut.h>

void display()
{
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);

    glPushMatrix();///備份矩陣
    ///移動會累積, 因為它會修改矩陣
        glTranslatef(0.2, 0.6, 0.8);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    /*備份與還原間要縮排*/
    glutSwapBuffers();
}

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

    glutDisplayFunc(display);
    glutMainLoop();
}

```
結果:

2.利用坐標軸在視窗上畫出4個茶壺:
```
#include <GL/glut.h>
void MyTeapot(float x, float y)
{
    glPushMatrix();///備份矩陣
    ///移動會累積, 因為它會修改矩陣
    /// glTranslatef(0.5,0.5,0.5)
        glTranslatef(x, y, 0);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    /*備份與還原間要縮排*/
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    MyTeapot(0.5, 0.5);
    MyTeapot(0.5, -0.5);
    MyTeapot(-0.5, -0.5);
    MyTeapot(-0.5, 0.5);
    glutSwapBuffers();
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week_02");

    glutDisplayFunc(display);
    glutMainLoop();
}

```
結果:

滑鼠事件 glutMouseFunc()
1.新增滑鼠事件,茶壺會移動到指定位置
```
#include <GL/glut.h>
float mouseX=0, mouseY=0;
void MyTeapot(float x, float y)
{
    glPushMatrix();///備份矩陣
    ///移動會累積, 因為它會修改矩陣
    /// glTranslatef(0.5,0.5,0.5)
        glTranslatef(x, y, 0);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    /*備份與還原間要縮排*/
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    MyTeapot( (mouseX-150.0)/150.0, -(mouseY-150)/150.0 );
    glutSwapBuffers();
}
void MouseClick(int button, int state, int x, int y)
{
    mouseX=x; mouseY=y;
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week_03");
    glutMouseFunc(MouseClick);///滑鼠事件

    glutDisplayFunc(display);
    glutMainLoop();
}
```
結果:
2.座標換算
新增printf函式輸出座標可以查看當前座標
3.利用printf函式印出座標與程式碼,協助完成
HW2
```
#include <GL/glut.h>
#include <stdio.h>
float mouseX=0, mouseY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
///    MyTeapot( (mouseX-150.0)/150.0, -(mouseY-150)/150.0 );
    glutSwapBuffers();
}
void MouseClick(int button, int state, int x, int y)
{
    mouseX=x; mouseY=y;
    printf("    glVertex3f((%d-150.0)/150.0, -(%d-150)/150.0);\n", x, y);
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week_03_mouse");
    glutMouseFunc(MouseClick);///滑鼠事件

    glutDisplayFunc(display);
    glutMainLoop();
}

```


2022年2月22日 星期二

爆肝Week01

。第一個OpenGL程式

    1. 打開CodeBlocks新建project



    2.選取OpenGL專案


    3.下一步,替專案命名,下一步,完成


    4.選取程式檔,執行結果


。第一個GLUT程式
    事先從Moodle下載freeglut-MinGW-3.0.0-1.mp.zip並解壓縮
    將其中的lib_libfreeglut.a改名為libglut32.a


    1.打開ClodeBlocks新建project



    2.選取GLUT專案


    3.下一步,替專案命名,下一步,選取解壓縮的資料夾,下一步,完成


    4.選取程式檔,執行結果

。又双叒是Github (使用Github上傳,備份檔案)
    事先安裝Git for windows
    1.登入Github後新建專案2022graphics
    2.勾選Add README file, Add gitnore(選擇C++)
    3.開啟Git Bash 輸入下列指令:
        3-1 cd desktop (進入桌面)
        3-2 git clone http://個人網址
        3-2 cd 2022graphics 
    4.使用Git Bash指令上傳檔案
        4-1 將程式碼資料夾丟進從Github下載的資料夾裡
        4-2 git status (查看狀態)
        4-3 git add . (加入檔案"'Spacebar'+'.'不能省略")
        4-4 git status (查看狀態,檔名變為綠色)
        4-5 git config --global user.email "信箱"
        4-6 git config --global user.name "使用者名稱"
        4-7 git commit -m "訊息"
        4-8 git push (送上雲端,打開Github網頁確認成功)

VERY BEAUTIFUL, VERY POWERFUL

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