Introduction to Computer Graphics
GAMES101, Lingqi Yan, UC Santa Barbara
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
- Upside down? No problem
- Active discussions in the BBS, pretty good
• Next homework is for shading
• Today’s topics
- Easy, but a lot
GAMES101 2 Lingqi Yan, UC Santa Barbara
Last Lecture
• Shading 1
- Blinn-Phong reflectance model
^ ^
- Diffuse l n
v^
- Specular
- Ambient
- At a specific shading point
GAMES101 3 Lingqi Yan, UC Santa Barbara
Today
• Shading 2
- Blinn-Phong reflectance model
- Specular and ambient terms
- Shading frequencies
- Graphics pipeline
- Texture mapping
- Barycentric coordinates
GAMES101 4 Lingqi Yan, UC Santa Barbara
Recap: Lambertian (Diffuse) Term
Shading independent of view direction
energy arrived
at the shading point
l n 2
Ld = kd (I/r ) max(0, n · l)
v
diffuse energy received
coefficient by the shading point
(color)
diffusely
reflected light
GAMES101 5 Lingqi Yan, UC Santa Barbara
Recap: Lambertian (Diffuse) Term
Produces diffuse appearance
[Foley et al.]
kd
GAMES101 6 Lingqi Yan, UC Santa Barbara
Specular Term (Blinn-Phong)
Intensity depends on view direction
• Bright near mirror reflection direction
R
l n
v
GAMES101 7 Lingqi Yan, UC Santa Barbara
Specular Term (Blinn-Phong)
V close to mirror direction half vector near normal
• Measure “near” by dot product of unit vectors
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
Increasing p narrows the reflection lobe
[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
GAMES101 11 Lingqi Yan, UC Santa Barbara
Blinn-Phong Reflection Model
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)
GAMES101 12 Lingqi Yan, UC Santa Barbara
Questions?
Shading Frequencies
Shading Frequencies
What caused the shading difference?
GAMES101 15 Lingqi Yan, UC Santa Barbara
Shade each triangle (flat shading)
Flat shading
• Triangle face is
flat — one normal
vector
• Not good for
smooth surfaces
GAMES101 16 Lingqi Yan, UC Santa Barbara
Shade each vertex (Gouraud shading)
Gouraud shading
• Interpolate colors
from vertices across
triangle
• Each vertex has a
normal vector (how?)
GAMES101 17 Lingqi Yan, UC Santa Barbara
Shade each pixel (Phong shading)
Phong shading
• Interpolate normal
vectors across each
triangle
• Compute full shading
model at each pixel
• Not the Blinn-Phong
Reflectance Model
GAMES101 18 Lingqi Yan, UC Santa Barbara
Shading Frequency: Face, Vertex or Pixel
Num
Vertices
Shading freq. : Face Vertex Pixel
Shading type : Flat Gouraud Phong
Image credit: Happyman, http://cg2010studio.com/
Defining Per-Vertex Normal Vectors
Best to get vertex normals from
the underlying geometry
• e.g. consider a sphere
Otherwise have to infer vertex
normals from triangle faces
• Simple scheme: average
surrounding face normals
GAMES101 20 Lingqi Yan, UC Santa Barbara
Defining Per-Pixel Normal Vectors
Barycentric interpolation (introducing soon)
of vertex normals
Don’t forget to normalize the interpolated directions
GAMES101 21 Lingqi Yan, UC Santa Barbara
Graphics (Real-time Rendering)
Pipeline
Graphics Pipeline
3
1
Application 4 Input: vertices in 3D space
2
Vertex Processing
Vertex Stream Vertices positioned in screen space
Triangle Processing
Triangle Stream Triangles positioned in screen space
Rasterization
Fragment Stream Fragments (one per covered sample)
Fragment Processing
Shaded Fragments Shaded fragments
Framebuffer Operations
Display Output: image (pixels)
GAMES101 23 Lingqi Yan, UC Santa Barbara
Graphics Pipeline
Application
Vertex Processing Model, View, Projection transforms
Vertex Stream
Triangle Processing y
Triangle Stream
Rasterization z
Fragment Stream x
Fragment Processing
Shaded Fragments
Framebuffer Operations
Display
GAMES101 24 Lingqi Yan, UC Santa Barbara
Graphics Pipeline
Application
Vertex Processing Sampling triangle coverage
Vertex Stream
Triangle Processing
Triangle Stream
Rasterization
Fragment Stream
Fragment Processing
Shaded Fragments
Framebuffer Operations
Display
GAMES101 25 Lingqi Yan, UC Santa Barbara
Rasterization Pipeline
Application
Vertex Processing Z-Buffer Visibility Tests
Vertex Stream
Triangle Processing
Triangle Stream
Rasterization
Fragment Stream
Fragment Processing
Shaded Fragments
Framebuffer Operations
Display
GAMES101 26 Lingqi Yan, UC Santa Barbara
Graphics Pipeline
Application
Vertex Processing Shading
Vertex Stream
Triangle Processing
Triangle Stream
Rasterization
Ambient + Diffuse
Fragment Stream
Fragment Processing
Shaded Fragments
Framebuffer Operations
+ Specular = Blinn-Phong
Display
Reflectance Model
GAMES101 27 Lingqi Yan, UC Santa Barbara
Graphics Pipeline
Application Texture mapping
Vertex Processing (introducing soon)
Vertex Stream
Triangle Processing
Triangle Stream
Rasterization
Fragment Stream
Fragment Processing
Shaded Fragments
Framebuffer Operations
Display
GAMES101 28 Lingqi Yan, UC Santa Barbara
Shader Programs
• Program vertex and fragment processing stages
• Describe operation on a single vertex (or fragment)
Example GLSL fragment shader program
uniform sampler2D myTexture; • Shader function executes
uniform vec3 lightDir; once per fragment.
varying vec2 uv;
• Outputs color of surface
varying vec3 norm;
at the current fragment’s
screen sample position.
void diffuseShader()
{ • This shader performs a
vec3 kd; texture lookup to obtain
kd = texture2d(myTexture, uv); the surface’s material
kd *= clamp(dot(–lightDir, norm), 0.0, 1.0); color at this point, then
gl_FragColor = vec4(kd, 1.0);
performs a diffuse
lighting calculation.
}
Shader Programs
• Program vertex and fragment processing stages
• Describe operation on a single vertex (or fragment)
Example GLSL fragment shader program
uniform sampler2D myTexture; // program parameter
uniform vec3 lightDir; // program parameter
varying vec2 uv; // per fragment value (interp. by rasterizer)
varying vec3 norm; // per fragment value (interp. by rasterizer)
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
Inigo Quilez, https://youtu.be/XuSnLbB1j6E
Goal: Highly Complex 3D Scenes in Realtime
• 100’s of thousands to millions of triangles in a scene
• Complex vertex and fragment shader computations
• High resolution (2-4 megapixel + supersampling)
• 30-60 frames per second (even higher for VR)
Unreal Engine Kite Demo (Epic Games 2015)
Graphics Pipeline Implementation: GPUs
Specialized processors for executing graphics pipeline
computations
Discrete GPU Card Integrated GPU:
(NVIDIA GeForce Titan X) (Part of Intel CPU die)
GAMES101 34 Lingqi Yan, UC Santa Barbara
GPU: Heterogeneous, Multi-Core Procesor
Modern GPUs offer ~2-4 Tera-FLOPs of performance for
executing vertex and fragment shader programs
Tera-Op’s of fixed-function
compute capability over here
Texture Mapping
Different Colors at Different Places?
L_d = k_d * (I / r^2) * (n dot l)
Pattern on ball Wood grain on floor
GAMES101 37 Lingqi Yan, UC Santa Barbara
Surfaces are 2D
Surface lives in 3D world space
Every 3D surface point also has a place
where it goes in the 2D image (texture).
y
x
ys v
xs z u
image space world space texture space
Screen space World space Texture space
GAMES101 38 Lingqi Yan, UC Santa Barbara
Texture Applied to Surface
Rendering without texture Rendering with texture Texture
Zoom
Each triangle “copies” a piece of
the texture image to the surface.
GAMES101 39 Lingqi Yan, UC Santa Barbara
Visualization of Texture Coordinates
Each triangle vertex is assigned a texture coordinate (u,v)
Triangle vertices in texture space
Visualization of texture coordinates v
u
GAMES101 40 Lingqi Yan, UC Santa Barbara
Texture Applied to Surface
Rendered result Triangle vertices in texture space
v
u
GAMES101 41 Lingqi Yan, UC Santa Barbara
Textures applied to surfaces
GAMES101 42 Lingqi Yan, UC Santa Barbara
Visualization of texture coordinates
GAMES101 43 Lingqi Yan, UC Santa Barbara
Textures can be used multiple times!
example textures
used / tiled
GAMES101 44 Lingqi Yan, UC Santa Barbara
Thank you!
(And thank Prof. Ravi Ramamoorthi and Prof. Ren Ng for many of the slides!)