10.lighting - Clarkson UniversityThe normalization in frustum requires an initial shear to form a...

Preview:

Citation preview

CS452/552; EE465/505

Intro to Lighting 2-10–15

! Projection Normalization ! Introduction to Lighting (and Shading)

Read: Angel •Chapter 5., sections 5.4 - 5.7 Parallel Projections •Chapter 6, sections 6.1 - 6.3 Lighting & Shading

Lab2: use ASDW to move a 2D shape around

Outline

Consider the vertex shaders for each of these examples:

!Coordinate systems (from Chastine)

!ColorCube example (from Matsuda)

! perspective example (from Angel)

3

From model to viewing

(c) 2015 Jeff Chastine

From model to view: Chastinein vec4 vPosition; // The vertex in the local coordinate system uniform mat4 mM; // The matrix for the pose of the model uniform mat4 mV; // The matrix for the pose of the camera uniform mat4 mP; // The projection matrix (perspective)

void main () { gl_Position = mP*mV*mM*vPosition;

}Original (local) position

New position in NDC

attribute vec4 a_Position; attribute vec4 a_Color; uniform mat4 u_MvpMatrix; varying vec4 v_Color;

void main() { gl_Position = u_MvpMatrix * a_Position; v_Color = a_Color; }

ColoredCube Vertex Shader

Angel: perspective2.htmlattribute vec4 vPosition; attribute vec4 vColor; varying vec4 fColor; uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix;

void main() { gl_Position = projectionMatrix*modelViewMatrix*vPosition; fColor = vColor; }

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

ortho(left,right,bottom,top,near,far)

WebGL Orthogonal Viewing

near and far measured from camera

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

frustum(left,right,bottom,top,near,far)

WebGL Perspective

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

!With frustum it is often difficult to get the desired view

✦perpective(fovy, aspect, near, far) often provides a better interface

Using Field of View

aspect = w/h

front plane

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Computing Matrices! Compute in JS file, send to vertex shader with gl.uniformMatrix4fv

! Dynamic: update in render() or shader

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! Rather than derive a different projection matrix for each type of projection, we can convert all projections to orthogonal projections with the default view volume

! This strategy allows us to use standard transformations in the pipeline and makes for efficient clipping

Projection Normalization

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Pipeline View modelview

transformation projection

transformationperspective

division

clipping projection

4D → 3D

against default cube3D → 2D

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

!We stay in four-dimensional homogeneous coordinates through both the modelview and projection transformations Both these transformations are nonsingular Default to identity matrices (orthogonal view)

! Normalization lets us clip against simple cube regardless of type of projection

! Delay final projection until end Important for hidden-surface removal to retain depth information as long as possible

Notes

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

ortho(left,right,bottom,top,near,far)

Orthogonal Normalization

normalization ⇒ find transformation to convert specified clipping volume to default

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! Two steps Move center to origin T(-(left+right)/2, -(bottom+top)/2,(near+far)/2))

Scale to have sides of length 2 S(2/(left-right),2/(top-bottom),2/(near-far))

Orthogonal Matrix

2right − left

0 0 −right − leftright − left

0 2top − bottom

0 −top+ bottomtop − bottom

0 0 2near − far

far + nearfar − near

0 0 0 1

#

$

% % % % % % %

&

'

