You are on page 1of 26

# OpenGL Notes a

Stu Pomerantz
smp@psc.edu
http://www.psc.edu/~smp
December 8, 2004

a Mostmaterial is adapted from: OpenGL ARB, et. al, “The OpenGL Pro-
gramming Guide”, Third Ed., Reading: Addison-Wesley, 1999

1
2D Parametric Curves

## In two dimensions a parametric curve is defined by:

x = x(u)
y = y(u)
Where u is the parameter that is free to vary. For example:

x = cos(θ)
y = sin(θ)
x and y will trace out a circle as 0 ≤ θ ≤ 2π .

222
2D Parametric Curves

## Drawing of the parametric circle:

223
2D Parametric Curves

## Code for the parametric circle:

glBegin(GL_LINES) ;
du = PI/32 ;
for( u = 0 ; u < 8*PI ; u += du) {
x = cos(u) ; y = sin(u) ; z = 0 ;
glVertex3f(x,y,z) ;

x = cos(u+du) ; y = sin(u+du) ; z = 0 ;
glVertex3f(x,y,z) ;
}
glEnd() ;

224
3D Parametric Curves

## In two dimensions a parametric curve is defined by:

x = x(u)
y = y(u)
z = z(u)
Where u is the parameter that is free to vary. For example:

x = cos(θ)
y = sin(θ)
z=θ
x, y and z will trace out a circlular helix as 0 ≤ θ ≤ 2π .

225
3D Parametric Curves

## Drawing of the parametric helix:

226
3D Parametric Curves

## Code for the parametric helix:

glBegin(GL_LINES) ;
du = PI/32 ;
for( u = 0 ; u < 8*PI ; u += du) {
x = cos(u) ; y = sin(u) ; z = u ;
glVertex3f(x,y,z) ;

## x = cos(u+du) ; y = sin(u+du) ; z = u+du ;

glVertex3f(x,y,z) ;
}
glEnd() ;

227
3D Parametric Surfaces

## In three dimensions a parametric surface is defined by:

x = x(u, v)
y = y(u, v)
z = z(u, v)
Where u and v are the parameters that are free to vary. For
example:

x = cos(θ) sin(φ)
y = sin(θ) sin(φ)
z = cos(φ)
x, y and z will trace out a sphere as 0 ≤ θ ≤ 2π and 0 ≤ φ ≤ 2π .

228
3D Parametric Surfaces

## Drawing of the parametric sphere:

229
Polynomial Parametric Curves

n
X
p~(u) = uk~c k

k=0

where

 
cxk
k
 
~c =  cyk 

czk

230
Polynomial Parametric Curves

For example if n = 3,
3
X
p~(u) = uk~c k

k=0

expands to:

       
cx3 cx2 cx1 cx0

3
 2
     
 + u  cy2  + u  cy1  +  cy0 
p~(u) = u  cy3       

## cz3 cz2 cz1 cz0

231
Polynomial Parametric Curves

Continuing,
       
cx3 cx2 cx1 cx0
3
 
2
    
p~(u) = u  cy3  + u  cy2  + u  cy1  +  cy0 
     

cz3 cz2 cz1 cz0

expands to

## x(u) = cx3 u3 + cx2 u2 + cx1 u + cx0

p~(u) = y(u) = cy3 u3 + cy2 u2 + cy1 u + cy0
z(u) = cz3 u3 + cz2 u2 + cz1 u + cz0

## Notice that 4 coefficient vectors which must be specified.

232
Polynomial Parametric Surfaces

 
x(u, v) n X m
  X i j
p~(u, v) = 
 y(u, v) =
 ~
c ij u v
i=0 j=0
z(u, v)

## So, if n = m = 3 then 16 coefficient vectors will be needed to

evaluate the formula and determine the value of p~
• u for curves, and u and v for surfaces have an infinite domain.
• Generally, though, only an interval of values for the domain of
u and v is desired.
• So, without lost of generality, restrict the range of u and v to
[0, 1].

233
Interpolation Revisited

