2022年3月14日 星期一

Qinye_week04


111/03/14上課筆記

WEEK04-1開啟老師上課網頁

1.至https://jsyeh.org/3dcg10/下載data&win32
2.解壓縮windows至windows,解壓縮data(data為很多模型檔)至windows資料夾中
3.點選Transformation.exe開啟

WEEK04-2新增一課堂作業(Translate)

1.FileànewàprojectàGlutà檔名:week04_Translate
2.刪除全部main程式碼,貼上上周茶壺的程式碼

#include <GL/freeglut.h>
void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();///push備份矩陣
        glTranslatef(0.3,0.2,0);///移動
        glColor3f(0,0,1);///藍色
        glutSolidTeapot(0.3);///茶壺
    glPopMatrix();///pop還原矩陣

    glutSwapBuffers();///2被交換的buffer
}

int main(int argc, char**argv)
{///進階的main函式
    glutInit( &argc, argv);///初始化
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);///兩倍交換避免畫面閃爍+3D深度
    glutCreateWindow("week04的移動練習視窗");///建視窗

    glutDisplayFunc( display );///畫圖函式

    glutMainLoop();///主要迴圈
    return 0;
}

*glTranslatef(X,Y,Z),f為浮點數
*glTranslated(X,Y,Z),d為double
*$ git commit -m "add week02" --date="2022-03-02 12:00:00" à修改git hub時間

3.將茶壺移動寫成一函式

#include <GL/freeglut.h>
void myTeapot(float x,float y)
{
    glPushMatrix();///push備份矩陣
        glTranslatef(x,y,0);///移動
        glColor3f(0,0,1);///藍色
        glutSolidTeapot(0.3);///茶壺
    glPopMatrix();///pop還原矩陣
}

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

    glColor3f(0,1,1);
    myTeapot(+0.5,+0.5);
    myTeapot(+0.5,-0.5);
    myTeapot(-0.5,-0.5);
    myTeapot(-0.5,+0.5);
    glutSwapBuffers();///2被交換的buffer
}

int main(int argc, char**argv)
{///進階的main函式
    glutInit( &argc, argv);///初始化
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);///兩倍交換避免畫面閃爍+3D深度
    glutCreateWindow("week04的移動練習視窗");///建視窗

    glutDisplayFunc( display );///畫圖函式

    glutMainLoop();///主要迴圈
    return 0;
}

WEEK04-3新增一課堂作業(mouse)

1.FileànewàprojectàGlutà檔名:week04_mouse
2.使用mouse事件,設一函式輸入滑鼠按鈕+狀態+座標
*Button:          *state:             *x:           *y:
0à左鍵          0à按下          x座標      y座標
1à中鑑          1à放開
2à右鍵

#include <GL/freeglut.h>

#include <stdio.h>///printf()

void display()

{

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    glutSwapBuffers();///2被交換的buffer

}

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

{

    printf("%d %d %d %d\n",button,state,x,y);///印出滑鼠狀態至exe

}

int main(int argc, char**argv)

{///進階的main函式

    glutInit( &argc, argv);///初始化

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);///兩倍交換避免畫面閃爍+3D深度

    glutCreateWindow("week04_mouse");///建視窗


    glutDisplayFunc( display );///畫圖函式

    glutMouseFunc(mouse);


    glutMainLoop();///主要迴圈

    return 0;

}


3.將Translate與mouse合併使用,當滑鼠點擊方形黑式窗,則茶壺位移
#include <GL/freeglut.h>
#include <stdio.h>///printf()
int mouseX=0, mouseY=0;
void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,0);///黃色的
    glPushMatrix();///(滑鼠座標-一半的滑鼠座標)/一半的滑鼠座標=視窗中心
        glTranslatef((mouseX-150)/150.0,-(mouseY-150)/150.0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();///2被交換的buffer
}
void mouse(int button,int state,int x,int y)
{
    ///printf("%d %d %d %d\n",button,state,x,y);
    mouseX=x;
    mouseY=y;
}
int main(int argc, char**argv)
{///進階的main函式
    glutInit( &argc, argv);///初始化
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);///兩倍交換避免畫面閃爍+3D深度
    glutCreateWindow("week04_mouse");///建視窗

    glutDisplayFunc( display );///畫圖函式
    glutMouseFunc(mouse);

    glutMainLoop();///主要迴圈
    return 0;
}
*上課講解圖,紅色為視窗座標,綠色為滑鼠事件座標
4.用滑鼠印出程式碼並畫圖
#include <GL/freeglut.h>
#include <stdio.h>///printf()
int mouseX=0, mouseY=0, N=0;///N為滑鼠的總點擊數
int mx[100],my[100];///陣列空間預設到100
///補充(3/21):最多可以有1000個頂點,陣列位置可設1000較保險
void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,0);///黃色的
    glBegin(GL_LINE_LOOP);
        for(int i=0; i<N; i++){///迴圈+陣列
            glVertex2f((mx[i]-150)/150.0,-(my[i]-150)/150.0);
        }
    glEnd();
    glutSwapBuffers();///2被交換的buffer
}
void mouse(int button,int state,int x,int y)
{
    ///printf("%d %d %d %d\n",button,state,x,y);
    mouseX=x;    mouseY=y;
    if(state == GLUT_DOWN){
        printf("    glVertex2f((%d-150)/150.0,-(%d-150)/150.0);\n",x,y);
        N++;
        mx[N-1]=x;     my[N-1]=y;///陣列 備份座標
    }
}
int main(int argc, char**argv)
{///進階的main函式
    glutInit( &argc, argv);///初始化
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);///兩倍交換避免畫面閃爍+3D深度
    glutCreateWindow("week04_mouse");///建視窗

    glutDisplayFunc( display );///畫圖函式
    glutMouseFunc(mouse);

    glutMainLoop();///主要迴圈
    return 0;
}

5.上傳至git hub














沒有留言:

張貼留言