2024年6月6日 星期四

電腦圖學 Week16 🥴

 week16-0_sample

1. moodle下載 freeglut 拖到桌面

2. 把桌面\freglut\lib\libfreeglut.a 複製成 libglut32.a

3. 安裝 OpenCV 2.1

- 安裝第三步驟, Add OpenCV to system PATH(勾第2或3個, 不要勾第1個)

- 目錄不要改,要用預設的

- 要重開CodeBlocks

4. CodeBlocks 的 settings>compiler 加入 opencv 的3個設定

- Search directories > Compiler 要加上 C:\OpenCV2.1\include

- Search directories > Linker 要加上 C:\OpenCV2.1\lib

- Linker settings 要加上 cv210 / cxcore210 / highgui210

5. CodeBlocks開GLUT專案, 目錄選桌面, 專案名取 week16-0_sample

6. GLUT要選桌面的 freeglut

7. 下載課本的範例 https://jsyeh.org/3dcg10  win32、data、source

8. windows.zip 先解壓縮, data.zip 直接點開拉到windows資料夾

9. 打開Projection.exe

week16-1_sample_gluLookAt

1. CodeBlocks開GLUT專案, 目錄選桌面, 專案名取 week16-1_sample_gluLookAt

2. GLUT要選桌面的 freeglut

3. 在第39行加上一行程式碼,讓畫面的中心點在左上角的圓
 gluLookAt(0, 0, 0, -2.4, 1.2, -6, 0, 1, 0);

4. 先把39行註解,在void key()加上一段程式碼
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);
}

5. 執行結果按1的時候看左上方;按2的時候看上方;按3的時候看右上方


week16-2_glutReshapeFunc_gluPerspective_gluLookAt

1. 觀察課本範例

    gluPerspective 透視投影

    gluPerspective( 視角,長寬比,Z近,Z遠)

2. CodeBlocks開GLUT專案, 目錄選桌面, 專案名取 week16-1_sample_gluLookAt

3. GLUT要選桌面的 freeglut

4. 貼上11行程式碼

#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");
    glutDisplayFunc(display);
    glutMainLoop();
}

5. 加上一個 reshape() 函式

void reshape(int w, int h)
{
   glViewport(0, 0, w, h); ///設定視窗會看到的區域
   float ar = w / (float) h; ///長寬比
   glMatrixMode(GL_PROJECTION); ///切換投影矩陣
   glLoadIdentity();
   gluPerspective(60, ar, 0.01, 100); ///透視投影
   glMatrixMode(GL_MODELVIEW); ///切model view矩陣
   glLoadIdentity();
   gluLookAt(0, 0, -2, 0, 0, 0, 0, 1, 0); ///透視投影
}

6. 在 main() 裡多加一行程式
glutReshapeFunc(reshape);

7. 以背後視角來看茶壺,茶壺也不會因視窗改變而比例改變

8. 在 main() 裡多加一行程式,這行可以改視窗長寬
glutInitWindowSize(300, 600);

兩張貼圖:week16-3_myTexture_id1_id2_glBindTexture

1. 把github上之前寫過的 week05_rotating_earth 的main.cpp 拿來用

2. CodeBlocks開GLUT專案, 目錄選桌面, 專案名week16-3_myTexture_id1_id2_glBindTexture

3. GLUT要選桌面的 freeglut

4. 貼上 week05_rotating_earth 的程式

5. 把 earth.jpg 放到 C:/

6. 第六行加上程式碼,要準備兩個整數,用來存貼圖的代號id

  int id1, id2;

7. main() 加上3行,改變視窗大小,加上背景圖

glutInitWindowSize(500, 500);
id1 = myTexture("c:/earth.jpg");
id2 = myTexture("c:/background.jpg");

8. 修改display()

glBindTexture(GL_TEXTURE_2D, id2);///背景
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, id1);///地球
glPushMatrix();
    glRotatef(90, 1, 0, 0);
    glRotatef(angle++, 0, 0, 1);
    gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot( 0.3 );
glPopMatrix();

7. 找一張背景圖放到 C:/


沒有留言:

張貼留言