cgmb214_topic 6_ geometric transformation

Upload: muhammad-javeed

Post on 14-Apr-2018

239 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    1/66

    T O P I C 6 :

    G E O M E T R I C T R A N S F O R M A T I O N

    CGMB214: Introduction to

    Computer Graphics

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    2/66

    Objectives

    To provide an understanding on the geometrictransformation

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    3/66

    So far.

    We have been discussing the basic elements ofgeometric programming. We have discussed points,

    vectors and their operations and coordinate frames

    and how to change the representation of points andvectors from one frame to another.

    Next topic involves how to map points from oneplace to another (transformation).

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    4/66

    Affine Transformation

    In this topic, we will concentrate on one particulartransformation called affine transformations.Examples of affine transformations are:

    translations

    Rotations

    Uniform and non-uniform scaling

    reflections (flipping objects about a line)

    shearing ( which deform squares into parallelogram)

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    5/66

    Affine Transformation

    Examples of affine transformation

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    6/66

    Affine Transformation

    Transformation in 2-D and 3-D

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    7/66

    Affine Transformation-Characteristics

    These transformations all have a number of thingsin common.

    They all map lines to lines ( parallel lines will still be parallelafter the transformations)

    Translation, rotation and reflection preserve the lengths ofline segments and the angles between segments

    Uniform scaling preserve angles but not length

    Non-uniform scaling and shearing do not preserve angles or

    lengths

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    8/66

    Affine Transformation-Usage

    Transformations are useful in a number ofsituations:

    1. We can compose a scene out a number of objects

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    9/66

    Affine Transformation-Usage

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    10/66

    Affine Transformation-Usage

    2. Can design a single motif and manipulate themotif to produce the whole shape of an objectespecially if the object has certain symmetries.

    Example of snowflake after reflections, rotations and translations of themotif

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    11/66

    Affine Transformation-Usage

    3. To view an object from a different angle

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    12/66

    Affine Transformation-Usage

    4. To produce animation

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    13/66

    Affine Transformation

    The two special types of affine transformation are

    1. Rigid transformation: These transformationspreserve both angles and lengths (I.e. translations,

    rotations and reflections) 2. Orthogonal transformation: These

    transformations preserve angles but not necessarilylength.

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    14/66

    Affine Transformation -why?

    The most common transformations used incomputer graphics.

    They simplify operations to scale, rotate and

    reposition objects. Several affine transformations can be combined into

    a simple overall affine transformation in terms of acompact matrix representation

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    15/66

    Transformation Matrix (2-D)

    For a point P that map to Q after affinetransformation has a matrix representation asshown:

    11001

    232221

    131211

    y

    x

    y

    x

    PP

    mmmmmm

    QQ

    NB: the third of the resultant matrix will always be 1 fora point

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    16/66

    Transformation Matrix (2-D)

    For a vector V that maps to W after affinetransformation has the matrix representation asshown:

    01000

    232221

    131211

    y

    x

    y

    x

    V

    V

    mmm

    mmm

    W

    W

    NB: the third row of the resultant matrix will always be 0for a vector

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    17/66

    Transformation Matrix

    The scalar m that we have in the transformationmatrix will determine which affine transformationthat we want to perform.

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    18/66

    OpenGL graphics pipeline

    Normally in OpenGL, many transformationprocesses occur before the final objects are displayedon the screen. Basically the object that we define inour world will go through the same procedure.

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    19/66

    Transformations

    Transformations: changes in size, shape andorientation that alter the coordinate descriptions ofobjects.

    Usually, transformations are represented andcalculated using matrices.

    OpenGL also uses the same approach to performtransformations

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    20/66

    Sine and Cosine

    Sin = b/c if c = 1 then b = sin

    Cos = a/c if c = 1 then c = cos

    c

    a

    b

    Recall our algebra lesson!!

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    21/66

    Sine and Cosine (cont.)

    90

    0180

    270

    y

    x

    z

    -

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    22/66

    Sine and Cosine (cont.)

    Cos = x/z if z = 1 then cos = x

    Sin = y/z if z = 1 then sin = y

    Cos (- ) = x/z = cosSin (- ) = -y/z = -sin

    Cos ( ) = cos cos - sin sinSin ( ) = sin cos + cos sin

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    23/66

    Matrices

    In graphics most of the time matrix operation that we willdeal with is matrix multiplication. The formula formultiplication of matrix A with m xp dimension and matrixB withp x n dimension is:

    mnm

    ij

    n

    pnpjp

    nj

    mpm

    ipi

    p

    cc

    c

    cc

    bbb

    bbb

    aa

    aa

    aa

    ...

    .....

    ..

    .....

    ...

    ......

    .........

    .........

    .........

    ......

    ...

    .....

    ...

    .....

    ...

    1

    111

    1

    1111

    1

    1

    111

    where

    p

    k

    kjikpjipjijiij babababac1

    2211...

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    24/66

    Example

    987

    654

    321

    A

    10

    21

    01

    B

    251

    161

    71

    AB

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    25/66

    Characteristics of Matrix

    Multiplication of matrices is not commutative

    AB != BA

    Multiplication of several matrices is associative

    A(BC) = (AB)C

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    26/66

    Identity Matrix

    Set of matrices that when they multiply anothermatrix which reproduce that matrix is called identitymatrices i.e:

    1000

    0100

    0010

    0001

    100

    010

    001

    10

    011

    1D 2D 3D 4D

    Obj T f i C di

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    27/66

    Object Transformation vs CoordinateTransformation

    Object Transformation: alters the coordinates ofeach point on the object according to some rule. Nochange of coordinate system

    Coordinate Transformation: defines a newcoordinate system in terms of the old one and thenrepresents all of the objects point in the newcoordinate system

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    28/66

    Translation

    Reposition an object along a straight line path fromone coordinate location to another

    2D point is translated by adding translation

    distances to the x and y coordinates When translating (x,y) to (x,y) by value t x = x + tx y = y + ty

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    29/66

    Translation

    The (tx,ty) is the translation distances calledTRANSLATION VECTOR

    In matrix form

    Then point P = P + T

    yxP

    '

    ''yxP

    y

    x

    ttT

    y

    x

    y

    x

    tytx

    tt

    yxP'

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    30/66

    Translation

    For 2-D translation the transformation matrix Thasthe following form:

    100

    10

    01

    y

    x

    m

    m

    Where mxand myare the translation values in x and y

    axis

    T=

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    31/66

    Translation (cont.)

    For 3-D translation the transformation matrix T hasthe following form:

    1000

    100

    010

    001

    z

    y

    x

    mm

    m

    Where mx, myand mzare the translation values in x, y andz axis

    T=

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    32/66

    Translation

    Translation is a rigid body transformation the object is not being deformed

    all points are moved in the same distance

    How to translate? Straight lines based on end points

    Polygons - based on vertices

    Circles - based on centre

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    33/66

    Rotations

    Reposition an object along a circular path in aspecified plane

    Rotations are specified by:

    a rotation angle a rotation point (pivot point) at position (x,y)

    Positive rotation angle means rotate counter-clockwise negative rotation angle means rotate

    clockwise

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    34/66

    Rotations

    (x,y)

    (x,y)

    r

    r

    The original point (x,y) can be represented in polarcoordinates form:

    x = r cos (1)y = r sin (2)

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    35/66

    Rotations

    The new point (x,y) can be expressed as x = r cos ( )

    y = r sin ( )

    These equation can be written as x = r cos cos - r sin sin

    y = r cos sin + r sin cos

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    36/66

    Rotations

    Substituting the equations with equation (1) and (2),we get

    x = x cos - y sin

    y = x sin + ycos

    Therefore, the rotation matrixR can be expressed as

    cossin

    sincosR=

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    37/66

    Rotations

    For 2-D rotation the transformation matrixR has thefollowing form (in homogeneous coordinate system):

    100

    0cossin

    0sincos

    NB: for counter-clockwise rotation

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    38/66

    Rotations

    In 3-D world, the rotation is more complex since wehave to consider rotation about three different axis;x, y and z axis.

    Therefore we have three different transformationmatrices in 3-D world.

    1000

    0100

    00cossin

    00sincos

    Rz( ) =

    Rotation about z-axis (ccw)

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    39/66

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    40/66

    Rotations

    This rotation matrix is for case where the rotation isat the origin.

    For a rotation at any other points, we need to

    perform the following transformations: Translating the object so the rotation point is at the origin.

    Rotating the object around the origin

    Translating the object back to its original position

    Recall that rotation is a rigid affine transformation

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    41/66

    Scaling

    Scaling changes the size of an object

    Scaling can also reposition the object but not always

    Uniform scaling is performed relative to some

    central fixed point (I.e at the origin). The scalingvalue (scale factor) for uniform scaling must be bothequal.

    Non-uniform scaling has different scaling factors.

    Also refers as differential scaling

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    42/66

    Scaling

    The value of the scale factors (Sx, Sy, Sz) determinethe size of the scaled object.

    if equal to 1 -> no changes

    if greater than 1 -> increase in size (magnification)

    if 0 < scale factors < 1 -> decrease in size

    (demagnification)

    if negative value -> reflection !!!

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    43/66

    Scaling (cont.)

    For 2-D scaling the transformation matrix S willhave the following form

    100

    00

    00

    y

    x

    S

    S

    S =

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    44/66

    Scaling

    For 3-D scaling the transformation matrix S has thefollowing form:

    1000

    000

    000

    000

    z

    y

    x

    S

    S

    S

    S =

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    45/66

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    46/66

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    47/66

    Reflection

    For 3-D reflection, the transformation matrixFhasthe following form

    1000

    0100

    0010

    0001

    F(z) =

    Reflection about z-axis

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    48/66

    Reflection

    Reflection can be generalized by concatenatingrotation and reflection matrices.

    Example: If reflection at y=x axis (45 degree), thetransformations involved are:

    1. Clockwise rotation of 45 degree

    2. Reflection about x axis

    3. Counter clockwise rotation of 45 degree

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    49/66

    Shearing

    Distort an object by moving one side relative toanother

    It neither rigid body nor orthogonal transformation.i.e. changing a square into parallelogram in 2-D orcube into parallelepiped in 3-D space

    It is normally used to display italic text using regularones

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    50/66

    Shearing

    For 2-D shearing transformation the transformationmatrix has the following form

    X direction y direction

    100

    010

    01 xsh

    100

    01

    001

    ysh

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    51/66

    Shearing

    The values can be positive or negative numbers

    Positive: moves points to the right

    Negative: moves points to the left

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    52/66

    Shearing

    For 3-D space shearing transformations the numberof transformation matrix are many.

    Basically, to get one transformation matrix forshearing, we can substitute any zero term in identitymatrix with a value like example below:

    1000

    010

    01

    0001

    b

    ca

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    53/66

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    54/66

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    55/66

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    56/66

    Example

    Let say we want to produce an object that goesthrough the following transformation operations:

    translate by (3, -4),M1 then rotate through 30 degree,M2 then scale by (2, -1),M3 then translate by (0, 1.5),M4 and finally, rotate through 30 degree,M5

    All of these transformation can be represented by asingle matrix,M

    M = M5M4M3M2M1

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    57/66

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    58/66

    Affine transformation in OpenGL

    Recall our lesson on OpenGL graphics pipeline.

    CT = Current Transformation

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    59/66

    OpenGL

    All the transformations will be performed in CT bychanging the current transformation matrix. In other

    way, the CT matrix is an example of compositematrix.

    Typically, in OpenGL, we will have the followingfragment of code in our program before we startperforming transformations.

    glMatrixMode(GL_MODELVIEW);glLoadIdentity();

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    60/66

    OpenGL

    By having this code in our program, we set ourmatrix mode to be GL_MODELVIEW and initializethe CT matrix to be identity

    CT I

    Once we have set this, we can performtransformation which means we modify the CT bypost-multiplication by a matrix

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    61/66

    OpenGL

    CT

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    62/66

    OpenGL

    The three transformation supported in most graphicssystem(including OpenGL) are translation, rotation

    with a fixed point of the origin and scaling with afixed point of the origin.

    The functions for these transformation in OpenGLare: glTranslatef(dx, dy, dz);

    glRotatef (angle, vx, vy, vz);

    glScalef(sx, sy, sz);

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    63/66

    OpenGL

    If we want to perform rotation at any other point usingthe provided functions in OpenGL, (I.e. 45 degreerotation aboutthe line through the origin and the point(1,2,3) with a fixed point of (4,5,6).) the following codefragment shows us how toperform this transformation.

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glTranslatef(4.0,5.0,6.0);

    glRotatef(45.0,1.0,2.0,3.0);glTranslatef(-4.0,-5.0,-6.0);

    NB: Take note at the reverse order implementation

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    64/66

    OpenGL

    For most purposes, rotation, translation and scalingcan be used to form our desired object. However, insome cases the provided transformation matrices arenot enough. For example, if we want to form a

    transformation matrix for shearing and reflection.

    For these transformations, it is easier if we set up thematrix directly. We can load a 4 x 4 homogeneous-

    coordinate matrix as the current matrix (CT)

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    65/66

    O

  • 7/30/2019 CGMB214_topic 6_ Geometric Transformation

    66/66

    OpenGL

    To define a user-defined matrix (for shearing andreflection) we can follow the same way as shown

    below:

    Glfloat myarray[16];

    for (i=0;i