程序代写代做代考 cse3431-lecture12-illumination

cse3431-lecture12-illumination

Illumination

Local Illumination physics
Law of reflection and Snell’s law of
refraction

What are we trying to model ?

• Keep things simple and computationally efficient
• Sufficient expressive power for a wide range of

materials

Surface

Text

Lambert’s cosine law

n
dB

dA

How do we model specular
reflection?

We want a
• simple,
• efficient,
• and intuitive

model

Computing R
• Convention L towards light
• n,R,L unit vectors

R = (n⋅L)n+S
S = (n⋅L)n – L

substituting we get
R = 2(n⋅L)n – L

L R

n

a a

S

Computing R
• Convention L towards light
• n,R,L unit vectors

R = (n⋅L)n+S
S = (n⋅L)n – L

substituting we get
R = 2(n⋅L)n – L
Sanity check: we can visualize what we computed for
R as the doted vector

-L R

n

a a

S

The effect of the exponent n

cos(a)
cos4(a)
cos50(a)

Comparison

Agrees better with experimental results
Halfway vector H between L,V

It measures how far from the normal N, H is, which is
somewhat similar to how far from the V, R is.

The Blinn-Torrance Specular Model

N
H

V

L

✓ ✓


Is = IiKspec(H ·N)n

Is = IiKspec cos(a)
n

R

Advantages of the Blinn Specular
Model

• Theoretical basis
• No need to compute reflective direction R
• N·H cannot be negative if


N·L>0 and N·V>0
• If the light is directional and


we have orthographic projection

then N*H constant

: ADS – Lighting

Lights and materials
ObjectColorr = Ir= Ia,r Ka,r + Ii,r Kdiff,r(N·L)+Ii,r Kspec,r (R·V)n

ObjectColorg = Ig=Ia,gKa,g + Ii,gKdiff,g(N·L)+Ii,gKspec,g(R·V)n

ObjectColorb = Ib= Ia,bKa,b + Ii,bKdiff,b(N·L)+Ii,bKspec,b(R·V)n

Material properties:
Ka,Kdiff,Kspec,n

Light properties
Ia,Idiff,Ispec

Questions
If you shine red light (1,0,0) to a white object
what color does the object appear to have?

Questions
If you shine red light (1,0,0) to a white object
what color does the object appear to have?

• Red: ~(1,0,0)*(1,1,1) = ~(1,0,0)
• May not be exactly (1,0,0) but it would be a shade of

red

Questions
What if you shine red light (1,0,0) to a green
object (0,1,0) ?

Questions
What if you shine red light (1,0,0) to a green
object (0,1,0) ?

• Object will look black

Questions

What is the color of the highlight?

Questions

What is the color of the highlight?

• For non-metallic materials it is the color of the light
• For certain metallic materials it is the color of the

material

Special cases
ObjectColorr = Ir= Ia,r Ka,r + Ii,r Kdiff,r(N·L)+Ii,r Kspec,r (R·V)n

ObjectColorg = Ig=Ia,gKa,g + Ii,gKdiff,g(N·L)+Ii,gKspec,g(R·V)n

ObjectColorb = Ib= Ia,bKa,b + Ii,bKdiff,b(N·L)+Ii,bKspec,b(R·V)n

• What should be done if Ir,b,g >1? 


Special cases
ObjectColorr = Ir= Ia_rKa_r + Ii_rKdiff_r(N·L)+Ii_rKspec_r(R·V)n

ObjectColorg = Ig=Ia_gKa_g + Ii_gKdiff_g(N·L)+Ii_gKspec_g(R·V)n

ObjectColorb = Ib= Ia_bKa_b + Ii_bKdiff_b(N·L)+Ii_bKspec_b(R·V)n

• What should be done if Ir,b,g >1?
• This is an important issue that falls under the general

notion of tone mapping.
• 


– Clamp the value of I to one. Problem? (10,1,1) -> (1,1,1)

– Scale so that maximum becomes 1? (10,1,1) –> (1,0.1,0.1)

– Scale non-linearly?

Special cases
ObjectColorr = Ir= Ia_rKa_r + Ii_rKdiff_r(N·L)+Ii_rKspec_r(R·V)n

ObjectColorg = Ig=Ia_gKa_g + Ii_gKdiff_g(N·L)+Ii_gKspec_g(R·V)n

ObjectColorb = Ib= Ia_bKa_b + Ii_bKdiff_b(N·L)+Ii_bKspec_b(R·V)n