( ( ( ( ( ( (

P = ST =

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! Set z =0 ! Equivalent to the homogeneous coordinate transformation

! Hence, general orthogonal projection in 4D is

Final Projection

!!!!

"

#

$$$$

%

&

1000000000100001

Morth =

P = MorthST

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! The OpenGL/WebGL projection functions cannot produce general parallel projections

✦oblique parallel projections are useful ✦an oblique projection is characterized by the angle the projectors make with the projection plane

! However if we look at the example of the cube it appears that the cube has been sheared

! Oblique Projection = Shear + Orthogonal Projection

Oblique Projections

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

side view

General Shear

top view

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

xy shear (z values unchanged)

Projection matrix

General case:

Shear Matrix

!!!!

"

#

$$$$

%

&

100001000φcot100θcot01

H(θ,φ) =

P = Morth H(θ,φ)

P = Morth STH(θ,φ)

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Shearing matrix

Consider a simple perspective with the COP at the origin, the near clipping plane at z = -1, and a 90 degree field of view determined by the planes

x = ±z, y = ±z

Simple Perspective

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Simple projection matrix in homogeneous coordinates

Note that this matrix is independent of the far clipping plane

Perspective Matrices

!!!!

"

#

$$$$

%

&

− 0100010000100001

M =

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

N =

Generalization

!!!!

"

#

$$$$

%

&

− 0100βα0000100001

after perspective division, the point (x, y, z, 1) goes to

x’’ = x/z y’’ = y/z Z’’ = -(α+β/z)

which projects orthogonally to the desired point regardless of α and β

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Picking α and β

If we pick

α =

β =

nearfarfarnear

+

farnearfarnear2

the near plane is mapped to z = -1 the far plane is mapped to z =1 and the sides are mapped to x = ± 1, y = ± 1

Hence the new clipping volume is the default clipping volume

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

original clipping volume

Normalization Transformation

original object new clipping volume

distorted object projects correctly

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! Although our selection of the form of the perspective matrices may appear somewhat arbitrary, it was chosen so that if z1 > z2 in the original clipping volume then the for the transformed points z1’ > z2’

! Thus hidden surface removal works if we first apply the normalization transformation

! However, the formula z’’ = -(α+β/z) implies that the distances are distorted by the normalization which can cause numerical problems especially if the near distance is small

Normalization and Hidden-Surface Removal

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

✦gl.frustum allows for an unsymmetric viewing frustum (although gl.perspective does not)

WebGL Perspective

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! The normalization in frustum requires an initial shear to form a right viewing pyramid, followed by a scaling to get the normalized perspective volume. Finally, the perspective matrix results in needing only a final orthogonal transformation

OpenGL Perspective Matrix

P = NSH

our previously defined perspective matrix shear and scale

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

!Normalization allows for a single pipeline for both perspective and orthogonal viewing

!We stay in four dimensional homogeneous coordinates as long as possible to retain three-dimensional information needed for hidden-surface removal and shading

!We simplify clipping

Why do we do it this way?

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Perspective Matrices

P =

2* nearright − left

0 right − leftright − left

0

0 2* neartop − bottom

top+ bottomtop − bottom

0

0 0 −far + nearfar − near

−2* far * nearfar − near

0 0 −1 0

#

$

% % % % % % %

&

'

( ( ( ( ( ( (

P =

nearright

0 0 0

0 neartop

0 0

0 0 −far + nearfar − near

−2* far * nearfar − near

0 0 −1 0

#

$

% % % % % % %

&

'

( ( ( ( ( ( (

frustum

perspective

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Lighting

Jeff Chastine

What is Light?! A very complex process ◦ Find a dark area – how is it being lit? ◦ Light bounces (mirrors, shiny objects) ◦ Light refracts through other media (water, heat) ◦ Light comes from everywhere (Global Illumination) ◦ Light bounces off of lakes in weird ways (Fresnel effect)

! THUS ◦ We’re forced to make approximations ◦ Tradeoff between time and realism ◦ “If it looks good, it is good” – Michael Abrash

http://darrentakenaga.com/3d.html

http://en.wikipedia.org/wiki/File:Global_illumination.JPG

Lighting Principles! Lighting simulates how objects reflect light ■ material composition of object ■ light’s color and position ■ global lighting parameters

! Lighting functions deprecated in 3.1 ! Can implement in ■ Application (per vertex) ■ Vertex or fragment shaders

!Why does the image of a real sphere look like

! Light-material interactions cause each point to have a different color or shade

! Need to consider Light sources Material properties Location of viewer Surface orientation

Shading

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! Light strikes A Some scattered Some absorbed

! Some of scattered light strikes B Some scattered Some absorbed

! Some of this scattered light strikes A and so on

Scattering

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! The infinite scattering and absorption of light can be described by the rendering equation Cannot be solved in general Ray tracing is a special case for perfectly reflecting surfaces

! Rendering equation is global and includes Shadows Multiple scattering from object to object

Rendering Equation

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Global Effects

translucent surface

shadow

multiple reflection

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

!Correct shading requires a global calculation involving all objects and light sources Incompatible with pipeline model which shades each polygon independently (local rendering)

!However, in computer graphics, especially real time graphics, we are happy if things “look right” Exist many techniques for approximating global effects

Local vs Global Rendering

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! Light that strikes an object is partially absorbed and partially scattered (reflected)

! The amount reflected determines the color and brightness of the object A surface appears red under white light because the red component of the light is reflected and the rest is absorbed

! The reflected light is scattered in a manner that depends on the smoothness and orientation of the surface

Light-Material Interaction

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

General light sources are difficult to work with because we must integrate light coming from all points on the source

Light Sources

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! Point source Model with position and color Distant source = infinite distance away (parallel)

! Spotlight Restrict light from ideal point source

! Ambient light Same amount of light everywhere in scene Can model contribution of many sources and reflecting surfaces

Simple Light Sources

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! The smoother a surface, the more reflected light is concentrated in the direction a perfect mirror would reflected the light

! A very rough surface scatters light in all directions

Surface Types

smooth surface rough surface

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! A simple model that can be computed rapidly ! Has three components

Diffuse Specular Ambient

! Uses four vectors To source To viewer Normal Perfect reflector

Phong Model

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

!Normal is determined by local orientation ! Angle of incidence = angle of reflection ! The three vectors must be coplanar

Ideal Reflector

r = 2 (l · n ) n - l

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! Perfectly diffuse reflector ! Light scattered equally in all directions ! Amount of light reflected is proportional to the vertical component of incoming light reflected light ~cos θi

cos θi = l · n if vectors normalized There are also three coefficients, kr, kb, kg that show how much of each color component is reflected

Lambertian Surface

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

!Most surfaces are neither ideal diffusers nor perfectly specular (ideal reflectors)

! Smooth surfaces show specular highlights due to incoming light being reflected in directions concentrated close to the direction of a perfect reflection

Specular Surfaces

specular highlight

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! Phong proposed using a term that dropped off as the angle between the viewer and the ideal reflection increased

Modeling Specular Relections

φ

Ir ~ ks I cosαφ

shininess coef

absorption coef

incoming intensityreflected intensity

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

! Values of α between 100 and 200 correspond to metals

! Values between 5 and 10 give surface that look like plastic

The Shininess Coefficient

cosα φ

φ 90-90

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Modified Phong Model! Computes a color or shade for each vertex using a lighting model (the modified Phong model) that takes into account ■ Diffuse reflections ■ Specular reflections ■ Ambient light ■ Emmision

! Vertex shades are interpolated across polygons by the rasterizer

The Modified Phong Model! The model is a balance between simple computation and physical realism

! The model uses ■ Light positions and intensities ■ Surface orientation (normals) ■ Material properties (reflectivity) ■ Viewer location

! Computed for each source and each color component

Surface Normals! Normals define how a surface reflects light ■ Application usually provides normals as a vertex atttribute

■ Current normal is used to compute vertex’s color ■ Use unit normals for proper lighting ● scaling affects a normal’s length

Material Properties! Define the surface properties of a primitive ■ you can have separate materials for front and back

Property Description

Diffuse Base object color

Specular Highlight color

Ambient Low-light color

Emission Glow color

Shininess Surface smoothness

JEFF CHASTINE

A BASIC LIGHTING CONCEPT• How can we determine how much light should be cast onto a triangle from a directional light?

P2

P0

P1

Directional light - position doesn’t matter - triangle is almost fully lit

52

JEFF CHASTINE

A BASIC LIGHTING CONCEPT• How can we determine how much light should be cast onto a triangle from a directional light?

P2

P0

P1

(Triangle less lit)

53

JEFF CHASTINE

A BASIC LIGHTING CONCEPT• How can we determine how much light should be cast onto a triangle from a directional light?

P2

P0

P1

(Little to no light hits the surface)

54

JEFF CHASTINE

A BASIC LIGHTING CONCEPT• How can we determine how much light should be cast onto a triangle from a directional light?

P2

P0

P1

(Directional light)

55

JEFF CHASTINE

A BASIC LIGHTING CONCEPT• How can we determine how much light should be cast onto a triangle from a directional light?

P2

P0

P1

(Directional light)

56

JEFF CHASTINE

A BASIC LIGHTING CONCEPT• How can we determine how much light should be cast onto a triangle from a directional light?

P2

P0

P1

(Directional light)

Lesson learned: Lighting depends on angles between vectors!

57

JEFF CHASTINE

A BASIC LIGHTING CONCEPT• How can we determine how much light should be cast onto a triangle from a directional light?

P2

P0

P1

(Directional light)

Assuming N and L are normalized, and N·L isn’t negative58

JEFF CHASTINE

BASIC LIGHTING• Four independent components:

• Diffuse – the way light “falls off” of an object • Specular – the “shininess” of the object • Ambient – a minimum amount of light used to simulate “global illumination” • Emit – a “glowing” effect

Only diffuse

59

JEFF CHASTINE

BASIC LIGHTING• Four independent components:

• Diffuse – the way light “falls off” of an object • Specular – the “shininess” of the object • Ambient – a minimum amount of light used to simulate “global illumination” • Emit – a “glowing” effect

Diffuse+Specular

60

JEFF CHASTINE

BASIC LIGHTING• Four independent components:

• Diffuse – the way light “falls off” of an object • Specular – the “shininess” of the object • Ambient – a minimum amount of light used to simulate “global illumination” • Emit – a “glowing” effect

Ambient

Diffuse+Specular+Ambient

61

JEFF CHASTINE

BASIC LIGHTING• Four independent components:

• Diffuse – the way light “falls off” of an object • Specular – the “shininess” of the object • Ambient – a minimum amount of light used to simulate “global illumination” • Emit – a “glowing” effect

D+S+A+Emit

Note: emit does not produce light! 62

JEFF CHASTINE

INTERACTION BETWEEN MATERIAL AND LIGHTS• Final color of an object is comprised of many things:

• The base object color (called a “material”) • The light color • Example: a purple light on a white surface • Any textures we apply (later)

• Materials and lights have four individual components • Diffuse color (cd and ld) • Specular color (cs and ls) • Ambient color (ca and la) • Emit color (ce and le) • cd * ld = [cd.r*ld.r , cd.g*ld.g , cd.b*ld.b] // R, G, B

63

JEFF CHASTINE

GENERAL LIGHTING• Primary vectors

• l – the incoming light vector • n – the normal of the plane/vertex • r – the reflection vector • v – the viewpoint (camera)

l n

r

θ θ

v

JEFF CHASTINE

LAMBERTIAN REFLECTANCE(DIFFUSE COMPONENT)

l n

θ

JEFF CHASTINE

LAMBERTIAN REFLECTANCE(DIFFUSE COMPONENT)

l n

θ

Note: final_colordiffuse has R, G, B

scalar 3 parts (R, G, B)

JEFF CHASTINE

LAMBERTIAN REFLECTANCE(DIFFUSE COMPONENT)

l n

θ

JEFF CHASTINE

BLINN-PHONG REFLECTION(SPECULAR COMPONENT)

• Describes the specular highlight and is dependent on viewpoint v • Also describes a “half-vector” h that is halfway between v and l

l

n

r

θ θ

v

h

JEFF CHASTINE

BLINN-PHONG REFLECTION(SPECULAR COMPONENT)

l

n

r

θ θ

v

h

Note: vectors should be normalized

JEFF CHASTINE

BLINN-PHONG REFLECTION(SPECULAR COMPONENT)

l

n

r

θ θ

v

h

JEFF CHASTINE

s = ~1 s = ~30 s = ~255

JEFF CHASTINE

AMBIENT AND EMIT COMPONENTS•

JEFF CHASTINE

FINAL COLOR• To determine the final color (excluding textures) we sum up all components:

http://en.wikipedia.org/wiki/Phong_reflection_model

final_colordiffuse final_colorspecular final_colorambient final_coloremit

final_color

+

JEFF CHASTINE

WHAT ABOUT MULTIPLE LIGHTS?•

Recommended