GAMES101 Lecture 08
GAMES101 Lecture 08
Lecture 8:
Shading 2
(Shading, Pipeline and Texture Mapping)
http://www.cs.ucsb.edu/~lingqi/teaching/games101.html
Announcements
• Homework 2
- 45 submissions so far
• Today’s topics
- Easy, but a lot
- Ambient
- Shading frequencies
- Graphics pipeline
- Texture mapping
- Barycentric coordinates
energy arrived
at the shading point
l n 2
Ld = kd (I/r ) max(0, n · l)
v
[Foley et al.]
kd
R
l n
v
h = bisector(v, l)
(半程向量)
v+l
h =
l n v+l
v
Ls = ks (I/r2 ) max(0, cos ↵)p
= ks (I/r2 ) max(0, n · h)p
specularly
reflected
light specular
coefficient
GAMES101 8 Lingqi Yan, UC Santa Barbara
Cosine Power Plots
[Foley et al.]
GAMES101 9 Lingqi Yan, UC Santa Barbara
Specular Term (Blinn-Phong)
2 p
Ls = ks (I/r ) max(0, cos ↵)
Blinn-Phong 2 pp
n · h)
Ls = ks (I/r ) max(0, cos ↵)
2 p
= ks (I/r ) max(0, n · h)
[Foley et al.]
ks
Note: showing
Ld + Ls together
p
GAMES101 10 Lingqi Yan, UC Santa Barbara
Ambient Term
Shading that does not depend on anything
• Add constant color to account for disregarded
illumination and fill in black shadows
• This is approximate / fake!
La = ka Ia
ambient
coefficient
reflected
ambient light
Blinn-Phong
Ambient + Diffuse + Specular =
Reflection
L = La + Ld + Ls
2 2 p
= ka Ia + kd (I/r ) max(0, n · l) + ks (I/r ) max(0, n · h)
Flat shading
• Triangle face is
flat — one normal
vector
• Not good for
smooth surfaces
Gouraud shading
• Interpolate colors
from vertices across
triangle
• Each vertex has a
normal vector (how?)
Phong shading
• Interpolate normal
vectors across each
triangle
• Compute full shading
model at each pixel
• Not the Blinn-Phong
Reflectance Model
Num
Vertices
Triangle Processing
Rasterization
Fragment Processing
Framebuffer Operations
Triangle Processing y
Triangle Stream
Rasterization z
Fragment Stream x
Fragment Processing
Shaded Fragments
Framebuffer Operations
Display
Triangle Processing
Triangle Stream
Rasterization
Fragment Stream
Fragment Processing
Shaded Fragments
Framebuffer Operations
Display
Triangle Processing
Triangle Stream
Rasterization
Fragment Stream
Fragment Processing
Shaded Fragments
Framebuffer Operations
Display
Triangle Processing
Triangle Stream
Rasterization
Ambient + Diffuse
Fragment Stream
Fragment Processing
Shaded Fragments
Framebuffer Operations
+ Specular = Blinn-Phong
Display
Reflectance Model
Triangle Processing
Triangle Stream
Rasterization
Fragment Stream
Fragment Processing
Shaded Fragments
Framebuffer Operations
Display
void diffuseShader()
{
vec3 kd;
kd = texture2d(myTexture, uv); // material color from texture
kd *= clamp(dot(–lightDir, norm), 0.0, 1.0); // Lambertian shading model
gl_FragColor = vec4(kd, 1.0); // output fragment color
}
Snail Shader Program
Inigo Quilez
Procedurally modeled, 800 line shader.
http://shadertoy.com/view/ld3Gz2
GAMES101 31 Lingqi Yan, UC Santa Barbara
Snail Shader Program
y
x
ys v
xs z u
image space world space texture space
Screen space World space Texture space
u
GAMES101 40 Lingqi Yan, UC Santa Barbara
Texture Applied to Surface
u
GAMES101 41 Lingqi Yan, UC Santa Barbara
Textures applied to surfaces
example textures
used / tiled