期中模擬考(可先練習):https://jsyeh.org/gl/opengl_10_func.html
課本範例:jsyeh.org/3dcg10,下載 source、data、win32
1.打光
1-0. 安裝 freeglut,複製一個「libfreeglut.a」,並將其名稱改為「libglut32.a」
1-1. File ⮕ New ⮕ Project,開啟 GLUT project,檔案名稱 week06- 0_sample_code
1-2. 觀察程式
1-3. File ⮕ New ⮕ Project,開啟 GLUT project,檔案名稱week06-1_lighting
1-4. 新增20行程式碼(8+14(2+12))(8+14(2+12))
打光相關陣列
```cpp
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 };
```
打光的呼叫程式
```cpp
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);
```
2. 打開課本範例,研究光的位置及數值,嘗試讓物品從背光變成面光
3. week06-2_lighting_glNormal3f
3-1. File ⮕ New ⮕ Project,開啟 GLUT project,檔案名稱 week06-2_lighting_glNormal3f
3-2. 把剛剛的茶壺變成一個正方,順便加上 mouse 轉動這個正方
3-3. 複製前一個的程式碼,並新增程式
```cpp
void diaplay()
{
glPushMatrix();
glRotatef(angle++, 0, 1, 0);
glScalef(0.8, 0.8, 0.8);
glBegin(GL_POLYGON);
glNormal3f(0, 0, 1);
glVertex2f(-1, -1);
glVertex2f(+1, -1);
glVertex2f(+1, +1);
glVertex2f(-1, +1);
glEnd();
glPopMatrix();
}
int main(int argc, char * argv[])
{
glutIdleFunc(display);
}
```
4. week06-3_glm_obj_model
4-1. File ⮕ New ⮕ Project,開啟 GLUT project,檔案名稱 week06-3_glm_obj_model
4-2. 貼上11行程式碼
```cpp
#include <GL/glut.h>
#include "glm.h"
GLMmodel * pemodel = NULL;
void drawmodel(void)
{
if(!pmodel){
pmodel = glmReadOBJ("data/porsche.obj");
if(!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}
4-3. 從課本範例的 source 中,將 glm.h 和 glm.c 放到專案目錄,並讓後者改名為 glm.cpp
4-4. 在專案名稱上按右鍵,Add File 將 glm.cpp 加入專案
4-5. 最後把課本範例的 data 資料夾,複製到 freeglut/bin
4-6. 修改程式碼
```cpp
void drawmodel(void)
{
pmodel = glmReadOBJ("data/Al.obj");
}
void display()
{
drawmodel(); ///glutSolidTeapot( 0.3 );
}
```
.png)
沒有留言:
張貼留言