Computer Graphics
COMP3421/9415 2021 Term 3 Lecture 12
What did we learn last lecture?
Introduction to Lighting
¡ñ Real world vs Simulation
¡ñ The possibilities of accurate simulation
¡ñ due to processing limitations
¡ñ Beginning to look closely at the maths for Ambient and Diffuse lighting
What are we covering today?
Continuing the deep dive into
¡ñ Diffuse Lighting
¡ñ Specular Lighting
¡ñ Dealing with multiple lights
Diffuse Lighting Walkthrough
A worked example
A worked example
Let’s calculate some Ambient and Diffuse Lighting
¡ñ A scene with a light at (4,5,0)
¡ð The light’s colours are (0.8, 0.8, 0.5), so a bit yellowish
¡ñ Ambient light is (0.1, 0.1, 0.1)
¡ñ The current fragment is at (1,2,0)
¡ð The surface normal is (0,1,0)
¡ð The diffuse reflective colour is (0.1, 0.8, 0.3), mostly green
What Information do we have?
Ia + Id = ia * ka + kd * (L.N) * id
¡ñ ia = (0.1, 0.1, 0.1)
¡ñ ka = (0.1, 0.8, 0.3) we’re using the diffuse colour
¡ñ kd = (0.1, 0.8, 0.3)
¡ñ L = needs to be calculated
¡ñ N = (0,1,0)
¡ñ id = (0.8, 0.8, 0.5)
L: Direction to the Light Source
Calculate L
L: Direction Vector aiming at the light
¡ñ We have a start and end of the vector
¡ñ end – start = vector
¡ñ Remember to Normalise!
¡ñ Normalise((4,5,0) – (1,2,0))
¡ñ Normalise(3,3,0) Divide the vector by its own length to normalise it!
¡ñ Remember Pythagorean Triangles? a2 + b2 = c2
L.N
What does the dot product tell us?
¡ñ Dot product of two unit (length 1) vectors
¡ñ is the cosine of the angle between them
The complete equation
Ia + Id = ia * ka + kd * (L.N) * id
¡ñ Ia = (0.1, 0.1, 0.1) * (0.1, 0.8, 0.3)
¡ñ Id = (0.1, 0.8, 0.3) * ((1/¡Ì2, 1/¡Ì2, 0).(0,1,0)) * (0.8,
0.8, 0.5)
¡ñ Ia + Id = . . .
¡ñ Try this out yourself!
¡ñ Also try changing the light position and see the effect
In the Shaders
Our Graphics card will be doing this maths!
¡ñ Vertex Shader Outputs:
¡ð Fragment Position
¡ð Surface Normal (either generated from vertices or lerped from normals at vertices)
¡ð Colour and/or TexCoord
¡ñ Fragment Shader Inputs:
¡ð the outputs above
¡ð Light Position or Direction
¡ð Light Colour
¡ñ Frag shader will calculate the Light Direction
¡ñ Frag shader will complete the algorithm and calculate the frag colour
Specular Lighting
Specular Lighting Equation
Is = ks * (R.V)a * is
¡ñ Is Final intensity of specular light
¡ñ ks Specular reflectivity of the fragment
¡ñ R Direction of reflected light
¡ñ V Direction to the viewer
¡ñ a
¡ñ is Specular intensity of light source
(R.V)a – What’s this part of the Equation?
Important directions for reflections
¡ñ R Direction of reflected light
¡ð Calculated based on the Light Direction and the Surface Normal
¡ñ V Direction to the viewer
¡ð The direction from the Fragment to the Camera
¡ñ This dot product is similar to the Diffuse L.N
¡ñ How close is the reflected light to the camera?
¡ñ This means: Is the light reflecting directly into the camera?
Reflected and Viewer Vectors
What is the ?
A measure of “shininess”
¡ñ An abstract concept, not grounded in reality
¡ñ The concept:
¡ð The shinier something is, the more focused a reflection is
¡ð Something less shiny still reflects the light, but in a “wider” fashion
¡ñ The maths:
¡ð R.V will be between 0 and 1
¡ð Any positive power of 0-1 will “narrow” its curve
The in action
Image credit: learnopengl.com
Break Time
Phong’s specular lighting
¡ñ The “bright spot” is like a reflection of the light source
¡ñ But it’s all a trick!
¡ñ Our eyes are used to not being able to see a
bright light reflected clearly
¡ñ So we just get a “splodge” of the light’s colour
¡ñ Like being dazzled by brightness
¡ñ Specular highlights make us believe in metal,
liquids etc
Warframe,
Image credit: Digital Extremes
Specular Lighting Walkthrough
Another worked example
Another worked example
Let’s calculate some Specular Lighting
¡ñ A scene with a light at (4,5,0)
¡ð The light’s colours are (0.8, 0.8, 0.5), so a bit yellowish
¡ñ Ambient light is (0.1, 0.1, 0.1)
¡ñ The current fragment is at (1,2,0)
¡ð The surface normal is (0,1,0)
¡ð The specular reflective colour is (1.0, 1.0, 1.0), it’s pure reflective
¡ñ The camera is at (-4,3,0)
¡ñ The is 20 (this can be experimented with!)
The Equation
Is = ks * (R.V)a * is
¡ñ ks = (1.0, 1.0, 1.0)
¡ñ L=
¡ñ N=
¡ñ R=
¡ñ V=
¡ñ a =
¡ñ is = (0.8, 0.8, 0.5)
Normalise((4, 5, 0) – (1, 2, 0))
(0, 1, 0)
needs to be calculated from L and N
Normalise((-4, 3, 0) – (1, 2, 0))
20
The Reflected Vector
Deciding the direction of a reflection
¡ñ We have a vector to the light L
¡ñ and a surface normal N
¡ñ A formula for reflection:R = 2 * (N.L) * N – L
¡ñ The maths behind this formula is interesting if you want to look it up
¡ñ R will be a direction vector that is an exact reflection of L
¡ñ R = 2 * 1/¡Ì2 * (0,1,0) – (1/¡Ì2, 1/¡Ì2, 0)
¡ñ R = (0, 2/¡Ì2, 0) – (1/¡Ì2, 1/¡Ì2, 0)
¡ñ R = (-1/¡Ì2, 1/¡Ì2, 0)
The complete equation
Is = ks * (R.V)a * is
¡ñ Is = (1.0, 1.0, 1.0) *
((-1/¡Ì2, 1/¡Ì2, 0).(-5/¡Ì26,1/¡Ì26,0))20 * (0.8, 0.8, 0.5)
¡ñ Is = . . .
¡ñ Try moving the camera upwards to see the intensity change
In the Shaders
Again, our Graphics card will be doing the maths!
¡ñ Shaders will have similar inputs/outputs to earlier with Diffuse
¡ñ Shader Input:
¡ð Camera/Viewer Position
¡ñ Frag shader will calculate the Reflected Direction (GLSL reflect())
¡ñ Frag shader will complete the algorithm and calculate the frag colour
¡ð Adds together Ambient, Diffuse and Specular
Multiple Different Lights
Multiple Lights and their different types
Different Types of Lights
¡ñ Directional Lights
¡ñ Point Lights
¡ñ Spot Lights
Handling Multiple Lights in
¡ñ Looping through multiple lights
Directional Lights
Lights so far away, they don’t have a position
¡ñ Represent distant lights like the Sun
¡ñ Represented by a direction vector
¡ñ Mathematically easy!
¡ñ We no longer calculate the vector to the light, we just use the light’s vector
Point Lights
We’ve been using these in our equations already!
¡ñ Lights with a location in the scene
¡ñ Represent smaller lights like lamps etc
¡ñ Use attenuation to make smaller lights more realistic
¡ñ Attenuation is the lowering of intensity based on distance
Spot Lights
Modified Point Lights
¡ñ Represent objects like electric torches, vehicle headlights etc
¡ñ Adds an aim direction and a cutoff angle
¡ñ Some extra calculation needed to see if a
fragment is inside the cutoff angle
Image credit: learnopengl.com
Multiple Lights
How do we process multiple lights?
¡ñ The fragment shader will only run once per fragment
¡ñ Each fragment will have one colour, regardless of the number of lights!
¡ñ Different code for different lights!
¡ñ Calculate Ambient Light
¡ñ Loop through all Directional Light(s) diffuse and specular
¡ñ Loop through all Point Lights diffuse and specular
¡ñ Loop through all Spot Lights diffuse and specular
A discussion of
Pros
¡ñ Computes fast
¡ñ A good approximation of real light
¡ñ Gives us directional lighting
¡ñ Gives a simple model for different materials
¡ð Just alter ambient/diffuse/specular reflectivity
¡ñ Can handle a few different light types
¡ñ Easy to modify to add capability
A discussion of
Cons
¡ñ Doesn’t always look realistic (you can tell it’s not real light)
¡ñ Specular highlights can get a bit beyond real
¡ñ A few genuine issues with the maths
¡ð Colours can overflow their RGB values
¡ð Reflected vectors are limited to 0-90 degrees
¡ñ Scales by fragments * lights
¡ð The more lights and objects in a scene multiplies the amount of work for the frag shader
What did we learn today?
Completion of
¡ñ Diffuse walkthrough
¡ñ Specular Lighting and walkthrough
¡ñ Working with different light types
¡ñ and multiple lights
¡ñ Some discussion of the pros and cons of
¡ñ We’re going to work on some of these issues next week!