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

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

Upload: virgil-haynes

Post on 01-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

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

CS 376Introduction to Computer Graphics

02 / 16 / 2007

Instructor: Michael Eckmann

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Today’s Topics• Questions?

• 3d– 3 dimensional vectors– dot product– cross product– parametric equation of a line in 3 dimensions– equation of a plane in 3 dimensions and its derivation– 3d primitive transform matrices

• translation• scaling• rotation about each coordinate axis

– Inverses– homogeneous coordinates reminder– rotation about an arbitrary axis– fixed point scaling

Page 3: CS 376 Introduction to Computer Graphics 02 / 16 / 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-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 4: CS 376 Introduction to Computer Graphics 02 / 16 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

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

Page 5: CS 376 Introduction to Computer Graphics 02 / 16 / 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 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 6: CS 376 Introduction to Computer Graphics 02 / 16 / 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 7: CS 376 Introduction to Computer Graphics 02 / 16 / 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 8: CS 376 Introduction to Computer Graphics 02 / 16 / 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 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 9: CS 376 Introduction to Computer Graphics 02 / 16 / 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 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 10: CS 376 Introduction to Computer Graphics 02 / 16 / 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, 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 11: CS 376 Introduction to Computer Graphics 02 / 16 / 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, 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 12: CS 376 Introduction to Computer Graphics 02 / 16 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

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 13: CS 376 Introduction to Computer Graphics 02 / 16 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

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 14: CS 376 Introduction to Computer Graphics 02 / 16 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

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 15: CS 376 Introduction to Computer Graphics 02 / 16 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

3d transformations• Translation• Scale• Rotation• Shear

Page 16: CS 376 Introduction to Computer Graphics 02 / 16 / 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 17: CS 376 Introduction to Computer Graphics 02 / 16 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

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.

Page 18: CS 376 Introduction to Computer Graphics 02 / 16 / 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 19: CS 376 Introduction to Computer Graphics 02 / 16 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Right-handed 3d coordinate system

Page 20: CS 376 Introduction to Computer Graphics 02 / 16 / 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, as an example rotate the

point (4,6,0) (in homogeneous

it is (4,6,0,1)) about x-axis,

positive 60 degrees.

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

xy Shear

[ 1 0 shx 0 ] [x] [x+sh

xz]

[ 0 1 shy 0

] [y] = [y+sh

yz]

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

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

Let's transform a unit cube with this shear and set sh

x = 1 and sh

y = 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 22: CS 376 Introduction to Computer Graphics 02 / 16 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

xy Shear

[ 1 0 shx 0 ] [x] [x+sh

xz]

[ 0 1 shy 0

] [y] = [y+sh

yz]

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

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

Let's transform a unit cube with this shear and set sh

x = 1 and sh

y = 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)

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Rotation of θ about an arbitrary axis• To rotate about an arbitrary axis:

• The axis can be specified by two points on it, P1 and P

2

• The vector P2 - P

1 is on this axis.

• Before anything else, we should translate P1 to the origin.

• If P1 = (x

1,y

1,z

1) then T =

[ 1 0 0 -x1

] [ 0 1 0 -y

1 ]

[ 0 0 1 -z1

] [ 0 0 0 1 ]

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

• What we want to do next is to rotate the axis so that it is on

the z-axis.

• Then rotate by an angle of about the z axis.

• Then we need to do the inverses of the transforms in

reverse order. That is, rotate back from z-axis to original angle. Then translate back from origin to P

1.

• Does that make sense?

Rotation of θ about an arbitrary axis

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

• We know how to rotate about the x-axis, y-axis and z-axis.

We also know how to translate.

• Therefore we will use a composition of these primitive

transforms to get the rotation about an arbitrary axis.

Rotation of θ about an arbitrary axis

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

• What we want to do now is to rotate the axis so that it is on

the z-axis. To rotate the axis from where it is now to the z-

axis we can do this in two steps (rotate about 2 different

coordinate axes). One way to do this is:

– a) rotate the axis so that it is on the y-z plane• What axis will we rotate about, to get it on y-z plane?

– b) rotate the axis so that it is on the z-axis• Once we have it on the y-z plane, what axis will we

rotate about to get it on z-axis?

Rotation of θ about an arbitrary axis

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

• What we want to do now is to rotate the axis so that it is on

the z-axis. To rotate the axis from where it is now to the z-

axis we can do this in two steps

– a) rotate the axis so that it is on the y-z plane• Rotate about y-axis to get it on y-z plane.

– b) rotate the axis so that it is on the z-axis• Rotate about x-axis to get it on z-axis.

Rotation of θ about an arbitrary axis

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

a) rotate the axis so that it is on the y-z plane• Rotate about y-axis to get it on y-z plane.

• See handout diagram.

U = (u1,u

2,u

3)

We need to find angle β , the triangle with side lengths u

3 u

1 and a is a right triangle:

cos β = u3 / a

sin β = u1 / a

a = sqrt(u1

2 + u3

2)Rotating U towards y-z is a negative angle,

so the matrix is Ry(-β)

Rotation of θ about an arbitrary axis

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

• b) rotate the axis so that it is on the z-axis• Rotate about x-axis to get it on z-axis.

• See second handout diagram. This is the y-z plane It may not be immediately obvious, but the length a, on

this diagram is the same as from the previous.

U = (u1,u

2,u

3)

We need to find angle μ, the triangle with side lengths u

2 a and |U| is a right triangle:

cos μ = a / |U|sin μ = u

2 / |U|

a = sqrt(u1

2 + u3

2)This rotation is in a positive direction, so the

matrix is Rx(μ)

Rotation of θ about an arbitrary axis

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

• The remaining steps are:

• Rotate by an angle of θ about the z axis, • which is simply R

z(θ)

• Then we need to do the inverses of the transforms in

reverse order. That is, rotate back from z-axis to original angle. Then translate back from origin to P

1.

• Rx(-μ)

• Ry(β)

• T-1

Rotation of θ about an arbitrary axis

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

• To rotate an angle of θ about an arbitrary axis the

composite transform is:

T-1 Ry(β) R

x(-μ) R

z(θ) R

x(μ) R

y(-β) T

• Notice that we are multiplying this matrix by a point and the point goes on the right. Therefore, it acts like the first transform applied to the point is T, then R

y(-β), then

the rest from right to left.

Rotation of θ about an arbitrary axis

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

3d Scale w/ fixed point• First transform the fixed point to the origin with a translation

matrix.

• Then do the 3d Scale

[ sx 0 0 0

]

[ 0 sy 0 0

]

[ 0 0 sz 0

]

[ 0 0 0 1 ]

• Then transform the origin back to the fixed point via the

inverse of the first translation matrix.

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

Michael Eckmann - Skidmore College - CS 376 - Spring 2007

Rigid body vs. Affine• Transformations that preserve line lengths and angles do not

deform shapes. Therefore they are called rigid body

transformations.– Translation and Rotation (and any combination of these)

are examples of rigid body transformations.

• Transformations that preserve parallel lines, that is, lines that

were parallel before the transform remain parallel after the

transform are called affine transformations.– Line lengths and angles need not be preserved for a

transformation to be considered affine– Rotation, Translation, Scale and Shear (and any

combination of these) are examples of affine transformations.