Rendering the Teapot
Total Page:16
File Type:pdf, Size:1020Kb
Rendering the Teapot 1 Utah Teapot 2 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015 vertices.js var numTeapotVertices = 306; var vertices = [ vec3(1.4 , 0.0 , 2.4), vec3(1.4 , -0.784 , 2.4), vec3(0.784 , -1.4 , 2.4), vec3(0.0 , -1.4 , 2.4), vec3(1.3375 , 0.0 , 2.53125), . ]; 3 patches.js var numTeapotPatches = 32; var indices = new Array(numTeapotPatches); indices[0] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]; indices[1] = [3, 16, 17, 18, . ]; 4 Evaluation of Polynomials 5 Modeling Topics • Introduce types of curves and surfaces – Explicit – Implicit – Parametric • Discuss Modeling and Approximations Escaping Flatland • Lines and flat polygons – Fit well with graphics hardware – Mathematically simple • But world is not flat – Need curves and curved surfaces – At least at the application level – Render them approximately with flat primitives Modeling with Curves interpolating data point data points approximating curve 9 Good Representation? • Properties – Stable – Smooth – Easy to evaluate – Must we interpolate or can we just come close to data? – Do we need derivatives? Explicit Representation • Function y=f(x) • Cannot represent all curves y – Vertical lines – Circles x • Extension to 3D y – y=f(x), z=g(x) – The form z = f(x,y) defines a surface x z Implicit Representation • Two dimensional curve(s) g(x,y)=0 • Much more robust – All lines ax+by+c=0 – Circles x2+y2-r2=0 • Three dimensions g(x,y,z)=0 defines a surface – Intersect two surface to get a curve Algebraic Surface i j k ∑∑∑ x y z = 0 i j k Quadric surface 2 ≥ i+j+k At most 10 terms 13 Parametric Curves • Separate equation for each spatial variable x=x(u) y=y(u) p(u)=[x(u), y(u), z(u)]T z=z(u) • For umax ≥ u ≥ umin we trace out a curve in two or three dimensions p(u) p(umax) p(umin) 14 Parametric Lines We can normalize u to be over the interval (0,1) Line connecting two points p and p 0 1 p(1)= p1 p(u)=(1-u)p0+up1 p(0) = p0 p(1)= p0 +d Ray from p0 in the direction d p(u)=p +ud d 0 p(0) = p0 15 Parametric Surfaces • Surfaces require 2 parameters x=x(u,v) y p(u,1) y=y(u,v) p(0,v) (1,v) z=z(u,v) p x T p(u,v) = [x(u,v), y(u,v), z(u,v)] z p(u,0) • Want same properties as curves: – Smoothness – Differentiability – Ease of evaluation 16 Normals We can differentiate with respect to u and v to obtain the normal at any point p ⎡∂x(u,v) / ∂u⎤ ⎡∂x(u,v) / ∂v⎤ ∂p(u,v) ∂p(u,v) ⎢ ⎥ = ⎢∂y(u,v) / ∂u⎥ = ∂y(u,v) / ∂v ⎢ ⎥ ∂v ⎢ ⎥ ∂u ⎢∂z(u,v) / ∂v⎥ ⎣⎢∂z(u,v) / ∂u⎦⎥ ⎣ ⎦ ∂p(u,v) ∂p(u,v) n = × ∂u ∂v 17 Curve Segments p(u) join point p(1) = q(0) (u) q(1) p(0) q 18 Parametric Polynomial Curves N M L x(u) i j z(u) k = ∑cxi u y(u) = ∑cyj u = ∑czk u i=0 j=0 k=0 • If N=M=L, we need to determine 3(N+1) coefficients • Curves for x, y and z are independent, we can define each independently in an identical manner L • We will use the form k where p can be any of x, y, z p(u) = ∑ck u k =0 19 Why Polynomials • Easy to evaluate • Continuous and differentiable everywhere – Continuity at join points including continuity of derivatives p(u) q(u) join point p(1) = q(0) but p’(1) ≠ q’(0) Cubic Polynomials • N=M=L=3, 3 k p(u) = ∑ck u k =0 • Four coefficients to determine for each of x, y and z • Seek four independent conditions for various values of u resulting in 4 equations in 4 unknowns for each of x, y and z – Conditions are a mixture of continuity requirements at the join points and conditions for fitting the data Matrix-Vector Form 3 k p(u) = ∑ck u k =0 ⎡c0⎤ ⎡ 1 ⎤ ⎢ ⎥ ⎢ u ⎥ ⎢c1⎥ ⎢ ⎥ define c = u = 2 ⎢c2⎥ ⎢u ⎥ ⎢ ⎥ ⎢ 3⎥ ⎣c3⎦ ⎣u ⎦ T T then p(u) = u c = c u 22 Interpolating Curve p 1 p3 p 0 p2 Given four data (control) points p0 , p1 ,p2 , p3 determine cubic p(u) which passes through them 3 k p(u) = ∑ck u Must find c0 ,c1 ,c2 , c3 k =0 p(u) = uT c = cT u 23 Interpolation Equations apply the interpolating conditions at u=0, 1/3, 2/3, 1 p0=p(0)=c0 2 3 p1=p(1/3)=c0+(1/3)c1+(1/3) c2+(1/3) c2 2 3 p2=p(2/3)=c0+(2/3)c1+(2/3) c2+(2/3) c2 p3=p(1)=c0+c1+c2+c2 T or in matrix form with p = [p0 p1 p2 p3] ⎡1 0 0 0 ⎤ 2 3 ⎢ ⎛ 1⎞ ⎛ 1⎞ ⎛ 1⎞ ⎥ ⎢1 ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎥ ⎢ ⎝ 3⎠ ⎝ 3⎠ ⎝ 3⎠ ⎥ p=Ac A = 2 3 ⎢ ⎛ 2 ⎞ ⎛ 2 ⎞ ⎛ 2 ⎞ ⎥ ⎢1 ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎥ ⎢ ⎝ 3 ⎠ ⎝ 3 ⎠ ⎝ 3 ⎠ ⎥ ⎣⎢1 1 1 1 ⎦⎥ 24 Interpolation Matrix Solving for c we find the interpolation matrix ⎡ 1 0 0 0 ⎤ ⎢ ⎥ −1 − 5.5 9 − 4.5 1 A = ⎢ ⎥ MI = ⎢ 9 − 22.5 18 − 4.5⎥ ⎢ ⎥ ⎣− 4.5 13.5 −13.5 4.5 ⎦ c=MIp Note that MI does not depend on input data and can be used for each segment in x, y, and z 25 Interpolating Multiple Segments T T use p = [p3 p4 p5 p6] use p = [p0 p1 p2 p3] Get continuity at join points but not continuity of derivatives 26 Blending Functions Rewriting the equation for p(u) T T T p(u)=u c=u MIp = b(u) p T where b(u) = [b0(u) b1(u) b2(u) b3(u)] is an array of blending polynomials such that p(u) = b0(u)p0+ b1(u)p1+ b2(u)p2+ b3(u)p3 b0(u) = -4.5(u-1/3)(u-2/3)(u-1) b1(u) = 13.5u (u-2/3)(u-1) b2(u) = -13.5u (u-1/3)(u-1) b3(u) = 4.5u (u-1/3)(u-2/3) 27 Blending Functions – NOT GOOD b0(u) = -4.5(u-1/3)(u-2/3)(u-1) b1(u) = 13.5u (u-2/3)(u-1) b2(u) = -13.5u (u-1/3)(u-1) b3(u) = 4.5u (u-1/3)(u-2/3) 28 As Opposed to … Parametric Surface Cubic Polynomial Surfaces p(u,v)=[x(u,v), y(u,v), z(u,v)]T where 3 3 i j p(u,v) = ∑∑cij u v i=0 j=0 p is any of x, y or z Need 48 coefficients ( 3 independent sets of 16) to determine a surface patch 31 Interpolating Patch 3 3 ui j p(u,v) = ∑ ∑cij v i=o j=0 Need 16 conditions to determine the 16 coefficients cij Choose at u,v = 0, 1/3, 2/3, 1 32 Matrix Form Define v = [1 v v2 v3]T C = [cij] P = [pij] p(u,v) = uTCv If we observe that for constant u (v), we obtain interpolating curve in v (u), we can show C=MIPMI T T p(u,v) = u MIPMI v 33 Blending Patches 3 3 p(u,v) (u ) (v ) p = ∑ ∑bi b j ij i=o j=0 Each bi(u)bj(v) is a blending patch Shows that we can build and analyze surfaces from our knowledge of curves 34 Bezier and Spline Curves and Surfaces Bezier Bezier Surface Patches Utah Teapot – Bezier Avatar Available as a list of 306 3D vertices and the indices that define 32 Bezier patches 38 Bezier • Do not usually have derivative data • Bezier suggested using the same 4 data points as with the cubic interpolating curve to approximate the derivatives 39 Approximating Derivatives p p1 2 p located at u=2/3 p1 located at u=1/3 2 p − p p − p p'(0) ≈ 1 0 p'(1) ≈ 3 2 1/ 3 1/ 3 slope p’(0) slope p’(1) p p0 u 3 40 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015 Equations Interpolating conditions are the same p(0) = p0 = c0 p(1) = p3 = c0+c1+c2+c3 Approximating derivative conditions p’(0) = 3(p1- p0) = c0 p’(1) = 3(p3- p2) = c1+2c2+3c3 Solve four linear equations for c=MBp 41 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015 Bezier Matrix ⎡ 1 0 0 0⎤ ⎢− 3 3 0 0⎥ = ⎢ ⎥ MB ⎢ 3 − 6 3 0⎥ ⎢ ⎥ ⎣−1 3 − 3 1⎦ T T p(u) = u MBp = b(u) p blending functions 42 Blending Functions # (1− u)3 & % ( 3u(1− u)2 b(u) = % ( %3 u2 (1− u)( % ( $ u3 ' Note that all zeros are at 0 and 1 which forces € the functions to be smooth over (0,1) 43 Bernstein Polynomials d! k d −k bkd (u) = u (1−u) k!(d − k)! Blending polynomials for any degree d – All zeros at 0 and 1 – For any degree they all sum to 1 – They are all between 0 and 1 inside (0,1) 44 Rendering Curves and Surfaces 45 Evaluation of Polynomials 46 Evaluating Polynomials • Polynomial curve – evaluate polynomial at many points and form an approximating polyline • Surfaces – approximating mesh of triangles or quadrilaterals 47 Evaluate without Computation? Convex Hull Property • Bezier curves lie in the convex hull of their control points • Do not interpolate all the data; cannot be too far away p1 p2 convex hull Bezier curve p p0 3 49 deCasteljau Recursion • Use convex hull property to obtain an efficient recursive method that does not require any function evaluations – Uses only the values at the control points • Based on the idea that “any polynomial and any part of a polynomial is a Bezier polynomial for properly chosen control data” 50 Splitting a Cubic Bezier p0, p 1 , p2 , p3 determine a cubic Bezier polynomial and its convex hull Consider left half l(u) and right half r(u) l(u) and r(u) Since l(u) and r(u) are Bezier curves, we should be able to find two sets of control points {l0, l 1, l 2, l 3} and {r0, r 1, r 2, r 3} that determine them Convex Hulls {l0, l1, l2, l3} and {r0, r1, r2, r3}each have a convex hull that that is closer to p(u) than the convex hull of {p0, p1, p2, p3} This is known as the variation diminishing property.