Matrices en computacion grafica

Embed Size (px)

Citation preview

  • 8/9/2019 Matrices en computacion grafica

    1/12

    Matrices in Computer Graphics

    Ting Yip

    Math 308A12/3/2001

  • 8/9/2019 Matrices en computacion grafica

    2/12

    Ting YipMath 308A

    2

    Abstract

    In this paper, we discuss and explore the basic matrix operation such as translations,rotations, scaling and we will end the discussion with parallel and perspective view.

    These concepts commonly appear in video game graphics.

    Introduction

    The use of matrices in computer graphics is widespread. Many industries like

    architecture, cartoon, automotive that were formerly done by hand drawing now are doneroutinely with the aid of computer graphics. Video gaming industry, maybe the earliestindustry to rely heavily on computer graphics, is now representing rendered polygon in 3-

    Dimensions.

    In video gaming industry, matrices are major mathematic tools to construct andmanipulate a realistic animation of a polygonal figure. Examples of matrix operationsinclude translations, rotations, and scaling. Other matrix transformation concepts like

    field of view, rendering, color transformation and projection. Understanding of matricesis a basic necessity to program 3D video games.

    Homogeneous Coordinate Transformation

    Points (x, y, z) in R3 can be identified as a homogeneous vector ( )

    1,,,,,,

    h

    z

    h

    y

    h

    xhzyx with

    h 0 on the plane in R4. If we convert a 3D point to a 4D vector, we can represent atransformation to this point with a 4 x 4 matrix.

    The last coordinate is a scalar ter .

    Graphics

    (Screenshots taken from Operation Flashpoint)

    Polygon figures like these use many flat or conic surfaces to represent a realistic human soldier.

  • 8/9/2019 Matrices en computacion grafica

    3/12

    Ting YipMath 308A

    3

    Transformation of Points

    In general, transformation of points can be represented by this equation:

    Transformed Point = Transformation Matrix

    Original Point

    In a more explicit case, a plane spanned by two vectors can be represented by this

    equation:

    =

    =

    y

    x

    fc

    eb

    da

    MatrixtionTransforma

    f

    e

    d

    ,

    c

    b

    a

    spanMatrixtionTransforma

    PlaneOriginalMatrixtionTransforma=PlanedTransforme

    Representation of a plane using matrices

    EXAMPLE

    Point (2, 5, 6) in R3 a Vector (2, 5, 6, 1) or (4, 10, 12, 2) in R4

    NOTEIt is possible to apply transformation to 3D points without converting them to 4D

    vectors. The tradeoff is that transformation can be done with a single matrixmultiplication after the convertion of points to vectors. (More on this after

    Translation.)

    x and y are scalars

    cb

    a

    +

    f

    e

    d

    y

    c

    b

    a

    x

    f

    e

    d

  • 8/9/2019 Matrices en computacion grafica

    4/12

    Ting YipMath 308A

    4

    Translation

    A translation basically means adding a vector to a point, making a point transforms to anew point. This operation can be simplified as a translation in homogeneous coordinate

    (x, y, z, 1) to (x + tx, y + ty, z + tz, 1). This transformation can be computed using a single

    matrix multiplication.

    Translation Matrix for Homogeneous Coordinates in R4 is given by this matrix:

    =

    1000

    100

    010

    001

    ),,(z

    y

    x

    zyxt

    t

    t

    tttT

    Given any point (x, y, z) in R3, the following will give the translated point.

    +++

    =

    111000

    100

    010

    001

    z

    y

    x

    z

    y

    x

    tz

    ty

    tx

    z

    y

    x

    t

    t

    t

    For a sphere to move to a new position, we can think of this as all the points on the sphere move to the

    translated sphere by adding the blue vector to each point.

  • 8/9/2019 Matrices en computacion grafica

    5/12

    Ting YipMath 308A

    5

    Graphics

    (Screenshots taken from Operation Flashpoint)

    In video game, objects like airplane that doesnt change its shape dynamically (rigid body) uses

    Translation to move across the sky. All the points that make up the plane have to be translated by the

    same vector or the image of the plane will appear to be stretched.

    NOTE

    ++

    +

    =

    +

    kz

    jy

    ix

    k

    j

    i

    z

    y

    x

    If we have more than one point, we would have to apply this addition to every point.

    ++++

    ++

    ++

    +

    =

    +

    ++

    +

    =

    +

    kzkz

    jyjy

    ixix

    kz

    jy

    ix

    k

    j

    i

    z

    y

    x

    kz

    jy

    ix

    k

    j

    i

    z

    y

    x

    zz

    yy

    xx

    21

    21

    21

    2

    2

    2

    2

    2

    2

    1

    1

    1

    1

    1

    1

    21

    21

    21

    & aa

    With homogeneous coordinate, we can use a single matrix multiplication.

    ++++

    ++

    ++++++

    =

    kzkzjyjy

    ixix

    kzkz

    jyjy

    ixix

    zz

    yy

    xx

    k

    j

    i

    21

    21

    21

    21

    21

    21

    21

    21

    21

    11111000

    100

    010

    001

    As we can see, linear system is easier to solve with homogenenous coordinate

    transformation.

  • 8/9/2019 Matrices en computacion grafica

    6/12

    Ting YipMath 308A

    6

    Scaling

    Scaling of any dimension requires one of the diagonal values of the transformation matrix

    to equal to a value other than one. This operation can be viewed as a scaling in

    homogeneous coordinate (x, y, z, 1) to (sxx, syy, szz, 1). Values for sx, sy, szgreater thanone will enlarge the objects, values between zero and one will shrink the objects, and

    negative values will rotate the object and change the size of the objects.

    Scaling Matrix for Homogeneous Coordinates in R4 is given by this matrix:

    =

    1000

    000

    000

    000

    ),,(z

    y

    x

    zyxs

    s

    s

    sssS

    Given any point (x, y, z) in R3, the following will give the scaled point.

    =

    111000

    000

    000

    000

    zs

    ys

    xs

    z

    y

    x

    s

    s

    s

    z

    y

    x

    z

    y

    x

    If we want to scale the hexahedron proportionally, we apply the same scaling matrix to each point that

    makes up the hexahedron.

  • 8/9/2019 Matrices en computacion grafica

    7/12

    Ting YipMath 308A

    7

    Rotations

    Rotations are defined with respect to an axis. In 3 dimensions, the axis of rotation needs

    to be specified.

    A rotation about the x axis is represented by this matrix:

    +

    =

    =

    =

    1

    cossin

    sincos

    11000

    0cossin0

    0sincos0

    0001

    1

    )(

    1000

    0cossin0

    0sincos0

    0001

    )(

    zy

    zy

    x

    z

    y

    x

    z

    y

    x

    RR xx

    A rotation about the y axis is represented by this matrix:

    A rotation about the z axis is represented by this matrix:

    +

    =

    =

    =

    1

    cossin

    sincos

    11000

    0100

    00cossin

    00sincos

    1

    )(

    1000

    0100

    00cossin

    00sincos

    )(z

    yx

    yx

    z

    y

    x

    z

    y

    x

    RR zz

    +

    +

    =

    =

    =

    1

    cossin

    sincos

    11000

    0cos0sin

    0010

    0sin0cos

    1

    )(

    1000

    0cos0sin

    0010

    0sin0cos

    )(

    zx

    y

    zx

    z

    y

    x

    z

    y

    x

    RR yy

    3D rotation can be viewed as replacing x1and x2with two axes.

  • 8/9/2019 Matrices en computacion grafica

    8/12

    Ting YipMath 308A

    8

    EXAMPLEThis wire polygon cube is

    represented by a matrix that contains

    its vertex point in every column.

    11111111

    9977977933331111

    75757755

    Rotated Cube

    Original Cube

    If we want to rotate this cube with respect to the x axis by

    3

    :

    11111111

    99779779

    33331111

    75757755

    1000

    03

    cos3

    sin0

    03

    sin3

    cos0

    0001

    =

    5 5 7 7 5 7 5 7

    1

    2

    9

    23

    1

    2

    7

    23

    1

    2

    7

    23

    1

    2

    9

    23

    3

    2

    7

    23

    3

    2

    7

    23

    3

    2

    9

    23

    3

    2

    9

    23

    +9

    2

    1

    2 3 +7

    2

    1

    2 3 +7

    2

    1

    2 3 +9

    2

    1

    2 3 +7

    2

    3

    2 3 +7

    2

    3

    2 3 +9

    2

    3

    2 3 +9

    2

    3

    2 3

    1 1 1 1 1 1 1 1

  • 8/9/2019 Matrices en computacion grafica

    9/12

    Ting YipMath 308A

    9

    Projection Transformation

    Even though we programmed objects in 3-Dimensions, we have to actually view the

    objects as 2-Dimensions on our computer screens. In another word, we want to transformpoints in R3to points in R2.

    Parallel Projection

    In parallel projection, we simply ignore the z-coordinate. This operation can be viewedas a transformation in homogeneous coordinate (x, y, z, 1) to (x, y, 0, 1).

    Parallel Matrix for Homogeneous Coordinates in R4 is given by this matrix:

    =

    1000

    0000

    0010

    0001

    P

    Given any point (x, y, z) in R3, the following will give the parallel projected point.

    =

    1

    0

    11000

    0000

    0010

    0001

    y

    x

    z

    y

    x

    Perspective Projection

    Video game tends to use perspective projections over other projections to represent a realworld, where parallelism is not preserved. Perspective Projections is the way we see

    things, i.e. bigger when the object is closer.

    http://mane.mech.virginia.edu/~engr160/Graphics/Perspective.html

  • 8/9/2019 Matrices en computacion grafica

    10/12

    Ting YipMath 308A

    10

    Important:1) Translate the eye to the Origin

    2) Rotation until direction of eye is toward the negative z-axis

    D is the distance of the eye to the view plane

    z is the distance of the eye to the object(Note: not your eyes but the eyes of the computer polygon person)

    Perspective Matrix for Homogenous Coordinates in R4is given by this matrix:

    01

    00

    0000

    00100001

    d

    Given any point (x, y, z) in R3, the following will give the parallel projected point.

    =

    1

    00

    10

    1

    00

    0000

    0010

    0001

    d

    zy

    d

    zx

    d

    z

    y

    x

    z

    y

    x

    d

    (Note: This matrix transformation does not give pixel coordinate on the monitor. The transformed

    coordinate is with respect to the objects coordinate. We have to translate the objects coordinate to pixel

    coordinate on the monitor.)

    Eye

    Object

    Perspective Projection of the cube

  • 8/9/2019 Matrices en computacion grafica

    11/12

    Ting YipMath 308A

    11

    Conclusion

    I chose to do this project to show my curiosity in math and computer science. I had thechance to talk about video games and math that are often overlooked as unrelated. Asshown in this project,Linear Algebrais extremely useful for video game graphics. Using

    matrices to manipulate points is a common mathematical approach in video gamegraphics.

    Graphics

    (Screenshots taken from Operation Flashpoint)

    In sniping mode, the eye moves closer to the object.

  • 8/9/2019 Matrices en computacion grafica

    12/12

    Ting YipMath 308A

    12

    References

    Dam, Andries. Introduction to Computer Graphicshttp://www.cs.brown.edu/courses/cs123/lectures/Viewing3.pdf

    Holzschuch, Nicola. Projections and Perspectiveshttp://www.loria.fr/~holzschu/cours/HTML/ICG/Resources/Projections/index.html

    Lay, David. Linear Algebra and its Applications. Second Edition.

    Runwal, Rachana. Perspective Projectionhttp://mane.mech.virginia.edu/~engr160/Graphics/Perspective.html