cs 376 introduction to computer graphics 02 / 14 / 2007 instructor: michael eckmann

35
CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Upload: jocelyn-mcbride

Post on 20-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

Michael Eckmann - Skidmore College - CS Spring 2007 Clipping Liang-Barsky line clipping –based on parametric equation of a line given end points (x 0, y 0 ) and (x end, y end ) the parametric equations of a line are: x = x 0 + u (x end – x 0 ) y = y 0 + u (y end – y 0 ) 0

TRANSCRIPT

Page 1: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

CS 376Introduction to Computer Graphics

02 / 14 / 2007

Instructor: Michael Eckmann

Page 2: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Today’s Topics• Questions?

• Programming assignment 02 posted

• Clipping lines with Liang Barsky

• Clipping polygons

• 3d– right handed vs. left handed coordinate systems– 3 dimensional vectors– dot product– cross product– parametric equation of a line in 3 dimensions– equation of a plane in 3 dimensions

Page 3: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Clipping• Liang-Barsky line clipping

– based on parametric equation of a line• given end points (x0 , y0) and (xend , yend) the parametric equations

of a line are:• x = x0 + u (xend – x0)• y = y0 + u (yend – y0)• 0 <= u <= 1

– recall that our clipping window has the following vertices in the world (xL, yB), (xR, yB), (xR, yT), (xL, yT)

– and a point (x,y) is visible ifxL <= x <= xRyB <= y <= yT

– replace the parametric equations for x and y in those inequalitesxL <= x0 + u (xend – x0) <= xRyB <= y0 + u (yend – y0) <= yT

Page 4: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Liang-Barsky Line ClippingxL <= x0 + u (xend – x0) <= xRyB <= y0 + u (yend – y0) <= yT

– which can be expressed as 4 inequalities of the form• u pk <= qk, where k = 1, 2, 3, or 4 and

– p1= (x0 – xend) p2= (xend – x0) p3= (y0 – yend) p4= (yend – y0)– q1= (x0 – xL) q2= (xR – x0) q3= (y0 – yB) q4= (yT – y0)

example: xL <= x0 + u (xend – x0) subtract x0 from both sides: xL – x0 <= u (xend – x0) multiply both sides by –1 and reverse the <= to >= : x0 – xL >= u (x0 – xend) which is the same as: u (x0 – xend ) <= x0 – xLwhich is: u p1 < = q1

Page 5: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Liang-Barsky Line ClippingxL <= x0 + u (xend – x0) <= xRyB <= y0 + u (yend – y0) <= yT

– which can be expressed as 4 inequalities of the form• u pk <= qk, where k = 1, 2, 3, or 4 and

– p1= (x0 – xend) p2= (xend – x0) p3= (y0 – yend) p4= (yend – y0)– q1= (x0 – xL) q2= (xR – x0) q3= (y0 – yB) q4= (yT – y0)

– a line parallel to a clipping edge has pk= 0, where k represents the edge to which it is parallel: 1=Left, 2=Right, 3=Bottom, 4=Top

– if (pk== 0 and qk< 0) then line completely outside boundary, done– if (pk== 0 and qk>= 0) then the line is inside the boundary– if (pk< 0) then the line goes from outside to inside of the infinite

extension of a clipping window edge– if (pk> 0) then the line goes from inside to outside of the infinite

extension of a clipping window edge

Page 6: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Liang-Barsky Line Clipping– Recall what k's mean: 1=Left, 2=Right, 3=Bottom, 4=Top– when pk is non-zero, we find the intersection of the line with the infinite

extension of the clipping window edge k• recall that u pk <= qk so this intersection is simply when u = qk/pk

– initialize u1 to be 0 and u2 to be 1– For each of the four infinite extensions of a clipping window edge we

calculate either r1 or r2• r1 is the u parameter for the intersection of the line with the edge when

coming from the outside of the clipping window to the inside (p < 0)• r2 is the u parameter for the intersection of the line with the edge when

