Toon Shading and Surface Patches

Computer Graphics CSE 167 Lecture 14 CSE 167:

• Toon shading – A non‐photorealistic rendering method • Surface patches – Generalization of curves

CSE 167, Winter 2018 2 Based on slides courtesy of Jurgen Schulze Toon shading

• Also called Shading (“Cel” is short for “celluloid” sheets, on which was hand‐drawn) • Gives any 3D model a cartoon‐style look • Emphasizes silhouettes • Discrete steps for diffuse shading, highlights • Non‐photorealistic rendering method (NPR) • Programmable allow real‐time performance Source: Wikipedia

CSE 167, Winter 2018 3 Off-line toon GLSL toon shader Approach

• Start with regular 3D model • Apply two rendering tricks – Silhouette edges • Emphasize pixels with normals perpendicular to viewing direction – Discretized shading • Conventional (smooth) lighting values calculated for each pixel, then mapped to a small number of discrete shades

Source: Wikipedia CSE 167, Winter 2018 4 Silhouette edges

• Silhouette edge detection – Compute dot product of viewing direction v and normal n

– Use cutoff value for edge: • If edge < 0.01 draw black, else do not change pixel color – Use 1D texture to define edge ramp for smoother edgeramp transition 1 uniform sample1D edgeramp; e = texture1D(edgeramp,edge); 0 edge

CSE 167, Winter 2018 5 Discretized shading

• Compute diffuse and specular shading

• Discretize shading – Approaches • If then else tree comparing values against thresholds if (diffuse < A) diffuse = 0.0; else if (diffuse < B) diffuse = B; else if (diffuse < C) diffuse = C; else diffuse = D; • 1D textures to map diffuse and specular shading to colors uniform sampler1D diffuseramp; uniform sampler1D specularramp; color = e * (texture1D(diffuse,diffuseramp) + texture1D(specular,specularramp));

CSE 167, Winter 2018 6 Toon shading demo

http://www.bonzaisoftware.com/gldemos/non‐photorealistic‐rendering/ CSE 167, Winter 2018 7 Surface patches

• Bilinear patch • Bicubic Bézier patch

CSE 167, Winter 2018 8 Curved surfaces

• Curves – Described by a 1D series of control points – A function x(t) – Segments joined together to form a longer curve • Surfaces – Described by a 2D mesh of control points – Parameters have two dimensions (two dimensional parameter domain) – A function x(u,v) – Patches joined together to form a bigger surface

CSE 167, Winter 2018 9 Parametric surface patch

• x(u,v) describes a point in space for any given (u,v) pair – u,v each range from 0 to 1

v x(0.8,0.7)

z u 1 y v

x 0 u 1 2D parameter domain

CSE 167, Winter 2018 10 Parametric surface patch

• x(u,v) describes a point in space for any given (u,v) pair – u,v each range from 0 to 1

v x(0.8,0.7) x(0.4,v) z u x(u,0.25) 1 y v

x 0 u 1 • Parametric curves 2D parameter domain – For fixed u0 , have a v curve x(u0,v) – For fixed v0 , have a u curve x(u,v0) – For any point on the surface, there are a pair of parametric curves through that point

CSE 167, Winter 2018 11 Tangents

• The tangent to a parametric curve is also tangent to the surface • For any point on the surface, there are a pair of (parametric) tangent vectors • Note: these vectors are not necessarily perpendicular to each other x v v x u

u CSE 167, Winter 2018 12 Surface normal

• Normal is cross product of the two tangent vectors r x x n(u,v)  (u,v)  (u,v) Note: order matters u v • Typically,Typically we are we interested desire in the a un unitit normal, normal, so we need soto normalize we need to unitize x

r * x x r n (u,v)  (u,v)  (u,v) v u v n r * r n (u,v) x n(u,v)  r * n (u,v) u

CSE 167, Winter 2018 13 Bilinear patch

• Control mesh with four points p0, p1, p2, p3 • Compute x(u,v) using a two‐step construction scheme

p2

p3

v

p0 p1 u

CSE 167, Winter 2018 14 Bilinear patch (step 1)

• For a given value of u, evaluate the linear curves on the two u‐direction edges • Use the same value u for both

q1 = Lerp(u, p2, p3)

p2 q1

p3

v

q0 = Lerp(u, p0, p1)

p0 q 0 p1 u

CSE 167, Winter 2018 15 Bilinear patch (step 2)

• Consider that q0, q1 define a line segment • Evaluate it using v to get x

x  Lerp(v,q0 ,q1 )

p2 q1 p x 3 v

p0 q 0 p1 u

CSE 167, Winter 2018 16 Bilinear patch

• Combining the steps, we get

x(u,v)  Lerp(v, Lerp(u,p0 ,p1 ), Lerp(u,p2 ,p3 ))

p2 q1 p x 3 v

p0 q 0 p1 u

CSE 167, Winter 2018 17 Bilinear patch

• Try the other order • Evaluate first in the v direction

r0  Lerp(v,p0 ,p2 ) r1  Lerp(v,p1,p3 )

p2

p3 r0 v r1

p0 p1 u

CSE 167, Winter 2018 18 Bilinear patch

• Consider that r0, r1 define a line segment • Evaluate it using u to get x

x  Lerp(u,r0 ,r1 )

p2 p x 3 r0 v r1

p0 p1 u

CSE 167, Winter 2018 19 Bilinear patch

• Formula for the v direction first

x(u,v)  Lerp(u, Lerp(v,p0 ,p2 ), Lerp(v,p1,p3 ))

p2 p x 3 r0 v r1

p0 p1 u

CSE 167, Winter 2018 20 Bilinear patch

• Patch geometry is independent of the order of u and v

x(u,v)  Lerp(v, Lerp(u,p0 ,p1 ), Lerp(u,p2 ,p3 ))

x(u,v)  Lerp(u, Lerp(v,p0 ,p2 ), Lerp(v,p1,p3 ))

p2 q1 p x 3 r0 v r1

p0 q 0 p1 u

CSE 167, Winter 2018 21 Bilinear patch

• Weighted sum of control points

• Bilinear polynomial

• Matrix form

 p0 p2 1 v x(u,v)  1 u u     p1 p3  v 

CSE 167, Winter 2018 22 Bilinear patch

• Properties – Patch interpolates the control points – The boundaries are straight line segments – If all 4 points of the control mesh are co‐planar, the patch is flat – If the points are not co‐planar, we get a curved surface • Saddle shape (hyperbolic paraboloid) – The parametric curves are all straight line segments • A (doubly) ruled surface: has (two) straight lines through every point – Not terribly useful as a modeling primitive

CSE 167, Winter 2018 23 Bicubic Bézier patch

• Grid of 4x4 control points, p0 through p15 • Four rows of control points define Bézier curves along u

p0,p1,p2,p3; p4,p5,p6,p7; p8,p9,p10,p11; p12,p13,p14,p15 • Four columns define Bézier curves along v

p0,p4,p8,p12; p1,p6,p9,p13; p2,p6,p10,p14; p3,p7,p11,p15

p12 p13 p 8 p v 9 p p4 14 p5 p15

p0 p10 p1 p11 p6

u p7 p2

p3 CSE 167, Winter 2018 24 Bicubic Bézier patch (step 1)

• Evaluate four u‐direction Bézier curves at scalar value u [0..1] q0  Bez(u,p0 ,p1,p2 ,p3 ) q  Bez(u,p ,p ,p ,p ) • Get points q0,q1,q2,q3 1 4 5 6 7

q2  Bez(u,p8 ,p9 ,p10 ,p11 ) p12 p13 q3 q3  Bez(u,p12 ,p13,p14 ,p15 ) p8 v p9 p p14 4 p p15 5 q2 q p 1 p 0 p 10 1 p p 11 q0 6 u p7 p2

p3 CSE 167, Winter 2018 25 Bicubic Bézier patch (step 2)

• Points q0,q1,q2,q3 define a Bézier curve • Evaluate it at v [0..1]

x(u,v)  Bez(v,q0 ,q1,q2 ,q3 ) p12 p13 q3 p8 v p9 x p p14 4 p p15 5 q2 q p 1 p 0 p 10 1 p p 11 q0 6 u p7 p2

p3 CSE 167, Winter 2018 26 Bicubic Bézier patch

q  Bez(u,p ,p ,p ,p ) r  Bez(v,p ,p ,p ,p ) • Same result in 0 0 1 2 3 0 0 4 8 12 q1  Bez(u,p4 ,p5 ,p6 ,p7 ) r1  Bez(v,p1,p5 ,p9 ,p13 )

either order q2  Bez(u,p8 ,p9 ,p10 ,p11 )  r2  Bez(v,p2 ,p6 ,p10 ,p14 ) q  Bez(u,p ,p ,p ,p ) r  Bez(v,p ,p ,p ,p ) (evaluate u 3 12 13 14 15 3 3 7 11 15 x(u,v)  Bez(v,q ,q ,q ,q ) x(u,v)  Bez(u,r ,r ,r ,r ) before v or 0 1 2 3 0 1 2 3 p 12 p q vice versa) r0 13 3 r1 p8 v p9 x p p14 4 p p15 5 q2 r q 2 r3 p 1 p 0 p 10 1 p p 11 q0 6 u p7 p2

