1 of 22

���Chapter IX

Lighting

All class materials including this PowerPoint file are available at

https://github.com/medialab-ku/openGLESbook

Introduction to Computer Graphics with OpenGL ES (J. Han)

2 of 22

Phong Lighting Model

9-2

  • Lighting or illumination refers to the technique that handles the interaction between lights and objects. It plays the key role for achieving photorealism, and many efforts have been made for it.
  • Lighting or illumination models are divided into two categories.
    • Local illumination
    • Global illumination
  • This chapter presents the old representative of local illumination models, named Phong lighting model whereas Chapter 16 presents a global illumination model, named ray tracing.
  • The Phong model is composed of four terms:
    • diffuse
    • specular
    • ambient
    • emissive

Introduction to Computer Graphics with OpenGL ES (J. Han)

3 of 22

  • There are various light types. Let’s take the simplest, the directional light source, such as the sun, which is quite strong and located far from the scene, making all objects in the scene receive the sunlight along a universal direction.

9-3

Phong Lighting Model - Diffuse Term

point light source

directional light source

Introduction to Computer Graphics with OpenGL ES (J. Han)

4 of 22

  • The diffuse term is based on Lambert’s law. Reflections from ideally diffuse surfaces (Lambertian surfaces) are scattered with equal intensity in all directions.
  • So, the amount of perceived reflection is independent of the view direction and is proportional to the amount of incoming light.
  • The light vector, l, is made to point to the light source for computational efficiency. Consider the angle, θ, between l and the surface normal, n. As θ becomes smaller, p receives more light. It is described as cosθ, which is simply n·l if both n and l are unit vectors.

9-4

Phong Lighting Model - Diffuse Term

Introduction to Computer Graphics with OpenGL ES (J. Han)

5 of 22

9-5

  • So far, intensity. Now, color.
  • Suppose a white light (1,1,1). If an object lit by the light appears red, it means that the object reflects only R but absorbs G and B. We can easily implement this mechanism through material parameter, i.e., if it is (1,0,0), then (1,1,1)⊗(1,0,0)=(1,0,0) where ⊗ is component-wise multiplication.

  • The diffuse term:

  • In general, the texture provides md.

R

G

B

intensity 1

1

1

1=1

0=0

0=0

(1,0,0)

Phong Lighting Model - Diffuse Term (cont’d)

R

G

B

intensity 1

0.5

1

0.5=0.5

0.5×1=0.5

0=0

(0.5,1,0)

md = (1,0,0)

md from the texture

(0.5,1,0)

(0.5,0.5,0)

(1,0.5,1)

Introduction to Computer Graphics with OpenGL ES (J. Han)

6 of 22

  • The specular term is used to make a surface look shiny via highlights, and it requires reflection vector (r) and view vector (v).

  • Computing the reflection vector

9-6

Phong Lighting Model - Specular Term

Introduction to Computer Graphics with OpenGL ES (J. Han)

7 of 22

  • Whereas the diffuse term is view-independent, the specular term is highly view-dependent.
    • For a perfectly shiny surface, the highlight at p is visible only when ρ = 0.
    • For a surface that is not perfectly shiny, the maximum highlight occurs when ρ = 0 but falls off sharply as ρ increases.
    • The rapid fall-off of highlights is often approximated by cosρ sh, where sh denotes shininess.
    • cosρ = rv.

  • The specular term:
  • Unlike md, ms is usually a gray-scale value

rather than an RGB color. It enables the

highlight on the surface to end up being

the color of the light source.

9-7

cos2ρ

cos64ρ

cosρ

Phong Lighting Model - Specular Term (cont’d)

ss = (1,1,1) &

ms = (0.95,0.95,0.95)

Introduction to Computer Graphics with OpenGL ES (J. Han)

8 of 22

  • The ambient light describes the light reflected from the various objects in the scene, i.e., it accounts for indirect lighting.
  • As the ambient light has bounced around so much in the scene, it arrives at a surface point from all directions, and reflections from the surface point are also scattered with equal intensity in all directions.

  • The last term of the Phong model is the emissive term me that describes the amount of light emitted by a surface itself.

9-8

Phong Lighting Model – Ambient and Emissive Terms

Introduction to Computer Graphics with OpenGL ES (J. Han)

9 of 22

9-9

Phong Lighting Model

  • The Phong model sums the four terms!

Introduction to Computer Graphics with OpenGL ES (J. Han)

10 of 22

9-10

Vertex Shader and Rasterizer (revisited)

  • Our first vertex shader (given in Chapter 6)

    • The per-vertex outputs returned by the vertex shader are interpolated to determine the per-fragment inputs. The fragment shader runs once for each fragment.

