2024年6月3日 星期一

SY-BlingBangBang電腦圖學🎬-Week16

 1. 

    step1: 下載 freeglut-MinGW-3.0.0-1.mp.zip 

    step2: 進到資料夾,把檔案 freeglut 拉到桌面

    step3: 點進 freeglut 👉 lib 資料夾

    step4: 複製 libfreeglut.a 並且改名為libglut32.a

    step5: 安裝 OpenCV 要勾 Add PATH (第二個),裝在預設目錄

              👉 Setting-Compiler 裡,要把 OpenCV 的三個設定設好

                  👇 

                  (1) 重開 CodeBlocks

                       👇 原本的檔案在 View 👉 Start Page

                  (2) 在 Search directories 加入2個目錄

                        1-1 Compiler 👉 C:\OpenCV2.1\include

                        1-2 Linker 👉 C:\OpenCV2.1\lib

                  (3) 在 Linker setting 裡,加入 👉 cv210

                                                            👉 cxcore210

                                                            👉 highgui210

    step6: 開啟上課教材: https://jsyeh.org/3dcg10/

    step7: 將 data 資料夾放到解壓縮後的 win32 👉開啟 Projection.exe


    step8: gluLookAt 可以轉換鏡頭

    step9: 開新專案,專案名稱: week16-0_sample

2. 

    step1: 複製 week16-0 整個目錄,複製成 week16-1_sample_gluLookAt

    step2: 修改目錄名、.cbp專案檔名,用 Notepad++ 改內容

    step3: 修改程式碼,是想要 Look At 看著某個物體

              先看第50行程式碼:

                    我們的眼睛: 0,0,0

                    要看的主角 center:-2.4,1.2,-6

                    我們的up向量: 0,1,0

             我們要注入的函式是:

                    glutReshapeFunc(resize);

                    在第39、40行加入: 

                        ///要看左上角的那個轉動的紅色實心圓球 -2.4,1.2,-6

                        gluLookAt(0,0,0, -2.4,1.2,-6 , 0,1,0);



    step4: 重新修改程式碼

              先複製37-41的程式碼,貼到 97 行的 key() 函式

              再加 if 條件式和鍵盤控制的條件

                if(key=='0'){ ///原來的視角,模仿 resize() 函式

                    glMatrixMode(GL_MODELVIEW);

                    glLoadIdentity() ;

                }

                else if(key=='1'){ ///看左上角

                    glMatrixMode(GL_MODELVIEW);

                    glLoadIdentity() ;

                    gluLookAt(0,0,0, -2.4,1.2,-6 , 0,1,0);

                }

                else if(key=='2'){ ///看正上方

                    glMatrixMode(GL_MODELVIEW);

                    glLoadIdentity() ;

                    gluLookAt(0,0,0, 0,1.2,-6 , 0,1,0);

                }

                else if(key=='3'){ ///看右上角

                    glMatrixMode(GL_MODELVIEW);

                    glLoadIdentity() ;

                    gluLookAt(0,0,0, +2.4,1.2,-6 , 0,1,0);

                }




3.

    step1: 開新專案,專案名稱: week16-2_teapot_gluLookAt_glutReshapeFunc_

              reshape

    step2: 貼上11行 glut 程式碼

    #include <GL/glut.h>

    void display()

    {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glutSolidTeapot( 0.3 );

        glutSwapBuffers();

    }

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

    {

        glutInit(&argc, argv);

        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

        glutCreateWindow("week16-2");

        glutDisplayFunc(display);

        glutMainLoop();

    }



    step3: 看課本教學,了解 glutReshapeFunc(resize); 怎麼用

    step4: gluPerspective(fovy視野/張角,aspoct長寬比,zNear近,zFar遠) 透視投影

              gluOrtho(左,右,下,上,近,遠) 垂直投影

              gluFrustum(左,右,下,上,近,遠)

    step5: 寫程式,在 int main() 裡

                    glutReshapeFunc(reshape);

              偷 week16-1 第33行

              在前面準備

                void reshape(int w, int h){

                    float ar = w / (float) h; ///aspect ratio 長寬比

                    glViewport(0, 0, w, h); ///設定可以看到的範圍,全看到

                    glMatrixMode(GL_PROJECTION); ///現在要設定 Projection 矩陣

                    glLoadIdentity(); ///最原始的單位矩陣I

                    gluPerspective(60, ar, 0.1, 100); ///透視投影的參數

                        ///fovy視野/張角,aspoct長寬比,zNear近,zFar遠

                    glMatrixMode(GL_MODELVIEW); 

                        ///現在要切換回 model view 矩陣 (畫圖的T,R,S的)

                    glLoadIdentity();

                    gluLookAt(0, 0, -3,  0, 0, 0,  0, 1, 0);

                }





    step6: 調整視窗初始大小,在 int main()

              glutInitWindowSize(600,50);




4. 

    step1: 開新專案,專案名: week16-3_myTexture_id1_id2_glBindTexture

    step2: 貼上11行程式碼,再貼上 week05-2  的貼圖程式碼

    step3: 寫程式碼

              在 disply()

              int id1, id2; ///新加2個變數

              void display()

              {

                    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


                    glBindTexture(GL_TEXTURE_2D, id1);

                    glBegin(GL_POLYGON);

                        glTexCoord2f(0,0); glVertex2f(-1, +1);

                        glTexCoord2f(0,1); glVertex2f(-1, -1);

                        glTexCoord2f(1,1); glVertex2f(+1, -1);

                        glTexCoord2f(1,0); glVertex2f(+1, +1);

                    glEnd();

                    glBindTexture(GL_TEXTURE_2D, id2);

                    glutSolidTeapot( 0.3 );


                    glutSwapBuffers();

                }

                在 int main()

                id1 = myTexture("C:/background.jpg");///去下載earth map 地圖

                id2 = myTexture("C:/earth.png");///再準備第2張貼 C:/background.jpg



    step4: 將圖片放到目錄下






5. glmUnitize() v.s 自己調大小








沒有留言:

張貼留言