• Mindscape ๐Ÿ”ฅ
    • Playlist ๐ŸŽง
    • Vim ์‚ฌ์šฉ ๋งค๋‰ด์–ผ
  • ๐Ÿง  Algorithm

    • Python ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•œ ํŒ
    • 1966๋ฒˆ: ํ”„๋ฆฐํ„ฐ ํ
    • 1018๋ฒˆ: ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ
  • ๐Ÿ’ฐ Finance

    • ๋น„ํŠธ์ฝ”์ธ(Bitcoin)
  • ๐Ÿ›๏ธ Humanities

    • Nordvik, Russia
    • North Sentinel Island
    • ๋กฑ๊ณ ๋กฑ๊ณ (Rongorongo)
  • ๐Ÿ‹๏ธ Wellness

    • ๐Ÿซ’ ์—‘์ŠคํŠธ๋ผ ๋ฒ„์ง„ ์˜ฌ๋ฆฌ๋ธŒ์œ  (Extra Virgin Olive Oil)
    • ์ฐจ์ „์žํ”ผ(Psyllium Husk)
  • ๐Ÿ–ฅ๏ธ Computer Graphics

    • 8 - Lighting
    • 9 - Orientation & Rotation
    • 10 - Character Animation
    • 11 - Curves
    • 12 - More Lighting, Texture
  • ๐Ÿ—‚๏ธ Operating System

    • 7. Deadlocks
    • 8. Memory Management(1)
    • 9. Memory Management(2)
    • 10. Virtual Memory(1)
    • 11. Virtual Memory(2)
    • 12. File System
    • 13. Mass Storage Management
    • 14. I/O Systems
  • ๐Ÿ”ฃ Programming Language Theory

    • 13. FPL(1)

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์— ๋น„๋ก€ํ•จ

Ireflected=Iincidentcosโกฮธ=Iincidentโ‹…(N^โ‹…L^)I_{\text{reflected}} = I_{\text{incident}} \cos{\theta} \\ = I_{\text{incident}} \cdot (\hat{\mathbf{N}} \cdot \hat{\mathbf{L}}) Ireflectedโ€‹=Iincidentโ€‹cosฮธ=Iincidentโ€‹โ‹…(N^โ‹…L^)

  • IreflectedI_\text{reflected}Ireflectedโ€‹: ๋ฐ˜์‚ฌ๊ด‘์˜ ์„ธ๊ธฐ
  • IincidentI_\text{incident}Iincidentโ€‹: ์ž…์‚ฌ๊ด‘์˜ ์„ธ๊ธฐ
  • N^\hat{\mathbf{N}}N^: ์ž…์‚ฌ ์ง€์ ์—์„œ์˜ ํ‘œ๋ฉด ๋ฒ•์„  ๋ฒกํ„ฐ
  • L^\hat{\mathbf{L}}L^: ์ •๊ทœํ™”๋œ ์ž…์‚ฌ๊ด‘ ๋ฒกํ„ฐ
  • Lambert ๋ฒ•์น™์˜ 2D ์‹œ๊ฐํ™”

์ด ์ด๋ฏธ์ง€๋Š” Brown ๋Œ€ํ•™ Andy van Dam ๊ต์ˆ˜ ๊ฐ•์˜์ž๋ฃŒ์—์„œ ์ธ์šฉ๋จ
http://cs.brown.edu/courses/cs1230/lectures.shtml

์™„์ „ ์ •๋ฐ˜์‚ฌ

  • ๋งค๋„๋Ÿฝ๊ณ  ํ‰ํƒ„ํ•œ ํ‘œ๋ฉด์—์„œ ๊ฑฐ์šธ๊ณผ ๊ฐ™์€ ๋ฐ˜์‚ฌ ๋ฐœ์ƒ
    โ†’ ๊ฑฐ์šธ์ƒ์˜ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
  • ์‹œ์ ์— ์˜์กด์ ์ธ(view-dependent) ํŠน์„ฑ

