Transformations Opengl 11 12 eBook

Embed Size (px)

Citation preview

  • 8/13/2019 Transformations Opengl 11 12 eBook

    1/29

    Transformations in OpenGL

    Lecturer: Carol OSullivanProfessor of Visual Computing

    [email protected]

    Course www: http://isg.cs.tcd.ie/cosulliv/

  • 8/13/2019 Transformations Opengl 11 12 eBook

    2/29

    o e ng rans orma ons

    transformations are: glTranslate*(),lRotate* and lScale*

    These routines transform an object (or

    stretching, shrinking, or reflecting it.

    producing an appropriate translation, rotation,

    or scalin matrix, and then callinglMultMatrix*() with that matrix as the

    argument.

  • 8/13/2019 Transformations Opengl 11 12 eBook

    3/29

    o e ng rans orma ons

    glLoadIdentity()

    creates an id e nt ity m a trix(used for clearingall trans ormatons

    glLoadMatrixf(matrixptr)

    loads a use r sp e c if ie d tra nsfo rm a t io n m a trix

    where mat r i xpt r is defined as GLf l oat

    mat r i xpt r [ 16] ;

  • 8/13/2019 Transformations Opengl 11 12 eBook

    4/29

    ransa e , ,

    translates by d isp la c em en t vec to r(dx, dy, dz)

    moves (translates) an object by the given x-, y-,

    -

    system by the same amounts).

    . , . , .means it has no effect on an object or its local

    coordinate s stem.

  • 8/13/2019 Transformations Opengl 11 12 eBook

    5/29

    o a e lRotatef an le vx v vz

    rotates about axis (vx, vy, vz) by angl e (specified

    in degrees)

    rotates an object (or the local coordinates stem) in a counterclockwise direction about

    the ray from the origin through the point (x,y,z).The angle parameter specifies the angle of

    .The effect of glRotatef(45.0, 0.0, 0.0, 1.0)

    is a rotation of 45 de rees about the z-axis. If angle = 0.0 the command has no effect.

  • 8/13/2019 Transformations Opengl 11 12 eBook

    6/29

    ca e

    apply scaling of sx in x direction, sy in y direction

    and sz in zdirection (note that these values specifyt e d i agonao a requ re matrx

    Multiplies the current matrix by a matrix that, ,

    axes. Each x-, y-, and z-coordinate of every point inthe object is multiplied by the correspondingargumen x,y, or z.

    With the local coordinate system approach, the, ,

    reflected by the x-, y-, and z-factors, and theassociated object is transformed with them.

  • 8/13/2019 Transformations Opengl 11 12 eBook

    7/29

    rans orma ons n pen OpenGL defines 3 matrices for manipulation of 3D scenes:

    GL_MODELVIEW: manipulates the view and models simultaneouslyGL_PROJECTION: per orms pro ec on or sp ay

    GL_TEXTURE: for manipulating textures prior to mapping on objects

    altered.

    _ ,

    vertices are created with the specified transformation.

    Matrix transformation o erations a l to the currentlselected system matrix:

    use glMatrixMode(GL_MODELVIEW) to select modeling

    matrix

  • 8/13/2019 Transformations Opengl 11 12 eBook

    8/29

    tages o ertex rans ormaton

    x

    Vertex Geometry Pipeline

    MODELVIEW

    matrix

    PROJECTION

    matrix

    perspective

    division

    viewport

    transformation

    z

    y

    win

    win

    y

    x

    eye

    eye

    y

    x

    dev

    dev

    y

    x

    projxoriginal

    vertex /final window

    eye

    eye

    w

    z 1 projwobject

    coordinates

    2d projection normalised devicevertex n t e

    eye coordinate

    space

    of vertex onto

    viewing plane /

    clip coordinates

    (foreshortened)

  • 8/13/2019 Transformations Opengl 11 12 eBook

    9/29

    matr ixand therefore has12 d e g re e s o f fre e d om:

    tcba

    y

    tih

    tfed

    1000z

    The MODELVI EWmatrix is used for both the model andthe camera transformation

    camera in the opposite direction OpenGL usesthe same transformation matrix

    this sometimes causes confusion!

  • 8/13/2019 Transformations Opengl 11 12 eBook

    10/29

    the currentMODELVIEWmatrix C is altered:

    = =

    glTranslatef(1.5, 0.0, 0.0);

    glRotatef(45.0, 0.0, 0.0, 1.0);

    CTRvv =

  • 8/13/2019 Transformations Opengl 11 12 eBook

    11/29

    xamp e

    glMatrixMode(GL_MODELVIEW);

    lLoadIdentit ();

    glMultMatrix(N);glMultMatrix(M);

    glMultMatrix(L);

    glBegin(GL_POINTS);

    glEnd();

  • 8/13/2019 Transformations Opengl 11 12 eBook

    12/29

    Transformations are applied in the o rd e r sp e c if ie d(with

    glMatrixMode(GL_MODELVIEW);

    glTranslatef(1.5, 0.0, 0.0);

    glRotatef(45.0, 0.0, 0.0, 1.0);

    glVertex3f(1.0, 0.0, 0.0);

    original rotate translate

  • 8/13/2019 Transformations Opengl 11 12 eBook

    13/29

    transformations are specified

    using a single matrix we musttransformations on the coo rd i na tef ramesof the camera and

    .

    Assume we wish to orbi tan objectat a f ixe d o rie nta t io n:

    transate o ect away romcamera

    rotate around X to look at top ofo ect

    then pivot around objects Y inorder to orbit properly.

  • 8/13/2019 Transformations Opengl 11 12 eBook

    14/29

    If the GL MODELVIEWmatrix is an identit matrix then the_

    camera frame and the model frame are the same.

    i.e. they are specified using the same co-ordinate system If we issue command glTranslatef(0.0,0.0,-10.0);

    and then create the model: , , , , -

    the camera frame i.e. we have moved the object away from the camera

    This may be viewed conceptually in 2 ways:

    we have positioned the object with respect to thewor rame we have moved the world-frame with respect to the

    camera frame

  • 8/13/2019 Transformations Opengl 11 12 eBook

    15/29

    coordinate system approach) is:

    lMatrixMode GL MODELVIEWmove away from

    read transformations

    in reverse order_

    glLoadIdentity();glTranslatef(0.0, 0.0, -10.0);

    rotate around world X. , . , . , .

    glRotatef(Pivot_Angle, 0.0, 1.0, 0.0);(tilt towards camera)

    _

    glEnd();

    draw required object

  • 8/13/2019 Transformations Opengl 11 12 eBook

    16/29

    view) is:

    glMatrixMode(GL_MODELVIEW);clear previous transformations

    glTranslatef(0.0, 0.0, -10.0);glRotatef(30.0, 1.0, 0.0, 0.0);

    rotate object into view

    g o a e vo _ ng e, . , . , . ;

    glBegin(GL_POLYGON);

    pivot around objects Y axis

    glEnd();draw required object

  • 8/13/2019 Transformations Opengl 11 12 eBook

    17/29

    objects new Y

    rotate

    translate

  • 8/13/2019 Transformations Opengl 11 12 eBook

    18/29

    naturally to the specification of h ie ra rc h ic a l m o d e l

    f rames.

    This allows creation ofjo in te d a ssem b lie s

    articulated figures (animals, robots etc.)

    In the hierarchical model, each sub-component hasits own lo c a l fra m e.

    Changes made to the p a re n t fra m eare propagated

    down to the c h ild fra m e s(thus all models in a branchare g o a y con ro e y e paren .

    This simplifies the specification of an ima t ion.

  • 8/13/2019 Transformations Opengl 11 12 eBook

    19/29

    It is often expensive to compute or create a model for.

    We would prefer not to have to recalculate it each time for

    each new animation frame.

    Disp la y listsallow the creation of an object in memory,where it resides until destroyed.

    Objects are drawn by issuing a request to the server to displaya given display list. number of list IDs to create

    list = glGenLists(1);

    glNewList(list, GL_COMPILE);

    create_model();

    GLui nt of first

    list ID

    begin specification of the list

    glEndList();

    glCallList(list);

    used later to display the list

    glDeleteLists(list, 1);delete when finished

    (specifying number of lists)

  • 8/13/2019 Transformations Opengl 11 12 eBook

    20/29

    o e rans orma ons

    created with respect to a changing transformation.

    orCTM .

    The CTMbehaves like a 3D ointe selectin new

    positions and orientations for the creation ofgeometries.

    The only complication with this view is that each newCTMis derived from a previousCTM, i.e. all CTM sarespec e re a ve o prev ous versons.

    A sc a ling tra nsfo rm a tio ncan cause some confusion.

  • 8/13/2019 Transformations Opengl 11 12 eBook

    21/29

    Scalin Transformation and the CTM

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    lTranslatef 3 0 0

    glTranslatef(1, 0, 0);

    lMatrixMode GL MODELVIEW_

    glLoadIdentity();

    glTranslatef(3, 0, 0);

    . , . , .. , . , .glTranslatef(1, 0, 0);

    g uCy n er ;

  • 8/13/2019 Transformations Opengl 11 12 eBook

    22/29

    erarc ca rans orma ons

    For geometries with an

    implicit hie ra rc hy we wish toassociate local frames withsub-objects in the assembly.

    Pa re nt-c h ild fra m e sarerelated via a transformation.

    rans orma on n age sdescribed by a tree:

    ac no e as s own o c ac o -o rd ina te system.

  • 8/13/2019 Transformations Opengl 11 12 eBook

    23/29

    erarc ca rans orma ons

    R

    RT

    over sub-parts of an assembly

  • 8/13/2019 Transformations Opengl 11 12 eBook

    24/29

    translate base rotatej oi nt 1

    rotate j oi nt 2 complex hierarchical transformation

  • 8/13/2019 Transformations Opengl 11 12 eBook

    25/29

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glTranslatef(bx, by, bz);

    create base_

    glTranslatef(0, j1y, 0);

    glRotatef(joint1_orientation);

    _

    glTranslatef(0, uay, 0);create_upperarm();

    ,

    glRotatef(joint2_orientation);

    create_joint2();g Trans ate , ay, ;

    create_lowerarm();

    glTranslatef(0, py, 0);

    glRotatef(pointer_orientation);

    create_pointer();

  • 8/13/2019 Transformations Opengl 11 12 eBook

    26/29

    - -parent-child linkages.

    In eneral there ma be man c h ild fra m e sderived from a single parent frame.

    We need some mechanism to remember the

    parent frame and return to it when creating newchildren.

    pen prov e a ma rx s ac or us spurpose:

    glPopMatrix() returns to the last saved CTM

  • 8/13/2019 Transformations Opengl 11 12 eBook

    27/29

    erarc ca rans orma ons

    Each finger is a child of the parent (wrist)

    independent control over the orientationof the fingers relative to the wrist

  • 8/13/2019 Transformations Opengl 11 12 eBook

    28/29

  • 8/13/2019 Transformations Opengl 11 12 eBook

    29/29

    glMatrixMode(GL_MODELVIEW);

    lLoadIdentit

    glTranslatef(bx, by, bz);create_base();

    glTranslatef(0, jy, 0);

    lRotatef oint1 orientation_

    create_joint1();

    glTranslatef(0, ay, 0);

    create_upperarm();

    lTranslatef 0 w

    glRotatef(wrist_orientation);

    create_wrist();

    glPushMatrixglPushMatrix();(); // save frame

    lTranslatef -xf f 0 0

    glRotatef(lowerfinger1_orientation);

    glTranslatef(0, fy1, 0);

    create_lowerfinger1();

    lTranslatef(0 f 2 0);

    glRotatef(upperfinger1_orientation);

    create_fingerjoint1();

    glTranslatef(0, fy3, 0);

    create u erfin er1();_glPopMatrixglPopMatrix();(); // restore frame

    glPushMatrixglPushMatrix();();

    // do finger 2...

    lPo MatrixlPo Matrix();();Finger1

    glPushMatrixglPushMatrix();();

    // do finger 3...glPopMatrixglPopMatrix();();