Você está na página 1de 69

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
Dont lock the door of
the lab!

Dont 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
Operation

Rasterization
(Interpolation)

Fragment
Operation

Framebuffer

Po
s
N itio
or ns
Co ma
lor ls
s

More Detail

Vertex
Operation

Eye Positions

Position

Eye Normals

Normal

Colors

Rasterization
(Interpolation)

Color

Fragment
Operation

(Interpolated)

Framebuffer

Po
s
N itio
or ns
Co ma
lor ls
s

Programmability

Vertex
Operation
Programmable

Eye Positions

Position

Eye Normals

Normal

Colors

Rasterization
(Interpolation)

Color
(Interpolated)

Fragment
Operation
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.

Po
s
N itio
or ns
Co ma
l
l
At ors s
tri
bu
te
s

Attribute Variables

Vertex
Operation
Programmable

Eye Positions

Position

Eye Normals

Normal

Colors

Rasterization
(Interpolation)

Color
(Interpolated)

Fragment
Operation
Programmable

Framebuffer

Po
s
N itio
or ns
Co ma
l
l
At ors s
tri
bu
te
s

Varying Variables

Vertex
Operation
Programmable

Eye Positions

Position

Eye Normals

Normal

Colors
Varying

Rasterization
(Interpolation)

Color
Varying
(Interpolated)

Fragment
Operation
Programmable

Framebuffer

Po
s
N itio
or ns
Co ma
l
l
At ors s
tri
bu
te
s

Uniform Variables

Vertex
Operation
Programmable

Uniform

Eye Positions

Position

Eye Normals

Normal

Colors
Varying

Rasterization
(Interpolation)

Color
Varying
(Interpolated)

Fragment
Operation
Programmable

Framebuffer

Po
s
N itio
or ns
Co ma
l
l
At ors s
tri
bu
te
s

Why So Fast?

Vertex
Operation
Programmable

~4 Vertex
Shaders

Uniform
~16 Fragment
Shaders

Eye Positions

Position

Eye Normals

Normal

Colors
Varying

Rasterization
(Interpolation)

Color
Varying
(Interpolated)

Fragment
Operation
Programmable

Framebuffer

Types and Functions