์™„์ „ ์ •๋ฐ˜์‚ฌ - ๋ฐ˜์‚ฌ ๋ฒ•์น™

  • N^\hat{\mathbf{N}}N^, L^\hat{\mathbf{L}}L^, R^\hat{\mathbf{R}}R^์€ ๋™์ผํ•œ ํ‰๋ฉด์ƒ์— ์กด์žฌ

  • ์ž…์‚ฌ๊ฐ ฮธi=ฮธr\theta_i = \theta_rฮธiโ€‹=ฮธrโ€‹

  • L^\hat{\mathbf{L}}L^๊ณผ R^\hat{\mathbf{R}}R^์€ N^\hat{\mathbf{N}}N^์˜ ์–‘์ชฝ์— ์œ„์น˜

  • ๊ธฐํ˜ธ ์„ค๋ช…:

    • N^\hat{\mathbf{N}}N^: ์ž…์‚ฌ ์ง€์ ์—์„œ์˜ ํ‘œ๋ฉด ๋ฒ•์„ 
    • L^\hat{\mathbf{L}}L^: ์ •๊ทœํ™”๋œ ์ž…์‚ฌ๊ด‘ ๋ฒกํ„ฐ
    • R^\hat{\mathbf{R}}R^: ์ •๊ทœํ™”๋œ ๋ฐ˜์‚ฌ๊ด‘ ๋ฒกํ„ฐ

