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 .

u < 8*PI . } glEnd() . u += du) { x = cos(u) . du = PI/32 . y = sin(u) . z = 0 .z) .z) .y. glVertex3f(x. y = sin(u+du) . 224 . z = 0 . x = cos(u+du) . for( u = 0 . glVertex3f(x. 2D Parametric Curves Code for the parametric circle: glBegin(GL_LINES) .y.

y and z will trace out a circlular helix as 0 ≤ θ ≤ 2π . 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. 225 . For example: x = cos(θ) y = sin(θ) z=θ x.

3D Parametric Curves Drawing of the parametric helix: 226 .

z) .y. x = cos(u+du) .y. } glEnd() . glVertex3f(x. u += du) { x = cos(u) . for( u = 0 . 227 . y = sin(u) . 3D Parametric Curves Code for the parametric helix: glBegin(GL_LINES) . z = u . glVertex3f(x.z) . z = u+du . du = PI/32 . u < 8*PI . y = sin(u+du) .

228 . v) y = y(u. 3D Parametric Surfaces In three dimensions a parametric surface is defined by: x = x(u. y and z will trace out a sphere as 0 ≤ θ ≤ 2π and 0 ≤ φ ≤ 2π . v) Where u and v are the parameters that are free to vary. For example: x = cos(θ) sin(φ) y = sin(θ) sin(φ) z = cos(φ) x. v) z = z(u.

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 .

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 For example if n = 3.

        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 Curves Continuing.

• Generally. v) n X m   X i j p~(u. and u and v for surfaces have an infinite domain. only an interval of values for the domain of u and v is desired. if n = m = 3 then 16 coefficient vectors will be needed to evaluate the formula and determine the value of p~ • u for curves. 233 . 1]. though. v) So. restrict the range of u and v to [0. without lost of generality. • So. v) =  ~ c ij u v i=0 j=0 z(u. Polynomial Parametric Surfaces   x(u. v) =   y(u.

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

235 . Rewrite this equation. Interpolation using Cubic Polynomials Recall the cubic polynomial 3 X p~(u) = uk~c k k=0 Requires 4 vectors of coefficients in order for it to be completely determined...

using a matrix representation of the coefficient vectors. p~(u) = U · M · G Call M the basis matrix. 236 .. 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. Call G the Geometry matrix.

Interpolation using Cubic Polynomials For example. 237 . 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.

238 . They are called control points. • 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. contains 4 points. G. • Analogous to the 2 control points in the previous linear interpolation example. Interpolation using Cubic Polynomials • The geometry matrix.

The construction the Hermite basis matrix is as follows: Given 4 control points: p~1 . 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. p~2 . 239 . p~3 . The Hermite Basis The Hermite basis is named after the Mathematician.

The Hermite Basis Recall. 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 . £ 3 2 ¤ U= u u u1 so.

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 .

(2u3 − 3u2 + 1)~ p1 + (−2u3 + 3u2 )~ p4 + p~(u) = (u3 − 2u2 + u)~ p3 + (u3 − u2 )~ p4 242 . The Hermite Basis The Hermitian basis matrix:   2 −2 1 1    −3 3 −2 −1  M =     0 0 1 0    1 0 0 0 So.

The Hermite Basis A picture of the Hermitian basis functions: 243 .

Example Hermite Curves 244 .

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. 245 . Comparison of Curve Types Curves differ by basis matrix. the curve is called C n continuous.

Example NURBS Surface 246 .