CSE 381 – Advanced Game Programming GLSL Lighting.

37
CSE 381 – Advanced Game Programming GLSL Lighting

Transcript of CSE 381 – Advanced Game Programming GLSL Lighting.

Page 1: CSE 381 – Advanced Game Programming GLSL Lighting.

CSE 381 – Advanced Game ProgrammingGLSL Lighting

Page 2: CSE 381 – Advanced Game Programming GLSL Lighting.

Shading

• Adding shadows to a rendering– adds realism– improves viewer’s spatial understanding of scene– shadows need lights

Page 3: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 4: CSE 381 – Advanced Game Programming GLSL Lighting.

Lighting/Shading is the difference between this:

Page 5: CSE 381 – Advanced Game Programming GLSL Lighting.

And this:

Page 6: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 7: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 8: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 9: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 10: CSE 381 – Advanced Game Programming GLSL Lighting.

Light Reflected

• Surfaces reflect photons selectively

Page 11: CSE 381 – Advanced Game Programming GLSL Lighting.

Light Transmitted

• Light passes through a surface

• Partially redirected

• Transparent surface– glass– water– etc.

Page 12: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 13: CSE 381 – Advanced Game Programming GLSL Lighting.

Lights

• Each can have their own components for:– Ambient– Diffuse– Specular– Emissive

Page 14: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 15: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 16: CSE 381 – Advanced Game Programming GLSL Lighting.

Specular Light

• Light comes from a direction

• Light reflects off in a direction

• Is affected by:– position of light source– position of viewer

Page 17: CSE 381 – Advanced Game Programming GLSL Lighting.

Emissive Light

• Light an object emits

• Ex: glowing machinery

Page 18: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 19: CSE 381 – Advanced Game Programming GLSL Lighting.

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.

Page 20: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 21: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 22: CSE 381 – Advanced Game Programming GLSL Lighting.

Materials

• Properties of a surface– effect on lighting calculation during rendering– i.e. is it shiny, matte, in between?

• Provides realistic contrast between objects

Page 23: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 24: CSE 381 – Advanced Game Programming GLSL Lighting.

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.

Page 25: CSE 381 – Advanced Game Programming GLSL Lighting.

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 =

Page 26: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 27: CSE 381 – Advanced Game Programming GLSL Lighting.

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)

Page 28: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 29: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 30: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 31: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 32: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 33: CSE 381 – Advanced Game Programming GLSL Lighting.

GLSL Lighting

• Let’s look at different types of lights:– Directional– Point – Spotlights

• We can add multiples of each to our scenes

Page 34: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 35: CSE 381 – Advanced Game Programming GLSL Lighting.

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

Page 36: CSE 381 – Advanced Game Programming GLSL Lighting.

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

θ

Page 37: CSE 381 – Advanced Game Programming GLSL Lighting.

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