coming from the inside of the clipping window to the outside (p > 0)• if p<0, update u1 to be r1 if it results in a shorter line (i.e. if r1 > u1)• if p>0, update u2 to be r2 if it results in a shorter line (i.e. if r2 < u2)• if u1 > u2 we reject the line, and we're done.

– if it makes it through all four boundary checks without rejection, then the line to be drawn is between u1 and u2.

Page 7: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Liang-Barsky Line Clipping• Example of Liang-Barsky line clipping algorithm in action

– (x0,y0) = (10,5)– (xend,yend) = (30,30)– Clipping window: xL = 20, xR = 35, yB = 10, yT = 20.– p1= (x0 – xend) = -20– p2= (xend – x0) = 20– p3= (y0 – yend) = -25– p4= (yend – y0) = 25– q1= (x0 – xL) = -10– q2= (xR – x0) = 25– q3= (y0 – yB) = -5– q4= (yT – y0) = 15– u1= 0 and u2= 1 to start, let's run the algorithm on the board

Page 8: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Liang-Barsky Line Clipping• Another example

– (x0,y0) = (10,18)– (xend,yend) = (25,25)– Clipping window: xL = 20, xR = 35, yB = 10, yT = 20.– p1= (x0 – xend) = -15– p2= (xend – x0) = 15– p3= (y0 – yend) = -17– p4= (yend – y0) = 17– q1= (x0 – xL) = -10– q2= (xR – x0) = 25– q3= (y0 – yB) = 8– q4= (yT – y0) = 2– u1= 0 and u2= 1 to start, let's run the algorithm on the board

Page 9: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Line Clipping• Liang-Barsky (L-B) vs. Cohen-Sutherland (C-S)

– L-B in general more efficient• updates of u1 & u2 require only one divide• window edge intersections are computed only once when u1 & u2 are

final– C-S

• repeatedly calcs intersections along a line path even though line may be completely outside clipping window

• each intersection calc requires a divide and a multiply

• Both line clipping algorithms can be extended to 3-d

• A third line clipping algorithm, Nicholl-Lee-Nicholl (N-L-N), which we will

not go into is faster than both L-B & C-S but it cannot be extended to 3-d.

Page 10: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Polygon Clipping• Polygon clipping

– Polygons can be clipped against successive infinite extensions of the clipping window edges

– Keep track of new vertices (intersections with edge of clipping window) at each stage.

– Notice the number of vertices increased in the example below.

Page 11: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Polygon Clipping

Page 12: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Polygon Clipping• The text describes two polygon clipping algorithms.

• The Sutherland-Hodgman algorithm and the Weiler-Atherton algorithm.

• A major difference between the two is that the Sutherland-Hodgman algorithm

produces as output one polygon.

• Think about a concave polygon that when clipped, really should result in two

polygons. Example on board.

Page 13: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

3d math (Ch. 5, Appdx A, Sec. 3-15)• Coordinate systems

• Vectors

• Lines

• Plane

• Homogeneous Coordinates of a 3d point

• Transformations– Translation– Scaling– Rotation (about x, y, z)– Shear (in xy)

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Right-handed 3d coordinate system

Page 15: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Left-handed 3d coordinate system

Page 16: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

vectors• A vector is a directed line segment that has magnitude (length)

and direction.

• We can define a vector as the difference between two points. (Example on board.)

• In 2 dimensions: V = P2-P1 = (vx, vy)

• In 3 dimensions: V = P2-P1 = (vx, vy, vz)

• The vx, vy, and vz values are the projections of the line segment

onto the x, y and z axes, respectively.

• Magnitude of a Vector is determined by the Pythagorean theorem:

• For 2d: |V| = sqrt(vx2 + vy

2) and for 3d: |V| = sqrt(vx2 + vy

2 + vz2)

Page 17: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

vectors• For 2d vectors: the direction of a vector is often specified as an

angle in relation to the horizontal.