p3 CSE 167, Winter 2018 27 Bicubic Bézier patch, matrix form u 3  v3  1331     u2 v2  3 630 U    V    B     BT  u   v  Bez 33 00 Bez        1   1   1000  p p p p  C  BT G B 0 x 1x 2 x 3x x Bez x Bez  p p p p  T  4 x 5x 6 x 7 x  Cy  BBezGyBBez Gx  , Gy = L , Gz = L T  p8 x p9x p10x p11x  Cz  BBezGzBBez    p12x p13x p14 x p15x  T V CxU  T  C stores the coefficients of the bicubic equation xu,v  V CyU G stores the control point geometry  T  BBez is the basis matrix (Bézier basis) V CzU U and V are the vectors formed from the powers of u and v

CSE 167, Winter 2018 28 Bicubic Bézier patch

• Properties – Convex hull • Any point on the surface will fall within the convex hull of the control points – Interpolates 4 corner points – Approximates other 12 points, which act as “handles” – The boundaries of the patch are the Bézier curves defined by the points on the mesh edges – The parametric curves are all Bézier curves

CSE 167, Winter 2018 29 Tangents of a Bézier patch

• Recall parametric curves x(u,v0) and x(u0,v), where v0 and u0 are fixed, respectively • Tangents to surface = tangents to parametric curves • Tangents are partial derivatives of x(u,v) • Normal is cross product of the tangents x p p13 12 v v0 p8 v p9 x p14 p4 p5 x p15 u p0 p10 p1 p11 p6 u0 u p7 p2

p3 CSE 167, Winter 2018 30 Tangents of a Bézier patch

q0  Bez(u,p0 ,p1,p2 ,p3 ) r0  Bez(v,p0 ,p4 ,p8 ,p12 )

q1  Bez(u,p4 ,p5 ,p6 ,p7 ) r1  Bez(v,p1,p5 ,p9 ,p13 ) q  Bez(u,p ,p ,p ,p ) r  Bez(v,p ,p ,p ,p ) 2 8 9 10 11 2 2 6 10 14 q3  Bez(u,p12 ,p13,p14 ,p15 ) r3  Bez(v,p3,p7 ,p11,p15 ) x x (u,v)  Bez(v,q ,q ,q ,q ) (u,v)  Bez(u,r ,r ,r ,r ) v 0 1 2 3 u 0 1 2 3 x p13 v p12 r0 q r1 3 p8 v p9 x p14 p4 x p5 p15 q2 u r  q 2 r3 p 1 p 0 p 10 1 p p 11 q0 6 u p7 p2

p3 CSE 167, Winter 2018 31 Tessellating a Bézier patch

• Uniform tessellation is most straightforward – Evaluate points on a grid of u,v coordinates – Compute tangents at each point, take cross product to get per‐ vertex normal – Draw triangle strips with glBegin(GL_TRIANGLE_STRIP)

• Adaptive tessellation/recursive subdivision – Potential for “cracks” if patches on opposite sides of an edge divide differently – Tricky to get right, but can be done

CSE 167, Winter 2018 32 Piecewise Bézier surface

• Lay out grid of adjacent meshes of control points

• For C0 continuity, must share points on the edge – Each edge of a Bézier patch is a Bézier curve based only on the edge mesh points – So if adjacent meshes share edge points, the patches will line up exactly • But, results in a crease

Grid of control points CSE 167, Winter 2018Piecewise Bézier surface 33 C1 continuity

• We want the parametric curves that cross

each edge to have C1 continuity – So the handles must be equal‐and‐opposite across the edge

CSE 167, Winter 2018 34 Modeling with Bézier patches

• Original Utah teapot, from Martin Newell's PhD thesis, consisted of 28 Bézier patches. • The original had no rim for the lid and no bottom • Later, four more patches were added to create a bottom, bringing the total to 32 • The data set was used by a number of people, including graphics guru Jim Blinn. In a demonstration of a system of his he scaled the teapot by .75, creating a stubbier teapot. He found it more pleasing to the eye, and it was this scaled version that became the highly popular dataset used today.

Source: http://www.holmes3d.net/graphics/teapot/ Pixar’s walking teapot CSE 167, Winter 2018 35