2024年3月4日 星期一

小葉老師教圖學 Week03 mouse滑鼠, translate移動, rotate旋轉

 # Week03



## mouse滑鼠

1. 桌面有「葉正聖老師上課用軟體」 freeglut 安裝(拉到桌面),再把 lib的 libfreeglut.a 複製成 libglut32.a

2. CodeBlocks File-New-Project, GLUT專案, week03_mouse

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

4. 再3行


```cpp

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


}

```


glutCreateWindow()之後, 在 glutMainLoop()之前,


```cpp

glutMouseFunc(mouse);

```


## week03_mouse



```cpp

///請貼上10行GLUT程式

#include <GL/glut.h>

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

void display()

{

    glutSolidTeapot( 0.3 );

    glutSwapBuffers();

}               ///state:0:下去, 1上來

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

{///定義mouse函式 button:0左鍵, 1:中鍵, 2:右鍵

    ///printf("Hello World\n"); ///遇到mouse印Hello

    ///printf("%d %d %d %d\n", button, state, x, y);

    if(state==GLUT_DOWN){

        printf("glVertex2f((%d-150)/150.0, -(%d-150)/150.0);\n", x, y);

    }

}

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

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week03 mouse");


    glutDisplayFunc(display);

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


    glutMainLoop();

}

```



## Transformation.exe




https://jsyeh.org/3dcg10/

下載 data.zip 下載 win32的zip

windows.zip 

=> 下載\windows\Transformation.exe

data.zip    

=> 下載\windows\data\一堆模型


## week03_mouse_glTranslatef



```cpp

///請貼上10行GLUT程式

#include <GL/glut.h>

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

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

void display()

{

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

    glPushMatrix(); ///備份矩陣

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

        glutSolidTeapot( 0.3 );

    glPopMatrix(); ///還原矩陣

    glutSwapBuffers();

}               ///state:0:下去, 1上來

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

{///我們這裡,要巧妙的修正

    teapotX = (x-150)/150.0;

    teapotY = -(y-150)/150.0;

}

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

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week03 mouse glTranslatef");


    glutDisplayFunc(display);

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


    glutMainLoop();

}

```


## week03_mouse_gRoltatef



```cpp

#include <GL/glut.h>

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

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

}               ///state:0:下去, 1上來

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

{///我們這裡,要巧妙的修正

    teapotX = (x-150)/150.0;

    teapotY = -(y-150)/150.0;

}

void motion(int x, int y)

{

    angle = x;

    display();

}

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

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week03 mouse glTranslatef");


    glutDisplayFunc(display);

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

    glutMotionFunc(motion); ///這行,設定mouse的motion動作

    glutMainLoop();

}

```



## week03_mouse_translate_rotate


```cpp

#include <GL/glut.h>

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

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_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week03 mouse glTranslatef");


    glutDisplayFunc(display);

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

    glutMotionFunc(motion); ///這行,設定mouse的motion動作

    glutKeyboardFunc(keyboard);

    glutMainLoop();

}

```





沒有留言:

張貼留言