CS 5 4 3 : Com puter Graphics Lecture 4 ( Part I ) : 3 D Affine - - PowerPoint PPT Presentation

cs 5 4 3 com puter graphics lecture 4 part i 3 d affine
SMART_READER_LITE
LIVE PREVIEW

CS 5 4 3 : Com puter Graphics Lecture 4 ( Part I ) : 3 D Affine - - PowerPoint PPT Presentation

CS 5 4 3 : Com puter Graphics Lecture 4 ( Part I ) : 3 D Affine transform s Emmanuel Agu I ntroduction to Transform ations Introduce 3D affine transformation: Position (translation) Size (scaling) Orientation (rotation)


slide-1
SLIDE 1

CS 5 4 3 : Com puter Graphics Lecture 4 ( Part I ) : 3 D Affine transform s Emmanuel Agu

slide-2
SLIDE 2

I ntroduction to Transform ations

Introduce 3D affine transformation:

Position (translation) Size (scaling) Orientation (rotation) Shapes (shear)

Previously developed 2D (x,y) Now, extend to 3D or (x,y,z) case Extend transform matrices to 3D Enable transformation of points by multiplication

slide-3
SLIDE 3

Point Representation

Previously, point in 2D as column matrix Now, extending to 3D, add z-component:

        y x

          1 y x

  • r

              1 z y x               = 1

z y x

P P P P

slide-4
SLIDE 4

Transform s in 3 D

2D: 3x3 matrix multiplication 3D: 4x4 matrix multiplication: homogenous coordinates Recall: transform object = transform each vertice General form:

              = 1

34 33 32 31 24 23 22 21 14 13 12 11

m m m m m m m m m m m m M               =               1 1

z y x z y x

P P P M Q Q Q

Xform of P

slide-5
SLIDE 5

Recall: 3 x3 2 D Translation Matrix

        ' ' y x         y x        

y x

t t = +

          1 ' ' y x           1 1 1

y x

t t           1 y x

=

*

Previously, 2D :

slide-6
SLIDE 6

4 x4 3 D Translation Matrix

          ' ' ' z y x           z y x          

z y x

t t t = +

              1 ' ' ' z y x               1 1 1 1

z y x

t t t               1 z y x

=

*

Now, 3D : Where: x’= x.1 + y.0 + z.0 + tx.1 = x + tx, … etc

OpenGL: gltranslated(tx,ty,tz)

slide-7
SLIDE 7

2 D Scaling

Scale: Alter object size by scaling factor (sx, sy). i.e

x’ = x . Sx y’ = y . Sy

(1,1) (2,2) Sx = 2, Sy = 2 (2,2) (4,4)

                =         y x Sy Sx y x ' '

slide-8
SLIDE 8

Recall: 3 x3 2 D Scaling Matrix

                =         y x Sy Sx y x ' '           ∗           =           1 1 1 ' ' y x Sy Sx y x

slide-9
SLIDE 9

4 x4 3 D Scaling Matrix

          ∗           =           1 1 1 ' ' y x Sy Sx y x               ∗               =               1 1 1 ' ' ' z y x S S S z y x

z y x

  • Example:
  • If Sx = Sy = Sz = 0.5
  • Can scale:
  • big cube (sides = 1) to

small cube ( sides = 0.5)

  • 2D: square, 3D cube

OpenGL: glScaled(Sx,Sy,Sz)

slide-10
SLIDE 10

Exam ple: OpenGL Table Leg

/ / define table leg / / --------------------------------------------------------------------------

  • void tableLeg(double thick, double len){

glPushMatrix(); glTranslated(0, len/ 2, 0); glScaled(thick, len, thick); glutSolidCube(1.0); glPopMatrix(); }

slide-11
SLIDE 11

Recall: 3 x3 2 D Rotation Matrix

(x,y) (x’,y’)

θ φ