Introduction to Computer Graphics with OpenGL ES (J. Han)

11 of 22

Vertex and Fragment Shaders (revisited)

  • Our first fragment shader (given in Chapter 8)

  • What were provided for our first fragment shader?
    • The per-fragment inputs, v_normal and v_texCoord, were available, but v_normal was not used.
    • The only uniform was a texture.

  • Now, you will see more uniforms and how v_normal is used.

9-11

Introduction to Computer Graphics with OpenGL ES (J. Han)

12 of 22

9-12

Per-fragment Lighting

  • The fragment shader computes lighting using l, n, r and v “in the world space.”
  • Consider two points on an object's surface, each of which is assumed to make up a distinct fragment.
    • Since we assume a directional light, l is constant for all surface points. It is provided for the fragment shader as a uniform.
    • In contrast, n, r and v vary across the object's surface. Our strategy is to give a distinct pair of n and v to each execution of the fragment shader so that it computes r, which equals 2n(n·l)-l.

  • Let’s see n first and then v.

Introduction to Computer Graphics with OpenGL ES (J. Han)

13 of 22

9-13

Per-fragment Lighting (cont’d)

  • Again, r = 2n(n·l)-l. Because l is a world-space vector, n should also be defined in the world space. This is why the vertex shader world-transforms the object-space normal of each vertex and passes the world-space normal, v_normal, to the rasterizer.
  • Consider the vertices, p1 and p2, of a triangle. Their world-space normals, n1 and n2, correspond to v_normal.
  • The rasterizer interpolates the vertex normals to provide n for each fragment. Suppose two fragments, a and b, are generated on the edge connecting p1 and p2. The rasterizer interpolates n1 and n2 to assign na to a and nb to b.

Introduction to Computer Graphics with OpenGL ES (J. Han)

14 of 22

Scan Conversion (revisited)

9-14

 

 

 

 

 

a scan line 🡪

Introduction to Computer Graphics with OpenGL ES (J. Han)

15 of 22

9-15

Per-fragment Lighting (cont’d)

  • Let’s now consider v.
  • For the fragments, a and b, we also need “world-space view vectors” denoted by va and vb, respectively. For this, the vertex shader should transform the object-space position of each vertex to the world space and connect it to the camera position, EYE, which is defined in the world space.
  • The per-vertex world-space view vectors are v1 and v2. They are passed to the rasterizer and interpolated to produce va and vb.

  • Our first vertex shader will be extended to compute the world-space view vector.

Introduction to Computer Graphics with OpenGL ES (J. Han)

16 of 22

  • The vertex shader.

9-16

Per-fragment Lighting (cont’d)

Introduction to Computer Graphics with OpenGL ES (J. Han)

17 of 22

  • Given n and v provided by the rasterizer and l provided as a uniform by the GL program, the fragment shader first computes the reflection vector, i.e., r = 2n(n·l)-l, and finally implements the Phong model.

9-17

Per-fragment Lighting (cont’d)

Introduction to Computer Graphics with OpenGL ES (J. Han)

18 of 22

  • The fragment shader.

9-18

Per-fragment Lighting (cont’d)

Introduction to Computer Graphics with OpenGL ES (J. Han)

19 of 22

  • The fragment shader.

9-19

Per-fragment Lighting (cont’d)

Introduction to Computer Graphics with OpenGL ES (J. Han)

20 of 22

  • The fragment shader processes the world-space vectors, l, n, r and v, for determining the color of the screen-space fragment. Don’t get confused. It’s possible and is not unnatural.
  • It is worth paying attention to normalize(v_normal) in the fragment shader. Even though v_normal output by the vertex shader is a unit vector, the fragment shader’s input, v_normal, is not necessarily so because it is generated by interpolating the vertex normals across the triangle. Because every normal used for lighting is assumed to be a unit vector, v_normal should be normalized by invoking the built-in function normalize. By the same token, another input variable, v_view, is also normalized.

9-20

Per-fragment Lighting (cont’d)

Introduction to Computer Graphics with OpenGL ES (J. Han)

21 of 22

Texturing and Lighting (revisited)

9-21

texturing only

texturing +

lighting (CH9)

Introduction to Computer Graphics with OpenGL ES (J. Han)

22 of 22

  • An alternative of per-fragment lighting is per-vertex lighting.
    • Lighting is taken by the vertex shader so that it returns a color per vertex.
    • The vertex colors are interpolated by the rasterizer.
    • The fragment shader takes the fragment color.
  • Per-vertex lighting saves a lot of computing cost but reveals serious problems.

9-22

Why Per-fragment Lighting?

per-vertex lighting

per-fragment lighting

Introduction to Computer Graphics with OpenGL ES (J. Han)