57
242-515 AGD: 9. Illumination 1 1 • Objectives o light sources, reflection models, affects on materials Animation and Games Development 242-515, Semester 1, 2014-2015 9. Illumination

242-515 AGD: 9. Illumination11 Objectives o light sources, reflection models, affects on materials Animation and Games Development 242-515, Semester 1,

Embed Size (px)

Citation preview

242-515 AGD: 9. Illumination

11

• Objectiveso light sources, reflection

models, affects on materials

Animation and Games

Development242-515, Semester 1, 2014-2015

9. Illumination

242-515 AGD: 9. Illumination

22

1. Lighting2. jME Light Sources3. The Phong Reflection Model4. Three Kinds of Reflection5. The Blinn-Phong Model6. Material and Lighting7. Orbiting a Torus8. Surface (Face) Normals9. Shading

Overview

242-515 AGD: 9. Illumination

33

• Light source(s) send out light.• Light hits points on a shape, and may be

absorbed, or reflected.• Reflected light that reaches our eyes determines

the color of a point.

1. Lighting

242-515 AGD: 9. Illumination

44

• Lighting in jME is the process of determining the color of each point in the scene. It depends on:o light source properties

• colour, position, direction, shape … o the shape's material properties (which affect

reflection)

242-515 AGD: 9. Illumination

55

1.1. Some Light-Material

Interactions

Specularmaterial

Diffusematerial

Translucentmaterial

242-515 AGD: 9. Illumination

66

• Real-world lighting of an object involves adding in the light reflected/refracted from other objects in the sceneo called global illumination

• Global illumination takes too long to calculate.

• JME uses the OpenGL local illumination model which only considers how light sources affect an object

1.2. Local Illumination

242-515 AGD: 9. Illumination

77

• Lighting in jME is separate from how objects cast a shadow on the floor or other objects.

• Shadow processing is a sepate step in jME (described later)o casting shadows slows down performance, so they are

not activated by default

1.3. Shadows

242-515 AGD: 9. Illumination

88

• Four types of light source: o ambient lightingo point sourceso directional lighto spotlights

• You must set the color and intensity of each source.o typically you set the color to white

• new ColorRGBA(1f,1f,1f,1f) or ColorRGBA.White

2. jME Light Sources

242-515 AGD: 9. Illumination

99

• An ambient light source uniformly lights the entire scene.o It has no direction and no location and shines equally

everywhere.

• An AmbientLight does not cast any shadows, and it lights all sides of a shape evenlyo this makes shapes look unnaturally flato other light sources can improve the look of a shape

2.1. Ambient Light

242-515 AGD: 9. Illumination

1010

• Bathe all the lit objects in white light:

AmbientLight al = new AmbientLight(); al.setColor( ColorRGBA.White ) ;rootNode.addLight(al);

Example

242-515 AGD: 9. Illumination

1111

• A PointLight has a location and shines from that point in all directionso the shining range (radius) can be specifiedo

• The light intensity decreases with distance from the light source.

• A PointLight can notcast shadows in jME (at the moment)

2.2. Point Sources

242-515 AGD: 9. Illumination

1212

• Locate a yellow point light at (1,0,2), and limits its effect to a radius of 4 units:

PointLight lamp = new PointLight();lamp.setColor(ColorRGBA.Yellow);lamp.setRadius(4f); lamp.setPosition(new Vector3f(1, 0, 2));rootNode.addLight(lamp);

Example

242-515 AGD: 9. Illumination

1313

• A DirectionalLight has no position, only a direction.

• It sends out parallel beams of light and is considered "infinitely" far away from the objects in the scene.

• You typically have one directional light per sceneo acting as the 'sun'

• Shadows can be used with a directional lightin jME

2.3. Directional Light

242-515 AGD: 9. Illumination

1414

• A white light pointing in the direction (-1,-1,1):DirectionalLight sun = new DirectionalLight(); sun.setColor(ColorRGBA.White); sun.setDirection(new Vector3f(-1f, -1f,1f) .normalizeLocal()); rootNode.addLight(sun);