• What should be done if N*L < 0?
 Clamp the value of I to zero or flip the normal. Special cases ObjectColorr = Ir= Ia_rKa_r + Ii_rKdiff_r(N·L)+Ii_rKspec_r(R·V)n ObjectColorg = Ig=Ia_gKa_g + Ii_gKdiff_g(N·L)+Ii_gKspec_g(R·V)n ObjectColorb = Ib= Ia_bKa_b + Ii_bKdiff_b(N·L)+Ii_bKspec_b(R·V)n • How can we handle multiple light sources?
 Sum the intensity of the individual contributions. Apply the ADS model in the vertex shader
 Tell the rasterizer not to interpolate per pixel (-- keyword “flat” find out the details on your own) Apply the ADS lighting model in the vertex shader Default interpolation Apply ADS, in the fragment shader with the interpolated normal per pixel Examples of Phong Illuminated materials IMPORTANT: Which coordinate system? In which system do we normally do the lighting calculations • Viewing coordinate system Shader based ADS Lighting Per vertex Per pixel Per vertex ADS lighting Vertex Shader applies the Phong illumination model per vertex Fragment shader receives the interpolated colour from the rasterizer Per vertex ADS lighting Vertex Shader // Parameters attribute vec4 vPosition; attribute vec3 vNormal; attribute vec4 vColor ; attribute vec2 vTexCoord ; uniform vec4 ambientProduct, diffuseProduct, specularProduct; uniform vec4 lightPosition; uniform float shininess; varying vec4 fColor; Per vertex ADS lighting Vertex Shader uniform vec4 ambientProduct, diffuseProduct, specularProduct; uniform vec4 lightPosition; uniform float shininess; void main() { // Transform vertex position into eye coordinates vec3 pos = (modelViewMatrix * vPosition).xyz; // Transform vertex normal into eye coordinates vec3 N = normalize( (normalMatrix*vec4(vNormal,0.0)).xyz); // Outputs fColor = ads(pos, lightPosition.xyz, N); // Anything interesting // about light’s position? gl_Position = projectionMatrix * modelViewMatrix*vPosition ; } Homework Modify what is needed so that the light is stationary in the word coordinate system Per vertex ADS lighting Vertex Shader vec4 ads(vec3 pos, vec3 lpos, vec3 N) { vec3 L = normalize(lpos - pos) ; vec3 V = normalize(-pos) ; // why? vec3 R = reflect(-L, N) ; // Compute terms in the illumination equation vec4 ambient = ambientProduct; float Kd = max( dot(L, N), 0.0 ); vec4 diffuse = vec4(0.0, 0.0, 0.0, 1.0); vec4 specular = vec4(0.0, 0.0, 0.0, 1.0); diffuse = Kd*diffuseProduct; float Ks = pow( max(dot(R, V), 0.0), shininess ); specular = Ks * specularProduct; if( dot(L, N) < 0.0 ) { specular = vec4(0.0, 0.0, 0.0, 1.0); } vec4 color = ambient + diffuse + specular; color.a = 1.0 ; // WHY?? return color ; } Per vertex ADS lighting Fragment Shader varying vec4 fColor; void main() { gl_FragColor = fColor; } Per fragment ADS lighting Vertex shader outputs the necessary information to the fragment shader Fragment Shader receives the interpolated information and applies the Phong illumination model per fragment What is this “necessary” information? Per fragment ADS lighting Vertex shader outputs the necessary information to the fragment shader Fragment Shader receives the interpolated information and applies the Phong illumination model per fragment What is this “necessary” information? • Remember the fragment shader does not have access to vertex attributes Per fragment ADS lighting Vertex Shader attribute vec4 vPosition; attribute vec3 vNormal; attribute vec4 vColor ; uniform mat4 modelViewMatrix; uniform mat4 normalMatrix; uniform mat4 projectionMatrix; uniform vec4 lightPosition; varying vec3 fPos ; // vertex position in eye coords varying vec3 fLpos ; // light position in eye coords varying vec3 fN ; // vertex normal in eye coords void main() { Per fragment ADS lighting Vertex Shader varying vec3 fPos ; // vertex position in eye coords varying vec3 fLpos ; // light position in eye coords varying vec3 fN ; // vertex normal in eye coords void main() { // Transform vertex position into eye coordinates fPos = (modelViewMatrix * vPosition).xyz; //transform normal in eye coordinates fN = normalize( (normalMatrix*vec4(vNormal,0.0)).xyz); // pass through light position fLpos = lightPosition.xyz ; // Transform vertex position in clip coordinates gl_Position = projectionMatrix * modelViewMatrix * vPosition; } Per fragment ADS lighting Fragment Shader precision mediump float; uniform vec4 ambientProduct, diffuseProduct, specularProduct; uniform float shininess; varying vec3 fPos ; varying vec3 fLpos ; varying vec3 fN ; varying vec2 fTexCoord ; void main() { gl_FragColor = ads(fPos,fLpos,fN) ; } Per fragment ADS lighting Fragment Shader // EXACTLY the same as in the case of per vertex ADS vec4 ads(vec3 pos, vec3 lpos, vec3 N) { vec3 L = normalize(lpos - pos) ; vec3 V = normalize(-pos) ; // why? vec3 R = reflect(-L, N) ; // Compute terms in the illumination equation vec4 ambient = ambientProduct; float Kd = max( dot(L, N), 0.0 ); vec4 diffuse = vec4(0.0, 0.0, 0.0, 1.0); vec4 specular = vec4(0.0, 0.0, 0.0, 1.0); diffuse = Kd*diffuseProduct; float Ks = pow( max(dot(R, V), 0.0), shininess ); specular = Ks * specularProduct; if( dot(L, N) < 0.0 ) { specular = vec4(0.0, 0.0, 0.0, 1.0); } vec4 color = ambient + diffuse + specular; color.a = 1.0 ; return color ; } Homework What do you need to do to support multiple lights?