2024年3月4日 星期一

Yxin week03

 WEEK03

#mouse滑鼠

1. 安裝freeglut

2.將lib中libfreeglut.a複製成libglut32.a

3.Codeblocks File-New-Project,Glut專案 week03_mouse

4.把github的10行程式貼過來

5.再3行

->void mouse(int button, int state, int x, int y){

        printf("Hello World\n");

    }

    glutDisplayFuncWindow(display);

    glutMouseFunc(mouse);


6.將printf("Hello World\n");註解掉
6.1.改成printf("%d %d %d %d\n", button, state, x, y);  → 按茶壺任意點可以出現座標





#https://jsyeh.org./3dcg10/

下載data.zip window.zip壓縮檔 data資料夾windows資料夾裡transformation.exe


#glTranslatef移動

float teapotX=0.teapotY=0;  新加的程式,用來放茶壺的座標

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();  ///備份矩陣

glTranslatef(teapotX,teapotY,0);  ///把座標移動

glutSolidTeapot(0.3);

glPopMatrix();  ///還原矩陣

glutSwapBuffers();


mouse函式改成→ teapotX=(x-150)/150.0;  teapotY=-(y-150)/150.0;

執行後 就可以利用滑鼠 點出所有座標程式


#glRotatef旋轉(角度,x,y,z)右手安培

float angle=0;  ///旋轉角度

    glRotatef(angle,0,0,1);  ///z軸

glutMotionFunc(motion);  ///設定motion的motion動作 mouse在拖曳motion動



#glutKeyboardFunc(keyboard);

key正在按的鍵(key=='e')  ///旋轉

int method=1;  ///1:旋轉,2:移動

int oldX=0,oldY=0;


void keyboard(unsigned char key,int x,int y)

{

    if(key=='e')method=1;  ///旋轉rotate

    if(key=='w')method=2;  ///移動translate

}

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

{

    ///teapotX=(x-150)/150.0;

    ///teapotY=-(y-150)/150.0;

    oldX=x;

    oldY=y;

}

void motion(int x,int y)

{

    if(method==1){

    angle+=x-oldX;

}

else if(method=2)

{

    teapotX+=(x-oldX)/150.0;

    teapotY -= (y-oldY)/150.0;

}

oldX=x;

oldY=y;

display();

}


沒有留言:

張貼留言