r

                − =         y x y x ) cos( ) sin( ) sin( ) cos( ' ' θ θ θ θ

                    − =           1 1 ) cos( ) sin( ) sin( ) cos( 1 ' ' y x y x θ θ θ θ

slide-12
SLIDE 12

Rotating in 3 D

Cannot do mindless conversion like before Why?

Rotate about what axis? 3D rotation: about a defined axis Different Xform matrix for:

  • Rotation about x-axis
  • Rotation about y-axis
  • Rotation about z-axis

New terminology

X-roll: rotation about x-axis Y-roll: rotation about y-axis Z-roll: rotation about z-axis

slide-13
SLIDE 13

Rotating in 3 D

New terminology

X-roll: rotation about x-axis Y-roll: rotation about y-axis Z-roll: rotation about z-axis

Which way is + ve rotation

Look in –ve direction (into + ve arrow) CCW is + ve rotation

x y z +

slide-14
SLIDE 14

Rotating in 3 D

slide-15
SLIDE 15

Rotating in 3 D

For a rotation angle, β about an axis Define:

( )

β cos = c

( )

β sin = s

( )

              − = 1 1 c s s c Rx β

A x-roll:

OpenGL: glrotated(θ, 1,0,0)

slide-16
SLIDE 16

Rotating in 3 D

( )

              − = 1 1 c s s c Ry β

A y-roll:

( )

              − = 1 1 c s s c Rz β

A z-roll:

Rules:

  • Rotate row,

column int. is 1

  • Rest of row/ col is 0
  • c,s in rect pattern

OpenGL: glrotated(θ, 0,1,0) OpenGL: glrotated(θ, 0,0,1)

slide-17
SLIDE 17

Exam ple: Rotating in 3 D

              =                             − = 1 964 . 1 1 6 . 4 1 4 1 3 1 1 c s s c Q

Q: Using y-roll equation, rotate P = (3,1,4) by 30 degrees: A: c = cos(30) = 0.866, s = sin(30) = 0.5, and E.g. first line: 3.c + 1.0 + 4.s + 1.0 = 4.6

slide-18
SLIDE 18

Matrix Multiplication Code

Q: Write C code to Multiply point P = (Px, Py, Pz, 1) by a 4x4 matrix shown below to give new point Q = (Qx,Qy,Qz, 1). i.e.

              =               1 1

z y x z y x

P P P M Q Q Q               = 1

34 33 32 31 24 23 22 21 14 13 12 11

m m m m m m m m m m m m M

where

slide-19
SLIDE 19

Matrix Multiplication Code

Outline of solution:

Declare P,Q as array:

  • Double P[ 4] , Q[ 4] ;

Declare transform matrix as 2-dimensional array

  • Double M[ 4] [ 4] ;

Remember: C indexes from 0, not 1 Long way:

  • Write out equations line by line expression for Q[ i]
  • E.g. Q[ 0] = P[ 0] * M[ 0] [ 0] + P[ 1] * M[ 0] [ 1] + P[ 2] * M[ 0] [ 2] +

P[ 3] * M[ 0] [ 3]

Cute way:

  • Use indexing, say i for outer loop, j for inner loop
slide-20
SLIDE 20

Matrix Multiplication Code

Using loops looks like:

for(i= 0; i< 4; i+ + )

{ temp = 0; for(j= 0; j< 4; j+ + ) { temp + = P[ j] * M[ i] [ j] ; } Q[ i] = temp; }

Test matrice code rigorously Use known results (or by hand) and plug into your code

slide-21
SLIDE 21

3 D Rotation About Arbitrary Axis

Arbitrary rotation axis (rx, ry, rz)

  • penGL: rotate(θ, rx, ry, rz)

Without openGL: a little hairy!! Important: read Hill and Kelley, pg 220 - 223

x z y (rx, ry, rz)

slide-22
SLIDE 22

3 D Rotation About Arbitrary Axis

Can compose arbitrary rotation as combination of:

X-roll Y-roll Z-roll

) ( ) ( ) (

1 2 3

β β β

x y z

R R R M =

slide-23
SLIDE 23

3 D Rotation About Arbitrary Axis

Classic: use Euler’s theorem Euler’s theorem: any sequence of rotations = one rotation

about some axis

Our approach:

Want to rotate β about the axis u through origin and arbitrary

point

Use two rotations to align u and x-axis Do x-roll through angle β Negate two previous rotations to de-align u and x-axis

slide-24
SLIDE 24

3 D Rotation About Arbitrary Axis

) ( ) ( ) ( ) ( ) ( ) ( θ φ β φ θ β

y z x z y u

R R R R R R − − =

slide-25
SLIDE 25

Com posing Transform ation

Composing transformation – applying several transforms

in succession to form one overall transformation

Example:

M1 X M2 X M3 X P where M1, M2, M3 are transform matrices applied to P

Be careful with the order Matrix multiplication is not commutative

slide-26
SLIDE 26

References

Hill, chapter 5.3