Two points, p~0 and p~1 , can be linearly interpolated like this:

p~ = a p~0 + (1 − a) p~1
where a ∈ [0, 1]
In this context, interpolation is also called blending. The points are
mixed together by the a and 1 − a which are weights.
The points p~0 and p~1 can be thought of as control points since they
influence (constrain) the final interpolated value.
This is similar, but not identical to, OpenGL’s blending functions.
And, in this case the purpose is not to produce transparent
surfaces but rather to produce a new interpolated point.

234
Interpolation using Cubic Polynomials

3
X
p~(u) = uk~c k

k=0

## Requires 4 vectors of coefficients in order for it to be completely

determined.
Rewrite this equation...

235
Interpolation using Cubic Polynomials

## p~(u) = [x(u) y(u) z(u)] =

   
m11 m12 m13 m14 g1x g1y g1z
   
 m21 m22 m23 m24
¤    g g2y g2z 
£ 3 2   2x
u u u 1 · ·

 m31 m32 m33 m34   g3x g3y g3z 
   
m41 m42 m43 m44 g4x g4y g4z

Or more briefly,
p~(u) = U · M · G

## Call M the basis matrix. Call G the Geometry matrix.

236
Interpolation using Cubic Polynomials

For example,

## x(u) = (u3 m11 + u2 m21 + um31 + m41 )g1x +

(u3 m12 + u2 m22 + um32 + m42 )g2x +
(u3 m13 + u2 m23 + um33 + m43 )g3x +
(u3 m14 + u2 m24 + um34 + m44 )g4x +

## This form emphasizes that the curve is a weighted sum of the

elements of G (geometry) matrix.

237
Interpolation using Cubic Polynomials

## • The geometry matrix, G, contains 4 points. They are called

control points.
• Analogous to the 2 control points in the previous linear
interpolation example.
• The meaning of these 4 points is dependant on the basis matrix
M.
• The basis matrix M affects
– The continuity of the curve.
– Which and whether control points are interpolated.

238
The Hermite Basis

## The Hermite basis is named after the Mathematician. The

construction the Hermite basis matrix is as follows:
Given 4 control points: p~1 , p~2 , p~3 , p~4
• Constrain the curve so that it interpolates p~1 at u = 0 and p~4
at u = 1
• Let p~2 and p~3 be the tangent vectors for p~1 and p~4 respectively.

239
The Hermite Basis

Recall,
£ 3 2
¤
U= u u u1
so,
0
£ 2
¤
U = 3u 2u 1 0

## There are 4 equations and 4 unknowns:

p~1 = [0 0 0 1] (u = 0)
p~4 = [1 1 1 1] (u = 1)
p~2 = [0 0 1 0] (u0 = 0)
p~3 = [3 2 1 0] (u0 = 1)

240
The Hermite Basis

## Rewriting the 4 equations above as a matrix:

   
p~1 0 0 0 1
   
 p~   1 1 1 1 
 4  
=

 
 p~2   0 0 1 0 
   
p~3 3 2 1 0

## For these equations to be satisfied M , the Hermitian basis matrix

must be the inverse of this matrix.

241
The Hermite Basis

 
2 −2 1 1
 
 −3 3 −2 −1 
M =
 

 0 0 1 0 
 
1 0 0 0
So,

## (2u3 − 3u2 + 1)~

p1 + (−2u3 + 3u2 )~
p4 +
p~(u) =
(u3 − 2u2 + u)~
p3 + (u3 − u2 )~
p4

242
The Hermite Basis

## A picture of the Hermitian basis functions:

243
Example Hermite Curves

244
Comparison of Curve Types

## Curves differ by basis matrix.

Hermite Bezier Non-Uniform B-Spline Catmull-Rom
A Y Y N Y
B Y N N Y
C C0 C0 C2 C1
A = Interpolates some control points
B = Interpolates all control points
C = Inherent Continuity

## If the direction and magnitude of the nth derivative are equal at

the joint point, the curve is called C n continuous.

245
Example NURBS Surface

246