2024年3月4日 星期一

Sean的電腦圖學日誌 week03 mouse滑鼠,translate移動,rotate旋轉

 #Wewk03


##mouse滑鼠

1. 安裝桌面freeglut  再把lib的libfreeglut.a複製成libglut.32.a

2. Codeblocks file-new-project,GLUT 專案,week03_mouse

3. 把blog/github 的 10行程式貼近來

4.再三行

#include <stdio.h>為了printf()


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

    printf("Hello World\n");

}

glutCreateWindow()之後,在glutDisplayFunc(display);後面加

    glutMouseFunc(mouse);

5.茶壺座標printf("%d %d %d %d\n",button.state.x,y);

button:0左鍵 1:中鍵 2:右鍵


6.mouse 函式裡面改\

if(state==GLUT_DOWN){

    printf("glVertex2f((%d-150)/150.0,-(%d-150)/150.0);\n",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();

}



沒有留言:

張貼留言