๋ถˆ์™„์ „ ์ •๋ฐ˜์‚ฌ (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 + Diffuse + Specular = Phong Reflection

  • ์ด์ œ๋ถ€ํ„ฐ ๊ฐ ์ง€์ ์—์„œ Phong ์กฐ๋ช… ๋ชจ๋ธ์˜ ๊ฐ ๊ตฌ์„ฑ ์š”์†Œ(ambient, diffuse, specular)์˜ ์ƒ‰์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ด„
  • ์ด ์ง€์ ์€ ๋‹ค์Œ ์ค‘ ํ•˜๋‚˜์ผ ์ˆ˜ ์žˆ์Œ:
    • ๋‹ค๊ฐํ˜•์˜ ์ •์ 
    • ๋‹ค๊ฐํ˜• ๋‚ด๋ถ€์˜ ์ž„์˜ ์ง€์  (ํ•„๋ฆ„ ๊ณต๊ฐ„์˜ ํ”ฝ์…€์— ํ•ด๋‹น)

Ambient ์„ฑ๋ถ„

Ia=laโ‹…maI_a = l_a \cdot m_a Iaโ€‹=laโ€‹โ‹…maโ€‹

  • lal_alaโ€‹: ๊ด‘์›์˜ ambient ์ƒ‰
  • mam_amaโ€‹: ํ‘œ๋ฉด ์ง€์ ์˜ ์žฌ์งˆ ambient ์ƒ‰
  • IaI_aIaโ€‹: ์ตœ์ข… ambient ์ƒ‰ ๊ฒฐ๊ณผ
  • โ‹…\cdotโ‹…: ์„ฑ๋ถ„๋ณ„ ๊ณฑ์…ˆ (element-wise multiplication)

๊ฒฐ๊ณผ

I=laโ‹…maI = l_a \cdot m_a I=laโ€‹โ‹…maโ€‹

์ด ์ด๋ฏธ์ง€๋Š” ํ…์‚ฌ์Šค A&M ๋Œ€ํ•™๊ต์˜ Huamin Qu ๊ต์ˆ˜ ์Šฌ๋ผ์ด๋“œ์—์„œ ์ธ์šฉ๋จ
http://faculty.cs.tamu.edu/schaefer/CSCE441/notes.html

Diffuse ์„ฑ๋ถ„

Id=ldโ‹…mdโ‹…cosโกฮธ=ldโ‹…mdโ‹…(Lโ‹…N)I_d = l_d \cdot m_d \cdot \cos{\theta} = l_d \cdot m_d \cdot (\mathbf{L} \cdot \mathbf{N}) Idโ€‹=ldโ€‹โ‹…mdโ€‹โ‹…cosฮธ=ldโ€‹โ‹…mdโ€‹โ‹…(Lโ‹…N)

  • L\mathbf{L}L: ๊ด‘์› ๋ฐฉํ–ฅ ๋ฒกํ„ฐ

  • N\mathbf{N}N: ํ‘œ๋ฉด ๋ฒ•์„  ๋ฒกํ„ฐ

    • L\mathbf{L}L๊ณผ N\mathbf{N}N์€ ๋‹จ์œ„ ๋ฒกํ„ฐ (unit vector)
  • โ‹…\cdotโ‹…: ๋‚ด์  (dot product)

  • ldl_dldโ€‹: ๊ด‘์›์˜ diffuse ์ƒ‰์ƒ

  • mdm_dmdโ€‹: ์žฌ์งˆ์˜ diffuse ์ƒ‰์ƒ

  • IdI_dIdโ€‹: ํ‘œ๋ฉด ์ง€์ ์—์„œ์˜ diffuse ๊ฒฐ๊ณผ ์ƒ‰์ƒ

๊ฒฐ๊ณผ

I=laโ‹…maI = l_a \cdot m_a I=laโ€‹โ‹…maโ€‹

(๊ทธ๋ฆผ: ambient ์„ฑ๋ถ„๋งŒ ์ ์šฉ๋œ ๋ Œ๋”๋ง ๊ฒฐ๊ณผ)

I=laโ‹…ma+ldโ‹…mdโ‹…(Lโ‹…N)I = l_a \cdot m_a + l_d \cdot m_d \cdot (\mathbf{L} \cdot \mathbf{N}) I=laโ€‹โ‹…maโ€‹+ldโ€‹โ‹…mdโ€‹โ‹…(Lโ‹…N)

(๊ทธ๋ฆผ: ambient + diffuse ์„ฑ๋ถ„ ์ ์šฉ)

Specular ์„ฑ๋ถ„

Is=lsโ‹…msโ‹…cosโกnฮฑ=lsโ‹…msโ‹…(Vโ‹…R)nI_s = l_s \cdot m_s \cdot \cos^n{\alpha} = l_s \cdot m_s \cdot (\mathbf{V} \cdot \mathbf{R})^n Isโ€‹=lsโ€‹โ‹…msโ€‹โ‹…cosnฮฑ=lsโ€‹โ‹…msโ€‹โ‹…(Vโ‹…R)n

  • V\mathbf{V}V: ์‹œ์  (view) ๋ฐฉํ–ฅ ๋ฒกํ„ฐ

  • R\mathbf{R}R: ๋ฐ˜์‚ฌ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ

    • V\mathbf{V}V์™€ R\mathbf{R}R์€ ๋‹จ์œ„ ๋ฒกํ„ฐ
  • nnn: ๊ด‘ํƒ ๊ณ„์ˆ˜ (shininess coefficient)

  • lsl_slsโ€‹: ๊ด‘์›์˜ specular ์ƒ‰์ƒ

  • msm_smsโ€‹: ์žฌ์งˆ์˜ specular ์ƒ‰์ƒ

  • IsI_sIsโ€‹: ํ‘œ๋ฉด ์ง€์ ์—์„œ์˜ specular ๊ฒฐ๊ณผ ์ƒ‰์ƒ

๊ฒฐ๊ณผ

I=laโ‹…maI = l_a \cdot m_a I=laโ€‹โ‹…maโ€‹

(๊ทธ๋ฆผ: ambient ์„ฑ๋ถ„๋งŒ ์ ์šฉ๋œ ๋ Œ๋”๋ง)

I=laโ‹…ma+ldโ‹…mdโ‹…(Lโ‹…N)I = l_a \cdot m_a + l_d \cdot m_d \cdot (\mathbf{L} \cdot \mathbf{N}) I=laโ€‹โ‹…maโ€‹+ldโ€‹โ‹…mdโ€‹โ‹…(Lโ‹…N)

(๊ทธ๋ฆผ: ambient + diffuse ์„ฑ๋ถ„ ์ ์šฉ)

I=laโ‹…ma+ldโ‹…mdโ‹…(Lโ‹…N)+lsโ‹…msโ‹…(Vโ‹…R)nI = l_a \cdot m_a + l_d \cdot m_d \cdot (\mathbf{L} \cdot \mathbf{N}) + l_s \cdot m_s \cdot (\mathbf{V} \cdot \mathbf{R})^n I=laโ€‹โ‹…maโ€‹+ldโ€‹โ‹…mdโ€‹โ‹…(Lโ‹…N)+lsโ€‹โ‹…msโ€‹โ‹…(Vโ‹…R)n

  • n=5n = 5n=5

(๊ทธ๋ฆผ: ambient + diffuse + specular, n=5n=5n=5)

I=laโ‹…ma+ldโ‹…mdโ‹…(Lโ‹…N)+lsโ‹…msโ‹…(Vโ‹…R)nI = l_a \cdot m_a + l_d \cdot m_d \cdot (\mathbf{L} \cdot \mathbf{N}) + l_s \cdot m_s \cdot (\mathbf{V} \cdot \mathbf{R})^n I=laโ€‹โ‹…maโ€‹+ldโ€‹โ‹…mdโ€‹โ‹…(Lโ‹…N)+lsโ€‹โ‹…msโ€‹โ‹…(Vโ‹…R)n

  • n=50n = 50n=50

(๊ทธ๋ฆผ: ambient + diffuse + specular, n=50n=50n=50)

I=laโ‹…ma+ldโ‹…mdโ‹…(Lโ‹…N)+lsโ‹…msโ‹…(Vโ‹…R)nI = l_a \cdot m_a + l_d \cdot m_d \cdot (\mathbf{L} \cdot \mathbf{N}) + l_s \cdot m_s \cdot (\mathbf{V} \cdot \mathbf{R})^n I=laโ€‹โ‹…maโ€‹+ldโ€‹โ‹…mdโ€‹โ‹…(Lโ‹…N)+lsโ€‹โ‹…msโ€‹โ‹…(Vโ‹…R)n

  • n=500n = 500n=500

(๊ทธ๋ฆผ: ๊ด‘ํƒ ๊ณ„์ˆ˜ 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์„ ์–ป๋Š” ๋ฐฉ๋ฒ•
  • ์ •์ ์˜ ์ˆœ์„œ๋Š” ์ค‘์š”ํ•จ!
  • ์‚ผ๊ฐํ˜• <p1,ย p2,ย p3>\mathbf{<p_1,~p_2,~p_3>}<p1โ€‹,ย p2โ€‹,ย p3โ€‹>์˜ ๋ฒ•์„ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐ๋จ:
    • v1ร—v2\mathbf{v_1 \times v_2}v1โ€‹ร—v2โ€‹
      • v1\mathbf{v_1}v1โ€‹: p1\mathbf{p_1}p1โ€‹์—์„œ p2\mathbf{p_2}p2โ€‹๋กœ ํ–ฅํ•˜๋Š” ๋ฒกํ„ฐ
      • v2\mathbf{v_2}v2โ€‹: p1\mathbf{p_1}p1โ€‹์—์„œ p3\mathbf{p_3}p3โ€‹๋กœ ํ–ฅํ•˜๋Š” ๋ฒกํ„ฐ
  • ๊ทธ๋ž˜์„œ ์ •์ ์˜ ์‹œ๊ณ„ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ(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์— ์˜ํ•ด ๋ณ€ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ
  • ๊ทธ๋Ÿฌ๋‚˜ ๋ฒ•์„  ๋ฒกํ„ฐ(normal)๋Š” M์— ์˜ํ•ด ๋ณ€ํ™˜๋˜์–ด์„œ๋Š” ์•ˆ ๋จ
    • ๋ฒ•์„ ์€ ์ ‘์„ ์— ์ˆ˜์ง์ด์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ
  • ์œ„: ๋„ํ˜•์— M ์ ์šฉ โ†’ OK
  • ์ค‘๊ฐ„: ๋ฒ•์„ ์— M ์ ์šฉ โ†’ (X)
  • ์•„๋ž˜: ๋ฒ•์„ ์— ๋‹ค๋ฅธ ๋ณ€ํ™˜ ์ ์šฉ โ†’ (O)

tโ‹…n=tTn=0\mathbf{t} \cdot \mathbf{n} = \mathbf{t}^\mathrm{T} \mathbf{n} = 0tโ‹…n=tTn=0 (๊ธฐ์กด ์ˆ˜์ง ์กฐ๊ฑด)
์›ํ•˜๋Š” ์กฐ๊ฑด: Mtโ‹…Xn=tTMTXn=0M \mathbf{t} \cdot X \mathbf{n} = \mathbf{t}^\mathrm{T} M^\mathrm{T} X \mathbf{n} = 0Mtโ‹…Xn=tTMTXn=0
โ‡’X=(MT)โˆ’1\Rightarrow X = (M^\mathrm{T})^{-1}โ‡’X=(MT)โˆ’1 ๋กœ ์„ค์ •
โ‡’Mtโ‹…Xn=tTMT(MT)โˆ’1n=tTn=0\Rightarrow M \mathbf{t} \cdot X \mathbf{n} = \mathbf{t}^\mathrm{T} M^\mathrm{T} (M^\mathrm{T})^{-1} \mathbf{n} = \mathbf{t}^\mathrm{T} \mathbf{n} = 0โ‡’Mtโ‹…Xn=tTMT(MT)โˆ’1n=tTn=0

  • ๊ฒฐ๋ก :

X=(MT)โˆ’1X = (M^\mathrm{T})^{-1} X=(MT)โˆ’1

  • ๊ธฐํ˜ธ:
    • t\mathbf{t}t: ์ ‘์„  ๋ฒกํ„ฐ
    • n\mathbf{n}n: ๋ฒ•์„  ๋ฒกํ„ฐ

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);

์‹ ์ •๋ฆฌ:

Id=ldโˆ˜mdโ‹…maxโก(0,Lโ‹…N)I_d = \mathbf{l_d} \circ \mathbf{m_d} \cdot \max(0, \mathbf{L} \cdot \mathbf{N}) Idโ€‹=ldโ€‹โˆ˜mdโ€‹โ‹…max(0,Lโ‹…N)

  • ์—ฌ๊ธฐ์„œ maxโก()\max()max()๋Š” ์Œ์ˆ˜ ์ƒ‰์ƒ ๊ฐ’์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ

์˜ค๋ฅธ์ชฝ ๋„ํ•ด:

  • normal์— ๋ณ€ํ™˜ ํ–‰๋ ฌ X๋ฅผ ์ ์šฉ
  • X๋Š” begin:math:textbegin:math:textbegin:math:text M^T end:math:textend:math:textend:math:text ์˜ ์—ญํ–‰๋ ฌ: begin:math:textbegin:math:textbegin:math:text X = (M^T)^{-1} end:math:textend:math:textend:math:text

[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);

Is=lsโˆ˜msโ‹…cosโกฮฑฮธ=lsโˆ˜msโ‹…(Vโ‹…R)ฮฑI_s = \mathbf{l_s} \circ \mathbf{m_s} \cdot \cos^\alpha \theta = \mathbf{l_s} \circ \mathbf{m_s} \cdot (\mathbf{V} \cdot \mathbf{R})^\alpha Isโ€‹=lsโ€‹โˆ˜msโ€‹โ‹…cosฮฑฮธ=lsโ€‹โˆ˜msโ€‹โ‹…(Vโ‹…R)ฮฑ

  • ์—ฌ๊ธฐ์„œ 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() ํ˜ธ์ถœ๋งŒ ๋‹ค๋ฆ„
์ตœ๊ทผ ์ˆ˜์ •:: 25. 6. 19. ์˜คํ›„ 8:26
Contributors: kmbzn
Next
9 - Orientation & Rotation

BUILT WITH

CloudflareNode.jsGitHubGitVue.jsJavaScriptVSCodenpm

All trademarks and logos are property of their respective owners.
ยฉ 2025 kmbzn ยท MIT License