2024年3月4日 星期一

week03

 ##mouse滑鼠

1.桌面有「葉正聖老師上課用軟體」freeglut安裝(拉到桌面),再把lib的libfreeglut.a複製成libglut32.a
2.codeblocks file-new-project,glut專案,week03_mouse
3.把blog/github的10行程式貼進來
4.再3行

#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\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     windows.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();


}



---------------------------

#include <GL/glut.h>

#include <stdio.h>

float teapotX=0,teapotY=0;

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

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景

    glPushMatrix();/// 備份矩陣

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

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

    glutSolidTeapot(0.3);

    glPopMatrix();///還原矩陣

    glutSwapBuffers();


}

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

int oldX=0,oldY=0;

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


}

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

{

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

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

}

int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);


     glutCreateWindow("week03_mouse_glTranslatef");


     glutDisplayFunc(display);

     glutMouseFunc(mouse);///這一行,設定mouse函式

     glutMotionFunc(motion);///設定motion的motion動作

     glutKeyboardFunc(keyboard);


      glutMainLoop();

}




沒有留言:

張貼留言