本週主題: 打光Lighting
電腦圖學期中考模擬練習網址:https://jsyeh.org/gl/
11行程式+中文註解
week06-1_lighting
1.貼上GLUT 11行程式碼
2.貼上week06-0_sample 打光的程式碼
程式碼:
修改燈光位置和燈光顏色:
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 0.0f, 1.0f };///1.0改成0.0
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };///5.0加負號
執行後結果:
課本範例
1.開啟網址 https://jsyeh.org/3dcg10/
2.下載 source windows data 檔案 解壓縮
3.把data丟進windows裡
3.執行Light Material.exe
4.按右鍵可改models模型和materials顏色參數
week06-2_lighting_glNormal3f
1.貼上week06-1_lighting
2.在display裡加上程式
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle++, 0, 1, 0);
glBegin(GL_POLYGON);
glNormal3f(0, 0, 1);///打光的法向量
glVertex2f(-1, -1);
glVertex2f(+1, -1);
glVertex2f(+1, +1);
glVertex2f(-1, +1);
glEnd();
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
3.在main函式裡加上 glutIdleFunc(display);
程式碼:
執行後結果(會轉動的茶壺):
week06-3_glm_obj_model
1.課本範例source做設定
2.把glm.h放到桌面week06-3_glm_obj_model目錄裡
3.把glm.c放到桌面week06-3_glm_obj_model目錄裡,改名為glm.cpp
4.在專案裡按右鍵 Add Files, 加入glm.cpp
程式碼:
#include <GL/glut.h>
#include "glm.h"
GLMmodel * pmodel = NULL;
void drawmodel(void)
{
if(!pmodel){
pmodel = glmReadOBJ("data/Al.obj");
if(!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
drawmodel();
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week06 glm obg model");
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言