2024年3月11日 星期一

阿哲 week04

 week04

##1.下載課本範例

https://jsyeh.org/3dcg10/  data  win32

解壓縮windows.zip 將data.zip 內的data拉到 windows內

安裝freeglut lib內的libfreeglut.a複製成libglut32.a

##mosue_mouse_glScalef

1.file-week04_mouse_glScalef  

貼上10行程式

2.設定顏色

    void display()

    {

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

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glColor3f(R,G,B)///3f就是R,G,B

    }

void motion(int x,int y)
{
    s=1+(x-150)/150.0;/// 變大變小的變數 0~1~2
    display();////重畫畫面
}

main底下

glutmotionFunc(motion);拖動motion的時候

##試著交換 translate/rotate 兩行程式碼

1.課本範例 Transformation

先把 translate 移到右方,再rotate看看

下方程式,按右鍵,可swap translate/rotate 交換

再rotate看看,差在哪裡

公轉,自轉

選轉軸不一樣

##week04_translate_rotate_scale

1file-week04_translate_rotate_scale

複製week04_mouse_glScalef程式

修改

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

    } 


##week04_keyboard_mouse_motion

1.file-week04_keyboard_mouse_motion

2.把10行GLUT程式

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.加入滑鼠的函式

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

    } 





沒有留言:

張貼留言