1 CS 537/CPE 537 537: Interacti Interactive e Computer Graphics Lecture XII Instructor: Philippos Mordohai Webpage: www.cs.stevens.edu/~mordohai E E-mail: il Philippos.Mordohai@stevens.edu Phili M d h i@ d Overview • Recap of ANG Ch Ch. 10 • ANG Ch. 12: Curves and Surfaces 2 Hierarchical Modeling (recap) (ANG Ch. 10) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 3 Tree with Matrices Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 4 C Definition of treenode typedef struct treenode { GLfl t m[16]; GLfloat [16] void (*f)(); struct treenode *sibling; struct treenode *child; child; } treenode; Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 5 Display Function (figuretr (figuretr.c) c) display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_B ( IT); glLoadIdentity(); glColor3f(1.0, lC l 3f(1 0 0 0.0, 0 0 0.0); 0) traverse(&torso_node); glutSwapBuffers(); } Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 6 Traverse Function void traverse(treenode* root) { if(root==NULL) return; glPushMatrix(); glMultMatrixf(root->m); root->f(); if(root->child!=NULL) traverse(root->child); glPopMatrix(); if(root->sibling!=NULL) ( g ) traverse(root->sibling); } Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 7 Graphical Objects and Scene Graphs Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 8 Imperative Programming Model • Example: p rotate a cube cube data glRotate Application results • The rotation function must know how the cube is represented – Vertex list – Edge list Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 9 Object-Oriented Programming M d l Model • In this model, the representation is stored with the object Application message Cube Object • The application sends a message to the object • The object contains functions (methods) which allow it to transform itself Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 10 Scene Graph Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 11 Classes Node Camera Light TurnOff DrawStyle Color Material Transformation Geometry GLViewer Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Cube Cylinder S h Sphere Triangle P l Polygon Line 12 Tree Root Camera Light0 Color0 Transformation0 Object0 Transformation1 Object1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 13 Example //Transformation Nodes for both Robot and Chair: Transformation *Trans1=new Transformation; Trans1->SetValue(TRANSLATION, -0.5, 0, 0, 2); Trans1->AddChild(ChairColor); Trans1->AddChild(RobotMat); //Root Node: Node *Root=new Node; Root->AddChild(Trans1); Root->AddChild(Camera1); Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 14 Example //Viewer: GLViewer *MyViewer=new GLViewer; MyViewer->Init(argc, argv); MyViewer->SetValue(BACKCOLOR, GREY); MyViewer->SetValue(BUFFER, DOUBLE); MyViewer->CreateWin("Working Hard", 500, 500); GLViewer *MyViewer2=new GLViewer; MyViewer2->Init(argc, argv); MyViewer2->SetValue(BACKCOLOR, MAGENTA); MyViewer2->CreateWin("Working y e e eate ( o g Hard2", 200, 200); MyViewer2->SetValue(BUFFER, DOUBLE); MyViewer->Show(Root); MyViewer2->Show(Root); Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 15 Curves and Surfaces Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 16 Objectives • Introduce types of curves and surfaces – Explicit – Implicit – Parametric – Strengths and weaknesses eaknesses • Discuss Modeling and Approximations – Conditions – Stability Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 17 Escaping Flatland • Until now we have worked with flat entities such as lines and flat polygons – Fit well with g graphics p hardware – Mathematically simple • But the world is not composed p of flat entities – Need curves and curved surfaces – May only have need at the application level – Implementation can render them approximately with flat primitives Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 18 Modeling with Curves interpolating data point data points approximating curve Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 19 What Makes a Good Representation? • There are many ways to represent curves and surfaces • Want a representation that is – Stable – Smooth – Easy to evaluate – Must we interpolate p or can we jjust come close to data? – Do we need derivatives? Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 20 Explicit Representation • Most familiar form of curve in 2D y=f(x) • Cannot represent p all curves – Vertical lines – Circles • Extension to 3D – y=f(x), y=f(x) z=g(x) – The form z = f(x,y) defines a surface Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 21 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 • In general, we cannot solve for points that satisfy the equation Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 22 Algebraic Surface x y z i i j j k 0 k • Quadric surface 2 i+j+k • At most 10 terms • Can solve intersection with a ray by reducing problem to solving quadratic equation Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 23 Parametric Curves • Separate equation for each spatial variable x=x(u) T p(u)=[x(u), p( ) [ ( ), y( y(u), ), z(u)] ( )] yy=y(u) y(u) z=z(u) • For umax u umin we trace t outt a curve in i two t or three dimensions p(u) p(umax) p(umin) p( Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 24 Selecting Functions • Usually we can select “good” good functions – not unique for a given spatial curve – Approximate or interpolate known data – Want functions which are easy to evaluate – Want ffunctions nctions which hich are easy eas to differentiate • Computation of normals • Connecting pieces (segments) – Want functions which are smooth Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 25 Parametric Lines We can normalize u to be over the interval [0,1] Line connecting two points p0 and p1 p(1)= p1 p(u)=(1-u)p ( ) ( ) 0+up1 p(0)) = p0 p( p(1)= p0 +d Ray from p0 in the direction d d p(u)=p0+ud p(0) = p0 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 26 Parametric Surfaces • Surfaces require q 2p parameters y p(u,1) x=x(u,v) p(0,v) yy=y(u,v) y( , ) p(1 v) p(1,v) z=z(u,v) x p(u,v) = [x(u,v), y(u,v), z(u,v)]T z p(u,0) • Want same properties as curves: – Smoothness – Differentiability Diff i bili – Ease of evaluation Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 27 Normals We can differentiate with respect to u and v to obtain the normal at any point p x (u , v) / u p(u , v) y(u , v) / u u z(u , v) / u x (u , v) / v p(u , v) y(u , v) / v v z(u , v) / v p(u , v) p(u , v) n u v Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 28 Parametric Planes n point-vector form r p(u,v)=p p(u,v) p0+uq+vr q n=qxr p0 three-point form n p2 q = p1 – p0 r = p2 – p0 p1 p0 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 29 Parametric Sphere x(u,v) = r cos sin y(u,v) = r sin sin z(u,v) = r cos 360 0 180 0 constant: t t circles i l off constant t t longitude l it d constant: circles of constant latitude differentiate to show n = p Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 30 Curve Segments • After normalizing u, each curve is written p(u)=[x(u), y(u), z(u)]T, 1 u 0 • In classical numerical methods, we design a single global curve • In computer p g graphics p and CAD,, it is better to design small connected curve segments p(u) p(0) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 join point p(1) = q(0) q(u) q(1) 31 Parametric Polynomial Curves N M L i 0 j 0 k 0 x(u ) c xi u i y (u ) c yj u j z (u ) c zk u k •If N=M=K, we need to determine 3(N+1) coefficients •Equivalently q y we need 3(N+1) ( ) independent p conditions •Noting that the curves for x, y and z are independent, we can define each independently in an identical manner •We will use the form p(u ) where p can be any of x, y, z Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 L c k u k k 0 32 Why Polynomials • Easy to evaluate • Continuous and differentiable everywhere – Must M t worry about b t continuity ti it att join j i points i t including continuity of derivatives p(u) q(u) join point p(1) = q(0) but p’(1) q’(0) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 33 Cubic Parametric Polynomials • N=M=L=3, g gives balance between ease of evaluation and flexibility in design 3 p(u ) c k u k 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 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 34 Cubic Polynomial y Surfaces p(u,v)=[x(u,v), y(u,v), z(u,v)]T where 3 3 p(u , v) cij u i v j 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 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 35 Designing Parametric Cubic Curves Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 36 Objectives • Introduce the types of curves – Interpolating – Hermite – Bezier – B-spline B spline • Analyze their performance Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 37 Matrix-Vector Matrix Vector Form 3 p(u ) c k u k k 0 c 0 c1 define c c 2 c3 then 1 u u 2 u 3 u p(u ) u c c u Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 T T 38 Interpolating Curve p1 p0 p3 p2 Given four data (control) points p0 , p1 ,p2 , p3 determine cubic p(u) which passes through them Must find c0 ,c1 ,c2 , c3 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 39 Interpolation Equations Apply the interpolating conditions at u=0, u 0 1/3, 1/3 2/3, 2/3 1 p0=p(0)=c0 p1=p(1/3)=c p( ) 0+(1/3)c ( ) 1+(1/3) ( )2c2+(1/3) ( )3c3 p2=p(2/3)=c0+(2/3)c1+(2/3)2c2+(2/3)3c3 p3=p(1)=c0+c1+c2+c3 or in matrix form with p = [p0 p1 p2 p3]T p=Ac 1 1 A 1 1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 0 1 3 2 3 1 0 2 1 3 2 2 3 1 0 3 1 3 3 2 3 1 40 Interpolation Matrix Solving for c we find the interpolation matrix 0 0 0 1 5.5 9 4 . 5 1 1 M I A 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 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 41 Interpolating Multiple Segments use p = [p0 p1 p2 p3]T use p = [p3 p4 p5 p6]T Get continuity at join points but not continuity of derivatives Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 42 Blending Functions Rewriting the equation for p(u) p(u)=uTc=uTMIp = b(u)Tp where b(u) = [b0(u) b1(u) b2(u) b3(u)]T is an array of blending polynomials such that p(u) ( ) = b0(u)p ( ) 0+ b1(u)p ( ) 1+ b2(u)p ( ) 2+ b3(u)p ( ) 3 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 4 5 (u-1/3)(u-2/3) ( 1/3)( 2/3) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 43 Blending Functions • These functions are not smooth – Hence the interpolation polynomial is not smooth Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 44 Interpolating Patch 3 p (u , v) i o 3 j u cij v i j 0 Need 16 conditions to determine the 16 coefficients cij Choose at u,v = 0, 1/3, 2/3, 1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 45 Matrix Form Define fi 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=M C MIPMI p(u,v) = uTMIPMITv Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 46 Blending Patches 3 p (u , v) i o 3 b (u ) b i j 0 j (v ) pij Each bi(u)bj(v) is a blending patch Shows that we can build and analyze surfaces from our knowledge g of curves Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 47 Other Types of Curves and S f Surfaces • How can we get around the limitations of the interpolating form – Lack of smoothness – Discontinuous derivatives at join points • W We have h four f conditions diti (for (f cubics) bi ) that th t we can apply to each segment – Use them not only for f interpolation – Need only come close to the data Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 48 Hermite Form pp’(0) (0) p(0) (0) pp’(1) ( ) p(1) (1) Use two interpolating conditions and t derivative two d i ti conditions diti per segmentt Ensures continuity and first derivative continuity between segments Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 49 Equations Interpolating conditions are the same at ends p(0) = p0 = c0 p(1) = p3 = c0+c1+c2+c3 Differentiating we find p’(u) = c1+2uc2+3u2c3 Evaluating at end points p (0) = p’ p’(0) p 0 = c1 p’(1) = p’3 = c1+2c2+3c3 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 50 Matrix Form p 0 1 p 1 q 3 pp'0 0 p'3 0 0 0 0 1 1 1 c 1 0 0 1 2 3 Solving, we find cc=M MHq where MH is the Hermite matrix M H 0 0 0 1 0 0 1 0 3 3 2 1 1 2 2 1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 51 Blending Polynomials p(u) = b(u)Tq 2 u 3 3 u 2 1 3 2 2 3 u u b(u ) 3 u 2 u2 u 3 2 u u Although these functions are smooth, the Hermite form i nott usedd directly is di tl in i Computer C t Graphics G hi andd CAD because we usually have control points but not derivatives However the Hermite form is the basis of the Bezier form However, Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 52 Parametric and Geometric C i i Continuity • We can require the derivatives of x, y,and z to each be continuous at join points (parametric continuity) • Alternately, we can only require that the tangents of the resulting curve be continuous (geometric continuity) • The latter gives more flexibility as we need to satisfy f only two conditions rather than three at each join point Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 53 Example • Here the p and q have the same tangents at the ends of the segment but different derivatives • Generate different H Hermite i curves Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 54 Higher Dimensional Approximations • The techniques q for both interpolating p g and Hermite curves can be used with higher dimensional parametric polynomials • For F the h interpolating i l i form, f the h resulting l i matrix i becomes increasingly more ill-conditioned and the resulting curves less smooth and more prone to numerical errors • In both cases, there is more work in rendering the resulting polynomial curves and surfaces Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 55 Bezier and Spline Curves and Surfaces Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 56 Objectives • Introduce the Bezier curves and surfaces • Derive the required matrices • Introduce I d the h B B-spline li and d compare iit to the standard cubic Bezier Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 57 Bezier’ss Idea Bezier • In graphics and CAD, CAD we do not usually have derivative data • Bezier suggested using the same 4 data points as with the cubic interpolating curve to approximate the derivatives in the Hermite form Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 58 Approximating Derivatives p2 p1 p2 located at u=2/3 p1 located at u=1/3 p1 p0 p' (0) 1/ 3 p3 p 2 p' (1) 1/ 3 slope p’(1) slope p’(0) p0 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 u p3 59 Equations Interpolating conditions are the same p(0) = p0 = c0 p(1)) = p3 = c0+c1+c2+c3 p( Approximating derivative conditions p (0) = 3(p1- p0) = c0 p’(0) p’(1) = 3(p3- p2) = c1+2c2+3c3 Solve four linear equations for c=MBp Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 60 Bezier Matrix 0 0 1 3 3 0 MB 3 6 3 1 3 3 0 0 0 1 p(u)) = uTMBp = b(u) p( ( )Tp blending functions Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 61 Blending Functions (1 u )3 2 3u (1 u ) b(u ) 2 u 2 (1 u ) 3 u Note that all zeros are at 0 and 1 which forces the functions to be smooth over (0,1) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 62 Bernstein Polynomials • The blending functions are a special case of the Bernstein polynomials d! d k k bkd (u ) u (1 u ) k!(d k )! • These polynomials give the blending polynomials for any degree Bezier form – All zeros at 0 and 1 – For any degree they all sum to 1 – They are all between 0 and 1 inside (0,1) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 63 Convex Hull Property • The properties of the Bernstein polynomials ensure that all Bezier curves lie in the convex hull of their control points • Hence, even though we do not interpolate all the data, we cannot be too far away p1 p2 convex hull Bezier curve p0 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 p3 64 Bezier Patches U i same d Using data t array P=[pij] as with ith iinterpolating t l ti fform 3 3 p (u, v) bi (u ) b j (v) pij uT M B P MTB v i 0 j 0 Patch lies in convex hull Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 65 Analysis • Although the Bezier form is much better than the interpolating form, the derivatives are not continuous at join points • Can we do better? – Go to higher order Bezier • More work • Derivative continuity still only approximate • Supported by OpenGL – Apply different conditions • Tricky without letting order increase Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 66 B Splines B-Splines T • Basis splines: p use the data at p p=[p [pi-2 i 2 pi-1 i 1 pi pi+1] to define curve only between pi-1 and pi • Allows us to apply more continuity conditions to each h segmentt • For cubics, we can have continuity of function, first and second derivatives at join points • Cost is 3 times as much work for curves – Add one new p point each time rather than three • For surfaces, we do 9 times as much work Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 67 Cubic B-spline B spline p(u) = uTMSp = b(u)Tp 4 1 1 3 0 3 MS 3 6 3 1 3 3 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 0 0 0 1 68 Blending Functions 3 (1 u ) 2 3 1 4 6 u 3u b(u ) 6 1 3u 3 u 2 3 u 2 3 u convex hull property Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 69 B-Spline B Spline Patches 3 3 p (u , v) bi (u ) b j (v) pij u M S P M v T T S i 0 j 0 defined over only 1/9 of region Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 70 Splines and Basis • If we examine the cubic B-spline B spline from the perspective of each control (data) point, point contributes ((through g the each interior p blending functions) to four segments • We can rewrite p( p(u)) in terms of the data points as p(u) Bi(u) pi defining the basis functions {Bi(u)} Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 71 Basis Functions In terms of the blending gp polynomials y 0 (u 2) b0 b1 (u 1) Bi (u ) (u ) b2 b3 (u 1) 0 u i2 i 2 u i 1 i 1 u i i u i 1 i 1 u i 2 u i2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 72 Piecewise Polynomial Curve Linear combination of basis functions Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 73 Generalizing Splines • We can extend to splines of any degree • Data and conditions do not have to given at equally spaced values (the knots) – Nonuniform and uniform splines – Can C h have repeated d kknots • Can force spline to interpolate points • C Cox-de d B Boor recursion i gives i method h d off evaluation Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 74 NURBS • Nonuniform Rational B B-Spline Spline curves and surfaces add a fourth variable w to x,y,z – Can interpret p as weight g to g give more importance to some control data – Can also interpret as moving to homogeneous coordinate di • Requires a perspective division – NURBS act correctly for perspective viewing • Quadrics are a special case of NURBS Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 75 Rendering Curves and Surfaces Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 76 Objectives • Introduce methods to draw curves – Approximate with lines – Finite Differences • Derive the recursive method for evaluation off Bezier B i curves and d surfaces f • Learn how to convert all polynomial data to data for Bezier polynomials Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 77 Evaluating Polynomials • Simplest method to render a polynomial curve is to evaluate the polynomial at many points po sa and d form o a an app approximating o a g po polyline y e • For surfaces we can form an approximating mesh of triangles or quadrilaterals • Use Horner’s method to evaluate polynomials p(u)=c0+u(c1+u(c2+uc3)) – 3 multiplications/evaluation for cubic Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 78 Finite Differences For equally spaced {uk} we define finite differences p(u k ) p (u k ) (0) p(u k ) p (u k 1) p (u k ) (1) ( m 1) p(u k ) (m) p(u k 1) (m) p(u k ) For a polynomial of degree n, the nth finite difference is constant Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 79 Building a Finite Difference Table p(u)=1+3u+2u p(u) 1+3u+2u2+u3 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 80 Finding the Next Values Starting at the bottom, we can work up generating new values for the polynomial (m) ( m 1) p (u k 1) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 p (u k ) (m) p (u k ) 81 de Casteljau Recursion • We can use the convex hull property of Bezier curves to obtain an efficient q any y recursive method that does not require 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” Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 82 Splitting a Cubic Bezier p0, p1 , p2 , p3 determine a cubic Bezier polynomial and its convex hull Consider left half l(u) and right half r(u) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 83 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, l1, l2, l3} and {r0, r1, r2, r3} that determine them Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 84 Convex Hulls {l0, l1, l2, l3} and {r0, r1, r2, r3}each have a convex hull th t is that i closer l tto p(u) ( ) than th the th convex hull h ll off {p { 0, p1, p2, p3} This is known as the variation diminishing property. The polyline from l0 to l3 (= r0) to r3 is an approximation to p(u). Repeating recursively we get better approximations. Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 85 Equations Start with Bezier equations p(u) p(u)=uTMBp l(u) must interpolate p(0) and p(1/2) l(0) = l0 = p0 l(1) = l3 = p(1/2) = 1/8( p0 +3 p1 +3 p2 + p3 ) Matching slopes slopes, taking into account that l(u) l( ) and r(u) ( ) only go over half the distance as p(u) l (0) = 3(l1 - l0) = pp’(0) l’(0) (0) = 3/2(p1 - p0 ) l’(1) = 3(l3 – l2) = p’(1/2) = 3/8(- p0 - p1+ p2 + p3) S Symmetric t i equations ti h ld ffor r(u) hold ( ) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 86 Efficient Form l0 = p0 r3 = p3 l1 = ½(p0 + p1) r2 = ½(p2 + p3) l2 = ½(l1 + ½( p1 + p2)) r1 = ½(r2 + ½( p1 + p2)) l3 = r0 = ½(l2 + r1) Requires only shifts and adds! Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 87 Every Curve is a Bezier Curve • We can render a given polynomial using the recursive method if we find control points for its representation as a Bezier curve • Suppose that p(u) is given as an interpolating curve with control points q p(u)=uTMIq • There exist Bezier control points p such that p(u)=uTMBp • Equating and solving, we find p=MB-1MIq Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 88 Matrices Interpolating to Bezier B S li tto B B-Spline Bezier i Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 1 5 6 1 MB MI 1 3 0 0 0 3 2 3 3 2 0 1 0 1 MB MS 0 0 3 0 4 4 2 1 1 2 4 4 0 1 3 5 6 1 0 0 0 1 89 Example These three curves were all generated from the same original data using Bezier recursion by converting all control point data to Bezier control points Bezier Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Interpolating B Spline p 90 Surfaces • Can apply the recursive method to surfaces if we recall that for a Bezier patch curves of constant u (or v) are Bezier curves in u (or v) • First subdivide in u – Process creates new points – Some of the original points are discarded original and discarded original and kept Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 new 91 Second Subdivision 16 final points for 1 of 4 patches created Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 92 Surface Subdivision Limitations • The test for whether the new convex hull is flat enough is more difficult than for curves • Renderers use fixed number of subdivisions or let user pick the number Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 93 Utah Teapot • Most famous data set in computer graphics • Widely available as a list of 306 3D vertices and p the indices that define 32 Bezier patches Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 94 Rendering Quadrics • Anyy q quadric can be written as the q quadratic form pTAp+bTp+c=0 where p=[x, y, z]T with A, A b and c giving the coefficients • Render by ray casting – Intersect with parametric ray p() p()=p p0+d d that passes through a pixel – Yields a scalar quadratic equation • No solution: ray misses quadric • One solution: ray tangent to quadric • Two solutions: entry and exit points Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 95 Curves and Surfaces in OpenGL Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 96 Objectives • Introduce OpenGL evaluators • Learn to render polynomial curves and surfaces • Discuss quadrics in OpenGL – GLUT Quadrics – GLU Quadrics Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 97 What Does OpenGL p Support? pp • Evaluators: a g general mechanism for working g with the Bernstein polynomials – Can use any degree polynomials – Can use in 1-4 dimensions – Automatic generation of normals and texture coordinates – NURBS supported in GLU • Quadrics – GLU and GLUT contain polynomial approximations app o at o s o of quad quadrics cs Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 98 One-Dimensional O e e s o a Evaluators a ua o s • Evaluate a Bernstein polynomial of any degree at a set of specified values • Can evaluate a variety of variables – Points along a 2, 3 or 4 dimensional curve – Colors – Normals – Texture Coordinates • We can set up multiple evaluators that are all evaluated for the same value Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 99 Setting Up an Evaluator what we want to evaluate max and min of u glMap1f(type,u_min,u_max,stride, 1 order, pointer_to_array) separation bet between een 1+degree of polynomial data points pointer to control data Each type must be enabled by glEnable(type) Types include 3D and 4D points, RGBA colors, normals, t t texture coordinates di t etc. t Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 100 Example C Consider id an evaluator l t ffor a cubic bi B Bezier i curve over (0 (0,1) 1) point data[ ]={ ]={…………..}; }; * /3d data /* / glMap1f(GL_MAP_VERTEX_3,0.0,1.0,3,4,data); cubic data are 3D vertices d t are arranged data d as x,y,z,x,y,z…… three floats between data points in array glEnable(GL_MAP_VERTEX_3); Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 101 Evaluating • The function glEvalCoord1f(u) causes all enabled evaluators to be evaluated for the specified u – Can replace glVertex, glNormal, glTexCoord • The values of u need not be equally spaced Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 102 Example • Consider the previous evaluator that was set up for a cubic Bezier over (0,1) • Suppose that we want to approximate the curve with a 100 point polyline glBegin(GL_LINE_STRIP) for(i=0; i<100; i++) glEvalCoord1f( l l d1f( (float) (fl ) i/100.0); i/100 0) glEnd(); Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 103 Equally Spaced Points Rather than use a loop, we can set up an equally spaced mesh (grid) and then evaluate it with one function call glMapGrid(100, 0.0, 1.0); sets up 100 equally-spaced points on (0,1) glEvalMesh1(GL_LINE, lE lM h1(GL LINE 0, 0 99); 99) renders lines between adjacent evaluated points i t ffrom point i t 0 tto point i t 99 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 104 Bezier Surfaces • Similar S a p procedure ocedu e to 1D but use 2D e evaluators a uato s in u and v • Set up p with glMap2f(type, u_min, _ umax, u_stride, _ u_order, v_min, v_max, v_stride, v_order, pointer_to_data) • Evaluate with glEvalCoord2f(u,v) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 105 Example bicubic over (0,1) x (0,1) point data[4][4]={………}; glMap2f(GL_MAP_VERTEX_3, 0.0, 1.0, 3, 4, 0.0, 1.0, 12, 4, data); Note that in v direction data points are separated by 12 floats since array data is stored by rows Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 106 Rendering with Lines must draw in both directions for(j=0;j<100;j++) { glBegin(GL_LINE_STRIP); for(i=0;i<100;i++) glEvalCoord2f((float) 2 i/100.0, /100 0 (float) j/100.0); /100 0 glEnd(); glBegin(GL_LINE_STRIP); for(i=0;i<100;i++) glEvalCoord2f((float) j/100.0, (float) i/100.0); glEnd(); } Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 107 Rendering with Quadrilaterals We can form a quad mesh and render with lines for(j=0; j<99; j++) { glBegin(GL_QUAD_STRIP); l i (G Q S ) for(i=0; i<100; i++) { glEvalCoord2f ((float) i/100.0, (float) j/100 j/100.0); 0); glEvalCoord2f ((float)(i+1)/100.0, (float)j/100.0); } glEnd(): } Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 108 Uniform Meshes • We can form a 2D mesh (grid) in a similar manner to 1D for uniform spacing glMapGrid2(u g p ( _num, , u_min, , u_max, , v_num, v_min, v_max) • Can evaluate as before with lines or if want filled polygons glEvalMesh2( GL_FILL, u_start, u_num, v_start, v_num) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 109 Rendering with Lighting • If we use filled p polygons, yg we have to shade or we will see solid color uniform rendering • Can specify lights and materials but we need normals – Let OpenGL find them glEnable(GL_AUTO_NORMAL); Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 110 NURBS • OpenGL supports NURBS surfaces through the GLU library • Why GLU? – Can use evaluators in 4D with standard OpenGL library – However, there are manyy complexities p with NURBS that need a lot of code – There are five NURBS surface functions plus f nctions for trimming curves functions c r es that can remo remove e pieces of a NURBS surface Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 111 Quadrics • Quadrics Q are in both the GLU and GLUT libraries – Both use polygonal approximations where the application specifies the resolution – Sphere: lines of longitude and lattitude • GLU: disks disks, cylinders cylinders, spheres – Can apply transformations to scale, orient, and position • GLUT: Platonic solids, torus, Utah teapot, cone Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 112 Quadric Objects in GLU • GLU can automaticallyy generate g normals and texture coordinates • Quadrics are objects that include properties such h as h how we would ld lik like th the object bj t tto b be rendered disk Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 partial ti l di disk k sphere 113 Defining a Cylinder GLUquadricOBJ q *p; p P = gluNewQuadric(); /*set up object */ gluQuadricDrawStyle(GLU_LINE);/*render style*/ gluCylinder(p, BASE_RADIUS, TOP_RADIUS, BASE_HEIGHT, sections, slices); Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 114 GLUT Objects Each has a wire and a solid form glutWireCone() glutWireTorus() glutWireTeapot() Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 115 GLUT Platonic Solids glutWireTetrahedron() glutWireDodecahedron() glutWireOctahedron() glutWireIcosahedron() Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 116