Introduction to Computer Graphics
GAMES101, Lingqi Yan, UC Santa Barbara
Lecture 9:
Shading 3 (Texture Mapping cont.)
http://www.cs.ucsb.edu/~lingqi/teaching/games101.html
Announcements
• About homework
- Homework 1 is being graded
- Homework 2
- 271 submissions so far
- Homework 3 will be released soon
GAMES101 2 Lingqi Yan, UC Santa Barbara
Last Lectures
• Shading 1 & 2
- Blinn-Phong reflectance model
- Shading models / frequencies
- Graphics Pipeline
- Texture mapping
GAMES101 3 Lingqi Yan, UC Santa Barbara
Today
• Shading 3
- Barycentric coordinates
- Texture queries
- Applications of textures
• Shadow mapping
GAMES101 4 Lingqi Yan, UC Santa Barbara
Interpolation Across Triangles:
Barycentric Coordinates
(重⼼坐标)
Interpolation Across Triangles
Why do we want to interpolate?
• Specify values at vertices
• Obtain smoothly varying values across triangles
What do we want to interpolate?
• Texture coordinates, colors, normal vectors, …
How do we interpolate?
• Barycentric coordinates
GAMES101 6 Lingqi Yan, UC Santa Barbara
Barycentric Coordinates
A coordinate system for triangles (↵, , )
C
(x, y) = ↵ A + B+ C
↵+ + =1
(x, y)
Inside the triangle if
B all three coordinates
are non-negative
A
GAMES101 7 Lingqi Yan, UC Santa Barbara
Barycentric Coordinates
What’s the barycentric coordinate of A?
C
(↵, , ) = (1, 0, 0)
(x, y) = ↵ A + B+ C
=A
A
(x, y)
GAMES101 8 Lingqi Yan, UC Santa Barbara
Barycentric Coordinates
Geometric viewpoint — proportional areas
C
AA
↵=
AA + AB + AC
AB
AB AA =
AA + AB + AC
AC
AC =
AA + AB + AC
B
GAMES101 9 Lingqi Yan, UC Santa Barbara
Barycentric Coordinates
What’s the barycentric coordinate of the centroid?
1 1 1
(↵, , ) = 3, 3, 3
1 1 1
(x, y) = 3 A+ 3 B+ 3 C
(x, y)
B
A
GAMES101 10 Lingqi Yan, UC Santa Barbara
Barycentric Coordinates: Formulas
C
(x, y) = ↵ A + B+ C
↵+ + =1
(x, y) = ↵ A + B+ C
B
A
(x xB )(yC yB ) + (y yB )(xC xB )
↵=
(xA xB )(yC yB ) + (yA yB )(xC xB )
(x xC )(yA yC ) + (y yC )(xA xC )
=
(xB xC )(yA yC ) + (yB yC )(xA xC )
=1 ↵
GAMES101 11 Lingqi Yan, UC Santa Barbara
Using Barycentric Coordinates
Linearly interpolate values at vertices
VC
V = ↵ VA + V B + VC
V VA, VB, VC can be
positions, texture
coordinates, color,
normal, depth,
VB material attributes…
VA
However, barycentric coordinates are not invariant under projection!
GAMES101 12 Lingqi Yan, UC Santa Barbara
Applying Textures
Simple Texture Mapping: Diffuse Color
Usually a pixel’s center
for each rasterized screen sample (x,y):
(u,v) = evaluate texture coordinate at (x,y)
texcolor = texture.sample(u,v);
set sample’s color to texcolor; Using barycentric
coordinates!
Usually the diffuse albedo Kd
(recall the Blinn-Phong reflectance model)
GAMES101 14 Lingqi Yan, UC Santa Barbara
Texture Magnification
(What if the texture is too small?)
Texture Magnification - Easy Case
Generally don’t want this — insufficient texture resolution
A pixel on a texture — a texel (纹理元素、纹素)
Nearest Bilinear Bicubic
GAMES101 16 Lingqi Yan, UC Santa Barbara
Bilinear Interpolation
Want to sample
texture value f(x,y) at
red point
Black points indicate
texture sample
locations
GAMES101 17 Lingqi Yan, UC Santa Barbara
Bilinear Interpolation
u01 u11
Take 4 nearest sample
locations, with texture
values as labeled.
u00 u10
GAMES101 18 Lingqi Yan, UC Santa Barbara
Bilinear Interpolation
u01 u11
And fractional offsets,
t (s,t) as shown
u00 s u10
GAMES101 19 Lingqi Yan, UC Santa Barbara
Bilinear Interpolation
Linear interpolation (1D)
lerp(x, v0 , v1 ) = v0 + x(v1 v0 )
u01 u11 u0 = lerp(s, u00 , u10)
u1 = lerp(s, u01 , u11)
u = lerp(t, u0 , u1)
t
u00 s u10
GAMES101 20 Lingqi Yan, UC Santa Barbara
Bilinear Interpolation
u1
Linear interpolation (1D)
lerp(x, v0 , v1 ) = v0 + x(v1 v0 )
u01 u11 u0 = lerp(s, u00 , u10)
u = lerp(s, u01 , u11)
Two helper lerps (horizontal)
1
u0 = lerp(s,uu=00lerp(t,
, u10 ) u0 , u1)
t
u1 = lerp(s, u01 , u11 )
u00 s u10
f (x, y) = lerp(t, u0 , u1 )
u0
GAMES101 21 Lingqi Yan, UC Santa Barbara
Bilinear Interpolation
u1
Linear interpolation (1D)
lerp(x, v0 , v1 ) = v0 + x(v1 v0 )
u01 u11 u0 = lerp(s, u00 , u10)
u = lerp(s, u01 , u11)
Two helper lerps 1
u0 = lerp(s,uu=00lerp(t,
, u10 ) u0 , u1)
t
u1 = lerp(s, u01 , u11 )
u00 s u10 u0 = lerp(s, u00 , u10 )
f (x, y) = lerp(t, u0 , u1 )
u1 =lerp,
Final vertical lerp(s, u01 , u11 )
to get result:
f (x, y) = lerp(t, u0 , u1 )
u0
GAMES101 22 Lingqi Yan, UC Santa Barbara
Texture Magnification - Easy Case
Bilinear interpolation usually gives pretty good results
at reasonable costs
Nearest Bilinear Bicubic
GAMES101 23 Lingqi Yan, UC Santa Barbara
Texture Magnification (hard case)
(What if the texture is too large?)
Point Sampling Textures — Problem
Moire
Jaggies
Reference Point sampled
GAMES101 25 Lingqi Yan, UC Santa Barbara
Screen Pixel “Footprint” in Texture
Upsampling
upsampling Downsampling
downsampling
(Magnification)
magnification (Minification)
minification
GAMES101 26 Lingqi Yan, UC Santa Barbara
Will Supersampling Do Antialiasing?
Yes! But costly!
512x supersampling
GAMES101 27 Lingqi Yan, UC Santa Barbara
Antialiasing — Supersampling?
Will supersampling work?
• Yes, high quality, but costly
• When highly minified, many texels in pixel footprint
• Signal frequency too large in a pixel
• Need even higher sampling frequency
Let’s understand this problem in another way
• What if we don’t sample?
• Just need to get the average value within a range!
GAMES101 28 Lingqi Yan, UC Santa Barbara
Point Query vs. (Avg.) Range Query
upsampling downsampling
magnification minification
GAMES101 29 Lingqi Yan, UC Santa Barbara
Different Pixels -> Different-Sized Footprints
GAMES101 30 Lingqi Yan, UC Santa Barbara
Mipmap
Allowing (fast, approx., square) range queries
Mipmap (L. Williams 83)
“Mip” comes from the Latin “multum in parvo", meaning a multitude in a small space
Level 0 = 128x128 Level 1 = 64x64 Level 2 = 32x32 Level 3 = 16x16
Level 4 = 8x8 Level 5 = 4x4 Level 6 = 2x2 Level 7 = 1x1
GAMES101 32 Lingqi Yan, UC Santa Barbara
Mipmap (L. Williams 83)
D
D=2
D=1
v D=0
u
“Mip hierarchy”
level = D
What is the storage overhead of a mipmap?
GAMES101 33 Lingqi Yan, UC Santa Barbara
Computing Mipmap Level D
u
Screen space (x,y) Texture space (u,v)
Estimate texture footprint using texture coordinates of
neighboring screen samples
GAMES101 34 Lingqi Yan, UC Santa Barbara
Computing Mipmap Level D
(u,v)01
v (u,v)00 (u,v)10 du/dx
dv/dx
L
u
0s s 1
✓ ◆ 2 ✓ ◆2 ✓ ◆2 ✓ ◆ 2
du dv du dv A
D = log2 L L = max @ + , +
dx dx dy dy
GAMES101 35 Lingqi Yan, UC Santa Barbara
Computing Mipmap Level D
(u,v)01
v (u,v)00 (u,v)10 du/dx
L dv/dx
L
u
0s s 1
✓ ◆ 2 ✓ ◆2 ✓ ◆2 ✓ ◆ 2
du dv du dv A
D = log2 L L = max @ + , +
dx dx dy dy
GAMES101 36 Lingqi Yan, UC Santa Barbara
Visualization of Mipmap Level
D rounded to nearest integer level
GAMES101 37 Lingqi Yan, UC Santa Barbara
Trilinear Interpolation
Mipmap Level D Mipmap Level D+1
Bilinear result Bilinear result
Linear interpolation based on continuous D value
GAMES101 38 Lingqi Yan, UC Santa Barbara
Visualization of Mipmap Level
Trilinear filtering: visualization of continuous D
GAMES101 39 Lingqi Yan, UC Santa Barbara
Mipmap Limitations
Point sampling
GAMES101 40 Lingqi Yan, UC Santa Barbara
Mipmap Limitations
Supersampling 512x (assume this is correct)
GAMES101 41 Lingqi Yan, UC Santa Barbara
Mipmap Limitations
Overblur
Why?
Mipmap trilinear sampling
GAMES101 42 Lingqi Yan, UC Santa Barbara
Anisotropic Filtering
Better than Mipmap!
GAMES101 43 Lingqi Yan, UC Santa Barbara
Irregular Pixel Footprint in Texture
image space texture space
Screen space Texture space
GAMES101 44 Lingqi Yan, UC Santa Barbara
Anisotropic Filtering
Ripmaps and summed area tables
• Can look up axis-aligned
rectangular zones
• Diagonal footprints still a problem
Wikipedia
GAMES101 45 Lingqi Yan, UC Santa Barbara
space the contours of Q are concentric ellipses (Figure 8), Q = (C*V+B*U)*V+A*U*U
but when mapped to screen space, they are nearly circu- for u=ul to u2 do begin
lar. Since Q is parabolic it is proportional to r2, where r is 1* ignore pixel if Q out of range *1
if Q<F then begin
the distance from the center of a pixel in screen space.
Anisotropic Filtering
WEIGHT = WTAB[floor(Q)]
This radius r is just the parameter needed when indexing 1* read and weight texture pixel */
a kernel, so Q can serve two purposes: inclusion testing NUM NUM+WEIGHT*texture[v,u]
-
and kernel indexing. /* DEN is denominator (for normalization) */
The kernel f(r) is stored in a weight lookup table, DEN DEN+WEIGHT
=
WTAB. Rather than index WTAB by r, which would end
necessitate the calculation of r =V at each pixel, we Q = Q+DQ
define DQ = DQ+DDQ
WTAB[ Q]=f( \fQ) end
Ripmaps and summed area tables
end
so that the array can be indexed directly by Q. return(NUM/DEN)
Warping a lookup table for computational efficiency is end
a useful trick that has been applied by others3"7 A good
•
kernel to use is the Gaussian f(r) = e-ar, shown in Figure
Can look up axis-aligned 9, for which WTAB[Q] = e-aQ. The Gaussian is preferred
to the theoretically optimal sinc kernel because it decays
much more quickly. By properly scaling A, B, C, and F, the
This implementation can be optimized further by re-
moving redundant calculations from the v loop and, with
rectangular zones length of the WTAB array can be controlled to minimize
quantization artifacts (several thousand entries have
proven sufficient). The parameters F and a can be tuned
proper checking, by using integer variables throughout.
The EWA filter computes the weighted average of
elliptical areas incrementally, requiring one floating-point
to adjust the filter cutoff radius and the degree of pixel multiply, four floating-point adds, one integerization, and
• Diagonal footprints still a problem
overlap.
To evaluate Q efficiently, we employ the method of
finite differences. Since Q is quadratic, two additions
suffice to update Q from one pixel to the next? The
one table lookup per texture pixel. Blinn et al.'s method,
which is the most similar to EWA, appears to have
following pseudocode implements the EWA filter for
monochrome pictures (it is easily modified for color). Wikipedia
EWA filtering Integer variables are lowercase; floating-point variables
are uppercase.
• Use multiple lookups texture[v,uJ be
Compute
1* Let
<
a 2-dimensional array holding texture *1
texture space ellipse center (UO,VO)
from screen coordinates (x,y) >
• Weighted averageCompute (Ux,Vx). au av and (Uy,Vy) =
tax, ax J ay.
.]
ai a
/* Now find ellipse corresponding to a circular pixel: */
• Mipmap hierarchy still helps
C UX*UX+Uy*Uy
A Vx*Vx+Vy*Vy
-2.*(Ux*Vx+Uy*Vy)
B
-
-
-
F Ux*Vy-Uy*Vx
-
•
F F*F
Can handle irregular footprints
-
< scale A, B, C, and F equally so that F - WTAB length >
/* Ellipse is AU2+BUV+CV2=F, where U=u-UO, V=v-VO *1
Greene
Figure 8. Contours & Heckbert
of elliptical paraboloid Q ‘86
and box
EWA(UO,VO,A,B,C,F) around Q = F. Dots are centers of texture space pixels.
June 1986 25
GAMES101 46 Lingqi Yan, UC Santa Barbara
Thank you!
(And thank Prof. Ravi Ramamoorthi and Prof. Ren Ng for many of the slides!)