+<?4F$X(,T.K14J($.4J3.J?(8F$,$=(
Y315Q(@?Z?0?45?(F31K?$
$
H3<4%&13<$ONMN$23%<&)5$GPGQ/$"<$&54-,C3C$&5$R*35ON$2>PQ/$"5C$<*34&B&3C$16$
S9:3$R*35ONT$M:"C&58$N"58,"83S/$23%<&)5$GPGQPUVP$$M34#&)5$"5C$*"83$
5,!13%<$%3B3%$#)$#:"#$23%<&)5$)B$#:3$<*34P$

UI%I(%\PO,(8H:L(<LB=$
$

B-)"#/$234>/$234@/$234A$$
&5#/$&234>/$&234@/$&234A$$
1))-/$1234>/$1234@/$1234A$$
!"#>/$!"#@/$!"#A$$
2)&C$$
<"!*-3%GH/$<"!*-3%>H/$<"!*-3%@H$$
<"!*-3%(,13$$
<"!*-3%GHM:"C)D/$<"!*-3%>HM:"C)D$

UI%I(%\PO(Y"I$#)#O@,(8H:M(<NN=((
$

JA6].A([.01.]A?(K?5A.0./1647E(
,5&B)%!$
&5*,#$#)$[3%#3'$"5C$F%"8!35#$<:"C3%$B%)!$R*35ON$)%$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&234$=$&234>$?$&234@$?$&234A$
1234$=$1234>$?$1234@$?$1234A$
83596*3$=$B-)"#$?$234>$?$234@$?$234A$

I4JA?(.4K(%01J646G?/0D()345/1647(89:L(<;L=(
83596*3$$<&5.$83596*3$0$
83596*3$$4)<.$83596*3$0$
83596*3$$#"5.$83596*3$0$
$
83596*3$$"<&5.$83596*3$0$
83596*3$$"4)<.$83596*3$0$
83596*3$$"#"5.$83596*3/$83596*3$0$
83596*3$$"#"5.$83596*3$0$
$
83596*3$$%"C&"5<.$83596*3$0
83596*3$$C38%33<.$83596*3$0$
$
O2<64?4/1.A()345/1647(89:N(<;N=(
83596*3$$*)D.$83596*3/$83596*3$0$
83596*3$$3'*.$83596*3$0$
83596*3$$-)8.$83596*3$0$
83596*3$$3'*>.$83596*3$0$
83596*3$$-)8>.$83596*3$0$
83596*3$$<7%#.$83596*3$0$
83596*3$$&523%<3<7%#.$83596*3$0$
*6GG64()345/1647(89:M(<;N=(
83596*3$$"1<.$83596*3$0$
83596*3$$43&-.$83596*3$0$
83596*3$$4-"!*.$83596*3/$83596*3/$83596*3$0$
83596*3$$4-"!*.$83596*3/$B-)"#/$B-)"#$0$
83596*3$$B-))%.$83596*3$0$
83596*3$$B%"4#.$83596*3$0$
83596*3$$!"'.$83596*3/$83596*3$0$
83596*3$$!"'.$83596*3/$B-)"#$0$
83596*3$$!&5.$83596*3/$83596*3$0$
83596*3$$!&5.$83596*3/$B-)"#$0$
83596*3$$!&'.$83596*3/$83596*3/$83596*3$0$
83596*3$$!&'.$83596*3/$83596*3/$B-)"#$0$
83596*3$$!)C.$83596*3/$83596*3$0$
83596*3$$!)C.$83596*3/$B-)"#$0$
83596*3$$<&85.$83596*3$0$
83596*3$$<!))#:<#3*.$83596*3/$83596*3/$83596*3$0$
83596*3$$<!))#:<#3*.$B-)"#/$B-)"#/$83596*3$0$
83596*3$$<#3*.$83596*3/$83596*3$0$
83596*3$$<#3*.$B-)"#/$83596*3$0$
$
F?6G?/015()345/1647(89:H(<;H=(
234A$$
B#%"5<B)%!.0$$$$$$$$$$$!"#$"%&'()*$
234@$$
4%)<<.$234@/$234@$0$
B-)"#$$
C&<#"543.$83596*3/$83596*3$0$
B-)"#$$$
C)#.$83596*3/$83596*3$0$
83596*3$$
B"43B)%D"%C.$83596*3$!/$83596*3&+/$83596*3$($0$
B-)"#$$$
-358#:.$83596*3$0$
83596*3$$
5)%!"-&E3.$83596*3$0$
83596*3$$
%3B-34#.$83596*3&+/$83596*3$($0$
83596*3$$
%3B%"4#.$83596*3&+/$83596*3$(/$B-)"#$"$,$0$
)0.JG?4/(P065?7714J()345/1647(89:9(<;9=($&-#,./"0$&'()*$
83596*3$$CFC'.$83596*3$0$
83596*3$$CFC6.$83596*3$0$
83596*3$$BD&C#:.$83596*3$0

-./012()345/1647(89:;(<;;=(
!"#$$!"#%&'()!*+,-#.$!"#/$!"#$0$
(
>?5/60(@?A./164.A()345/1647(89:B(<;;=(
1))-$$"--.$1234$0$
1))-$$"56.$1234$0$
1234$$37,"-.$234/$234$0$
1234$$37,"-.$&234/$&234$0$
1234$$37,"-.$1234/$1234$0$
1234$$8%3"#3%9:"5.$234/$234$0$
1234$$8%3"#3%9:"5.$&234/$&234$0$
1234$$8%3"#3%9:"5;7,"-.$234/$234$0$
1234$$8%3"#3%9:"5;7,"-.$&234/$&234$0$
1234$$-3<<9:"5.$234/$234$0$
1234$$-3<<9:"5.$&234/$&234$0$
1234$$-3<<9:"5;7,"-.$234/$234$0$
1234$$-3<<9:"5;7,"-.$&234/$&234$0$
1234$$5)#.$1234$0$
1234$$5)#;7,"-.$234/$234$0$
1234$$5)#;7,"-.$&234/$&234$0$
1234$$5)#;7,"-.$1234/$1234$0$
$
%?2/30?($66Q3<()345/1647(89:R(<;B=$$$
'1$230,4&52,6&$"#/&26&-#,./"0$&'()*&
234A$$#3'#,%3GH.$<"!*-3%GH/$B-)"#$I/B-)"#$52,6J$0$
234A$$#3'#,%3GHK%)L.$<"!*-3%GH/$234>$I/B-)"#$52,6J$0$
234A$$#3'#,%3GHK%)L.$<"!*-3%GH/$234A$I/B-)"#$52,6J$0$
$
234A$$#3'#,%3>H.$<"!*-3%>H/$234>$I/B-)"#$52,6J$0$
234A$$#3'#,%3>HK%)L.$<"!*-3%>H/$234@$I/B-)"#$52,6J$0$
234A$$#3'#,%3>HK%)L.$<"!*-3%>H/$234A$I/B-)"#$52,6J$0$
$
234A$$#3'#,%3@H.$<"!*-3%@H/$234@$I/B-)"#$52,6J$0$
234A$$#3'#,%3@HK%)L.$<"!*-3%@H/$234A$I/B-)"#$52,6J$0$
$
234A$$#3'#,%3(,13.$<"!*-3%(,13/$234@$I/B-)"#$52,6J$0$
$
234A$$<:"C)DGH.$<"!*-3%GHM:"C)D/$234@$I/B-)"#$52,6J$0$
234A$$<:"C)D>H.$<"!*-3%>HM:"C)D/$234@$I/B-)"#$52,6J$0$
234A$$<:"C)DGHK%)L.$<"!*-3%GHM:"C)D/$234A$I/B-)"#$52,6J$0$
234A$$<:"C)D>HK%)L.$<"!*-3%>HM:"C)D/$234A$I/B-)"#$52,6J$0$
%?2/30?($66Q3<()345/1647(S1/T($+U(89:R(<;B=$$
!"#$"%&'()*7&"068#"&9):;<=:!>?@>=:@>=@A?>:+;<9>:A(+@B&C&D$
234A$$#3'#,%3GHN)C.$<"!*-3%GH/$B-)"#/$B-)"#&43E$0$
234A$$#3'#,%3GHK%)LN)C.$<"!*-3%GH/$234>/$B-)"#&43E$0$
234A$$#3'#,%3GHK%)LN)C.$<"!*-3%GH/$234A/$B-)"#&43E$0$
$
234A$$#3'#,%3>HN)C.$<"!*-3%>H/$234>/$B-)"#&43E$0$
234A$$#3'#,%3>HK%)LN)C.$<"!*-3%>H/$234@/$B-)"#&43E$0$
234A$$#3'#,%3>HK%)LN)C.$<"!*-3%>H/$234A/$B-)"#&43E$0$
234A$$#3'#,%3@HK%)LN)C.$<"!*-3%@H/$234A/$B-)"#&43E$0$
$
234A$$#3'#,%3(,13N)C.$<"!*-3%(,13/$234@/$B-)"#$43E&0$
$
234A$$<:"C)DGHN)C.$<"!*-3%GHM:"C)D/$234@/$B-)"#&43E&0$
234A$$<:"C)D>HN)C.$<"!*-3%>HM:"C)D/$234@/$B-)"#&43E$0$
234A$$<:"C)DGHK%)LN)C.$<"!*-3%GHM:"C)D/$234A/$B-)"#&43E$0$
234A$$<:"C)D>HK%)LN)C.$<"!*-3%>HM:"C)D/$234A/$B-)"#&43E&0
'617?()345/1647(89:V(<BW=(
B-)"#$$5)&<3G.$83596*3$0$
234>$$5)&<3>.$83596*3$0$
234@$$5)&<3@.$83596*3$0$
234A$$5)&<3A.$83596*3$0$

Type Properties
Matrix / vector / integer / floating point
types.

Strict casting requirements.


Const / attribute / varying / uniform
variables.

Control Loops in GLSL


if (condition) {
statements;
} else {
statements;
}

Beware:
If Statement
Implementation

for (int i=0 ; i<10 ; i++) {


statements;
}
while (condition) {
statements;
}

Builtins
!"#$"%&'()*"#&!)#+),-"'&
'234567&89:29:&!6;56<73=&>?@I&2BAD&&"##$%%&'(!
"#$%!!&'(B-8?0?->.!!!!!!!!!%>"5$2!?@%=!A2:=$!
7'-+0!!&'(B-?>0H?R#.!!!!!!!$<"B1$!C.DE*'/*FDG'8C',HDG8IJ/DKIL*!
"#$%!!&'(,'?:A#*0#3.
)::;5<9:3&+E29:=&>?@C&2BBD&!"##$%%&'8!
+00*?=60#!"#$%!!&'(A#*0#3.!
+00*?=60#!"#$F!!&'(G-*@+'.!
+00*?=60#!"#$%!!&'(,-'-*.!
+00*?=60#!"#$%!!&'(H#$-><+*L,-'-*.!
+00*?=60#!"#$%!!&'(26'0?D#3,--*<Y.!
+00*?=60#!"#$%!!&'(26'0?D#3,--*<V.!
+00*?=60#!"#$%!!&'(26'0?D#3,--*<Z.!
+00*?=60#!"#$%!!&'(26'0?D#3,--*<F.!
+00*?=60#!"#$%!!&'(26'0?D#3,--*<%.!
+00*?=60#!"#$%!!&'(26'0?D#3,--*<[.!
+00*?=60#!"#$%!!&'(26'0?D#3,--*<\.!
+00*?=60#!"#$%!!&'(26'0?D#3,--*<].!
+00*?=60#!7'-+0!!&'()-&,--*<.!
!6;J5EK&89:29:=&>?@L&2BMD&!"##$%%&'(!
"+*L?>&!"#$%!!&'()*->0,-'-*.!
"+*L?>&!"#$%!!&'(5+$Q,-'-*.!!$<"B1$!C.DE*'/*FDG'8C',HD/(8DKI)*!
"+*L?>&!"#$%!!&'()*->0H#$-><+*L,-'-*.!
"+*L?>&!"#$%!!&'(5+$QH#$-><+*L,-'-*.!
"+*L?>&!"#$%!!&'(D#3,--*<1!9.!!!!!!!!H,F&01DH";/$;=@2$34425%!
"+*L?>&!7'-+0!!&'()-&)*+&,--*<.

.#)/0"1$&'()*"#&!)#+),-"'&
!
'! 234567&89:29:&!6;56<73=&>?@A&2BCD&!"##$%%&'(!
"#$%!!&'()*+&,-'-*.!
"#$%!!&'()*+&/+0+1&'(2+3/*+45677#*89.!
7'-+0!!&'()*+&/#:0;.!!!!!!!!)*+,-./&01+2"03442567!
!6;J5EK&+E29:=&>?@L&2BMD&!"##$%%&'8!
"+*L?>&!"#$%!!&'(,-'-*.!
"+*L?>&!"#$%!!&'(H#$-><+*L,-'-*.!
"+*L?>&!"#$%!!&'(D#3,--*<1!9.!!!!!!!!H,F&01DH";/$;=@2$34425%!
"+*L?>&!7'-+0!!&'()-&)*+&,--*<.
'234567&+E29:&!6;56<73=&>?@A&2BCD&!"##$%%&'8!
"#$%!!&'()*+&,--*<.!!!!!!!!!!!!!!!!9:;$1!#4425:<"=$%!
=--'!!&'()*->0)+$?>&.!

,F+-$G+1&S81'$)1$'&>?@B&2BBD&
&
$->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.!!
$->80!?>0!&'(2+3D#306*#,--*<8.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*@!"#$%!!&'(NL#B'+>#D1&'(2+3D#306*#,--*<89.!
6>?7-*@!"#$%!!&'(NL#B'+>#I1&'(2+3D#306*#,--*<89.!
6>?7-*@!"#$%!!&'(NL#B'+>#S1&'(2+3D#306*#,--*<89.!
!
6>?7-*@!"#$%!!&'(X=C#$0B'+>#H1&'(2+3D#306*#,--*<89.!
6>?7-*@!"#$%!!&'(X=C#$0B'+>#D1&'(2+3D#306*#,--*<89.!
6>?7-*@!"#$%!!&'(X=C#$0B'+>#I1&'(2+3D#306*#,--*<89.!
6>?7-*@!"#$%!!&'(X=C#$0B'+>#S1&'(2+3D#306*#,--*<89.

.5E3&2;5E:&T&U5=4765R3;&
,-:L*?&;0!ZYY[!2?Q#!^#?='#>!!;00:_``@#4a$3`!
B'#+8#!8#><!7##<=+$Q`$-**#$0?->8`$-@@#>08!0-!&'8'b@#4a$3!
X:#>cM!?8!+!*#&?80#*#<!0*+<#@+*Q!-7!H?'?$->!c*+:;?$8!E>$a!
N3$#:0!+8!>-0#<!=#'-4d!?7!<?8$*#:+>$?#8!=#04##>!0;?8!&6?<#!+><!0;#!
cMHM!8:#$?7?$+0?->d!=#'?#"#!0;#!8:#$e!
I#"?8#<!ZYY[fVVfZ\!
!
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

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

Curve Usage Demo

Curve Representations
Explicit: y = f(x)
y

y = mx + b
Easy to generate points
Must be a function: big limitationvertical lines?

Curve Representations
Explicit: y = f(x)
y

y = mx + b
Easy to generate points
Must be a function: big limitationvertical lines?

x
y

Implicit: f(x,y) = 0
x2 + y 2 ! r 2 = 0
+Easy to test if on the curve
Hard to generate points

Curve Representations
Explicit: y = f(x)
y

y = mx + b
+ Easy to generate points
Must be a function: big limitationvertical lines?

x
y

Implicit: f(x,y) = 0
2

u="/2

x + y !r = 0

u= "

+Easy to test if on the curve


Hard to generate points

u=0
x

Parametric: (x,y) = ( f(u), g(u))


( x, y ) = (cos u , sin u )
+Easy to generate points

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

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

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
position

Continuous in
position and tangent
vector

11

Continuous in
position, tangent,
and curvature

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:
p(u) = [u

point that
gets drawn

&2
$ "3
u 1$
$0
$
%1

"2 1 1 # & p1 #
3 "2 "1!! $$ p2 !!
0 1 0 ! $ !p1 !
!$ !
0 0 0 " %!p2 "
basis

control matrix
(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:
p(u) = [u

point that
gets drawn

&2
$ "3
u 1$
$0
$
%1

"2 1 1 # & p1 #
3 "2 "1!! $$ p2 !!
0 1 0 ! $ !p1 !
!$ !
0 0 0 " %!p2 "
basis

control matrix
(what the user gets to pick)

& 2u 3 " 3u 2 + 1# & p1 #


$
!
3
2 !$
" 2u + 3u ! $ p2 !
$
p(u ) = $ 3
2
u " 2u + u ! $ !p1 !
$
!$
!
3
2
$% u " u
!" %!p2 "
15

4 Basis Functions

Four Basis Functions for Hermite splines


T

& 2u 3 " 3u 2 + 1# & p1 #


$
!
3
2 !$
" 2u + 3u ! $ p2 !
$
p(u ) = $ 3
u " 2u 2 + u ! $ !p1 !
$
!$
!
3
2
$% u " u
!" %!p2 "

4 Basis Functions

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 builtin C1 continuity.

P2
P1
tangent at pi = s(pi+1 - pi-1)
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 builtin C1 continuity.

chalkboard

18

Catmull-Rom Spline Matrix


[

p(u) = u3 u2

& ! s 2! s s !2
$ 2s s !3 3!2s
u 1$
$ !s 0
s
$
0
%0 1

spline coefficients

CR basis

s # & p1 #
$ !
! s!! $ p2 !
0 ! $ p3 !
!$ !
0 " % p4 "
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
$2s s ! 3 3 ! 2s
3
2
[x y z ] = u u u 1 $
$! s 0
s
$
1
0
%0

spline coefficients

CR basis

20

s # & x1
$
! s !! $ x2
0 ! $ x3
!$
0 " % x4

y1 z1 #
y2 z 2 !!
y3 z 3 !
!
y4 z 4 "

control vector

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 builtin C1 continuity.

Catmull-Rom Spline Matrix


[

p(u) = u3 u2

& ! s 2! s s !2
$ 2s s !3 3!2s
u 1$
$ !s 0
s
$
0
%0 1

CR basis

s # & p1 #
$ !
! s!! $ p2 !
0 ! $ p3 !
!$ !
0 " % p4 "
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
x(u) = axu3+bxu2+cxu+dx
y(u) = ayu3+byu2+cyu+dy
z(u) = azu3+bzu2+czu+dz

In matrix notation

[x(u )

y (u ) z (u )] = u 3 u 2

&ax
$b
x
u 1$
$ cx
$d
% x

11

ay
by
cy
dy

az #
bz !
!
cz !
d z !"

Catmull-Rom Spline Matrix in 3D


&! s 2 ! s s ! 2
$2 s s ! 3 3 ! 2s
[x(u ) y(u ) z (u )] = u 3 u 2 u 1 $
$! s 0
s
$
1
0
%0

CR basis

12

s # & x1
$x
!
! s! $ 2
0 ! $ x3
!$
0 " % x4

y1 z1 #
!
y2 z 2 !
y3 z 3 !
!
y4 z 4 "

control vector

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,


points P1 and P2 are off the curve
P'(u=0) = 3(P1-P0), P'(u=1) = 3(P3 P2)

P(u=1) = P3

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
[x y z ] = u 3 u 2 u 1 $
$! 3
$
%1

3 ! 3 1 # & x1
$x
!
! 6 3 0! $ 2
3 0 0! $ x3
!$
0 0 0 " % x4

Bezier basis

14

y1 z1 #
y2 z 2 !!
y3 z 3 !
!
y4 z4 "

Bezier
control vector

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
position

Continuous in
position and tangent
vector

16

Continuous in
position, tangent,
and curvature

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

Hermite
YES
Bezier
YES
Catmull-Rom YES
Natural
NO
B-Splines
YES

Continuity

Interpolation

C1
C1
C1
C2
C2

YES
YES
YES
YES
NO

Summary
Cant 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
Its 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 its
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
$ 3 !6 3
1$
MBs =
3
6 $! 3 0
$
4
1
%1
& Pi ! 3 #
$ Pi ! 2 !
!
GBsi = $
$ Pi ! 1 !
!
$
% Pi "
21

1#
!
0!
0!
!
0"

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
$2s s ! 3 3 ! 2s
[x y z ] = u 3 u 2 u 1 $
$! s 0
s
$
1
0
%0

CR basis
22

s # & x1
$x
!
! s! $ 2
0 ! $ x3
!$
0 " % x4

y1 z1 #
!
y2 z 2 !
y3 z 3 !
!
y4 z 4 "

control vector

How to Draw Spline Curves


Whats 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

Você também pode gostar