CSE 381 – Advanced Game Programming GLSL Lighting.
-
Upload
barry-ryan -
Category
Documents
-
view
223 -
download
1
Transcript of CSE 381 – Advanced Game Programming GLSL Lighting.
CSE 381 – Advanced Game ProgrammingGLSL Lighting
Shading
• Adding shadows to a rendering– adds realism– improves viewer’s spatial understanding of scene– shadows need lights
Lighting• Add light sources to a scene
• Compute effect of lights on:– vertices
– fragments
• What effects?– makes lighter (closer to white)?
– makes darker (closer to black)?
– make a channel lighter/darker
• Combine effects with:– materials
– textures
Lighting/Shading is the difference between this:
And this:
How does light behave in the real world?
• Light sources produce photons
• Photons have different wavelengths– i.e. colors
• Photons travel fast (duh)– hit surfaces and may be:
• absorbed
OR
• reflected
OR
• transmitted
– depends on surface properties
Selective Frequencies
• Surfaces selectively absorb, reflect, & transmit– Meaning different frequencies
• Ex: a surface may:– reflect green light– absorb all other frequencies
• Note: reflected photons hit our eyes– that’s what we see
Light Absorption
• Energy converted to heat
• What kinds of surfaces?– those with similar natural frequencies– refers to atoms’ vibrations
• Other factors:– smoothness of surface– angle light hits surface– dark absorbs more than light
Absorption Example
• Pure Red Ball– reflects red light– absorbs green and blue light
• What happens if we have blue ambient lighting?– it appears black
• Why?– no red to reflect
Light Reflected
• Surfaces reflect photons selectively
Light Transmitted
• Light passes through a surface
• Partially redirected
• Transparent surface– glass– water– etc.
Lighting Calculations
• Many models to choose from– we’ll use Phong-Blinn– note, different models produce different results
• Lighting Calculation Components:– Ambient– Diffuse– Specular– Emissive
Lights
• Each can have their own components for:– Ambient– Diffuse– Specular– Emissive
Ambient Light
• Light reflected so many times, source not apparent
• Result on color of surface rendered not affected by– light position– viewer position
• So far, we’ve only had ambient light
Diffuse Light
• Light has a direction
• When it hits a surface, it reflects equally– is affected by position of light source– not affected by position of viewer
Specular Light
• Light comes from a direction
• Light reflects off in a direction
• Is affected by:– position of light source– position of viewer
Emissive Light
• Light an object emits
• Ex: glowing machinery
Normals
• 3D unit vector– denotes direction a surface is facing
• Derived from vertices using?– cross product of 2 edges
• Ex:E1 = B – A
E2 = C – A
NA = E1 X E2
A B
C
E2
E1
NA
NOTE: coordinate system matters
Can vertices share a normal?
• Yes, depending on geometry
• What’s good for sharing normals?– Flat faces with sharply angled corners– Ex: box, building, wall, etc.
• What’s bad?– ball, balloon, etc.
For Smooth Surfaces
• Compute a normal for each vertex
• How?– average of faces it is part of
• What if a mesh has smooth parts and hard edges?– use smoothing groups
Remember to normalize your normals
• To normalize a normal, first, get length:
NLength = √ (Nx2 + Ny
2 + Nz2)
• Then divide each component by length
Nx = NX/NLength
Nx = NY/NLength
Nx = NZ/Nlength
• Make sure all normals have a length of 1– of course you can just use GLSL’s normalize method
Materials
• Properties of a surface– effect on lighting calculation during rendering– i.e. is it shiny, matte, in between?
• Provides realistic contrast between objects
Material Properties
• For each surface, specify color and intensity of:– Diffuse light reflected– Ambient light reflected– Specular light reflected– Emissive light emitted– Shininess, size of specular highlight
Attenuation
• Dimming with distance
• Lights should illuminate objects less intensely if they are far away from light source
• Exception: really big lights– i.e. sun, moon, etc.
Calculating Attenuation
• Three factors to set:– Constant (kc)
– Linear (kl)
– Quadratic (kq)
• And d = distance from light source
• What do we do with this value?– multiply the diffuse, specular, and source-specific
ambient light colors by this factor to reduce the intensity of the light as the object moves away from it
1
kc + kcd + kcd2
AttenuationFactor =
Blinn-Phong Model
• Calculate per vertex lighting
• Interpolate effect across fragment
• For calculation combine any or all of:– D: Diffuse effect (use Lambertian Reflection)– S: Specular term (use Blinn-Phong Reflection)– A: Light Specific Ambient Light
OR– A: Global Ambient Light
• Color = Color + D + S + A
Lambertian Reflection
• Intensity of diffuse light on surface depends on angle of the surface to the light source
• Diffuse Light Intensity = max(L ∙ N, 0.0) × C × I
L: light direction
i.e., light position – vertex position
N: surface normal
C: material’s diffuse color
I: light’s diffuse property (intensity)
Remember the Dot Product?
V1 ∙ V2 > 0
V1
V2V1 ∙ V2 = -1
V1 V2
V1 ∙ V2 < 0
V1
V2V1 ∙ V2 = 0
V1
V2
V1 ∙ V2 == V2 ∙ V1
The Specular Term• First calculate the half vector
• What’s that?– halfway between the eye vector
and the light vector
• How?– add light and eye vectors:
H = L + E
• Note again:– L is vertex to light source location
– E is vertex to eye location
Using the Half Vector
• Specular Term = max(N ∙ H, 0)S × Sm × Sl
H: half vector
N: surface normal
S: material’s shininess value
Sm: material’s specular color
Sl: light’s specular color
• What do we do with this?– multiply by an attenuation factor, then
– add it to the final fragment color
Ambient Light Component
• Light-specific ambient light– use ambient values of a light source– can be attenuated
OR
• Global ambient light– a constant that affects the whole scene– not attenuated
• To calculate:– multiply light’s ambient term by material’s ambient
reflectance and add to final color
Normal Matrix
• As models rotate, so do their normals
• To update normals, use a 3x3 normal matrix– rotation component of modelview matrix
To use::
// a_Normal is the normal vertex attribute
// normal holds the final transformed normal
mat3x3 normalMatrix = mat3x3(modelview_matrix);
vec3 normal = normalize(normalMatrix * a_Normal);
GLSL Lighting
• Let’s look at different types of lights:– Directional– Point – Spotlights
• We can add multiples of each to our scenes
Directional Lighting• Light comes from a direction
• Has no source location– infinitely far away (i.e., the sun)
– affects all objects similarly
• Cheap to use– no attenuation/distance calculations
• In calculations, the light position is the direction
• Let’s look at the ch08_terrain_lighting example’s vertex shader
Point Lights
• Have a position, not a direction
• Radiates light in all directions
• Suffer from attenuation
• Otherwise, it’s a similar calculation
• Let’s look at the ch08_point_light example
Spotlights• A specialized point light
• Radiates light only in a directional cone
• Properties:– location
– direction
– cutoff angle (θ)
– spotlight exponent
• how rapidly the intensity drops from center to wall of cone
• Let’s look at ch08_spot_light example
θ
What if we want multiple lights
• Simple Option:– render the scene multiple times, one for each light– blend results each pass using additive blending
• Expensive
• Better Option:– have shader loop through light sources passed as
uniform variables
• Look at the ch_08_multiple_lights example