comp trans lec

Upload: srivatsachakravarth

Post on 05-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Comp Trans Lec

    1/18

    Composite transformations:

    The heart of computer graphics

    Lecturer: Dr Dan Cornford

    [email protected]

    http://wiki.aston.ac.uk/DanCornford

    CS2150, Computer Graphics, Aston University, Birmingham, UKhttp://wiki.aston.ac.uk/C2150

    October 12, 2009

    Dan Cornford Composite Transformations 1/18

  • 7/31/2019 Comp Trans Lec

    2/18

    Outline: Composite transformations

    Combining transformations.

    Matrix times a matrix.

    How to transform things in OpenGL.

    Efficiency.

    Inverse transformations.

    Dan Cornford Composite Transformations 2/18

  • 7/31/2019 Comp Trans Lec

    3/18

    Composition of transformations

    Big advantage of homogeneous coordinates is that

    transformations can be very easily combined.

    All that is required is multiplication of the transformationmatrices.

    This makes otherwise complex transformations very easy

    to compute.

    Dan Cornford Composite Transformations 3/18

  • 7/31/2019 Comp Trans Lec

    4/18

    Composition of transformations

    For instance if we wanted to rotate an object about some

    point, p.

    To simply write down the transformation would be quite a

    challenge!However, the transformation matrix can be computed by:

    1 translate object by p,2 rotate object by angle ,3 translate object by p.

    Dan Cornford Composite Transformations 4/18

    M i l i li i

  • 7/31/2019 Comp Trans Lec

    5/18

    Matrices multiplication

    Given two matrices, A and B if we want to multiply Bby A

    (that is form AB) then if A is (nm), Bmust be (m p).

    This produces a result, C= AB, which is (n p), withelements:

    cij =m

    k=1

    aikbkj

    Basically we multiply the first row of A with the first column

    of Band put this in the c1,1 element of C. And so on ....

    The identity matrix (for multiplication) is denoted I:

    I=

    1 0 00 1 0

    0 0 1

    .

    This means that AI= A = IA.

    Dan Cornford Composite Transformations 5/18

    M t i b i

  • 7/31/2019 Comp Trans Lec

    6/18

    Matrices basics

    From GameDev.net

    Unlike scalar multiplication, AB= BA this means that

    applying a scaling after a translation will not have the sameeffect as a translation after a scaling.

    Multiplying composite transformation matrices is at the core of allmodern graphics libraries.

    Dan Cornford Composite Transformations 6/18

    C iti f t f ti

  • 7/31/2019 Comp Trans Lec

    7/18

    Composition of transformations

    Recall our previous example of rotation about a point p,

    which can be written as:

    T(p)R()T(p) =

    1 0 px0 1 py0 0 1

    cos sin 0sin cos 0

    0 0 1

    1 0 px0 1 py0 0 1

    =cos sin px(1 cos ) + py sin sin cos py(1 cos ) pxsin

    0 0 1

    .

    Note the ordering of the transformation matrices.

    For those interested, MATLAB provides an excellent

    platform for investigating these sort of transformations,

    since its natural matrix format makes things very easy to

    code.

    Dan Cornford Composite Transformations 7/18

    T f ti d O GL

  • 7/31/2019 Comp Trans Lec

    8/18

    Transformations and OpenGL

    Basic commands are:

    glTranslate#(dx, dy, dz)

    glScale#(sx, sy, sz)

    glRotate#(angle, x, y, z)

    The matrices are applied to the vertices in the oppositeorder they are specified (pre-multiplied by existing

    transformation matrix).

    Can define our own matrices: glLoadMatrix and

    glMultMatrix.

    Dan Cornford Composite Transformations 8/18

    Transformations and OpenGL stacks

  • 7/31/2019 Comp Trans Lec

    9/18

    Transformations and OpenGL stacks

    From GameDev.net

    There are two important matrices in OpenGL

    GL PROJECTION and GL MODELVIEW.

    OpenGL stores these as composite transformation

    matrices.

    OpenGL maintains matrix stacks which are used to store

    the composite transformation matrices (of all

    transformations so far specified).

    Dan Cornford Composite Transformations 9/18

    Transformations and OpenGL stacks

  • 7/31/2019 Comp Trans Lec

    10/18

    Transformations and OpenGL stacks

    Consider the following code fragment:

    g l L o a d I d e n t i t y ( ) ;

    g lR ot at e ( 45 .0 , 0 .0 , 0 .0 , 0 .0 ); /* a pp ly t ra ns fo rm at io n R */g l T ra n sl a t e ( 2. 0 , 0 .0 , 0 . 0) ; /* a pp ly t r an s fo r ma t io n T */

    g l B e g i n ( G L _ P O I N T S ) ;

    g l V e r t e x 3 f ( v ) ; /* d ra w t ra ns fo rm ed v er te x v */

    glEnd();

    The effect of this is:the GL MODELVIEW matrix successively contains I, IR, and

    finally I R T;

    the transformed vertex is I R T v = (I (R (T v)));

    the transformations to vertex v effectively occur in theopposite order to which they were specified.

    In reality only a single multiplication of the vertex by the GL MODELVIEW

    matrix occurs; the Rand T matrices are already multiplied into a

    single composite matrix before being applied to v.

    Dan Cornford Composite Transformations 10/18

    Transformations and OpenGL stacks

  • 7/31/2019 Comp Trans Lec

    11/18

    Transformations and OpenGL stacks

    From GameDev.net

    We use glPushMatrix() and glPopMatrix() to save and

    remove the current matrix from its stack.

    This is normally the GL MODELVIEW matrix.

    Stacks play a large role in defining scene graphs and

    animating models.

    Dan Cornford Composite Transformations 11/18

    Efficiency your job

  • 7/31/2019 Comp Trans Lec

    12/18

    Efficiency your job

    The composition of transformations involving translation,

    scaling and rotation leads to transformation matrices M of

    the general form:

    M=r1,1 r1,2 txr2,1 r2,2 ty

    0 0 1

    .

    How many elementary operations (+,, , /) are required

    to multiply a vector [x, y,w]

    by this matrix?

    Dan Cornford Composite Transformations 12/18

    Efficiency

  • 7/31/2019 Comp Trans Lec

    13/18

    Efficiency

    The composition of transformations involving translation,

    scaling and rotation leads to transformation matrices M of

    the general form:

    M=r1,1 r1,2 txr2,1 r2,2 ty

    0 0 1

    .

    Multiplying a point [x, y,w] by this matrix would take nine

    multiplies and six adds.

    Dan Cornford Composite Transformations 13/18

    Efficiency

  • 7/31/2019 Comp Trans Lec

    14/18

    Efficiency

    Using the fixed structure in the final row of the matrix, the

    actual transformation can be written:

    x = r1,1x+ r1,2y+ tx ,

    y = r2,2y+ r2,1x+ ty ,

    This takes four multiplies and four adds.

    Parallel hardware adders and multipliers effectively remove

    this concern in modern graphics.

    Dan Cornford Composite Transformations 14/18

    Inverse transformations

  • 7/31/2019 Comp Trans Lec

    15/18

    Inverse transformations

    If a general transformation (which may be composite) is

    given by the 3 3 matrix M, then the inverse

    transformation, which maps the new image back to the

    original, untransformed one is given by M1.

    The matrix inverse is defined so that MM1 = Iwhere I is

    the 3 3 identity matrix,

    I=

    1 0 0

    0 1 0

    0 0 1

    .

    Inverses only exist for one to one transformations, for many

    operations they are not defined.

    Dan Cornford Composite Transformations 15/18

    Inverse transformations

  • 7/31/2019 Comp Trans Lec

    16/18

    Inverse transformations

    The translation matrix has inverse:

    T1 =

    1 0 tx0 1 ty0 0 1

    ,

    The scaling matrix has inverse:

    S1 =

    1sx

    0 0

    0 1sy 0

    0 0 1

    .

    You should be able to work out the inverse of the rotation

    matrix ...

    Dan Cornford Composite Transformations 16/18

    Inverse of composite transformations

  • 7/31/2019 Comp Trans Lec

    17/18

    Inverse of composite transformations

    For a composite transformation matrix C= AB the inverseis less obvious.

    We know (AB)1 = B1A1 from linear algebra thus:

    C1 = B1A1 .

    This is logical the inverse transformations are applied in

    the opposite order.

    Dan Cornford Composite Transformations 17/18

    Summary

  • 7/31/2019 Comp Trans Lec

    18/18

    Summary

    Having finished this lecture you should:

    be able to compute composite transformation matrices;

    understand the way OpenGL implements transformations;

    be able to improve the efficiency of applying

    transformations;

    understand the role and concept of an inverse

    transformation.

    If you have trouble with the maths use the extra example sheets

    (and solutions) on the web site.

    Dan Cornford Composite Transformations 18/18