Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 381 – Advanced Game Programming GLSL Lighting.

Similar presentations


Presentation on theme: "CSE 381 – Advanced Game Programming GLSL Lighting."— Presentation transcript:

1 CSE 381 – Advanced Game Programming GLSL Lighting

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

3 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

4 Lighting/Shading is the difference between this:

5 And this:

6 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

7 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

8 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

9 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

10 Light Reflected Surfaces reflect photons selectively

11 Light Transmitted Light passes through a surface Partially redirected Transparent surface –glass –water –etc.

12 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

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

14 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

15 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

16 Specular Light Light comes from a direction Light reflects off in a direction Is affected by: –position of light source –position of viewer

17 Emissive Light Light an object emits Ex: glowing machinery

18 Normals 3D unit vector –denotes direction a surface is facing Derived from vertices using? –cross product of 2 edges Ex: E 1 = B – A E 2 = C – A N A = E 1 X E 2 AB C E2E2 E1E1 NANA NOTE: coordinate system matters

19 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.

20 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

21 Remember to normalize your normals To normalize a normal, first, get length: N Length = √ (N x 2 + N y 2 + N z 2 ) Then divide each component by length N x = N X /N Length N x = N Y /N Length N x = N Z /N length Make sure all normals have a length of 1 –of course you can just use GLSL’s normalize method

22 Materials Properties of a surface –effect on lighting calculation during rendering –i.e. is it shiny, matte, in between? Provides realistic contrast between objects

23 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

24 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.

25 Calculating Attenuation Three factors to set: –Constant (k c ) –Linear (k l ) –Quadratic (k q ) 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 k c + k c d + k c d 2 Attenuation Factor =

26 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

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

28 Remember the Dot Product? V1 ∙ V2 > 0 V1 V2 V1 ∙ V2 = -1 V1 V2 V1 ∙ V2 < 0 V1 V2 V1 ∙ V2 = 0 V1 V2 V1 ∙ V2 == V2 ∙ V1

29 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

30 Using the Half Vector Specular Term = max(N ∙ H, 0) S × S m × S l H:half vector N:surface normal S:material’s shininess value S m :material’s specular color S l :light’s specular color What do we do with this? –multiply by an attenuation factor, then –add it to the final fragment color

31 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

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

33 GLSL Lighting Let’s look at different types of lights: –Directional –Point –Spotlights We can add multiples of each to our scenes

34 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

35 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

36 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 θ

37 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


Download ppt "CSE 381 – Advanced Game Programming GLSL Lighting."

Similar presentations


Ads by Google