More GPU Stuff and Curves and Splines Outline • Announcements • Project 2 • GLSL • Hermite Splines • Catmull-Rom Splines • Bezier Curves • Higher Continuity: Natural and B-Splines • Drawing Splines Announcements
• Don’t lock the door of the lab! • Don’t paste solutions to the BBoards. • Final Exam: Tues 5/12 from 8:30AM - 11:30AM, room TBA. Outline • Announcements • Project 2 • GLSL • Hermite Splines • Catmull-Rom Splines • Bezier Curves • Higher Continuity: Natural and B-Splines • Drawing Splines Project 2
Get started early!!! Outline • Announcements • Project 2 • GLSL • Hermite Splines • Catmull-Rom Splines • Bezier Curves • Higher Continuity: Natural and B-Splines • Drawing Splines OpenGL Pipeline
! !"#$"%&&%'()*!+,)(+-) ! .)"/)0*,"#1)22#"2 ! 3"%$&)-/*,"#1)22#"2 Simplified OpenGL Pipeline
Vertex Data
Vertex Rasterization Fragment Operation (Interpolation) Operation
Framebuffer More Detail
PositionsNormalsColors
Eye Positions Position Eye Normals Normal Vertex Rasterization Fragment Colors Color Operation (Interpolation) Operation
(Interpolated)
Framebuffer Programmability
PositionsNormalsColors
Eye Positions Position Eye Normals Normal Vertex Rasterization Fragment Colors Color Operation (Interpolation) Operation
Programmable (Interpolated) Programmable
Framebuffer Variable Types • const - The declaration is of a compile time constant • attribute - Global variables that may change per vertex, that are passed from the OpenGL application to vertex shaders. This qualifier can only be used in vertex shaders. For the shader this is a read-only variable. • varying - used for interpolated data between a vertex shader and a fragment shader. Available for writing in the vertex shader, and read-only in a fragment shader. • uniform - Global variables that may change per primitive (may not be set inside glBegin,/glEnd), that are passed from the OpenGL application to the shaders. This qualifier can be used in both vertex and fragment shaders. For the shaders this is a read-only variable. Attribute Variables
PositionsNormalsColorsAttributes
Eye Positions Position Eye Normals Normal Vertex Rasterization Fragment Colors Color Operation (Interpolation) Operation
Programmable (Interpolated) Programmable
Framebuffer Varying Variables
PositionsNormalsColorsAttributes
Eye Positions Position Eye Normals Normal Vertex Rasterization Fragment Colors (Interpolation) Color Operation Varying Varying Operation
Programmable (Interpolated) Programmable
Framebuffer Uniform Variables
Uniform
PositionsNormalsColorsAttributes
Eye Positions Position Eye Normals Normal Vertex Rasterization Fragment Colors (Interpolation) Color Operation Varying Varying Operation
Programmable (Interpolated) Programmable
Framebuffer Why So Fast?
Uniform
PositionsNormalsColorsAttributes ~16 Fragment Shaders
Eye Positions Position Eye Normals Normal Vertex Rasterization Fragment Colors (Interpolation) Color Operation Varying Varying Operation
Programmable (Interpolated) Programmable
~4 Vertex Shaders Framebuffer Types and Functions
!"#$%'()"'*%#+',( +
C?DE( !"#$$!"#%&'()!*+,-#.$!"#/$!"#$0$ Y315Q(@?Z?0?45?(F31K?$ 234$=$234>$?$234@$?$234A$ ( $ !"#$=$!"#>$?$!"#@$?$!"#A$ >?5/60(@?A./164.A()345/1647(89:B(<;;= ( H3<4%&13<$ONMN$23%<&)5$GPGQ/$"<$&54-,C3C$&5$R*35ON$2>PQ/$"5C$<*34&B&3C$16$ &234$=$&234>$?$&234@$?$&234A$ 1))-$$"--.$1234$0$ S9:3$R*35ONT$M:"C&58$N"58,"83S/$23%<&)5$GPGQPUVP$$M34#&)5$"5C$*"83$ 1234$=$1234>$?$1234@$?$1234A$ 1))-$$"56.$1234$0$ 5,!13%<$%3B3%$#)$#:"#$23%<&)5$)B$#:3$<*34P$ 83596*3$=$B-)"#$?$234>$?$234@$?$234A$ 1234$$37,"-.$234/$234$0$ 1234$$37,"-.$&234/$&234$0$ 1234$$37,"-.$1234/$1234$0$ 1234$$8%3"#3%9:"5.$234/$234$0$ UI%I(%\PO,(8H:L(
"**-&4"#&)5$.\;`HaRZNb0$ 83596*3$$-)8>.$83596*3$0$ 234A$$#3'#,%3>H.$<"!*-3%>H/$234>$I/B-)"#$ 52,6J$0$
2"%6&58$ ),#*,#$B%)!$[3%#3'$<:"C3%$.\;`Hcd\Y9;0/$&5#3%*)-"#3C/$ 83596*3$$<7%#.$83596*3$0$ 234A$$#3'#,%3>HK%)L.$<"!*-3%>H/$234@$I/B-)"#$ 52,6J$0$ 83596*3$$&523%<3<7%#.$83596*3$0$ 234A$$#3'#,%3>HK%)L.$<"!*-3%>H/$234A$I/B-)"#$52,6J$0$ #:35$&5*,#$#)$F%"8!35#$<:"C3%$.\;`HaRZNb0$ 4)5<#$ 4)!*&-3a#&!3$4)5<#"5#$.\;`HaRZNb0$ $
$ *6GG64()345/1647(89:M(<;N=( 234A$$#3'#,%3@H.$<"!*-3%@H/$234@$I/B-)"#$ 52,6J$0$ 83596*3$$"1<.$83596*3$0$ 234A$$#3'#,%3@HK%)L.$<"!*-3%@H/$234A$I/B-)"#$52,6J$0$ Z345/164(<.0.G?/?07E( 83596*3$$43&-.$83596*3$0$ $ &5$$ 2"-,3$&5&"-&E3C$)5$35#%6/$5)#$4)*&3C$)5$%3#,%5$.C3B",-#0$ ),#$$ 4)*&3C$),#$)5$%3#,%5/$1,#$5)#$&5&"-&E3C$ 83596*3$$4-"!*.$83596*3/$83596*3/$83596*3$0$ 234A$$#3'#,%3(,13.$<"!*-3%(,13/$234@$I/B-)"#$52,6J$0$
&5),#$$ 2"-,3$&5&"-&E3C$)5$35#%6/$"5C$4)*&3C$),#$)5$%3#,%5$ 83596*3$$4-"!*.$83596*3/$B-)"#/$B-)"#$0$ $ 83596*3$$B-))%.$83596*3$0$ 234A$$<:"C)DGH.$<"!*-3%GHM:"C)D/$234@$I/B-)"#$ 52,6J$0$ 4)5<#$$ 4)5<#"5#$B,54#&)5$&5*,#$ 83596*3$$B%"4#.$83596*3$0$ 234A$$<:"C)D>H.$<"!*-3%>HM:"C)D/$234@$I/B-)"#$ 52,6J$0$ 83596*3$$!"'.$83596*3/$83596*3$0$ 234A$$<:"C)DGHK%)L.$<"!*-3%GHM:"C)D/$234A$I/B-)"#$ 52,6J$0$ >O*%+@(*+-P+'O'%,(8;:;(<(MW=$ 83596*3$$!"'.$83596*3/$B-)"#$0$ 234A$$<:"C)D>HK%)L.$<"!*-3%>HM:"C)D/$234A$I/B-)"#$ 52,6J$0$ $ 83596*3$$!&5.$83596*3/$83596*3$0$
F3/130"0$&0,/"6&/,G&03$&5"&/2%"E&,F#366&6"$6&& 83596*3$$!&5.$83596*3/$B-)"#$0$ $$$$$$$$'/$6/$E/$D$ 83596*3$$!&'.$83596*3/$83596*3/$83596*3$0$ %?2/30?($66Q3<()345/1647(S1/T($+U(89:R(<;B=$$ $$$$$$$$%/$8/$1/$"$ 83596*3$$!&'.$83596*3/$83596*3/$B-)"#$0$ !"#$"%&'()*7&"068#"&9):;<=:!>?@>=:@>=@A?>:+;<9>:A(+ @B&C&D$ $$$$$$$$$#/$*/$7$ 83596*3$$!)C.$83596*3/$83596*3$0$ 234A$$#3'#,%3GHN)C.$<"!*-3%GH/$B-)"#/$B-)"# &43E$0$ 83596*3$$!)C.$83596*3/$B-)"#$0$ 234A$$#3'#,%3GHK%)LN)C.$<"!*-3%GH/$234>/$B-)"# &43E$0$ 83596*3$$<&85.$83596*3$0$ 234A$$#3'#,%3GHK%)LN)C.$<"!*-3%GH/$234A/$B-)"# &43E$0$ 83596*3$$HN)C.$<"!*-3%>H/$234>/$B-)"# &43E$0$ 83596*3$$<#3*.$83596*3/$83596*3$0$ 234A$$#3'#,%3>HK%)LN)C.$<"!*-3%>H/$234@/$B-)"#&43E$0$ W$ $ WC3B&53$ XXNYZ;XX$ 83596*3$$<#3*.$B-)"#/$83596*3$0$ 234A$$#3'#,%3>HK%)LN)C.$<"!*-3%>H/$234A/$B-)"#&43E$0$
W,5C3B$ XXFYN;XX$ $ 234A$$#3'#,%3@HK%)LN)C.$<"!*-3%@H/$234A/$B-)"#&43E$0$ W&B$ XX[;\MYRZXX$ F?6G?/015()345/1647(89:H(<;H=( $ W&BC3B$ 234A$$ B#%"5HN)C.$<"!*-3%>HM:"C)D/$234@/$B-)"# &43E$0$ 83596*3$$ B"43B)%D"%C.$83596*3$!/$83596*3&+/$83596*3$($0$ W35C&B$ 234A$$<:"C)DGHK%)LN)C.$<"!*-3%GHM:"C)D/$234A/$B-)"#&43E$0$ B-)"#$$$ -358#:.$83596*3$0$ 234A$$<:"C)D>HK%)LN)C.$<"!*-3%>HM:"C)D/$234A/$B-)"#&43E&0 W3%%)%$ W*%"8!"$ 83596*3$$ 5)%!"-&E3.$83596*3$0$
W-&53$ 83596*3$$ %3B-34#.$83596*3&+/$83596*3$($0$ 83596*3$$ %3B%"4#.$83596*3&+/$83596*3$(/$B-)"#$"$,$0$ '617?()345/1647(89:V( F$,$([?07164(K?5A.0./164(.4K(?2/?471647(<06/656AE( )0.JG?4/(P065?7714J()345/1647(89:9(<;9=($&-#,./"0$&'()*$ 234>$$5)&<3>.$83596*3$0$ W23%<&)5$ 83596*3$$CFC'.$83596*3$0$ 234@$$5)&<3@.$83596*3$0$ $$$$$$$$C3B",-#$&<$SW23%<&)5$GGQS$.@P@$*GG0$ 234A$$5)&<3A.$83596*3$0$ 83596*3$$CFC6.$83596*3$0$ W3'#35<&)5$]0,/"$?$"--^$_$]%37,&%3$?$35"1-3$?$D"%5$?$C&<"1-3^$ 83596*3$$BD&C#:.$83596*3$0 $$$$$$$$C3B",-#$&<$SW3'#35<&)5$"--$_$C&<"1-3S$.@P@$*GG0$ Type Properties • Matrix / vector / integer / floating point types. • Strict casting requirements. • Const / attribute / varying / uniform variables. Control Loops in GLSL if (condition) { statements; Beware: } else { If Statement statements; Implementation } for (int i=0 ; i<10 ; i++) { statements; } while (condition) { statements; } Builtins !"#$"%&'()*"#&!)#+),-"'& ,F+-$G+1&F1+.8#0=&>?@H&2BHD&&"##$%%&'8! ! ! 80*6$0!!&'(M?&;02-<#'B+*+@#0#*8!J! '234567&89:29:&!6;56<73=&>?@I&2BAD&& "##$%%&'(! 6>?7-*@!@+0%!!&'(2-<#'A?#42+0*?3.! !!!!"#$%!!+@=?#>0.!! "#$%!!&'(B-8?0?->.!!!!!!!!!%>"5$2!?@%=!A2:=$! 6>?7-*@!@+0%!!&'(2-<#'A?#4B*-C#$0?->2+0*?3.! K.! 7'-+0!!&'(B-?>0H?R#.!!!!!!!$<"B1$!C.DE*'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3D#306*#,--*<89. K.! & 6>?7-*@!&'(M?&;0H-6*$#B+*+@#0#*8!&'(M?&;0H-6*$#1&'(2+3M?&;089. $->80!?>0!&'(2+3A#*0#3T>?7-*@,-@:->#>08.!! $->80!?>0!&'(2+3)*+&@#>0T>?7-*@,-@:->#>08.! $->80!?>0!&'(2+3A#*0#3O00*?=8.!! $->80!?>0!&'(2+3A+*L?>&)'-+08.!! $->80!?>0!&'(2+3/*+45677#*8.!! 823E'43E3/;62N&O;3=3:&FE5PQ;R=&& .5E3&2;5E:&T&U5=4765R3;& $->80!?>0!&'(2+3D#306*#,--*<8.!! !"#$%#&'(#)*+# ,-:L*?&;0!ZYY[!2?Q#!^#?='#>!!;00:_``@#4a$3`! B'#+8#!8#>8`$-@@#>08!0-!&'8'b@#4a$3! $->80!?>0!&'(2+3D#306*#T>?08.! ?>0!!!! -8&()*+@#G6@=#*.! X:#>cM!?8!+!*#&?80#*#!c*+:;?$8!E>$a! $->80!?>0!&'(2+3D#306*#E@+T>?08.!! 7'-+0!! -8&()*+@#D?@#.! N3$#:0!+8!>-0#$?#8!=#04##>!0;?8!&6?<#!+>80!?>0!&'(2+3A#*0#3D#306*#E@+T>?08.! 7'-+0!! -8&(/#'0+)*+@#D?@#.! cMHM!8:#$?7?$+0?->d!=#'?#"#!0;#!8:#$e! $->80!?>0!&'(2+3,-@=?># $->80!?>0!&'(2+3M?&;08.! @+0%!!! -8&(A?#42+0*?3E>"#*8#.! ! $->80!?>0!&'(2+3,'?:B'+>#8.!! ! 1Q:3=! Va!,-**#$08!+!0L:-!?>!0;#!X:#>cM!ZaY!8:#$?7?$+0?->a! Happy GPU Coding! Outline • Announcements • Project 2 • GLSL • Hermite Splines • Catmull-Rom Splines • Bezier Curves • Higher Continuity: Natural and B-Splines • Drawing Splines Modeling Complex Shapes • We want to build models of very complicated objects • An equation for a sphere is possible, but how about an equation for a telephone, or a face? • Complexity is achieved using simple pieces – polygons, parametric curves and surfaces, or implicit curves and surfaces – This lecture: parametric curves 3 What Do We Need From Curves in Computer Graphics? • Local control of shape (so that easy to build and modify) • Stability • Smoothness and continuity • Ability to evaluate derivatives Demo • Ease of rendering 4 Curve Usage Demo Curve Representations • Explicit: y = f(x) y y = mx + b – Easy to generate points – Must be a function: big limitation—vertical lines? x 5 Curve Representations • Explicit: y = f(x) y y = mx + b – Easy to generate points – Must be a function: big limitation—vertical lines? x •Implicit: f(x,y) = 0 y x2 + y 2 ! r 2 = 0 +Easy to test if on the curve –Hard to generate points x 6 Curve Representations • Explicit: y = f(x) y y = mx + b + Easy to generate points – Must be a function: big limitation—vertical lines? x y •Implicit: f(x,y) = 0 u="/2 2 2 2 x + y ! r = 0 u= " +Easy to test if on the curve u=0 –Hard to generate points x •Parametric: (x,y) = ( f(u), g(u)) (x, y) = (cosu,sin u) +Easy to generate points 7 Parameterization of a Curve • Parameterization of a curve: how a change in u moves you along a given curve in xyz space. • There are an infinite number of parameterizations of a given curve. Slow, fast, speed continuous or discontinuous, clockwise (CW) or CCW… 8 Polynomial Interpolation • An n-th degree polynomial fits a curve to n+1 points – called Lagrange Interpolation – result is a curve that is too wiggly, change to any control point affects entire curve (nonlocal) – this method is poor • We usually want the curve to be as smooth as possible – minimize the wiggles – high-degree polynomials are bad 9 Linear Interpolation Spline Interpolation Spine Interpolation Demo Splines: Piecewise Polynomials • A spline is a piecewise polynomial - many low degree polynomials are used to interpolate (pass through) the control points • Cubic piecewise polynomials are the most common: – piecewise definition gives local control 10 Piecewise Polynomials • Spline: lots of little polynomials pieced together • Want to make sure they fit together nicely Continuous in Continuous in Continuous in position position and tangent position, tangent, vector and curvature 11 Splines • Types of splines: – Hermite Splines – Catmull-Rom Splines – Bezier Splines – Natural Cubic Splines – B-Splines – NURBS 12 Hermite Curves • Cubic Hermite Splines P2 P1 Po That is, we want a way to specify the end points and the slope at the end points! 7 Splines chalkboard 13 The Cubic Hermite Spline Equation • Using some algebra, we obtain: & 2 "2 1 1 #& p1 # $ !$ ! 3 2 "3 3 "2 "1 p2 p(u)=[u u u 1]$ !$ ! $ 0 0 1 0!$!p1 ! $ !$ ! % 1 0 0 0"%!p2 " point that basis control matrix gets drawn (what the user gets to pick) • This form typical for splines – basis matrix and meaning of control matrix change with the spline type 14 The Cubic Hermite Spline Equation • Using some algebra, we obtain: & 2 "2 1 1 #& p1 # $ !$ ! 3 2 "3 3 "2 "1 p2 p(u)=[u u u 1]$ !$ ! $ 0 0 1 0!$!p1 ! $ !$ ! % 1 0 0 0"%!p2 " point that basis control matrix gets drawn (what the user gets to pick) T & 3 2 # 2u " 3u +1 & p1 # $ 3 2 ! $ ! $ " 2u + 3u ! p2 4 Basis Functions p(u) = $ ! $ 3 2 ! u " 2u + u $ !p1 ! $ ! $ ! 3 2 p %$ u " u "! %! 2 " 15 Four Basis Functions for Hermite splines T & 3 2 # 2u " 3u +1 & p1 # $ 3 2 ! $ ! $ " 2u + 3u ! p2 p(u) = $ ! $ 3 2 ! u " 2u + u $ !p1 ! $ ! $ ! 3 2 p %$ u " u "! %! 2 " 4 Basis Functions u Every cubic Hermite spline is a linear combination (blend) of these 4 functions 16 Piecing together Hermite Curves • It's easy to make a multi-segment Hermite spline – each piece is specified by a cubic Hermite curve – just specify the position and tangent at each “joint” – the pieces fit together with matched positions and first derivatives – gives C1 continuity • The points that the curve has to pass through are called knots or knot points 17 Outline • Announcements • Project 2 • GLSL • Hermite Splines • Catmull-Rom Splines • Bezier Curves • Higher Continuity: Natural and B-Splines • Drawing Splines Problem with Hermite Splines? • Must explicitly specify derivatives at each endpoint! • How can we solve this? Catmull-Rom Splines • Use for the roller-coaster assignment • With Hermite splines, the designer must specify all the tangent vectors • Catmull-Rom: an interpolating cubic spline with built- in C1 continuity. P2 P1 tangent at pi = s(pi+1 - pi-1) 8 Po Catmull-Rom Splines • Use for the roller-coaster (next programming assignment) • With Hermite splines, the designer must arrange for consecutive tangents to be collinear, to get C1 continuity. This gets tedious. • Catmull-Rom: an interpolating cubic spline with built- in C1 continuity. chalkboard 18 Catmull-Rom Spline Matrix &!s 2!s s!2 s #& p1 # $ !$ ! 3 2 2s s!3 3!2s !s p2 p(u)=[u u u 1]$ !$ ! $!s 0 s 0 !$ p3 ! $ !$ ! % 0 1 0 0 "%p4 " spline coefficients CR basis control vector • Derived similarly to Hermite • Parameter s is typically set to s=1/2. 19 Catmull-Rom Spline Matrix &! s 2 ! s s ! 2 s # &x1 y1 z1 # $ ! $ ! 2s s ! 3 3! 2s ! s x y z [x y z]= [u3 u 2 u 1]$ ! $ 2 2 2 ! $! s 0 s 0 ! $x3 y3 z3 ! $ ! $ ! % 0 1 0 0 " %x4 y4 z4 " spline coefficients CR basis control vector 20 Catmull-Rom Splines • Use for the roller-coaster assignment • With Hermite splines, the designer must specify all the tangent vectors • Catmull-Rom: an interpolating cubic spline with built- in C1 continuity. 9 Catmull-Rom Spline Matrix &!s 2!s s!2 s #& p1 # $ !$ ! 3 2 2s s!3 3!2s !s p2 p(u)=[u u u 1]$ !$ ! $!s 0 s 0 !$ p3 ! $ !$ ! % 0 1 0 0 "%p4 " CR basis control vector • Derived similarly to Hermite • Parameter s is typically set to s=1/2. 10 Cubic Curves in 3D • Three cubic polynomials, one for each coordinate 3 2 – x(u) = axu +bxu +cxu+dx 3 2 – y(u) = ayu +byu +cyu+dy 3 2 – z(u) = azu +bzu +czu+dz • In matrix notation &ax ay az # $ ! bx by bz [x(u) y(u) z(u)]= [u3 u2 u 1]$ ! $cx cy cz ! $ ! %d x d y d z " 11 Catmull-Rom Spline Matrix in 3D &! s 2 ! s s ! 2 s # &x1 y1 z1 # $ ! $ ! 2s s ! 3 3! 2s ! s x y z [x(u) y(u) z(u)]= [u3 u 2 u 1]$ ! $ 2 2 2 ! $! s 0 s 0 ! $x3 y3 z3 ! $ ! $ ! % 0 1 0 0 " %x4 y4 z4 " CR basis control vector 12 Outline • Announcements • Project 2 • GLSL • Hermite Splines • Catmull-Rom Splines • Bezier Curves • Higher Continuity: Natural and B-Splines • Drawing Splines Problem with Catmull-Rom Splines? • No control of derivatives at endpoints! • How can we solve this? • We want something intuitive. Bezier Curves* • Another variant of the same game • Instead of endpoints and tangents, four control points – points P0 and P3 are on the curve: P(u=0) = P0, P(u=1) = P3 – points P1 and P2 are off the curve – P'(u=0) = 3(P1-P0), P'(u=1) = 3(P3 – P2) • Convex Hull property – curve contained within convex hull of control points • Gives more control knobs (series of points) than Hermite • Scale factor (3) is chosen to make “velocity” approximately constant 13 Bezier Spline Example The Bezier Spline Matrix* &!1 3 ! 3 1# &x1 y1 z1 # $ ! $ ! 3 ! 6 3 0 x y z [x y z]= [u3 u 2 u 1]$ ! $ 2 2 2 ! $! 3 3 0 0! $x3 y3 z3 ! $ ! $ ! % 1 0 0 0" %x4 y4 z4 " Bezier basis Bezier control vector 14 Bezier Blending Functions* Also known as the order 4, degree 3 Bernstein polynomials Nonnegative, sum to 1 The entire curve lies inside the polyhedron bounded by the control points 15 Outline • Announcements • Project 2 • GLSL • Hermite Splines • Catmull-Rom Splines • Bezier Curves • Higher Continuity: Natural and B-Splines • Drawing Splines Piecewise Polynomials • Spline: lots of little polynomials pieced together • Want to make sure they fit together nicely Continuous in Continuous in Continuous in position position and tangent position, tangent, vector and curvature 16 Splines with More Continuity? • How could we get C2 continuity at control points? • Possible answers: – Use higher degree polynomials degree 4 = quartic, degree 5 = quintic, … but these get computationally expensive, and sometimes wiggly – Give up local control natural cubic splines A change to any control point affects the entire curve – Give up interpolation cubic B-splines Curve goes near, but not through, the control points 17 Comparison of Basic Cubic Splines Type Local Control Continuity Interpolation Hermite YES C1 YES Bezier YES C1 YES Catmull-Rom YES C1 YES Natural NO C2 YES B-Splines YES C2 NO • Summary – Can’t get C2, interpolation and local control with cubics 18 Natural Cubic Splines* • If you want 2nd derivatives at joints to match up, the resulting curves are called natural cubic splines • It’s a simple computation to solve for the cubics' coefficients. (See Numerical Recipes in C book for code.) • Finding all the right weights is a global calculation (solve tridiagonal linear system) 19 B-Splines* • Give up interpolation – the curve passes near the control points – best generated with interactive placement (because it’s hard to guess where the curve will go) • Curve obeys the convex hull property • C2 continuity and local control are good compensation for loss of interpolation 20 B-Spline Basis* • We always need 3 more control points than spline pieces &!1 3 ! 3 1# $ ! 1 3 ! 6 3 0 MBs = $ ! 6 $! 3 0 3 0! $ ! % 1 4 1 0" &Pi ! 3# $ ! $Pi ! 2! GBsi = $Pi ! 1! $ ! % Pi " 21 Outline • Announcements • Project 2 • GLSL • Hermite Splines • Catmull-Rom Splines • Bezier Curves • Higher Continuity: Natural and B-Splines • Drawing Splines How to Draw Spline Curves • Basis matrix eqn. allows same code to draw any spline type • Method 1: brute force – Calculate the coefficients – For each cubic segment, vary u from 0 to 1 (fixed step size) – Plug in u value, matrix multiply to compute position on curve – Draw line segment from last position to current position &! s 2 ! s s ! 2 s # &x1 y1 z1 # $ ! $ ! 2s s ! 3 3! 2s ! s x y z [x y z]= [u3 u 2 u 1]$ ! $ 2 2 2 ! $! s 0 s 0 ! $x3 y3 z3 ! $ ! $ ! % 0 1 0 0 " %x4 y4 z4 " CR basis control vector 22 How to Draw Spline Curves • What’s wrong with this approach? –Draws in even steps of u –Even steps of u ! even steps of x –Line length will vary over the curve –Want to bound line length »too long: curve looks jagged »too short: curve is slow to draw 23 Drawing Splines, 2 • Method 2: recursive subdivision - vary step size to draw short lines Subdivide(u0,u1,maxlinelength) umid = (u0 + u1)/2 x0 = P(u0) x1 = P(u1) if |x1 - x0| > maxlinelength Subdivide(u0,umid,maxlinelength) Subdivide(umid,u1,maxlinelength) else drawline(x0,x1) • Variant on Method 2 - subdivide based on curvature – replace condition in “if” statement with straightness criterion – draws fewer lines in flatter regions of the curve 24 In Summary... • Summary: – piecewise cubic is generally sufficient – define conditions on the curves and their continuity • Things to know: – basic curve properties (what are the conditions, controls, and properties for each spline type) – generic matrix formula for uniform cubic splines x(u) = uBG – given definition derive a basis matrix 25