Lesson 4 – Math / Lighting PV227 – GPU Rendering Jiˇrí Chmelík, Jan ˇCejka Fakulta informatiky Masarykovy univerzity 6. 10. 2015 PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 1 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 2 / 37 Points and Vectors points (before projection) are quadruples: (x, y, z, 1.0), can be transformed with a 4 × 4 matrix, vectors are also quadruples: (x, y, z, 0.0), can be transformed with a 4 × 4 or 3 × 3 matrix. PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 3 / 37 Transformation points are transformed to eye space with modelview matrix, vectors constructed from points (e.g. P2 − P1) are also transformed with this matrix, normals are not! PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 4 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 5 / 37 Normal Transformation Error Figure: Taken from lighthouse3d.com PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 6 / 37 Normal Transformation Solution caused by non-uniform scale, M is the modelview matrix, t is tangent vector (P2 − P1) and I is identity, we need another matrix (N) for transforming normal n. (M × t) • (N × n) = 0 (M × t)T × (N × n) = 0 tT × MT × N × n = 0 PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 7 / 37 Normal Transformation Solution (cont.) tT × MT × N × n = 0 t • n = 0 ⇒ tT × n = 0 ⇒ MT × N = I MT × N = I (MT )−1 × MT × N = (MT )−1 N = (MT )−1 PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 8 / 37 Normal Transformation Result N is inverse transpose of M (3 × 3 submatrix of M), for orthogonal matrices: AT = A−1 (rotation is orthogonal), if M is orthogonal, then: M = (MT )−1 ⇒ N = M. PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 9 / 37 Renormalization normals must be of unit length, can be destroyed by normal transformation → must be normalized in vertex shader, after the transformation, interpolation can also destroy vector length → must be normalized in fragment shader, before we do anything with it. Figure: Taken from lighthouse3d.com PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 10 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 11 / 37 Lighting Computation of light’s interaction with surfaces, in most cases – huge cheat; major simplification otherwise. Light: light components: ambient, diffuse and specular, shading types: flat, gouraud and phong, light-source types: directional, point and spot light, shadows... no shadow, no bouncing of light. PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 12 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 13 / 37 Ambient Lighting approximates lighting after infinite number of bounces, homogeneous, prevents black areas that look unnatural, usually chosen as fraction of the diffuse (material) color, I = Ka. Figure: Ambient spheres PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 14 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 15 / 37 Light-source type: Directional Light Figure: Taken from lighthouse3d.com far away light, defined by a direction vector (position is irelevant), can represent e.g. the sun. PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 16 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 17 / 37 Shading types: Gouraud Shading per vertex shading, interpolation of vertex colors, unable to capture lighting details inside polygons. PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 18 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 19 / 37 Light Components: Diffuse simulate light’s interaction with perfectly diffuse material, light angle dependent, significant color component, I = cos(α) · Kd . Figure: Diffuse spheres PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 20 / 37 Light Components: Diffuse (cont.) Figure: Taken from lighthouse3d.com amount of incoming light diminishes with increasing angle, cos(α) = L•N |L|·|N| , normalized vectors: I = (L • N) · Kd , all vectors must be in same space (usually defined in world space, computation in camera space). PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 21 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 22 / 37 Flat Shading per primitive shading, no interpolation, unable to capture smooth changes in light intensity. Figure: Flat shading PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 23 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 24 / 37 Light Components: Ambient plus Diffuse light from various sources can be combined (added), combination of ambient and diffuse prevents black areas, I = Ka + cos(α) · Kd , value should not be outside the [0.0, 1.0] range. Figure: Ambient + Diffuse spheres PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 25 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 26 / 37 Light Components: Specular simulate light’s interaction with reflective material, view angle dependent, highlight of the light’s color, not material color, I = cos(β)s · Ks, s controls size of the highlight. Figure: Specular spheres (Phong vs Blinn-Phong) PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 27 / 37 Phong Lighting Figure: Taken from lighthouse3d.com amount of reflected light diminishes with increasing angle, R = −L + 2 · (N • L) · N, cos(β) = R • Eye, all vectors must be in same space, normalized. PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 28 / 37 Blinn-Phong Lighting Figure: Taken from lighthouse3d.com amount of reflected light diminishes with increasing angle, H = L + Eye, cos(β) = H • N, all vectors must be in same space, normalized. PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 29 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 30 / 37 Basic Lighting ambient, diffuse and specular form the baseline lighting, I = Ka + cos(α) · Kd + cos(β)s · Ks, light from various sources can be combined (added), value should not be outside the [0.0, 1.0] range. Figure: Ambient + Diffuse + Specular pawns PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 31 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 32 / 37 Phong Shading per pixel shading, smooth lighting including details, interpolation of vertex attributes (normal, eye, light). Figure: Per pixel lighting pawns PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 33 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 34 / 37 Light-source type: Point Light light source inside the scene, defined by a position vector (all directions), can represent e.g. a lightbulb. Figure: Taken from lighthouse3d.com Point light pawns. PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 35 / 37 Outline 1 Basics – Repetition 2 Issues with Normals 3 Lighting Light Components: Ambient Light-source type: Directional Light Shading types: Gouraud Shading Light Components: Diffuse Shading types: Flat Shading Light Components: Ambient plus Diffuse Light Components: Specular Light Components: Ambient plus Diffuse plus Specular Shading types: Phong Shading Light-source type: Point Light Light-source type: Spot Light PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 36 / 37 Light-source type: Spot Light light source inside the scene, only a directed cone is illuminated, defined by a position vector, direction vector and angle, can represent e.g. a flashlight. Figure: Spot light pawns PV227 – GPU Rendering (FI MUNI) Lesson 4 – Math / Lighting 6. 10. 2015 37 / 37