• For 3d vectors: the direction of a vector is often specified as three angles in relation to the positive axes. (note: The alpha in the diagram would only be accurate if V was on the y-z plane.)

• cos α = vx / |V|

• cos β = vy / |V|

• cos γ = vz / |V|

• cos2 α + cos2 β + cos2 γ = 1

Page 18: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

vectors• A vector has a direction and a magnitude. Does it have a position

in the coordinate system?

Page 19: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

vectors• A vector has a direction and a magnitude. Does it have a position

in the coordinate system?– No– Example:– p1 = (5,5,5), p2 = (7,7,5), p3 = (2,7,3), p4 = (4,9,3)– The vectors generated by

• [p2 – p1] and [p4 – p3] result in the same vector • which is [2,2,0]• what's its magnitude?• what's its direction? • what's its position?

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

vectors• A vector has a direction and a magnitude. Does it have a position

in the coordinate system?– No– Example:– p1 = (5,5,5), p2 = (7,7,5), p3 = (2,7,3), p4 = (4,9,3)– The vectors generated by

• [p2 – p1] and [p4 – p3] result in the same vector • which is [2,2,0]• what's its magnitude? sqrt (4+4) = sqrt(8) = 2*sqrt(2)• what's its direction?

– cos-1(sqrt(2)/2) = 45 degrees– cos-1(sqrt(2)/2) = 45 degrees– cos-1(0) = 90 degrees

• what's its position?– it has no position

Page 21: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

vectors• Vector addition:

V1 + V2 = (v1x+v2x, v1y+v2y, v1z+v2z)

• Scalar multiplication:sV = (svx, svy, svz)

• Dot product (aka scalar product) of 2 vectors results in a scalar:V1 ● V2 = |V1| |V2|cos θθ is the (smaller) angle between the two vectors

alternatively:V1 ● V2 = v1xv2x+v1yv2y+v1zv2z

• What would be the dot product of two perpendicular vectors (those with the angle between them being 90 degrees)?

Page 22: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

vectors• Cross product (aka vector product) of 2 vectors results in a vector:

V1 x V2 = u |V1| |V2|sin θθ is the angle between the two vectorsu is a unit vector (length = 1) perpendicular to both V1 and V2 u's direction is determined by the right-hand rule

• Right-hand rule is: with your right hand, grasp the axis perpendicular to

the plane of the two vectors and make sure that the direction of your

fingers curve from v1 to v2. u's direction is the direction of your thumb.

• Alternatively:

• V1 x V2 = (v1yv2z – v1z v2y, v1zv2x – v1x v2z, v1xv2y – v1y v2x)

• Cross product is not commutative, nor associative.

• V1 x V2 = - (V2 x V1)

Page 23: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

vectors• Cross product of two vectors is a vector that is perpendicular to the two

vectors and has magnitude equal to the area of the parallelogram formed

by the two vectors. (picture on board)

Page 24: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

vectors• Recap

– a vector has magnitude and direction (but no position)– addition of 2 vectors results in a vector– a scalar times a vector results in a vector– Cross product of two vectors results in a vector– but– dot product of two vectors results in a scalar

Page 25: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

lines• Parametric equation of a line in 3 dimensions

– Given points P1 and P2 the equation of a line that contains these points is:

x = x1 + t(x2 – x1)y = y1 + t(y2 – y1)z = z1 + t(z2 – z1)

– Given a point P1 and vector V the equation of a line that contains the point and is in the direction of V is:

x = x1 + t(xv)y = y1 + t(yv)z = z1 + t(zv)

• Line = P1 + t (P2 – P1)

• Line = P1 + Vt

Page 26: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Planes (part of sec. 3.15)• The general plane equation: Ax + By + Cz + D = 0

• A, B, C, D are constants and (x,y,z) are the coordinates of the points on the

plane.

• A useful form of the plane equation is: A'x + B'y + C'z + D' = 0where A'=A/d, B'=B/d, C'=C/d, D'=D/d, and d = sqrt(A2 + B2 + C2)