Example

xz

y

242-515 AGD: 9. Illumination

1515

• A SpotLight sends out a cone of light. • A SpotLight has a direction, a location, distance

(range) and two angles:o the inner angle is the central maximum of the light coneo the outer angle the edge of the light cone.

• Everything outside the light cone's angles is not affected by the light.

2.4. Spotlights

outer angle

innerrange

242-515 AGD: 9. Illumination

1616

SpotLight spot = new SpotLight(); spot.setSpotRange(100f); // rangespot.setSpotInnerAngle(15f * FastMath.DEG_TO_RAD); // 15 degree inner light cone (central beam) spot.setSpotOuterAngle(35f * FastMath.DEG_TO_RAD); // 35 degree outer light cone (edge of the light) spot.setColor(ColorRGBA.White);

spot.setPosition(cam.getLocation()); // shine from camera location spot.setDirection(cam.getDirection()); // shine forward from camera loc

rootNode.addLight(spot);

Example

242-515 AGD: 9. Illumination

1717

• The default reflection model used in jME was developed by Phongo it's close enough to real-world reflection for a wide range

of light sources and object materialso it's efficient to calculate since it involves only four

vectors to calculate a color for a point P on a surface.

3. The Phong Reflection Model

P

242-515 AGD: 9. Illumination

1818

• p = a point on the surface• n = the normal vector at point p

• v = the vector from P to the user• l = the vector from P to the light source

• r = the direction that a perfectly reflected ray would take from po this requires a (costly) calculation involving n and l

242-515 AGD: 9. Illumination

1919

• The Phong model supports three types of material-light reflections:o ambient reflectiono diffuse reflectiono specular reflection

• Each of these is combined with the ambient, diffuse, and specular components of the light sources to get the light intensity at a point P, and so the color of the point.

4. Three Kinds of Reflection

242-515 AGD: 9. Illumination

2020

• The intensity of ambient reflection is the same at every point on an object.

• This means that a shape's ambient reflection will be just one color:

4.1. Ambient Reflection

242-515 AGD: 9. Illumination

2121

• A diffuse reflector scatters incoming light equally in all directions

• The amount of reflection depends on the:o object's materialo the position of the light source relative to the surface

• The result is a shape with varying light on it, but dull.

4.2. Diffuse Reflection

A rough surface

242-515 AGD: 9. Illumination

2222

• Specular reflection adds highlights to the reflection (the shiny area).

• The amount of specular reflection that the user sees depends on the angle θ between r (the direction of a perfect reflector vector) and v (the direction of the viewer).

4.3. Specular Reflection

242-515 AGD: 9. Illumination

2323

• There is also a shininess coefficiento as the value increases the reflected light becomes

concentrated into a smaller circle around the r vector

1 5 15

242-515 AGD: 9. Illumination

2424

4.4. Combining Reflections

Ambient Diffuse+ Combination=Specular+

242-515 AGD: 9. Illumination

2525

• In the Phong model, r· v needs to be calculated very often.

• The Blinn-Phong model reduces the overall work by using the halfway angle vector (h)o h lies halfway between the viewer vector (v) and the

light-source vector (l)

• It allows r· v to be replaced by the n· h, which is less work to calculate.

5. The Blinn-Phong Model

h

this angle isusually θ/2

242-515 AGD: 9. Illumination

2626

• If the viewer and light source are far enough away, then h can be treated like a constant and only needs to be computed once.

• This change only affects the look of the specular reflection (slightly).

• jME3 uses standard Phong by default, but can be switched to Blinn-Phong to improve performance.

242-515 AGD: 9. Illumination

2727

• In jME, there is a built-in material which causes a shape to be affected by the Phong lighting model:o Common/MatDefs/Light/Lighting.j3md

• This material supports:o ambient, diffuse, and specular reflectionso it also supports transparency, and can be combined with

textures and maps (see next part)

6. Material and Lighting

242-515 AGD: 9. Illumination

2828

6.1. PhongTeapot

242-515 AGD: 9. Illumination

