Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: simple matrix question

Hybrid View

  1. #1
    Intern Newbie
    Join Date
    May 2005
    Location
    NY, NY
    Posts
    30

    simple matrix question

    Hi,

    I'm writing a small matrix library to understand matrix transformations. Everything is working well up to the point where I translate THEN scale - the polygons seem to not get scaled "in place", because translations are being done before scaling. This is the correct behavior for the matrix operations, but I want to create a function that lets me scale "in place".

    Is there some way to have transformations applied to an object work in its own local coordinate system? For example:

    class Polygon {
    Matrix4x4 m_lcl; // relative matrix?

    ApplyTransform(Matrix4x4 m)
    {
    // Somehow manipulate the input matrix
    // so it works "in place"?
    }

    };

    Thanks for any ideas.

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: simple matrix question

    scale then translate.

  3. #3
    Intern Newbie
    Join Date
    May 2005
    Location
    NY, NY
    Posts
    30

    Re: simple matrix question

    Hi ZbuffeR,

    I would, but I can't figure out how to do just that -

    I'm binding my left mouse click to a transform matrix of (5,5,0), and my right mouse click to a scale of (3,3,0). So when I left mouse click a few times, my vertices are already translated - then when I right click, the scale is applied but it's already been affected by the translation -

    Now that I think about it again, should I do something like this:

    class Polygon {
    Vertex m_vertices[n];
    Matrix m_matrix;

    ApplyTranslate(Matrix m)
    {
    // Post multiply? Translations always at the back?
    m_matrix = m_matrix * m;
    }

    ApplyRotate(Matrix m)
    {
    // Pre multiply? Rotations always at the front?
    m_matrix = m * m_matrix;
    }

    ApplyScale(Matrix m)
    {
    // Pre multiply? Scales always at the front?
    m_matrix = m * m_matrix;
    }
    };

    Is that what you mean? Therefore translation multiplications are always done at the end?

    Thanks

  4. #4
    Intern Contributor
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    84

    Re: simple matrix question

    Hi,

    The easiest way to do it (using several transformation)
    1.before scaling translate to origin.
    2.scale as needed
    3.translate back to place

    Example: if you have a box from 10,10 to 20,20 and want to translate to 20,20 and scale by 2,2
    1. translate to origin: box is now 0,0 to 10,10
    2. scale: box is now 0,0 to 20,20
    3. translate back: box is now 20,20 to 40,40 - in this step you nay have to translate in different ratio if the center is 0,0 of the box for example.

    Ido
    Ido Ilan

  5. #5
    Member Regular Contributor
    Join Date
    Apr 2007
    Location
    Fairfax, VA
    Posts
    252

    Re: simple matrix question

    ^Just so. Using the homogeneous coordinate representation of translations, you can even combine all three steps into a single matrix easily enough, effectively getting what you're looking for.

  6. #6
    Junior Member Newbie
    Join Date
    Aug 2003
    Posts
    18

    Re: simple matrix question

    I believe SRT is most common matrix transformation order, Scale, Rotate, then Translate.
    Code :
    M = S * R * T;

  7. #7
    Member Regular Contributor
    Join Date
    May 2001
    Posts
    349

    Re: simple matrix question

    It all depends on how you fill your matrices.

    If you set up transformations so that the new coordinate system axes are stored as columns then you have do the vertex transformation as M * v, v being a column vector representing the vertex position.

    In this case if you combine some matrices they will be applied in reverse order. E.g. M = S * T will result in the translation being done before the scale, i.e. the translation will get scaled as well.

    This is the way OpenGL does it, so you better use this if you want to be consistent. OpenGL also normally expects matrices in column-major order in memory (i.e. as a series of column vectors), which is an entirely separate issue.


    You can also do it the other way round, storing the axes as rows of the matrix. In this case all the multiplications need to be reversed, i.e. you use v * M, and if you want to translate then scale you use M = T * S.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •