2024年5月6日 星期一

week04補

0.1  下載課本的範例 https://jsyeh.org/3dcg10/ 的 [data][win32]

0.2  解壓縮windows.zip,把data.zip中的data拉到windows裡

0.3  安裝freeglut,把lib的libfreeglut.a 複製成 libglut32.a

1.0  mouse_glScalef

1.1  開新專案week04_mouse_glscalef,貼上十行程式

1.2  設定顏色

        void display (){ ///加上以下

            glClearColor(R , G , B , A); ///背景顏色

            glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

            glColor3f(R, G, B); ///物體顏色

        } ///以上

1.3  用滑鼠左右移動,放大縮小

        float s = 1;

        void display (){ ///加上以下

            glPushMatrix();

                glScalef(s, s, s); ///縮放s倍

                glutSolidTeapot( 0.3 );

            glPopMatrix();

        } ///以上

        void motion(int x, int y){

            s = 1 + (x-150)/150.0;

            display();

        }

        int main(int argc,char * argv[]){ ///加上以下

            glutMotionFunc(motion);///拖動motion的時候

        } ///以上

2.0 交換 translate/rotate 兩行程式

2.1 打開課本範例Transformation

2.2 可先移動位置再旋轉較好觀察

2.3 程式按右鍵可交換程式順序

2.4 交換完再觀察一遍 👉🏾公轉and自轉

3.0 公轉and自轉展示

3.1 開新專案week04_mouse_translate_rotate_scale(自轉)

3.2 複製1.0的程式碼

3.3 修改

    float teapotX = 0.5, teapotY = 0; ///放右邊

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

    float s = 0.3;

    glPushMatrix(); ///加上以下

        glTranslatef(teapotX, teapotY, 0);

        glRotatef(angle++, 0, 0, 1);

    glPopMatrix(); ///以上

    int main(int argc,char * argv[]){ ///加上以下

        glutIdleFunc(display);

    } ///以上

3.4 開新專案week04_mouse_rotate_translate_scale(公轉)

3.5 複製3.1的程式碼

3.6 交換glRotatef(angle++, 0, 0, 1); 及 glTranslatef(teapotX, teapotY, 0); 的順序

4.0 大雜燴

4.1 開新專案week04_keyboard_mouse_motion

4.2 貼十行程式,刪背景、茶杯的程式碼

4.3 加入鍵盤事件的函式

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

            printf("key:%c x:%d y:%d\n", key, x, y);

        }

        int main(int argc,char * argv[]){ ///加上以下

            glutKeyboardFunc(keyboard); ///鍵盤事件的函式

        } ///以上

4.5 加入滑鼠事件的函式

    int N=0;

    float teapotX[1000], teapotY[1000];

    void display (){ ///加上以下

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

        for(int i=0; i<N; i++){

            glPushMatrix();

                glTranslatef(teapotX[i], teapotY[i], 0);

                glutSolidTeapot( 0.1 ); ///茶壺大小

            glPopMatrix();

        }

        glutSwapBuffers();

    } ///以上

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

        printf("key:%c x:%d y:%d\n", key, x, y);

    }

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

        teapotX[N] = (x-150)/150.0;

        teapotY[N] = -(y-150)/150.0;

        N++;

    }

    int main(int argc,char * argv[]){ ///加上以下

        glutMouseFunc(mouse); ///滑鼠事件的函式

    } ///以上

沒有留言:

張貼留言