2929

public void simpleInitApp() { Geometry teapot = (Geometry) assetManager.loadModel( "Models/Teapot/Teapot.obj"); teapot.setLocalScale(4f); // bigger teapot.center(); // center of screen

Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); mat.setBoolean("UseMaterialColors", true); mat.setColor("Ambient", ColorRGBA.Orange); mat.setColor("Diffuse", ColorRGBA.Orange); mat.setColor("Specular", ColorRGBA.White); mat.setFloat("Shininess", 12); teapot.setMaterial(mat);

rootNode.attachChild(teapot); :

Partial Code PhongTeapot.java

242-515 AGD: 9. Illumination

3030

// add a light to make the lit object visible DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal()); sun.setColor(ColorRGBA.White);

rootNode.addLight(sun); } // end of simpleInitApp()

242-515 AGD: 9. Illumination

3131

• Added code from slide 10:

With White Ambient Light

242-515 AGD: 9. Illumination

3232

With only Orange Diffuse

Reflection

242-515 AGD: 9. Illumination

3333

Material AMBIENT DIFFUSE SPECULAR SHININESS

Polished 0.23125 0.2775 0.773911 89.6

Silver 0.23125 0.2775 0.773911

0.23125 0.2775 0.773911

1.0 1.0 1.0

Emerald 0.0215 0.07568 0.633 76.8

0.1745 0.61424 0.727811

0.0215 0.07568 0.633

0.55 0.55 0.55

Jade 0.135 0.54 0.316228 12.8

0.2225 0.89 0.316228

0.1575 0.63 0.316228

0.95 0.95 0.95

Pearl 0.25 1.0 0.296648 11.264

0.20725 0.829 0.296648

0.20725 0.829 0.296648

0.922 0.922 0.922

Some Typical Materials

more at http://devernay.free.fr/cours/opengl/materials.html

242-515 AGD: 9. Illumination

3434

Jade Teapot

242-515 AGD: 9. Illumination

3535

ColorRGBA jade = new ColorRGBA(0.54f, 0.89f, 0.63f, 0.95f);

Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); mat.setBoolean("UseMaterialColors", true); mat.setColor("Ambient", jade); mat.setColor("Diffuse", jade); mat.setColor("Specular", ColorRGBA.White); mat.setFloat("Shininess", 12); teapot.setMaterial(mat);

Change the Material Settings

242-515 AGD: 9. Illumination

3636

• The scene contains a stationary directional green light and a point red light orbiting a torus (doughnut).

7. Orbiting a Torus

242-515 AGD: 9. Illumination

3737

• The directional green light is pointing upwardso note that the inside top half of the torus is lit when it

should be dark

• The point light geometry is a red circle, but this has nothing to do with the red color of the light it is emitting

• The point light is rotated by having the update loop (the simpleUpdate() method) rotate a spatial node linking the light to the sceneo a light geometry and a LighNode object are rotated

242-515 AGD: 9. Illumination

3838

Scene Graph

geom

mat

redPointLight

pl

rootNode

movingNode

LightNodelightBulbgeometry

red-litMaterial

green DirectionalLight

dl

b&w litMaterial

242-515 AGD: 9. Illumination

3939

private float angle = 0f; // globalsprivate Node movingNode;

