cs 376 introduction to computer graphics 04 / 20 / 2007 instructor: michael eckmann

28
CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Upload: corey-davis

Post on 03-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

CS 376Introduction to Computer Graphics

04 / 20 / 2007

Instructor: Michael Eckmann

Page 2: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Today’s Topics• Questions?

• review ray-polygon intersections

• Piecewise Cubic Parametric curves– Hermite– Bezier– Spline

Page 3: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Ray / Polygon Intersection• The first step in Ray/Polygon intersection is to compute the intersection

of the ray with the plane that the polygon lives on.

• At this point we have

– the plane equation Ax+By+Cz+D = 0

– the point of intersection PI = (x

I, y

I, z

I) of the ray and plane

– the vertices V = (xj, y

j, z

j) of the polygon on that plane

• A technique that makes computation easier at this point is to

orthographically project the polygon onto either the x-y, y-z, or z-x plane.

To do this we just have to ignore the same coordinate of all of the

vertices of the polygon and we end up with 2d points.

• The best coordinate to ignore is the one whose corresponding coefficient

in the plane equation is dominant (largest absolute value.)

Page 4: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Ray / Polygon Intersection• The best coordinate to ignore is the one whose corresponding coefficient

in the plane equation is dominant (largest absolute value.)

• It gives us a polygon in 2d with the largest area of the 3 choices.

Page 5: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Ray / Polygon Intersection• Note: this technique is a combination of ideas from

– Computer Graphics Principles and Practive by Foley, Van Dam, Feiner and Hughes, 1996 Addison-Wesley

– Dr. G. Drew Kessler's csc313 course 1999, Lehigh Univ. and – Dr. Xiaoyu Zhang's Advanced Computer Graphics & Visualization page

http://courses.csusm.edu/cs697exz/ray_polygon.htm

• Example: If the plane equation is

–0.2 x +0.4 y –0.8945 z +5 = 0

we would orthographically project the polygon onto the x-y plane (ignore

the z coordinate of each vertex) This will yield the largest (area) projection.

• Once we have done this projection, we can translate the intersection point PI to

the origin of this new 2d space (call it u-v coordinates).

• Then determine whether the origin is inside or outside the polygon by counting

the number of polygon edge crossings with the positive u axis.

Page 6: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Ray / Polygon Intersection• The next algorithm distinguishes between several different situations to

determine when to add 1 to the number of times the polygon's edges cross the positive u axis.

• For some edge– 1) if the signs of the v coordinate in both vertices are the same, then that

edge DOES NOT cross the positive u-axis– 2) if the signs of the v coordinate in both vertices are different AND both u

coordinates are positive then that edge DOES cross the positive u-axis.– 3) if the signs of the v coordinate in both vertices are different AND one of

the u coordinates is positive then compute the intersection of the line with v=0. If the u coordinate of this intersection is positive then that edge DOES cross the positive u-axis.

– 4) if the signs of the v coordinate in both vertices are different AND one of the u coordinates is positive then compute the intersection of the line with v=0. If the u coordinate of this intersection is not positive then that edge DOES NOT cross the positive u-axis.

– For cases 2 and 3 above, add 1 to a counter.

• Do this for all edges in the polygon in order and then check the value of the counter --- if it is odd

then the point is in the interior of the polygon and hence the ray intersects with the polygon.

Page 7: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Ray / Polygon Intersection• The n vertices of the polygon are (u

i, v

i) where i goes from 0 to n-1.

• Pseudocode to determine if the origin is inside the polygon

initialize numCross to 0

if v0 > 0 then signHold1 = +1 else signHold1 = -1

