
要有光
0. 介紹1. 如何設定材質及燈光
2. 筆記
3. 小技巧
4. 小思考
0. 介紹
黃色區塊 > 選 模型
紅色區塊 > 選 材質
藍色區塊 > 一些 程式設定
1. 如何設定材質及燈光?
從小葉資料夾(參考week02
(1) 設定
glLightfv( p1, p2, p3 ) -> 光的設定方法
glMaterialfv( p1, p2, p3 ) -> 物體材質的設定方法
(2) 怎麼寫
燈光:
glLightfv(p1, p2, p3);
p1:哪一個燈
P2:設定甚麼 ( GL_AMBIENT、
GL_DIFFUSE
GL_SPECULAR
GL_POSITION )
P3:設定值 [ 陣列 ] (請參考下圖)
示意圖
陣列設定方法:
GLfloat light_pos[] = { x, y, z, d }
x : 沿 x 軸
y : 沿 y軸
z : 沿z軸
d : 距離
GLfloat material_Kd[] = { R, G, B, alpha }
2.筆記:
kd:打甚麼顏色光英文單字:
pos 位置
Ka:
Kd:
Ks:
K:係數
a:ambient > 周圍漫射
d:diffuse > 隨著角度,直接照較亮,側著照較暗
s:specular > 特別亮的點
e:emission > 自體發光
Se: shininese emission 自體發亮
更多有關於 Ka、Kd、Ks
https://cg2010studio.com/2011/03/27/specular/
學習步驟:
(1) notepad++複製程式碼(2) 剪貼程式碼
(3) 改程式碼
todo:把茶壺打光
(1) file>new>project
(2) cp 177行 到notepad
(3) 寫10行GLUT茶壺
(4) 把 Light (打光)
(a) 8行陣列
(b) glutMainLoop()
第三堂
glutIdleDisplay(display) > 閒閒沒事時執行
3.小技巧:
知其然知其所以然:
小工具:cTour
程式上色:notepad++ NPPexport,丟到瀏覽器複製
4.小思考:
Q為何要用GLfloat而不是float ?
A根據compiler不同,不同 compiler 的 float
Q. 轉太快 > 因為電腦跑的速度不同
A. 計時器調整
Q. 轉太快 > 因為電腦跑的速度不同
A. 計時器調整
今天的課堂程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <GL/glut.h> | |
/*剪貼1--------------------------------------------------------*/ | |
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 }; | |
/*剪貼1--------------------------------------------------------*/ | |
float angle=0; | |
void display() | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glPushMatrix(); | |
glRotatef(angle, 0,1,0); | |
glutSolidTeapot(0.3); | |
glPopMatrix(); | |
angle+=0.05; | |
glutSwapBuffers(); | |
} | |
int main(int argc, char * argv[]) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); | |
glutCreateWindow("alanhc"); | |
/*剪貼2--------------------------------------------------------*/ | |
glClearColor(1,1,1,1); | |
glEnable(GL_CULL_FACE); | |
glCullFace(GL_BACK); | |
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--------------------------------------------------------*/ | |
glutDisplayFunc(display); | |
glutIdleFunc(display); | |
glutMainLoop(); | |
} |
沒有留言:
張貼留言