public void simpleInitApp() { Torus torus = new Torus(50, 30, 1, 3); // fine-detailed Geometry geom = new Geometry("Torus Geom", torus); geom.center(); // a white shiny doughnut with no ambient reflection Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); mat.setBoolean("UseMaterialColors", true); mat.setColor("Ambient", ColorRGBA.Black); mat.setColor("Diffuse", ColorRGBA.White); mat.setColor("Specular", ColorRGBA.White); mat.setFloat("Shininess", 32f); geom.setMaterial(mat); rootNode.attachChild(geom); :

Partial CodeTorusOrbiter.java

242-515 AGD: 9. Illumination

4040

// red sphere representing the light source Geometry lightBulb = new Geometry("Light", new Sphere(10, 10, 0.1f)); lightBulb.setMaterial( assetManager.loadMaterial( "Common/Materials/RedColor.j3m")); movingNode = new Node("lightParentNode"); movingNode.attachChild(lightBulb); rootNode.attachChild(movingNode);

// red point light, added to the scene graphPointLight pl = new PointLight(); pl.setColor(ColorRGBA.Red); pl.setRadius(4f); rootNode.addLight(pl);

// also attached to same node as the lightbulbLightNode lightNode = new LightNode("pointLight", pl); movingNode.attachChild(lightNode); :

242-515 AGD: 9. Illumination

4141

// green directional light, point upwards DirectionalLight dl = new DirectionalLight(); dl.setColor(ColorRGBA.Green); dl.setDirection(new Vector3f(0, 1, 0)); rootNode.addLight(dl);} // end of simpleInitApp()

242-515 AGD: 9. Illumination

4242

public void simpleUpdate(float tpf) { angle += tpf; // increase the angle angle %= FastMath.TWO_PI; // limit to 0-360 degrees in radians movingNode.setLocalTranslation( new Vector3f(FastMath.cos(angle) * 3f, 2, FastMath.sin(angle) * 3f)); } // end of simpleUpdate()

Circle around the (0,2,0) over the XZ plane with radius of 3

242-515 AGD: 9. Illumination

4343

• x == cos(angle), y == constant , z == sin(angle)• this is a circle in the XZ plane

Circle Maths

X

Z

242-515 AGD: 9. Illumination

4444

• Aim the light along the +x axis: dl.setDirection(new Vector3f(1, 0, 0));

Change the Directional Light

242-515 AGD: 9. Illumination

4545

• Surface normal vectors are essential in lighting calculations (they are the n vector).

• When you create a model in Blender, make sure to have it generate the surface (face) normals for you.

8. Surface (Face) Normals

242-515 AGD: 9. Illumination

4646

8.1. Vertex Normals• Vertex normals use the object's vertices as the

source for the normalso this is how jME implements normals itselfo vertex normals can be converted into surface normals

• Vertex normals can be set by jME method calls, but it's a lot of work for anything but simple shapes.

242-515 AGD: 9. Illumination

4747

• Lighting is only computed at the vertices of a shape (these are the P points).

• Shading is the process of filling in the colors for the pixels of the shape that lie between the verticies.

• There are three main approaches to shading:o flat shadingo Gouraud shading (smooth shading)o Phong shading

• this is different from Phong lighting

• Shading is nothing to do with shadows.

9. Shading

242-515 AGD: 9. Illumination

4848

• Compute a single lighting value for each triangle (or quadrilateral) making up the shape.

• The worst looking shader, but is very fast.

9.1. Flat Shading

242-515 AGD: 9. Illumination

4949

• Lighting color is calculated per-vertexo saves computation

• Color is bilinearly interpolated across the pixels of each triangle using the verticies.

9.2. Gouraud Shading

242-515 AGD: ?? 50

Gouraud in Action

Apply interpolated values across pixels.

242-515 AGD: ?? 51

242-515 AGD: ?? 52

242-515 AGD: ?? 53

242-515 AGD: ?? 54

242-515 AGD: 9. Illumination

5555

• Not the same as Phong lighting.• Calculate the lighting equation for every pixel of

the shapeo the surface normal is linearly interpolated across the

triangleo looks the best, but is the slowest

9.3. Phong Shading

242-515 AGD: 9. Illumination

5656

Phong vs Gouraud

Specular highlights don’t look as good in Gouraud

242-515 AGD: 9. Illumination

5757

• jME3 supports all three approacheso it uses Phong shading by default, but this can be changed

• It use to be possible to change the shaders using:

o ShadeState st = new LWJGLShadeState();st.setShade(ShadeState.SM_FLAT); // SM_SMOOTHst.setEnabled(true); rootNode.setRenderState(st); rootNode.updateRenderState();

o but ShadeState is not in v3 of jME o it looks like you need to use a programmable shader (see

later)

9.4. Shading in jME3