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

```


沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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