2024年3月7日 星期四

Bai's-電腦圖學-Week03

 #week03

#week03_mouse


0.安裝freeglut
1.10行程式
2.加3行 mouse


=> print "Hello mouse"

加上#include <stdio.h> ///for printf()

void mouse(int button, int state, int x, int y)
{
    printf("Hello Mouse!\n");

}



=>  print4個參數 
0=右鍵 1=中鍵 2=左鍵(button)
void mouse(int button, int state, int x, int y)
{
    printf("%d %d %d %d\n", button, state, x, y);
    
}




void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN)
        printf("glVertex2f((%d-150)/150.0, -(%d-150)/150.0);\n", x, y);
}

*下載課本範例 =>data & win32

解壓縮後data資料夾放進window資料夾

網址: https://jsyeh.org/3dcg10

使用window=> Transformation.exe




#week03_mouse_glTranslatef


拿46/49/54行程式碼

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

///程式從 week03_mouse 拿來用
#include <GL/glut.h>
#include <stdio.h>
float teapotX = 0, teapotY = 0;
void mouse(int button, int state, int x, int y)
{
    teapotX = (x-150)/150.0;
    teapotY = -(y-150)/150.0;
}
void display()
{///偷範例的第46行glClear()、49行、54行
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); ///第49行
        glTranslatef(teapotX, teapotY, 0);///自己寫
        glutSolidTeapot( 0.3 );
    glPopMatrix(); ///第54行
    glutSwapBuffers();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week03 mouse");
    glutDisplayFunc(display);
    glutMouseFunc(mouse); ///(1) 註冊mouse函式

    glutMainLoop();
}

-----------------------------------------------------------------------------------------------------------------------------
老師筆記:

#week03_mouse_glRotatef

把week03_mouse_glTranslatef程式改 寫
新增
1.float = 0
2.glRotatef(angle, 0, 0, 1); ///對z軸轉

要看到轉動細節 => mouse motion 滑鼠動作的細節

註冊一個函式 =>glutMotionFunc(motion);    ///讓motion動起來

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

///程式從 week03_mouse_glTranslatef 拿來用
#include <GL/glut.h>
#include <stdio.h>
float teapotX = 0, teapotY = 0;
float angle = 0; ///新增角度
void mouse(int button, int state, int x, int y)
{
    teapotX = (x-150)/150.0;
    teapotY = -(y-150)/150.0;
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0, 0, 1); ///對z軸轉
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
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 rotate");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion); ///(3) motion的細節

    glutMainLoop();
}
-----------------------------------------------------------------------------------------------------------------------------


#week03_mouse_translate_rotate


void keyboard(unsigned char key, int x, int y)
{
    if(key=='e') method = 1; ///轉動
    if(key=='w') method = 2; ///移動
}

然後宣告函式
glutKeyboardFunc(keyboard); ///按鍵