1. 程式碼重新再利用
step1: 下載 freeglut-MinGW-3.0.0-1.mp.zip
step2: 進到資料夾,把檔案 freeglut 拉到桌面
step3: 點進 freeglut 👉 lib 資料夾
step4: 複製 libfreeglut.a 並且改名為libglut32.a
step5: 開啟code blocks
step6: 開新專案,專案名稱: week09-0_sample
step7: 安裝 Git 軟體
step8: 將 GitHub 上週的程式 clone 下來 (先有程式碼)
在小黑 Git Bash 裡,輸入指令:
👉 cd desktop (先進入桌面)
👉 git clone https://github.com/你的帳號/2024graphicb
👉 cd 2024graphicb (進入你的倉庫目錄)
👉 start . (開啟檔案總管)
step9: 確認環境沒問題
👉 安裝 OpenCV 要勾 Add PATH (第二個),裝在預設目錄
👉 Setting-Compiler 裡,要把 OpenCV 的三個設定設好
👇
(1) 在 Search directories 加入2個目錄
1-1 Compiler 👉 C:\OpenCV2.1\include
1-2 Linker 👉 C:\OpenCV2.1\lib
(2) 在 Linker setting 裡,加入 👉 cv210
👉 cxcore210
👉 highgui210
(3) 重開 CodeBlocks
step10: 專案的設定是否需要修改 (專案的右鍵,Properties)
step11: 開啟上週檔案
👉 File / Open,點選剛剛 Git 下載的 week07- 2_obj_gundam_opencv_texture.cbp
👉 到專案,在專案名稱上按右鍵,Add File 將glm.cpp 加入專案
step12: 因無法順利執行,需到檔案內容修改
👉 專案名稱上按右鍵,選最下面的 Properties ,跳出視窗,選第2個 Build targets
👉 把 Execution working dir 工作執行目錄,改成小數點"."
註: 參考 Week07 筆記
step13: Build & Run
step14: 複製 week07-2 的資料夾
👉 改名為: week09-1_obj_gundam_opencv_texture_lighting
step15: 再用 Notepad++ 開啟 week09-1 目錄裡的 .cbp 檔,改內容裡的所有檔名
2. 修改程式碼 👉 打光
step1: 把打光的8行陣列,貼在 int main() 之前
👇
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
step2: 把打光的12+2行函式,貼到 glutMainLoop() 之前
👇
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
step3: glDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL | GLM_TEXTURE);
要刪掉中間的 GLM_MATERIAL
step4: 為了讓畫面好看一點、打光漂亮、模型旋轉,我們做幾個修改
👉 改變光的位置(改陣列的值)
👇
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
改 👉 const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
👉 在 display() 加入旋轉
👇
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle++, 0, 1, 0);
drawmodel(); ///glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
👉 在 main 加入旋轉 glutIdleFunc(display);
3.
step1: 開新專案,專案名稱: week09-2_glutSolidCube_glutSolidTeapot_
glutSolidSphere
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("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
}
step2: 修改、加入程式碼
👉void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-0.6, +0.3, 0);
glutSolidTeapot( 0.3 ); ///茶壺
glPopMatrix();
glPushMatrix();
glTranslatef(+0.0, +0.3, 0);
glutSolidCube( 0.3 ); ///方塊
glPopMatrix();
glPushMatrix();
glTranslatef(+0.6, +0.3, 0);
glutSolidSphere( 0.3, 30, 30 ); ///圓球
glPopMatrix();
glutSwapBuffers();
}
4. 線圈 Wire
step1: 開新專案,專站名稱: week09-3_glutWireCube_glutWireTeapot_
glutWireSphere
step2: 直接貼上 week09-2 的程式碼
step3: 修改程式碼
display 新加 👉 glPushMatrix();
glTranslatef(-0.6, -0.3, 0);
glutWireTeapot( 0.3 ); ///茶壺
glPopMatrix();
glPushMatrix();
glTranslatef(+0.0, -0.3, 0);
glutWireCube( 0.3 ); ///方塊
glPopMatrix();
glPushMatrix();
glTranslatef(+0.6, -0.3, 0);
glutWireSphere( 0.3, 30, 30 ); ///圓球
glPopMatrix();
5. 上傳 GitHub 注意事項
step1: 小黑關掉
step2: CodeBlocks 關掉(都要按 Yes 存檔)
step3: 把每個專案的兩個目錄刪掉( bin, obj )
step4: 把 .depend 、 .layout 刪掉
step5: 把今天的4個程式的專案目錄,上傳到 GitHub
沒有留言:
張貼留言