CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

29
CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann

Transcript of CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Page 1: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

CS 325Introduction to Computer Graphics

02 / 17 / 2010

Instructor: Michael Eckmann

Page 2: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

Today’s Topics• Questions/comments?

• 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 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

Line Clipping• Why use Liang-Barsky (L-B) vs. Cohen-Sutherland (C-S) line clipping?

Page 4: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 5: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 6: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

Polygon Clipping

Page 7: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 8: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 9: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

Right-handed 3d coordinate system

Page 10: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

Left-handed 3d coordinate system

Page 11: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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.) fig A-13 in text.

• In 2 dimensions: V = P2-P

1 = (v

x, v

y)

• In 3 dimensions: V = P2-P

1 = (v

x, v

y, v

z)

• The vx, v

y, and v

z 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(vx

2 + vy

2) and for 3d: |V| = sqrt(vx

2 + vy

2 + vz2)

Page 12: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 13: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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

in the coordinate system?

Page 14: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 15: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 16: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

vectors• Vector addition:

V1 + V

2 = (v

1x+v

2x, v

1y+v

2y, v

1z+v

2z)

• Scalar multiplication:sV = (sv

x, sv

y, sv

z)

• Dot product (aka scalar product) of 2 vectors results in a scalar:V

1 ● V

2 = |V

1| |V

2|cos θ

θ is the (smaller) angle between the two vectors

alternatively:V

1 ● V

2 = v

1xv

2x+v

1yv

2y+v

1zv

2z

• What would be the dot product of two perpendicular vectors (those

with the angle between them being 90 degrees)?

Page 17: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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

V1 x V

2 = u |V

1| |V

2|sin θ

θ is the angle between the two vectorsu is a unit vector (length = 1) perpendicular to both V

1 and V

2

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 V

2 = (v

1yv

2z – v

1z v

2y, v

1zv

2x – v

1x v

2z, v

1xv

2y – v

1y v

2x)

• Cross product is not commutative, nor associative.

• V1 x V

2 = - (V

2 x V

1)

Page 18: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 19: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 20: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

lines• Parametric equation of a line in 3 dimensions

– Given points P1 and P

2 the equation of a line that contains these

points is:x = x

1 + t(x

2 – x

1)

y = y1 + t(y

2 – y

1)

z = z1 + t(z

2 – z

1)

– 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 = x

1 + t(x

v)

y = y1 + t(y

v)

z = z1 + t(z

v)

• Line = P1 + t (P

2 – P

1)

• Line = P1 + Vt

Page 21: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 it's easy to find the distance between a point (x1, y

1, z

1) and the

plane which is simply: A'x1 + B'y

1 + C'z

1 + 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 22: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

The plane equation• From the drawing on the board

– Equation of the plane: Ax + By + Cz + D = 0– Given three non collinear points, P

1, P

2, P

3 these points uniquely

determine the plane– The cross product of P

1 - P

2 and P

3 - P

2, gives us a normal vector N.

– For an arbitrary point P = (x, y, z), P is on the plane if

N ● [P - P

2] = 0

– Why?

Page 23: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

The plane equation• From the drawing on the board

– Equation of the plane: Ax + By + Cz + D = 0– Given three non collinear points, P

1, P

2, P

3 these points uniquely

determine the plane– The cross product of P

1 - P

2 and P

3 - P

2, gives us a normal vector N.

– For an arbitrary point P = (x, y, z), P is on the plane if

N ● [P - P

2] = 0

– Why? Because if N is normal to the plane, and P-P2 is a vector

on the plane, then the angle between these is 90 degrees and so the dot product will be 0, since cos(90) = 0.

– If P is not on the plane, therefore P-P2 is not on the plane, so

what happens?

Page 24: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

The plane equation• For an arbitrary point P = (x, y, z), P is on the plane if

N ● [P - P

2] = 0

– So, we can derive the plane equation like so:– N = (A, B, C) and is normal to the plane– P = (x, y, z) and represents any point on the plane– P

2 = (x

2, y

2, z

2) is some fixed point on the plane

N ● [P - P

2] = [A,B,C]●[x-x

2,y-y

2, z-z

2]=

A(x-x2) + B(y-y

2) + C(z-z

2) =

Ax + By + Cz + A(-x2) + B(-y

2) + C(-z

2) = 0

Notice that x, y and z are variables and the rest are constant values so this gives Ax + By + Cz + D = 0 where

D = A(-x2) + B(-y

2) + C(-z

2)

Page 25: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

Inverse Transforms and Identity• The inverse of a Matrix is the matrix that

when multiplied by the original results in the Identity matrix.

• The identity matrix is an nxn matrix with 1's along the main diagonal (from upperleft to lowerright) and 0's elsewhere.

• MM-1 = I = M-1M

Page 26: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

Reminder about Homogeneous Coordinates

• When we transform some homogeneous point (x,y,z,1) we may

end up with something other than 1 in the homogeneous parameter slot i.e. (x

1,y

1,z

1,w), w!= 1.

• To figure out which point to plot in 3d, we need to divide each

coordinate by w

• x' = x1 / w

• y' = y1 / w

• z' = z1 / w

• 1 = w / w

• so we plot (x',y',z')

Page 27: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

3d transformations• Translation• Scale• Rotation• Shear

Page 28: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

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 29: CS 325 Introduction to Computer Graphics 02 / 17 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 325 - Spring 2010

3d Scale• 3d Scale

[ sx 0 0 0

] [x] [s

xx]

[ 0 sy 0 0

] [y] = [s

yy]

[ 0 0 sz 0

] [z] [s

zz]

[ 0 0 0 1 ] [1] [ 1 ]

• 3d Scale has the same problem/feature that 2d scaling has, namely

it also translates.