8 - Lighting
๊ฐ์
- ๋ฌผ์ฒด์ ๊ฐ์ ์์ (Visible Color of Objects)
- ๋น์ ๋ฐ์ฌ (Reflection of Light)
- Phong ์กฐ๋ช ๋ชจ๋ธ (Phong Illumination Model)
- ๋ค๊ฐํ ์์ด๋ฉ (Polygon Shading)
- ๋ฉด/์ ์ ๋ฒ์ (Face / Vertex Normal)
- ํ๋ฉด / Gouraud / Phong ์์ด๋ฉ (Flat / Gouraud / Phong Shading)
๋ฌผ์ฒด์ ๊ฐ์ ์์
- ๋น์ด ๋ฌผ์ฒด์ ๋ฟ์ผ๋ฉด, ์ผ๋ถ๋ ํก์๋๊ณ ์ผ๋ถ๋ ๋ฐ์ฌ๋จ
- ๋ฌผ์ฒด์ ์์์ ๋ฐ์ฌ๋๋ ๋น์ ํ์ฅ์ ๋ฐ๋ผ ๊ฒฐ์ ๋จ
- ์: ๋นจ๊ฐ ๋ฌผ์ฒด๋ ์ฃผ๋ก ๋นจ๊ฐ์ ๋น์ ๋ฐ์ฌํ๊ณ ๋๋จธ์ง ํ์ฅ์ ํก์ํ๋ฏ๋ก ๋นจ๊ฐ์์ผ๋ก ๋ณด์
- ์ด๋ค ์์ด ํก์๋๊ณ ๋ฐ์ฌ๋๋์ง๋ ํ๋ฉด์ ๊ณ ์ ํ ์ฑ์ง์ ์ํด ๊ฒฐ์ ๋จ
- ๋ฐ๋ผ์, ๋ฌผ์ฒด์ ๊ฐ์ ์์์ ๊ด์์ ์์์ ์ํฅ์ ๋ฐ์
Room for one colour, Olafur Eliasson
๋ฌผ์ฒด์ ๊ฐ์ ์์ ๊ณ์ฐํ๊ธฐ
- ์ปดํจํฐ ๊ทธ๋ํฝ์ค(CG)์์๋ ์์์ ์ผ๋ฐ์ ์ผ๋ก
R
,G
,B
์ฑ๋ถ์ผ๋ก ํํํจ - ๊ด์ ์์ (Light color): ๊ด์์์ ๋ฐฉ์ถ๋๋ ๊ฐ ์ ์ฑ๋ถ์ ์ธ๊ธฐ
- ์:
(1, 1, 1)
โ ํฐ์ ๊ด์
- ์:
- ์ฌ์ง ์์ (Material color): ์
์ฌ๊ด์์ ๋ฐ์ฌ๋๋ ๊ฐ ์ ์ฑ๋ถ์ ๋น์จ
- ์:
(0.5, 0, 0)
โ ๋นจ๊ฐ์์ ์ ๋ฐ๋ง ๋ฐ์ฌ๋๊ณ , ๋ น์๊ณผ ํ๋์ ์ ๋ถ ํก์๋จ
- ์:
- ๋น๊ณผ ์ฌ์ง์ RGB ์ฑ๋ถ์ ์ฑ๋ถ๋ณ ๊ณฑ์ (element-wise multiplication) ํ๋ ๊ฒ์ด ํ๋ฉด ๋ฐ์ฌ์ ๊ทผ์ฌ ๋ชจ๋ธ๋ก ์ ์ ํจ
๋ฌผ์ฒด์ ์์ ๊ณ์ฐํ๊ธฐ: ์์
- ์์:
- ์ด๋ค ํ๋ฉด์ ์ฌ์ง ์์์ด
(0.5, 0.8, 0.2)
์ผ ๊ฒฝ์ฐ- ์ด ํ๋ฉด์ ์ ์ฌ๊ด์ ๋นจ๊ฐ์ 50%, ๋ น์ 80%, ํ๋์ 20%๋ฅผ ๋ฐ์ฌํจ
- ์ด๋ค ํ๋ฉด์ ์ฌ์ง ์์์ด
- ๊ด์ ์์์ด
(1.0, 1.0, 1.0)
์ผ ๊ฒฝ์ฐ โ ํฐ์ ๊ด์- ๊ฐ์ ํ๋ฉด ์์ =
(0.5, 0.8, 0.2)
(์ฑ๋ถ๋ณ ๊ณฑ์ ๊ฒฐ๊ณผ)
- ๊ฐ์ ํ๋ฉด ์์ =
- ๊ด์ ์์์ด
(1.0, 0.0, 0.0)
์ผ ๊ฒฝ์ฐ โ ๋นจ๊ฐ์ ๊ด์- ๊ฐ์ ํ๋ฉด ์์ =
(0.5, 0.0, 0.0)
โ ๋ ์ด๋์ด ๋นจ๊ฐ์ ํ๋ฉด
- ๊ฐ์ ํ๋ฉด ์์ =
๋น์ ๋ฐ์ฌ
- ๋น์ ๋ฌผ์ฒด์ ์ํด ํก์(absorbed), ๋ฐ์ฐ(emitted), ์ฐ๋(scattered), ๋ฐ์ฌ(reflected), ๊ตด์ (refracted)๋ ์ ์์
- ์ฐ๋๊ณผ ๋ฐ์ฌ๋ ๋ถํฌ๋ช ๋ฌผ์ฒด ํ๋ฉด์ ์๊ฐ์ ํน์ฑ(์: ํ๋ฉด ์์, ํ์ด๋ผ์ดํธ ๋ฑ)์ ๊ฒฐ์ ํ๋ ์ฃผ์ ์์ธ
- ๋ฐ์ฌ์ ์ข
๋ฅ:
- Diffuse reflection (๋๋ฐ์ฌ)
- Specular reflection (์ ๋ฐ์ฌ)
- Ideal specular reflection
- Non-ideal specular reflection (Glossy reflection)
Computer Graphics์์๋ ์ฐ๋๊ณผ ๋ฐ์ฌ๋ฅผ ๋ชจ๋ ๋ฐ์ฌ(reflection)๋ก ํต์นญํจ
๋๋ฐ์ฌ Diffuse reflection
- ํน์ ํ์ฅ์ ๋น์ ๋ชจ๋ ๋ฐฉํฅ์ผ๋ก ๊ณ ๋ฅด๊ฒ ์ฐ๋ โ ํ๋ฉด ์์์ ๊ฒฐ์ ํจ
- ์์ ์ ๋ฌด๊ดํ(view-independent) ํน์ฑ
- ์:
- ์ํ์ ํ์ฅ์ ๊ฐํ๊ฒ ์ฐ๋
- ์ ํ์ฅ์ ๊ฑฐ์ ๊ท ์ผํ๊ฒ ์ฐ๋
- ๋ชจ๋ ํ์ฅ์ ํก์ (์ฐ๋ ๊ฑฐ์ ์์)
๋๋ฐ์ฌ - Lambert์ ์ฝ์ฌ์ธ ๋ฒ์น
- ์์ ํ๋ฉด์์ ๋ฐ์ฌ๋๋ ์๋์ง๋ ์ ์ฌ๊ด ๋ฐฉํฅ๊ณผ ํ๋ฉด ๋ฒ์ ์ฌ์ด์ ๊ฐ๋์ cosine์ ๋น๋กํจ
- : ๋ฐ์ฌ๊ด์ ์ธ๊ธฐ
- : ์ ์ฌ๊ด์ ์ธ๊ธฐ
- : ์ ์ฌ ์ง์ ์์์ ํ๋ฉด ๋ฒ์ ๋ฒกํฐ
- : ์ ๊ทํ๋ ์ ์ฌ๊ด ๋ฒกํฐ
- Lambert ๋ฒ์น์ 2D ์๊ฐํ
์ด ์ด๋ฏธ์ง๋ Brown ๋ํ Andy van Dam ๊ต์ ๊ฐ์์๋ฃ์์ ์ธ์ฉ๋จ
http://cs.brown.edu/courses/cs1230/lectures.shtml
์์ ์ ๋ฐ์ฌ
- ๋งค๋๋ฝ๊ณ ํํํ ํ๋ฉด์์ ๊ฑฐ์ธ๊ณผ ๊ฐ์ ๋ฐ์ฌ ๋ฐ์
โ ๊ฑฐ์ธ์์ ์ด๋ฏธ์ง ์์ฑ - ์์ ์ ์์กด์ ์ธ(view-dependent) ํน์ฑ
์์ ์ ๋ฐ์ฌ - ๋ฐ์ฌ ๋ฒ์น
, , ์ ๋์ผํ ํ๋ฉด์์ ์กด์ฌ
์ ์ฌ๊ฐ
๊ณผ ์ ์ ์์ชฝ์ ์์น
๊ธฐํธ ์ค๋ช :
- : ์ ์ฌ ์ง์ ์์์ ํ๋ฉด ๋ฒ์
- : ์ ๊ทํ๋ ์ ์ฌ๊ด ๋ฒกํฐ
- : ์ ๊ทํ๋ ๋ฐ์ฌ๊ด ๋ฒกํฐ
๋ถ์์ ์ ๋ฐ์ฌ (a.k.a. Glossy Reflection)
- ๊ดํ์ด ์์ผ๋ ๊ฑฐ์ธ์ฒ๋ผ ์์ ํ ๋งค๋๋ฝ์ง ์์ ํ๋ฉด์์์ ๋ฐ์ฌ
- ํ๋ฉด์ ๊ฑฐ์น ๊ธฐ๋ก ์ธํด ๋ฐ์ฌ๊ด์ด ํผ์ง
- ๋ฐ์ ํ์ด๋ผ์ดํธ ์์ฑ
- ์์ ์ ์์กด์ ์ธ(view-dependent) ํน์ฑ
์ผ๋ฐ ์ฌ์ง์ ๋ฐ์ฌ
- ๋๋ถ๋ถ์ ์ฌ์ง ํ๋ฉด์ diffuse reflection๊ณผ (๋น์ด์์ ์ธ) specular reflection์ ๋์์ ๊ฐ์ง
Total Scattering = Diffuse + Specular
โ ์ ์ฒด ์ฐ๋ ๋ถํฌ๋ ๋ ๋ฐ์ฌ์ ํฉ์ผ๋ก ๊ตฌ์ฑ๋จ
์ด ์ด๋ฏธ์ง๋ Brown ๋ํ Andy van Dam ๊ต์ ๊ฐ์์๋ฃ์์ ์ธ์ฉ๋จ
http://cs.brown.edu/courses/cs1230/lectures.shtml
Lighting (or Illumination)
- ์ปดํจํฐ ๊ทธ๋ํฝ์ค์์ lighting (๋๋ illumination)์
๋น์ ํจ๊ณผ๋ฅผ ๊ณ์ฐํ๋ ๊ณผ์ ์ ์๋ฏธํจ
โ ๋ฌผ์ฒด ํ๋ฉด ์์ ๋ฐ ํ์ด๋ผ์ดํธ ๊ณ์ฐ
Phong ์กฐ๋ช ๋ชจ๋ธ
- ์ปดํจํฐ ๊ทธ๋ํฝ์ค์์ ๊ฐ์ฅ ๋๋ฆฌ ์ฌ์ฉ๋๋ โ๊ณ ์ ์ โ ์กฐ๋ช
๋ชจ๋ธ ์ค ํ๋
- ์คํ์ (empirical) ๋ชจ๋ธ์ด๋ฉฐ, ๋ฌผ๋ฆฌ ๊ธฐ๋ฐ ๋ชจ๋ธ์ ์๋
Bรนi Tฦฐแปng Phong (1942 โ 1975)
- ์ธ ๊ฐ์ง ๊ตฌ์ฑ ์์:
- Ambient
- ๋นํน์ ํ ์ผ์ ํ ์ ์ญ ์กฐ๋ช
- ๊ฐ์ ์กฐ๋ช ์ ๋ํ ์กฐ์กํ ๊ทผ์ฌ
- Diffuse
- Lambert ๋ฒ์น์ ๋ฐ๋ฅด๋ ๋๋ฐ์ฌ ๋ชจ๋ธ
- ํ๋ฉด ์์ ๊ฒฐ์
- Specular
- cosโฟ(ฮฑ)์ ์ด์ฉํ ๊ดํ ๋ฐ์ฌ ๊ทผ์ฌ
- ๋น๋๋ ๋ฌผ์ฒด์ ํ์ด๋ผ์ดํธ ๊ณ์ฐ
- Ambient
Ambient + Diffuse + Specular = Phong Reflection
- ์ด์ ๋ถํฐ ๊ฐ ์ง์ ์์ Phong ์กฐ๋ช ๋ชจ๋ธ์ ๊ฐ ๊ตฌ์ฑ ์์(ambient, diffuse, specular)์ ์์ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ด
- ์ด ์ง์ ์ ๋ค์ ์ค ํ๋์ผ ์ ์์:
- ๋ค๊ฐํ์ ์ ์
- ๋ค๊ฐํ ๋ด๋ถ์ ์์ ์ง์ (ํ๋ฆ ๊ณต๊ฐ์ ํฝ์ ์ ํด๋น)
Ambient ์ฑ๋ถ
- : ๊ด์์ ambient ์
- : ํ๋ฉด ์ง์ ์ ์ฌ์ง ambient ์
- : ์ต์ข ambient ์ ๊ฒฐ๊ณผ
- : ์ฑ๋ถ๋ณ ๊ณฑ์ (element-wise multiplication)
๊ฒฐ๊ณผ
์ด ์ด๋ฏธ์ง๋ ํ ์ฌ์ค A&M ๋ํ๊ต์ Huamin Qu ๊ต์ ์ฌ๋ผ์ด๋์์ ์ธ์ฉ๋จ
http://faculty.cs.tamu.edu/schaefer/CSCE441/notes.html
Diffuse ์ฑ๋ถ
: ๊ด์ ๋ฐฉํฅ ๋ฒกํฐ
: ํ๋ฉด ๋ฒ์ ๋ฒกํฐ
- ๊ณผ ์ ๋จ์ ๋ฒกํฐ (unit vector)
: ๋ด์ (dot product)
: ๊ด์์ diffuse ์์
: ์ฌ์ง์ diffuse ์์
: ํ๋ฉด ์ง์ ์์์ diffuse ๊ฒฐ๊ณผ ์์
๊ฒฐ๊ณผ
(๊ทธ๋ฆผ: ambient ์ฑ๋ถ๋ง ์ ์ฉ๋ ๋ ๋๋ง ๊ฒฐ๊ณผ)
(๊ทธ๋ฆผ: ambient + diffuse ์ฑ๋ถ ์ ์ฉ)
Specular ์ฑ๋ถ
: ์์ (view) ๋ฐฉํฅ ๋ฒกํฐ
: ๋ฐ์ฌ ๋ฐฉํฅ ๋ฒกํฐ
- ์ ์ ๋จ์ ๋ฒกํฐ
: ๊ดํ ๊ณ์ (shininess coefficient)
: ๊ด์์ specular ์์
: ์ฌ์ง์ specular ์์
: ํ๋ฉด ์ง์ ์์์ specular ๊ฒฐ๊ณผ ์์
๊ฒฐ๊ณผ
(๊ทธ๋ฆผ: ambient ์ฑ๋ถ๋ง ์ ์ฉ๋ ๋ ๋๋ง)
(๊ทธ๋ฆผ: ambient + diffuse ์ฑ๋ถ ์ ์ฉ)
(๊ทธ๋ฆผ: ambient + diffuse + specular, )
(๊ทธ๋ฆผ: ambient + diffuse + specular, )
(๊ทธ๋ฆผ: ๊ดํ ๊ณ์ n์ด ์ปค์ง์๋ก specular highlight๊ฐ ์ข์์ง)
[Demo] Phong Illumination
- http://www.cs.toronto.edu/~jacobson/phong-demo/
- โPhong Shadingโ์ ์ ํํ ํ ๋ค์ ์์๋ค์ ๋ฐ๊ฟ๋ณด๋ฉฐ ์คํ
- ambient, diffuse, specular ๊ณ์ ๋ฐ ์์
- ๊ดํ ๊ณ์ (shininess)
- ๋ฌผ์ฒด ์ข ๋ฅ, ๊ด์ ์์น, ๋ฐฐ๊ฒฝ์ ๋ฑ๋ ๋ณ๊ฒฝ ๊ฐ๋ฅ
Shading
- ๋ฌผ์ฒด ํ๋ฉด์ ๋ํ๋๋ ์์ ๋ถํฌ์ ๋ณํ
- ์กฐ๋ช ์ ์ํฅ์ ๊ฐํ๊ฒ ๋ฐ์
(๊ทธ๋ฆผ: ์กฐ๋ช ์กฐ๊ฑด์ ๋ฐ๋ผ ํ๋ฉด์ ์์์ด ๋ค๋ฅด๊ฒ ๋ณด์)
- ์ปดํจํฐ ๊ทธ๋ํฝ์ค์์ shading์ด๋ผ๋ ์ฉ์ด๋ ๋ค์์ ์ค๋ช
ํจ:
- ์กฐ๋ช ๋ชจ๋ธ๋ก ์ธํด ๋ฐ์ํ๋ ํ๋ฉด ์์์ ๋ณํ
- ๋๋ ๋ค๊ฐํ ๋ด๋ถ์ ํฝ์ ์์ ๋ณํ
- ํ์ฌ ๋ค๋ฃจ๋ ์๋ฏธ๋ ํ์์
โ ํผ๋์ ํผํ๊ธฐ ์ํด polygon shading์ด๋ผ๋ ์ฉ์ด๋ฅผ ์ฌ์ฉํจ - Polygon shading:
์กฐ๋ช ๋ชจ๋ธ์ ๊ธฐ๋ฐ์ผ๋ก ๋ค๊ฐํ ๋ด๋ถ ๊ฐ ํฝ์ ์ ์์์ ๊ฒฐ์ ํ๋ ๊ณผ์
Surface Normal
- ํน์ ์ง์ ์์ ํ๋ฉด์ ์์ง์ธ ๋ฒกํฐ
โ ์ผ๋ฐ์ ์ผ๋ก ๋จ์ ๋ฒกํฐ ์ฌ์ฉ (๊ธธ์ด = 1) - shading ๋ฐ ์กฐ๋ช ๊ณ์ฐ์์ ํต์ฌ์ ์ธ ์ญํ
- Diffuse reflection:
- Lambert์ ์ฝ์ฌ์ธ ๋ฒ์น ์ ์ฉ
- Specular reflection:
- ๋ฐ์ฌ์ ๋ฒ์น ์ ์ฉ
(์์ ํฌํจ๋ ๊ทธ๋ฆผ: ์ฝ์ฌ์ธ ๋ฒ์น๊ณผ ๋ฐ์ฌ ๊ฐ๋)
Face Normal
- ๋ค๊ฐํ ๋ฉด์ surface normal์ ์ป๋ ๋ฐฉ๋ฒ
- ์ ์ ์ ์์๋ ์ค์ํจ!
- ์ผ๊ฐํ ์ ๋ฒ์ ์ ๋ค์๊ณผ ๊ฐ์ด ๊ณ์ฐ๋จ:
- : ์์ ๋ก ํฅํ๋ ๋ฒกํฐ
- : ์์ ๋ก ํฅํ๋ ๋ฒกํฐ
- ๊ทธ๋์ ์ ์ ์ ์๊ณ ๋ฐ๋ ๋ฐฉํฅ(counterclockwise) ๋์ด์ด ์ค์ํจ
- Face normal์ ๋ฐฉํฅ์ด ํ๋ฉด "๋ฐ๊นฅ์ชฝ"์ ํฅํ๊ฒ ํ๊ธฐ ์ํด
Flat Shading
- ๋ค๊ฐํ๋น ํ๋์ ๋ฒ์ (normal)์ ์ฌ์ฉ
- ๋ค๊ฐํ๋น ํ ๋ฒ๋ง ์์ ๊ณ์ฐ
- ๋น ๋ฅด์ง๋ง ๊ณก๋ฉด ํํ์๋ ๋ถ์ ํฉ
- ๋ค๊ฐํ ์๋ฅผ ์๋ฌด๋ฆฌ ๋๋ ค๋ ์ฌ์ ํ "๊ฐ์ ธ ๋ณด์(faceted)"
Smooth Shading
- ๊ผญ์ง์ ๋ง๋ค ํ๊ท ๋ธ ๋ฒ์ ๋ฒกํฐ ์ฌ์ฉ
- ์ธ์ ๋ค๊ฐํ ๊ฐ ๋ถ๋๋ฌ์ด ์์ ์ ํ ๊ฐ๋ฅ
- ๋ฐฉ๋ฒ:
- Gouraud shading
- Phong shading
Gouraud Shading
- ๊ผญ์ง์ ๋ง๋ค ํ๋์ vertex normal ์ฌ์ฉ
- ๊ฐ ๊ผญ์ง์ ์์ ์กฐ๋ช ๋ชจ๋ธ๋ก ์์์ ๊ณ์ฐ
- ๋ค๊ฐํ ๋ด๋ถ๋ ๊ผญ์ง์ ์์์ ๋ณด๊ฐํ์ฌ ์์ ๊ฒฐ์
- Barycentric interpolation ์ฌ์ฉ
Henri Gouraud (1944โ)
(์ด๋ฏธ์ง: Flat shading vs Gouraud shading ๋น๊ต)
- ๋ฌธ์ ์ : specular highlight๊ฐ ๋ถ์ ํํ๊ฒ ํํ๋จ
- ํ์ด๋ผ์ดํธ๊ฐ ์๊ณก๋๊ฑฐ๋ ์์ ์ฌ๋ผ์ง ์ ์์
- ๋ค๊ฐํ ์๋ฅผ ๋๋ฆฌ๋ฉด ์ด๋ฌํ ๋ฌธ์ ๋ ์ค์ด๋ฆ
Phong Shading
- ๊ผญ์ง์ ๋ง๋ค ํ๋์ vertex normal ์ฌ์ฉ
- ๋ค๊ฐํ ๋ด๋ถ๋ vertex normal์ ๋ณด๊ฐ(interpolation)
- ๋ณด๊ฐ๋ normal์ ์ฌ์ฉํ์ฌ ๊ฐ ํฝ์ ์์ ์์์ ๊ณ์ฐ
Bรนi Tฦฐแปng Phong (1942โ1975)
(์ด๋ฏธ์ง: Gouraud shading vs Phong shading ๋น๊ต)
- ํ์ด๋ผ์ดํธ๋ฅผ ํจ์ฌ ๋ ์ ํํ ํํํจ
- ๋ด๋ถ ํฝ์ ๋ง๋ค ๋ณด๊ฐ๋ normal์ ํด๋น ์ง์ ์ ์ค์ ํ๋ฉด normal์ ๋ ์ ๊ทผ์ฌํจ
- ํ์ง์ ์ข์ง๋ง ๊ณ์ฐ๋์ด ๋ ๋ง์
- Phong์ ์กฐ๋ช
๋ชจ๋ธ๊ณผ ํผ๋ํ์ง ๋ง ๊ฒ
- ๋์ผ ์ธ๋ฌผ์ด ๊ฐ๋ฐํ์ง๋ง ๋ค๋ฅธ ๊ฐ๋
[Demo] Polygon Shading
- Flat & Gouraud shading
- http://math.hws.edu/graphicsbook/demos/c4/smooth-vs-flat.html
- Gouraud & Phong shading
- http://www.cs.toronto.edu/~jacobson/phong-demo/
๋ฒ์ ๋ฒกํฐ ๋ณํ Normal Vector Transformation
- ์ด๋ค ํ๋ฉด ์ ์ ๋ค์ ์งํฉ์ affine ๋ณํ M์ด ์ ์ฉ๋๋ฉด,
- ์ ์ ๋ฒกํฐ(tangent)๋ M์ ์ํด ๋ณํ๋จ
- ์ ๊ฐ์ ์ฐจ์ด๋ M์ ์ํด ๋ณํ๋๊ธฐ ๋๋ฌธ
- ์ ์ ๋ฒกํฐ(tangent)๋ M์ ์ํด ๋ณํ๋จ
- ๊ทธ๋ฌ๋ ๋ฒ์ ๋ฒกํฐ(normal)๋ M์ ์ํด ๋ณํ๋์ด์๋ ์ ๋จ
- ๋ฒ์ ์ ์ ์ ์ ์์ง์ด์ด์ผ ํ๊ธฐ ๋๋ฌธ
- ์: ๋ํ์ M ์ ์ฉ โ OK
- ์ค๊ฐ: ๋ฒ์ ์ M ์ ์ฉ โ (X)
- ์๋: ๋ฒ์ ์ ๋ค๋ฅธ ๋ณํ ์ ์ฉ โ (O)
(๊ธฐ์กด ์์ง ์กฐ๊ฑด)
์ํ๋ ์กฐ๊ฑด:
๋ก ์ค์
- ๊ฒฐ๋ก :
- ๊ธฐํธ:
- : ์ ์ ๋ฒกํฐ
- : ๋ฒ์ ๋ฒกํฐ
8 - Lab - Lighting
Outline
- Flat / Smooth Shading์ ์ํ Vertex Normal ์ค์
- Phong ์กฐ๋ช
๊ณผ Gouraud Shading์ ์ด์ฉํ ํ๋ธ ๋ ๋๋ง
- Ambient ์ฑ๋ถ
- Diffuse ์ฑ๋ถ
- Specular ์ฑ๋ถ
- Phong ์กฐ๋ช ๊ณผ Phong Shading์ ์ด์ฉํ ํ๋ธ ๋ ๋๋ง
- Phong ์กฐ๋ช ๊ณผ Gouraud / Phong Shading์ ์ฌ์ฉํ โSmoothโ ํ๋ธ ๋ ๋๋ง
Setting Vertex Normal for Flat / Smooth Shading
(๋ณธ๋ฌธ ์์)
Example: a cube of length 2 again
ํ๋ธ์ ํ ๋ณ์ ๊ธธ์ด๊ฐ 2์ธ ์์
vertex index position
0 (-1, 1, 1)
1 ( 1, 1, 1)
2 ( 1, -1, 1)
3 (-1, -1, 1)
4 (-1, 1, -1)
5 ( 1, 1, -1)
6 ( 1, -1, -1)
7 (-1, -1, -1)
Flat Shading in OpenGL
๋ค๊ฐํ ์์ด๋ฉ ๋ฐฉ์์ ์ง์ ํ vertex normal ๋ฒกํฐ์ ๋ฐ๋ผ ๊ฒฐ์ ๋จ
Flat shading: ๊ฐ ์ ์ (vertex)์ ๋ํด ํด๋น ์ ์ ์ด ์ํ ๋ฉด(face)์ ๋ฒ์ (normal)์ ์ ์ ์ normal๋ก ์ค์
The normal at a vertex is the same as the face normal. Therefore, each vertex has as many normals as the number of faces it belongs to.
(์ ์ ์ normal์ face normal๊ณผ ๋์ผํ๋ฏ๋ก, ํ๋์ ์ ์ ์ ์์ ์ด ์ํ ๋ฉด์ ์๋งํผ์ normal์ ๊ฐ์ง)
Normals of the Cube for Flat Shading
๊ฐ ์ ์ ๋ง๋ค face์ ๋ฐ๋ผ normal์ด ๋ค๋ฅด๊ฒ ์ค์ ๋์ด ์์
vertex index position normal
0 (-1, 1, 1) (0,0,1)
0 (-1, 1, 1) (-1,0,0)
0 (-1, 1, 1) (0,1,0)
1 ( 1, 1, 1) (0,0,1)
1 ( 1, 1, 1) (1,0,0)
1 ( 1, 1, 1) (0,1,0)
2 ( 1, -1, 1) (0,0,1)
2 ( 1, -1, 1) (1,0,0)
2 ( 1, -1, 1) (0,-1,0)
3 (-1, -1, 1) (0,0,1)
3 (-1, -1, 1) (-1,0,0)
3 (-1, -1, 1) (0,-1,0)
4 (-1, 1, -1) (0,0,-1)
4 (-1, 1, -1) (-1,0,0)
4 (-1, 1, -1) (0,1,0)
5 ( 1, 1, -1) (0,0,-1)
5 ( 1, 1, -1) (1,0,0)
5 ( 1, 1, -1) (0,1,0)
6 ( 1, -1, -1) (0,0,-1)
6 ( 1, -1, -1) (1,0,0)
6 ( 1, -1, -1) (0,-1,0)
7 (-1, -1, -1) (0,0,-1)
7 (-1, -1, -1) (-1,0,0)
7 (-1, -1, -1) (0,-1,0)
Vertex Data
def prepare_vao_cube():
# 36๊ฐ์ ์ ์ : ์ด 12๊ฐ์ ์ผ๊ฐํ
vertices = glm.array(glm.float32,
# position normal
-1, -1, 1, 0, 0, 1, # v0
1, -1, 1, 0, 0, 1, # v1
1, 1, 1, 0, 0, 1, # v2
1, 1, 1, 0, 0, 1, # v2
-1, 1, 1, 0, 0, 1, # v3
-1, -1, 1, 0, 0, 1, # v0
...
)
Flat Shading in OpenGL
ํ์ง๋ง, ํ๋ OpenGL์์๋ "์ง์ ํ" flat shading (์ ํํ๋ '๋ค๊ฐํ ๋จ์ ์์ ๊ณ์ฐ')์ ์๊ฐ์ ์ผ๋ก ์ด์ํ ๊ฒฐ๊ณผ๋ฅผ ๋ณ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์
๋ค์๊ณผ ๊ฐ์ด flat ํ์ ์(qualifier)๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฅผ ๊ตฌํ ๊ฐ๋ฅํจ:
flat in vec4 vout_color; // fragment shader์์
์ด๋ ํ๋ OpenGL์์ ๋ชจ๋ polygon์ด ์ผ๊ฐํ์ผ๋ก ์ฒ๋ฆฌ๋๊ธฐ ๋๋ฌธ์
Flat Shading in OpenGL
Flat shading์ ์ํ ๋ชฉ์ ์ผ๋ก, ๋ณดํต์ "๊ฐ polygon๋ง๋ค ๋จ์ผ normal"์ด ์๋ ์๋ ๋ฐฉ์์ด ์ฌ์ฉ๋จ:
๊ฐ ์ ์ normal์ ํด๋น ์ ์ ์ด ์ํ face์ normal๋ก ์ค์
์์ ๊ณ์ฐ์ ์ ์ (vertex) ๋๋ ํ๋๊ทธ๋จผํธ(fragment) ๋จ์๋ก ์ํ
Normal์ด ๋์ผํ๋๋ผ๋ ์กฐ๋ช ๋ฒกํฐ๊ฐ ๊ฐ ์ ์ ์์ ์ฝ๊ฐ์ฉ ๋ค๋ฅด๋ฏ๋ก, ๊ณ์ฐ๋ ์์๋ ๋ฏธ๋ฌํ๊ฒ ๋ค๋ฅด๊ฒ ๋์ด
Smooth Shading in OpenGL
- Smooth shading: ์ ์ normal์ ํด๋น ์ ์ ์ด ํฌํจ๋ ๋ชจ๋ face normal์ ํ๊ท ์ผ๋ก ์ค์ ํจ
Only one vertex normal per vertex; average of face normals of the faces the vertex is part of
Normals of the Cube for Smooth Shading
vertex index position normal
0 (-1, 1, 1) (-0.57735026918963, 0.57735026918963, 0.57735026918963)
1 ( 1, 1, 1) ( 0.57735026918963, 0.57735026918963, 0.57735026918963)
2 ( 1, -1, 1) ( 0.57735026918963, -0.57735026918963, 0.57735026918963)
3 (-1, -1, 1) (-0.57735026918963, -0.57735026918963, 0.57735026918963)
4 (-1, 1, -1) (-0.57735026918963, 0.57735026918963, -0.57735026918963)
5 ( 1, 1, -1) ( 0.57735026918963, 0.57735026918963, -0.57735026918963)
6 ( 1, -1, -1) ( 0.57735026918963, -0.57735026918963, -0.57735026918963)
7 (-1, -1, -1) (-0.57735026918963, -0.57735026918963, -0.57735026918963)
Vertex and Index Data
# 8๊ฐ์ ์ ์
vertices = glm.array(glm.float32,
# position normal
1.0, 0.577f, 1.0, 0.577f, 0.577f, 0.577f, # v0
-1.0, 0.577f, 1.0, -0.577f, 0.577f, 0.577f, # v1
-1.0, -0.577f, 1.0, -0.577f, -0.577f, 0.577f, # v2
1.0, -0.577f, 1.0, 0.577f, -0.577f, 0.577f, # v3
1.0, 0.577f, -1.0, 0.577f, 0.577f, -0.577f, # v4
-1.0, 0.577f, -1.0, -0.577f, 0.577f, -0.577f, # v5
-1.0, -0.577f, -1.0, -0.577f, -0.577f, -0.577f, # v6
1.0, -0.577f, -1.0, 0.577f, -0.577f, -0.577f, # v7
)
# 12๊ฐ์ ์ผ๊ฐํ
indices = glm.array(glm.uint32,
0, 1, 2,
0, 2, 3,
4, 0, 3,
4, 3, 7,
5, 4, 7,
5, 7, 6,
1, 5, 6,
1, 6, 2,
4, 5, 1,
4, 1, 0,
2, 6, 7,
2, 7, 3,
)
How to Get Vertex Normals
vertex data array์ vertex normal์ ํ๋์ฝ๋ฉ
- ์์ ๋ณธ ์ฝ๋ ์์ ์ฒ๋ผ. ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋์ง๋ ์์
vertex position์ผ๋ก๋ถํฐ normal์ ๊ณ์ฐ
.obj ํ์ผ๊ณผ ๊ฐ์ ๋ชจ๋ธ ํ์ผ๋ก๋ถํฐ normal์ ์ฝ์ด์ค๊ธฐ
- ๊ฐ์ฅ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ๋ฐฉ์
Render a Cube using Phong Illumination and Gouraud Shading
- ์กฐ๋ช ์ฑ๋ถ๋ค์ ํ๋์ฉ ์ถ๊ฐํ์ฌ ๊ตฌํ
Light & Material Phong Illumination Components
๋ฌผ์ฒด์ ์ต์ข ์์์ ์กฐ๋ช ์๊ณผ ์ฌ์ง RGB ์์์ ์์๋ณ ๊ณฑ(element-wise multiplication) ์ผ๋ก ๊ณ์ฐ๋จ
๋ง์ฐฌ๊ฐ์ง๋ก, ๊ฐ Phong ์กฐ๋ช ์ฑ๋ถ(ambient, diffuse, specular)๋
์กฐ๋ช ์๊ณผ ์ฌ์ง ์์ ์์๋ณ ๊ณฑ์ผ๋ก ๊ณ์ฐ๋จ- ์:
diffuse_color = light_diffuse_color * material_diffuse_color
- ์:
Good Settings for Light & Material Phong Illumination Components
Light
- diffuse, specular: ๊ด์ ์์ฒด์ ์์
- ambient: ๊ฐ์ ์์์ด์ง๋ง ๊ฐ๋๋ ์ฝํ๊ฒ (์ฝ 10%)
Material
- diffuse, ambient: ๋ฌผ์ฒด์ ์์
- specular:
- ํฐ์ (๋น๊ธ์)
- ๋ฌผ์ฒด ์์ (๊ธ์)
Recall: Gouraud Shading
๊ฐ ์ ์ ๋ง๋ค ๋จ์ผ normal์ ์ฌ์ฉํจ
๊ฐ ์ ์ ์์ ์กฐ๋ช ๊ธฐ๋ฐ์ผ๋ก ์์์ ๊ณ์ฐํจ
โ ์กฐ๋ช ์ฐ์ฐ์ vertex shader์์ ์ํ๋ค๊ฐํ ๋ด๋ถ์ ์์์ ์ ์ ๊ฐ ๋ณด๊ฐ์ผ๋ก ์ฒ๋ฆฌ๋จ
- Barycentric ๋ณด๊ฐ ์ฌ์ฉ
[Code] 1-ambient-only-gouraud-facenorm
Vertex Shader
#version 330 core
layout (location = 0) in vec3 vin_pos;
layout (location = 1) in vec3 vin_normal;
out vec4 vout_color;
uniform mat4 MVP;
void main()
{
vec4 p3D_in_hcoord = vec4(vin_pos.xyz, 1.0);
gl_Position = MVP * p3D_in_hcoord;
// ๊ด์ ๋ฐ ์ฌ์ง ์์ฑ
vec3 light_ambient_color = vec3(1.1,1,1);
vec3 material_ambient_color = vec3(1,0,0);
// ์กฐ๋ช
์ฑ๋ถ ๊ณ์ฐ
vec3 light_ambient = 0.1 * light_ambient_color;
// ์ฌ์ง ์ฑ๋ถ ๊ฒฐํฉ
vec3 material_ambient = material_ambient_color;
vec3 ambient = light_ambient * material_ambient;
vec3 color = ambient;
vout_color = vec4(color, 1.0);
}
[Code] 1-ambient-only-gouraud-facenorm
Fragment shader
#version 330 core
in vec4 vout_color; // ๋ณด๊ฐ๋ ์์
out vec4 FragColor;
void main()
{
FragColor = vout_color;
}
[Code] 1-ambient-only-gouraud-facenorm
def prepare_vao_cube():
# 12๊ฐ์ ์ผ๊ฐํ์ ์ํ 36๊ฐ์ ์ ์
vertices = glm.array(glm.float32,
# position normal
-1, -1, 1, 0, 0, 1, # v0
1, -1, 1, 0, 0, 1, # v1
1, 1, 1, 0, 0, 1, # v2
1, 1, 1, 0, 0, 1, # v2
-1, 1, 1, 0, 0, 1, # v3
-1, -1, 1, 0, 0, 1, # v0
...
)
(page 5์ ๋์ผ)
[Code] 1-ambient-only-gouraud-facenorm
def main():
vao_cube = prepare_vao_cube()
while not glfwWindowShouldClose(window):
glUseProgram(shader_program)
# projection matrix
P = glm.perspective(glm.radians(45), 1, 1, 10)
# view matrix
view_pos = glm.vec3(3*np.sin(cam_ang), cam_height,
3*np.cos(cam_ang))
V = glm.lookAt(view_pos, glm.vec3(0,0,0), glm.vec3(0,1,0))
# model matrix (M)
M = glm.mat4()
# MVP ํ๋ ฌ ์ค์
MVP = P * V * M
glUniformMatrix4fv(loc_MVP, 1, GL_FALSE, glm.value_ptr(MVP))
glUniformMatrix4fv(loc_M, 1, GL_FALSE, glm.value_ptr(M))
draw_cube_with_current_MVP()
[Code] 2-ambient-diffuse-gouraud-shading
Vertex shader
#version 330 core
layout (location = 0) in vec3 vin_pos;
layout (location = 1) in vec3 vin_normal;
out vec4 vout_color;
uniform mat4 MVP;
uniform mat4 M;
void main()
{
vec4 p3D_in_hcoord = vec4(vin_pos.xyz, 1.0);
gl_Position = MVP * p3D_in_hcoord;
// ์กฐ๋ช
๋ฐ ์ฌ์ง ์์ฑ
vec3 light_pos = vec3(3,2,4);
vec3 light_color = vec3(1,1,1);
vec3 material_color = vec3(1,0,0);
// ambient ์ฑ๋ถ
vec3 light_ambient = 0.1 * light_color;
vec3 material_ambient = material_color;
vec3 ambient = light_ambient * material_ambient;
// normal ๊ณ์ฐ
vec3 normal = normalize(mat3(inverse(transpose(M))) * vin_normal);
vec3 pos = vec3(M * vec4(vin_pos,1)); // world space ์์น
vec3 light_dir = normalize(light_pos - pos);
float diff = max(dot(normal, light_dir), 0.0);
vec3 light_diffuse = light_color;
vec3 material_diffuse = material_color;
vec3 diffuse = diff * light_diffuse * material_diffuse;
vec3 color = ambient + diffuse;
vout_color = vec4(color, 1.0);
}
[Code] 2-ambient-diffuse-gouraud-shading
// ์กฐ๋ช
์ฑ๋ถ
vec3 light_ambient = 0.1 * light_color;
vec3 light_diffuse = light_color;
// ์ฌ์ง ์ฑ๋ถ
vec3 material_ambient = material_color;
vec3 material_diffuse = material_color;
// ambient
vec3 ambient = light_ambient * material_ambient;
// normal ๋ณํ
vec3 normal = normalize(mat3(inverse(transpose(M))) * vin_normal);
vec3 pos = vec3(M * vec4(vin_pos, 1.0));
vec3 light_dir = normalize(light_pos - pos);
// diffuse
float diff = max(dot(normal, light_dir), 0.0);
vec3 diffuse = diff * light_diffuse * material_diffuse;
vec3 color = ambient + diffuse;
vout_color = vec4(color, 1.0);
[Code] 2-ambient-diffuse-gouraud-shading
Normal ๋ฒกํฐ ๋ณํ ๋ฐฉ์:
vec3 normal = normalize(mat3(inverse(transpose(M))) * vin_normal);
์ ์ ๋ฆฌ:
- ์ฌ๊ธฐ์ ๋ ์์ ์์ ๊ฐ์ ๋ฐฉ์งํ๊ธฐ ์ํด ์ฌ์ฉ๋จ
์ค๋ฅธ์ชฝ ๋ํด:
- normal์ ๋ณํ ํ๋ ฌ X๋ฅผ ์ ์ฉ
- X๋ M^T ์ ์ญํ๋ ฌ: X = (M^T)^{-1}
[Code] 2-ambient-diffuse-gouraud-shading
vec3 surface_pos = vec3(M * vec4(vin_pos, 1)); // world space์์์ ํ๋ฉด ์์น
ํ๋ฉด์ ์์น๋ ์กฐ๋ช ์ด ์ ์ฉ๋๋ ์ง์ ์ ์๋ฏธ (world space ๊ธฐ์ค)
๋ชจ๋ ์กฐ๋ช ์ฐ์ฐ์ world space์์ ์ํ๋จ
โ ๋ชจ๋ ์์น์ ๋ฒกํฐ๋ world space๋ก ๋ณํ๋์ด์ผ ํจ๊ทธ๋ฌ๋ view space์์์ ์กฐ๋ช ๊ณ์ฐ์ด ๊ถ์ฅ๋จ
- ๊ณ ์ ์ ์ธ ๋ทฐ ๊ณต๊ฐ์ ํญ์ (0,0,0)์ ์์นํ ์นด๋ฉ๋ผ ๊ธฐ์ค
- ์ต๊ทผ ์์คํ
์์๋ world space๋ ์์ฃผ ์ฌ์ฉ๋จ
โ ์ดํ view space์์๋ ์กฐ๋ช ์ฐ์ฐ์ ์ํํ ์์
[Code] 3-all-components-gouraud-facenorm
Vertex shader
uniform mat4 MVP;
uniform mat4 M;
uniform vec3 view_pos;
void main()
{
vec4 p3D_in_hcoord = vec4(vin_pos.xyz, 1.0);
gl_Position = MVP * p3D_in_hcoord;
// ๊ด์ ๋ฐ ์ฌ์ง ์์ฑ
vec3 light_pos = vec3(3,2,4);
vec3 light_color = vec3(1,1,1);
vec3 material_color = vec3(1,0,0);
float material_shininess = 32.0;
// ์กฐ๋ช
์ฑ๋ถ
vec3 light_ambient = 0.1 * light_color;
vec3 light_diffuse = light_color;
vec3 light_specular = light_color;
[Code] 3-all-components-gouraud-facenorm
// ์ฌ์ง ์ฑ๋ถ
vec3 material_ambient = material_color;
vec3 material_diffuse = material_color; // ๋น๊ธ์ ์ฌ์ง
vec3 material_specular = vec3(1.0); // ํฐ์ ๋ฐ์ฌ๊ด
// ambient
vec3 ambient = light_ambient * material_ambient;
// diffuse ๋ฐ specular ๊ณ์ฐ์ฉ normal
vec3 normal = normalize(mat3(inverse(transpose(M))) * vin_normal);
vec3 surface_pos = vec3(M * vec4(vin_pos, 1.0));
vec3 light_dir = normalize(light_pos - surface_pos);
// diffuse
float diff = max(dot(normal, light_dir), 0.0);
vec3 diffuse = diff * light_diffuse * material_diffuse;
// specular
vec3 view_dir = normalize(view_pos - surface_pos);
vec3 reflect_dir = reflect(-light_dir, normal);
float spec = pow(max(dot(view_dir, reflect_dir), 0.0), material_shininess);
vec3 specular = spec * light_specular * material_specular;
vec3 color = ambient + diffuse + specular;
vout_color = vec4(color, 1.);
[Code] 3-all-components-gouraud-facenorm
vec3 reflect_dir = reflect(-light_dir, normal);
float spec = pow(max(dot(view_dir, reflect_dir), 0.0), material_shininess);
- ์ฌ๊ธฐ์
max()
๋ ์์ ์์์ ๋ฐฉ์งํ๊ธฐ ์ํด ์ฌ์ฉ๋จ
Render a Cube using Phong Illumination and Phong Shading
Recall: Phong Shading
- ๊ฐ ์ ์ ๋ง๋ค ๋จ์ผ vertex normal ์ฌ์ฉ
- ์ ์ normal์ ํด๋ฆฌ๊ณค ๋ด๋ถ์์ ๋ณด๊ฐ
- ํด๋ฆฌ๊ณค ๋ด ๊ฐ ํฝ์
๋ง๋ค ๋ณด๊ฐ๋ normal์ ์ด์ฉํด ์กฐ๋ช
๊ณ์ฐ
โ ์กฐ๋ช ๊ณ์ฐ์ fragment shader์์ ์ํ
[Code] 4-all-components-phong-facenorm
Vertex shader
#version 330 core
layout (location = 0) in vec3 vin_pos;
layout (location = 1) in vec3 vin_normal;
out vec3 vout_surface_pos;
out vec3 vout_normal;
uniform mat4 MVP;
uniform mat4 M;
void main()
{
vec4 p3D_in_hcoord = vec4(vin_pos.xyz, 1.0);
gl_Position = MVP * p3D_in_hcoord;
vout_surface_pos = vec3(M * vec4(vin_pos, 1));
vout_normal = normalize(mat3(inverse(transpose(M))) * vin_normal);
}
[Code] 4-all-components-phong-facenorm
Fragment shader
#version 330 core
in vec3 vout_surface_pos; // ๋ณด๊ฐ๋ ํ๋ฉด ์์น
in vec3 vout_normal; // ๋ณด๊ฐ๋ normal
out vec4 FragColor;
uniform vec3 view_pos;
void main()
{
// ์กฐ๋ช
๋ฐ ์ฌ์ง ์์ฑ
vec3 light_pos = vec3(3,2,4);
vec3 light_color = vec3(1,1,1);
vec3 material_color = vec3(1,0,0);
float material_shininess = 32.0;
// ์กฐ๋ช
์ฑ๋ถ
vec3 light_ambient = 0.1 * light_color;
vec3 light_diffuse = light_color;
vec3 light_specular = light_color;
// ์ฌ์ง ์ฑ๋ถ
vec3 material_ambient = material_color;
vec3 material_diffuse = material_color;
vec3 material_specular = vec3(1.0); // ๋๋ material_color
// ambient
vec3 ambient = light_ambient * material_ambient;
// diffuse ๋ฐ specular ๊ณ์ฐ
vec3 normal = normalize(vout_normal);
vec3 surface_pos = vout_surface_pos;
vec3 light_dir = normalize(light_pos - surface_pos);
// diffuse
float diff = max(dot(normal, light_dir), 0.0);
vec3 diffuse = diff * light_diffuse * material_diffuse;
// specular
vec3 view_dir = normalize(view_pos - surface_pos);
vec3 reflect_dir = reflect(-light_dir, normal);
float spec = pow(max(dot(view_dir, reflect_dir), 0.0), material_shininess);
vec3 specular = spec * light_specular * material_specular;
vec3 color = ambient + diffuse + specular;
FragColor = vec4(color, 1.);
}
Render a "Smooth" Cube using Phong Illumination and Gouraud / Phong Shading
[Code]
'5-all-components-gouraud-avgnorm'
:'3-all-components-gouraud-facenorm'
๊ณผ ๋์ผํ๋prepare_vao_cube()
ํธ์ถ๋ถ์glDrawElements()
ํธ์ถ๋ง ๋ค๋ฆ
'6-all-components-phong-avgnorm'
:'4-all-components-phong-facenorm'
๊ณผ ๋์ผํ๋prepare_vao_cube()
ํธ์ถ๋ถ์glDrawElements()
ํธ์ถ๋ง ๋ค๋ฆ