Computer Graphics
Computer Science & Information Technology Co p te Sc e ce & o at o Tec o ogy Yung-Yu Chuang
2009/03/2 2009/03/27
Introduction
• Instructor: Yung-Yu Chuang (莊永裕)
E il i d
• E-mail: cyy@csie.ntu.edu.tw
• Office: CSIE 527
• Grading: exam on the final exam week
What is computer graphics ?
• Definition
h i i l
h i
f l i i – the pictorial synthesis of real or imaginaryobjects from their computer-based models
OUTPUT
descriptions images
descriptions Computer Graphics
NPUT
images Computer Vision Image Processing
IN
Computer graphics
• Create a 2D image/animation of a 3D world
Applications
• Movies
I i i
• Interactive entertainment
• Industrial design
• Architecture
• Culture heritage
• Culture heritage
Computer graphics
modeling rendering
animation
Modeling
A simple example
# vertices 0 0 0 0 0 0
(0,0) (1.5,0)
0.0, 0.0, 0.0 1.5, 0.0, 0.0 0 0 1 5 0 0 0.0, 1.5, 0.0 1.5, 1.5, 0.0
# triangles 0 2 1 0, 2, 1 1, 2, 3
(0,1.5) (1.5,1.5)
x z
y
The power of triangles
• Every thing can be represented by triangles to a degree of precision
degree of precision.
20 triangles 80 triangles 320 triangles
More complex examples
a real buddha 4K mesh rendered 2.4M mesh
Modeling
• The position of the model can be acquired by 3D scanner or made by artists using modeling 3D scanner or made by artists using modeling tools.
Th th f ti
• There are other ways for representing geometric objects, but triangles have many
d t
advantages.
Triangle meshes
{f
{f } {} { }}
{f
{f11} : { v} : { v11, v, v22 , v, v33}}
{f
{f22} : { v} : { v33, v, v22 , v, v44}} connectivityconnectivity
…
…
geometry geometry {v
{v11} : (x,y,z)} : (x,y,z) {v
{v22} : (x,y,z)} : (x,y,z)
…
…
face attributes face attributes {f
{f11} : } : “skin material”“skin material”
{f
{f } :} : “brown hair”“brown hair”
{v
{v ff } : (n} : (n nn nn ) (u v)) (u v) {f
{f22} : } : brown hairbrown hair
…
… {v
{v22,f,f11} : (n} : (nxx,n,nyy,n,nzz) (u,v)) (u,v) {v
{v22,f,f22} : (n} : (nxx,n,nyy,n,nzz) (u,v)) (u,v)
…
…
corner attributes corner attributes
Copyright©1998, Microsoft
Composition of a scene
z x yy
x z
x y
Graphics pipeline
Transformations
Representation
Representation 2D transformations
Identity Scaling
Scaling Reflection
Shearing Rotation
Limitations of a 2X2 matrix
• Scaling
R i
• Rotation
• Reflection
• Shearing
• What do we miss?
Homogeneous coordinate
Translation 3D scaling
3D translation 3D rotation
3D shearing Graphics pipeline
Projections
Imaging with the synthetic camera
Specifying a viewer Projections
Parallel and perspective projections
orthographic perspective
Orthographic transformation
Perspective projection Perspective transform
Graphics pipeline review
Triangle meshes
{f
{f } {} { }}
{f
{f11} : { v} : { v11, v, v22 , v, v33}}
{f
{f22} : { v} : { v33, v, v22 , v, v44}} connectivityconnectivity
…
…
geometry geometry {v
{v11} : (x,y,z)} : (x,y,z) {v
{v22} : (x,y,z)} : (x,y,z)
…
…
face attributes face attributes {f
{f11} : } : “skin material”“skin material”
{f
{f } :} : “brown hair”“brown hair”
{v
{v ff } : (n} : (n nn nn ) (u v)) (u v) {f
{f22} : } : brown hairbrown hair
…
… {v
{v22,f,f11} : (n} : (nxx,n,nyy,n,nzz) (u,v)) (u,v) {v
{v22,f,f22} : (n} : (nxx,n,nyy,n,nzz) (u,v)) (u,v)
…
…
corner attributes corner attributes
Copyright©1998, Microsoft
Review of graphics pipeline
Transformation
Review of graphics pipeline
Projection & clipping
Review of graphics pipeline
• Rasterization Vi ibili
• Visibility
Visibility (Hidden surface removal)
Hidden surface removal
• Determining what to render at each pixel.
A i i i ibl if h i di li f
• A point is visible if there exists a direct line-of- sight to it, unobstructed by another other
bj t ( i ibl f
d t i ti )
objects (visible surface determination).• Moreover, some objects may be invisible because there are behind the camera, outside of the field-of-view, too far away (clipping) or back faced (backface culling).
Hidden surfaces: why care?
• Occlusion: Closer (opaque) objects along same viewing ray obscure more distant ones
viewing ray obscure more distant ones
• Reasons for removal
Effi i A ith – Efficiency: As with
clipping, avoid wasting work on invisible work on invisible objects
– Correctness: The image Correctness: The image will look wrong if we don’t model occlusion properly
Hidden surface removal algorithms
• Painter’s algorithm
• Binary space partitioning
• Z-buffer
• Ray casting
• And many others
• And many others
Painter’s algorithm
Draw primitives from back to from back to front to avoid need for depth comparisons
from Shirley
Painter’s algorithm
• Idea: Sort primitives by minimum depth, then rasterize from furthest to nearest
rasterize from furthest to nearest
• When there are depth overlaps, do more tests
f b di ll
of bounding areas, etc. to see one actually occludes the other
• Cyclical overlaps are a problemy p p
Z-buffer algorithm
• Resolve depths at the pixel level
Idea: add Z to frame buffer when a pixel is
• Idea: add Z to frame buffer, when a pixel is drawn, check whether it is closer than what’s already in the framebuffer
already in the framebuffer
• Proposed by Ed Catmull in 1975, widely used today especially in hardware
today, especially in hardware.
• Z-buffer texture subdivsion
• Z-buffer, texture, subdivsion surface, RenderMan
• Co-founder of PixarCo founder of Pixar
• 3 Oscars (1993, 1996, 2001),
SIGGRAPH Steven Coons Award (1993)( )
Z-buffer algorithm Z-buffer algorithm
The z-Buffer Algorithm
+ =
+ =
Z-buffer: example
color buffer depth bufferp
Z-Buffer
• Benefits
E t i l t
– Easy to implement
– Works for any geometric primitive
P ll l ti i h d (i d d t f
– Parallel operation in hardware (independent of order of polygon drawn)
Li it ti
• Limitations
– Memory required for depth buffer – Quantization and aliasing artifacts – Overfill
– Transparency does not work well
Clipping (view frustum culling)
Review of graphics pipeline
• Rasterization Vi ibili
• Visibility
Review of graphics pipeline
• Shading
Shading
Z-buffer algorithm
What is normal? Normal for a triangle
plane n ·(p - v0) = 0 n v2
plane n (p v0) 0 n = (v2- v0) ×(v1- v0)
v1 ( 2 0 ) ( 1 0 )
normalize n ← n/ |n|
p v0
normalize n ← n/ |n|
N t th t i ht h d l d t i t d f Note that right-hand rule determines outward face
Using average normals
N ( i ) l
N = true (geometric) normal
Using average normals
N N N N 2 N 1
Using average normals
N N N = 1 ( N N
1+
2) N N 2
N (
1 2)
2 N N
N 1
Using average normals
( N
1N
2N
3N
4)
N
=+ + +
1 2 3 4
N
υ=N
+N
+N
+N
M ll
N
∑
nMore generally,
N
1N
υ∑
=
i=N
iN
1N N
2υ
∑
=
nN
iN
υ 2N
3N
4=
i 1
N
3It l b ight d It can also be area-weighted.
Definitions of Triangle Meshes
{f
{f } {} { }}
{f
{f11} : { v} : { v11 , v, v22, v, v33 }}
{f
{f22} : { v} : { v33 , v, v22, v, v44 }} connectivityconnectivity
…
…
geometry geometry {v
{v11} : (x,y,z)} : (x,y,z) {v
{v22} : (x,y,z)} : (x,y,z)
…
…
face attributes face attributes {f
{f11} : } : “skin material”“skin material”
{f
{f } :} : “brown hair”“brown hair”
{v
{v ff } : (n} : (n nn nn ) (u v)) (u v) {f
{f22} : } : brown hairbrown hair
…
… {v
{v22,f,f11} : (n} : (nxx,n,nyy,n,nzz) (u,v)) (u,v) {v
{v22,f,f22} : (n} : (nxx,n,nyy,n,nzz) (u,v)) (u,v)
…
…
corner attributes corner attributes
Copyright©1998, Microsoft
Illumination (shading) models
• Interaction between light sources and objects in scene that results in perception of intensity in scene that results in perception of intensity and color at eye
• Local
vs global models• Local
vs. global models– Local: perception of a particular primitive only depends on light sources directly affecting that one p g y g primitive
• Geometry
Material properties
• Material properties
• Shadows cast (global?)
– Global: also take into account indirect effects on light of other objects in the scene
• Light reflected/refracted I di t li hti
• Indirect lighting
Local vs. global models
Direct lighting
Direct lighting Indirect lighting Indirect lighting Direct lighting
Direct lighting Indirect lighting Indirect lighting
Setup
v
Lv
E• Point P on a surface through a pixel p
• Normal N at P
• Lighting directionLighting direction
v
L• Viewing direction
• Compute color L for pixel p
v
Lv
E• Compute color L for pixel p
Surface types
• The smoother a surface, the more reflected light is concentrated in the direction a perfect mirror would concentrated in the direction a perfect mirror would reflected the light
• A very rough surface scatters light in all directionsy g g
smooth surface rough surface
smooth surface g
Basics of local shading
• Diffuse reflection
li ht h l d b bj t l
– light goes everywhere; colored by object color
• Specular reflection
– happens only near mirror configuration; usually white
• Ambient reflection
– constant accounted for other source of illumination
Ambient shading
• add constant color to account for disregarded illumination and fill in black shadows; a cheap illumination and fill in black shadows; a cheap hack.
ambient lightg
Diffuse shading
• Assume light reflects equally in all directions
Th f f l k l f ll i
– Therefore surface looks same color from all views;
“view independent”
Diffuse shading
• Illumination on an oblique surface is less than on a normal one (Lambertian cosine law)
on a normal one (Lambertian cosine law)
– Generally, illumination falls off as cosθ
Diffuse shading (Gouraud 1971)
• Applies to diffuse, Lambertian or matte surfaces
surfaces
(albedo)
( )
Diffuse shading
0.4 0.55 0.7 0.85 1.0
diffuse-reflection model with different kd
0.0 0.15 0.3 0.45 0.6
ambient and diffuse-reflection model with differentka
and Ia =Ip =1.0,kd =0.4
Diffuse shading
For color objects, apply the formula for each color channel separately
color channel separately
Specular shading
• Some surfaces have highlights, mirror like reflection; view direction dependent;
reflection; view direction dependent;
especially for smooth shinny surfaces
Specular shading (Phong 1975)
• Also known as glossy, rough specular and
directional diffuse reflection
directional diffuse reflection
Specular shading
• Fall off gradually from the perfect reflection direction
direction
Specular shading
• Increasing n narrows the lobe
σ cosn
n=1 n=2 n=2 n=3
°
0 90 °
° 90
n=10
°
0 90 °
°
− 90
Specular shading
ks
1 . 0
25 . 0
5 . 0
0 .
=3
n n=5.0 n=10.0 n=27.0 n=200.0
Specular shading
diffuse diffuse + specular diffuse diffuse + specular
Put it all together
• Include ambient, diffuse and specular
• Sum over many lights
• Sum over many lights
Choosing the parameters
Computing lighting at each pixel
• Most accurate approach: Compute component illumination at each pixel with individual illumination at each pixel with individual positions, light directions, and viewing directions
directions
• But this could be expensive...
y y1
ys Ip
Scan line y2
y3
Shading models for polygons
• Flat Shading
F t d Sh di – Faceted Shading – Constant Shading
d h d
• Gouraud Shading
– Intensity Interpolation Shading – Color Interpolation Shading
• Phong Shadingg g
– Normal-Vector Interpolation Shading
Flat Shading
• Compute constant shading function, over each polygon, p yg
• Same normal and light vector across whole polygonp yg
• Constant shading for polygon
I1
y s
I I
p =I2
I
s
I3
Ip
Intensity Interpolation (Gouraud)
1 2 2 1
y I y
y I y
I
a=
s− + −
s2 1 2 2 1
1
y y y y
a
− −
I a
Ib I1
3 1 1 3 3 1
3
1
y y
y I y
y y
y I y
I
b s s− + −
−
= − a
bI
y s
3 1 3
1
y y y
y
a p p
b
x x x
x
− −I
s
a b
a p b a b
p b a
p
I x x
x I x
I
+ −= − I2
I3
Ip
Normal Interpolation (Phong)
1
2
y y
y N N y
N
s− −
s N12 1 1 2 2 1
2
1
y y
y N y
y y
y N y
N
a s s+ −
= −
N a
Nb
1
y s
3 1 1 3 3 1
3
1
y y
y N y
y y
y N y
N
b s s− + −
−
= −
N2
s
3 N
1 3
1 2
N3
Np
Normal Interpolation (Phong)
⎥ ⎤
⎢ ⎡ −
⎥ ⎤
⎢ ⎡ − x
bx
pN
bx
px
aN N
~
⎥ ⎦
⎢ ⎣ −
⎥ +
⎢ ⎦
= ⎣ −
a b
a p b
b a
b p b a
p a
x N x
N x
N x N N
⎦
⎣
⎦
⎣ N
~
N p p
N N
=
~Normalizing makes
thi it t
Np
this a unit vector
Gouraud v.s. Phong Shading
Gouraud Phong Gouraud Phong
Flat shading
Gouraud shading Phong shading
Graphics Pipeline
Triangle meshes
{f
{f } {} { }}
{f
{f11} : { v} : { v11, v, v22 , v, v33}}
{f
{f22} : { v} : { v33, v, v22 , v, v44}} connectivityconnectivity
…
…
geometry geometry {v
{v11} : (x,y,z)} : (x,y,z) {v
{v22} : (x,y,z)} : (x,y,z)
…
…
face attributes face attributes {f
{f11} : } : “skin material”“skin material”
{f
{f } :} : “brown hair”“brown hair”
{v
{v ff } : (n} : (n nn nn ) (u v)) (u v) {f
{f22} : } : brown hairbrown hair
…
… {v
{v22,f,f11} : (n} : (nxx,n,nyy,n,nzz) (u,v)) (u,v) {v
{v22,f,f22} : (n} : (nxx,n,nyy,n,nzz) (u,v)) (u,v)
…
…
corner attributes corner attributes
Copyright©1998, Microsoft
Review of graphics pipeline
Transformation
Review of graphics pipeline
Projection & clipping
Review of graphics pipeline
• Rasterization Vi ibili
• Visibility
Review of graphics pipeline
• Shading
Animation
Hierarchical modeling: a robot arm
Hierarchical modeling Animator demos
Videos
•
TigerWang R i
•
Racing
Advanced topics
Global illumination Complex materials
Realistic motion Graphics hardware
Nvidia GT200 GPU 200 cores
Animation production
Animation production pipeline
t t t t t t t b d
story text treatment storyboard
voice storyreal look and feel
Animation production pipeline
l t animation
d li / ti l ti layout animation
modeling/articulation
shading/lighting rendering final touch