• Because then the distance between a point (x1, y1, z1) and the plane is simply:

A'x1 + B'y1 + C'z1 + D'

• A normal vector to a plane is perpendicular to the plane.

• If the equation of the plane is Ax + By + Cz + D = 0, then a normal to the plane

is the vector (A, B, C)

• Pictures on board.

Page 27: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

The plane equation• From the drawing on the board

– Equation of the plane: Ax + By + Cz + D = 0– Given three non collinear points, P1, P2, P3 these points uniquely

determine the plane– The cross product of P1 - P2 and P3 - P2, gives us a normal vector N.– For an arbitrary point P = (x, y, z), P is on the plane if N ● [P - P2] = 0– This normal vector gives us the 3 coefficients A, B and C.– To determine the value for D in the plane equation we can take

the dot product of N with any point in the plane and get -D.N ● P = [A,B,C]● [x,y,z] = Ax + By + CzSo, N ● P + D = 0, therefore N ● P = -D.

– let's

Page 28: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

3d transformations• Translation• Scale• Rotation• Shear

Page 29: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

3d Translation• 3d Translation in homogeneous coordinates is a direct extension of

translation in 2d.[ 1 0 0 tx ] [x] [x+tx][ 0 1 0 ty ] [y] = [y+ty][ 0 0 1 tz ] [z] [z+tz][ 0 0 0 1 ] [1] [ 1 ]

Page 30: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

3d Scale• 3d Scale

[ sx 0 0 0 ] [x] [sxx][ 0 sy 0 0 ] [y] = [syy][ 0 0 sz 0 ] [z] [szz][ 0 0 0 1 ] [1] [ 1 ]

• 3d Scale has the same problem/feature that 2d scaling has, namely it also translates.

Page 31: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

3d Rotation• Positive rotations are defined to be

• Rotation about the x-axis is positive going from y to z

• Rotation about the y-axis is positive going from z to x

• Rotation about the z-axis is positive going from x to y

• (see figures 5-36 and 5-38 in text.)

Page 32: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Right-handed 3d coordinate system

Page 33: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

3d Rotation• 3d Rotation

• About z-axis[ cos θ -sin θ 0 0 ][ sin θ cos θ 0 0 ][ 0 0 1 0 ][ 0 0 0 1 ]

• About x-axis[ 1 0 1 0 ][ 0 cos θ -sin θ 0 ][ 0 sin θ cos θ 0 ][ 0 0 0 1 ]

• About y-axis[ cos θ 0 sin θ 0 ][ 0 1 0 0 ][ -sin θ 0 cos θ 0 ][ 0 0 0 1 ]

• Let's do some calculations on the board – rotating some points to get a feel for these.

Page 34: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

xy Shear

[ 1 0 shx 0 ] [x] [x+shxz]

[ 0 1 shy 0 ] [y] = [y+shyz][ 0 0 1 0 ] [z] [ z ][ 0 0 0 1 ] [1] [ 1 ]

Let's transform a unit cube with this shear and set shx = 1 and shy = 2

points are: (0,0,0), (0,0,1), (0,1,0), (1,0,0), (0,1,1), (1,0,1), (1,1,0), (1,1,1)

Page 35: CS 376 Introduction to Computer Graphics 02 / 14 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

xy Shear

[ 1 0 shx 0 ] [x] [x+shxz]

[ 0 1 shy 0 ] [y] = [y+shyz][ 0 0 1 0 ] [z] [ z ][ 0 0 0 1 ] [1] [ 1 ]

Let's transform a unit cube with this shear and set shx = 1 and shy = 2

points are: (0,0,0), (0,0,1), (0,1,0), (1,0,0), (0,1,1), (1,0,1), (1,1,0), (1,1,1)

transformed they are: (0,0,0), (1,2,1), (0,1,0), (1,0,0), (1,3,1), (2,2,1), (1,1,0), (2,3,1)