for each edge (ua, v

a) to (u

b, v

b) {

if vb > 0, signHold2 = +1, else signHold2 = -1

if signHold1 != signHold2 {

if ua and u

b are both > 0, then numCross++

if one of ua or u

b is > 0, then {

if (crossPosUAxis(ua, v

a, u

b, v

b) then numCross++ }

}

signHold1 = signHold2}

if numCross is odd, then the ray and polygon intersect

Page 8: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Ray / Polygon Intersectionpseudocode for crossPosUAxis

recall the parametric equations of a line:

u = ub+ (u

a- u

b)t

v = vb+ (v

a- v

b)t = 0 (because we're looking for intersection with u axis)

so, use the second equation to solve for t then solve the first for u and get

u = (ub + ( u

a - u

b) * ( v

b / (v

b -v

a) ) )

crossPosUAxis(ua, v

a, u

b, v

b)

if ( (ub + ( u

a - u

b) * ( v

b / (v

b -v

a) ) ) > 0 )

return true

else

return false

Page 9: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Polygonal Meshes• A collection of edges, vertices and polygons

– which are connected– and where each edge is shared by at most 2 polygons (example on

board)– each vertex is shared by at least 2 edges

• When polygonal meshes are created interactively– care must be taken to make sure that the above constraints are

fulfilled.

• Different ways to store a polygonal mesh have consequences on – space (to store the mesh data)– and time (to operate on the polygonal mesh)

Page 10: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Polygonal Meshes• Some operations that are common on polygonal meshes

– find all edges containing a particular vertex– find all the polygons sharing a particular edge or vertex– find the two vertices for an edge– find all the edges of a polygon– determining if all the conditions of a polygonal mesh are met

• How the polygonal mesh is represented has consequences on the speed of

those operations and on how much space is needed to store a polygonal

mesh.

Page 11: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Polygonal Meshes• Representation schemes:

– represent each polygon as a list of vertices in order around the polygon

• edges exist between each pair of successive vertices as well as between the last and the first

• efficient in space for a single polygon • inefficient in space for a polygonal mesh, why?• interactively working on a polygonal mesh represented this way

would be time consuming (e.g. move a vertex --- need to find all the polygons that contain it and move it...)

• etc.– store all the vertices in a list, and represent a polygon as an ordered

list of pointers into the vertex list.• easier to find all polygons containing a vertex

– store all the vertices in a list, and all the edges in an edge list* and represent a polygon as an ordered list of pointers into the edge list

* edge list contains pointers to two vertices and pointers to one or two polygons

• can easily find all polys that share an edge now

Page 12: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Polygonal Meshes• In all 3 representation schemes just described it is still not easy to find all

the edges containing a particular vertex – in the description of a vertex, could add pointers to all the edges

Page 13: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Curves• Some motivations for having mathematical equations define curves and

surfaces• 1) polylines and polygons are 1st degree piecewise linear approximations

to curves and surfaces (they are approximations unless they are the exact description of the outline or shape)

– if they are approximations, then it takes large numbers of points to describe them for more accurate representations == BAD

– if interactively building these surfaces (or edges/outlines) then the more points, the more tedious it is to build

• We want to be able to get more accurate representations of curved

surfaces (or edges/outlines) than polygons or polylines give us AND we

want to be able to represent them with less points. This will benefit us by

having less storage and be more easily built interactively.

Page 14: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Curves• Let's consider 2d dimensional curves which can be later generalized to 3d

surfaces.• Instead of using piecewise linear functions, we use piecewise smooth

curves. These are of a higher degree than linear functions.• We have several choices

– a) we can use functions like y = f(x) • problem that for each value of x there is one and only one y value

– can't represent circles/ellipses etc.• problem of not being able to easily describe a rotated version of

the function• problem that you can't represent vertical lines and is problematic

to represent curves with a tangent/slope of infinity– b) we can use implicit equations like f(x,y) = 0 (e.g. a circle of radius

1 like: x2 + y2 – 1 = 0)• may have more solutions than we want --- e.g. to model half a

circle you'd need constraints on the values of x or y or both.• joining 2 implicitly defined curves is hard to make the tangents at

the join point agree

Page 15: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Curves• We have several choices

– c) we can use parametric equations of the curve• x = f(t) and y = g(t)

– where f(t) and g(t) are typically cubic polynomials• the problems of the other 2 choices are solved as we shall see

– we typically use cubic polynomials because• 1) quadratic or linear polynomials do not allow a curve segment

to be described with 2 endpoints and specific slopes at the 2 endpoints

• 2) higher degree (than 3) polynomials are more compute intensive and allow (possibly) unwanted artifacts in the curve

• A cubic polynomial to define a finite curve segment has 4 coefficients

per parametric equationx(t) = a

xt3 + b

xt2 + c

xt + d

x y(t) = a

yt3 + b

yt2 + c

yt + d

y

z(t) = azt3 + b

zt2 + c

zt + d

z where 0 <= t <= 1

Page 16: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Curves• x(t) = a

xt3 + b

xt2 + c

xt + d

x

• y(t) = ayt3 + b

yt2 + c

yt + d

y

• z(t) = azt3 + b

zt2 + c

zt + d

z where 0 <= t <= 1

• can be written in matrix form as:

• Q(t) = [x(t) y(t) z(t)] = T C • where T = [t3 t2 t 1] and

[ax

ay

az]

• C = [bx

by

bz]

[cx

cy

cz]

[dx d

y d

z]

Page 17: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann
Page 18: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann
Page 19: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Curves• Continuity - when joining two curve segments together (piecewise

curves). There are several different kinds of continuity to consider• Geometric continuity

– G0 geometric continuity = the curve segments are joined together

– G1 geometric continuity = the curve segments are joined together and

the directions of tangent vectors are equal at the point where they are joined

• Parametric continuity– C

0 parametric continuity = same as G

0 geometric continuity

– C1 parametric continuity = the curve segments are joined together and

the directions AND magnitudes of tangent vectors are equal at the point where they are joined

– Ck parametric continuity = the curve segments are joined together and

the directions AND magnitudes of all the derivatives up to the kth are equal at the point where they are joined (that is, the 1st through kth derivatives exist and are continuous themselves)

Page 20: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Curves• Let me draw on the board some example joined curve segments and point

out their continuity.

Page 21: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Curves• There are several families of cubic polynomial curve segments that are

determined by different things– Hermite

• determined by the 2 endpoints and the tangent vector at each of the 2 endpoints

– Bezier• determined by the 2 endpoints P

1 & P

4 and 2 other intermediate

points P2 & P

3 not on the curve

– the tangent vectors at the end points are the vectors P1P

2 and

P3P

4– which are [P

2 – P

1] and [P

4 – P

3]

– Spline• determined by 4 specific points

• Note: there can be higher degree polynomials of these families of curves

(see a few slides ahead for a 4th degree polynomial Bezier curve segment)

Page 22: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Hermite Curve segment examples (on the board)

Page 23: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Bezier Curve segment examples

Page 24: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

more Bezier Curve segment examples

Page 25: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Recall• x(t) = a

xt3 + b

xt2 + c

xt + d

x

• y(t) = ayt3 + b

yt2 + c

yt + d

y

• z(t) = azt3 + b

zt2 + c

zt + d

z where 0 <= t <= 1

• can be written in matrix form as:

• Q(t) = [x(t) y(t) z(t)] = T C • where T = [t3 t2 t 1] and

[ax

ay

az]

• C = [bx

by

bz]

[cx

cy

cz]

[dx d

y d

z]

Page 26: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Basis Matrix and Geometry Vector• Q(t) = [x(t) y(t) z(t)] = T C

• where T = [t3 t2 t 1] and

[ax

ay

az]

• C = [bx

by

bz]

[cx

cy

cz]

[dx d

y d

z]

• can be rewritten so that C = M G where – M is a 4x4 matrix called the Basis Matrix and – G is a 4x1 column vector called the Geometry Vector

• so Q(t) = T M G

Page 27: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Recall• Q(t) = [x(t) y(t) z(t)] = T M G• where T = [t3 t2 t 1] and

[m11

m12

m13

m14

]• M = [m

21 m

22 m

23 m

24]

[m31

m32

m33

m34

][m

41 m

42 m

43 m

44]

[G1]

• G = [G2]

[G3]

[G4]

• look at x(t) = (t3 m11

+ t2 m21

+ t m31

+ m41

)g1x

+

Page 28: CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Blending functions• look at x(t) • x(t) = (t3 m

11+ t2 m

21 + t m

31 + m

41)g

1x + (t3 m

12+ t2 m

22 + t m

32 + m

42)g

2x +

(t3 m13

+ t2 m23

+ t m33

+ m43

)g3x

+ (t3 m14

+ t2 m24

+ t m34

+ m44

)g4x

where g1x

is the x coordinate of G1

similar equations for y(t) and z(t)

the curve is a weighted sum of the elements of the Geometry Matrix where the weights are cubic polynomials of t which are called the Blending Functions

the Blending Functions B are given by B = T Mso Q(t) = B G