Thread: transfer a vector into a matrix

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    Question transfer a vector into a matrix

    i have a vector and want to transfer it to a matrix.

    its a directional vector for moving localZ.

    so i put it into matrix[2][0] and matrix[2][1] and matrix[2][2] //Z

    so when i move localZ it goes the correct way , but it distorts the model, how can i use this vector to put figures into the

    matrix[0][0] and matrix[0][1] and matrix[0][2] // X

    matrix[1][0] and matrix[1][1] and matrix[1][2] //Y

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    A row major translation matrix is:
    [1][0][0][X]
    [0][1][0][Y]
    [0][0][1][Z]
    [0][0][0][1]
    Remember that in C/C++ if you have a matrix[y][x], x is columns and y is rows.
    Devoted my life to programming...

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Remember that in C/C++ if you have a matrix[y][x], x is columns and y is rows.
    I would recommend the following structure (similar to D3DXMATRIX):

    Code:
    struct Matrix4x4
    {
       float _11, float _12, float _13, float _14;
       float _21, float _22, float _23, float _24;
       float _31, float _32, float _33, float _34;
       float _41, float _42, float _43, float _44;
       ...
    };
    I would also overload operators where it makes sense relative to Matrix4x4 to make operations much simpler.

    What has been shown is a simple translation matrix for OGL. Here is the Direct3D version:

    [1][0][0][0]
    [0][1][0][0]
    [0][0][1][0]
    [x][y][z][1]

    If you were wanting to know how to transform a vector by this matrix here is the math:

    Code:
    Vector3 resultVector;
    Vector3 srcVector;
    Matrix4x4 mat;
    
    resultVector.x = srcVector.x * mat._11 + srcVector.y * mat._21 + src.Vector.z * mat._31 + mat._41;
    resultVector.y = srcVector.x * mat._12 + srcVector.y * mat._22 + src.Vector.z * mat._32 + mat._42;
    resultVector.z = srcVector.x * mat._13 + srcVector.y * mat._23 + src.Vector.z * mat._33 + mat._43;
    Last edited by VirtualAce; 12-09-2010 at 05:14 PM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yeah, i guess my answer was misplaced since OpenGL and DirectX are using column major matrices...
    Devoted my life to programming...

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I believe your answer for OGL was correct. Direct3D matrices are the transpose of OGL and vice versa.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    But OpenGL too uses the following as the translation matrix:
    [1][0][0][0]
    [0][1][0][0]
    [0][0][1][0]
    [x][y][z][1]
    Direct3D matrices aren't the transpose of OGL, they just use opposite rotation direction.
    Both Direct3D and OGL use column-major matrices. I just showed the row-major version because it is easier to understand.

    'ROWman COLUMN'
    Devoted my life to programming...

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    But OpenGL too uses the following as the translation matrix:
    Interesting. When decoding file formats that have used OGL in the past we had to transpose the matrices in them to get the models to display correctly. As I have no experience with OGL this makes me wonder why transposing was required? However, your information is duly noted and I will keep it handy should I need it later. Thanks for clearing it up.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    At least according to the opengl standard 2.1 the translation matrix is as follows:
    Code:
    1 0 0 x
    0 1 0 y
    0 0 1 z
    0 0 0 1
    Which would indicate that bubba is correct. I however dont know about the current standard (4.1) and they may very well have changed that since they pretty much made everything old deprecated since (i think) 3.1.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The ordering of the matrix also represents the matrix handedness. Direct3D uses left hand matrices although it supports right as well. OGL uses right hand matrices.

    In a left hand matrix:

    • -x is left, +x is right
    • -y is down, +y is up
    • -z is away from the camera, +z is farther from the camera


    I believe right hand matrices are the same except for +z is towards the camera and -z is away from the camera. As well I think +y is down and -y is up.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Oh now, surely you know the handedness rule: take your hand (left hand for a lefthanded system, right hand for a righthanded system) and orient it so that your fingers point in the direction of +x and your palm faces in the direction of +y. Your thumb points in the direction of +z.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yep.

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I don't know, maybe i'm wrong. But each time i was passing a matrix to OGL i had to transpose it. And in my library i use row-major, matrices!!! ( row/column major isn't the same as right/left handed. The first speaks about representation, the latter about rotation direction ).
    Devoted my life to programming...

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    IIRC GL is column-major (and right-handed, multiplying on the left).

    (EDIT: Looking it up, from the man pages for MultMatrix:
    Name

    glMultMatrix — multiply the current matrix with the specified matrix
    C Specification
    void glMultMatrixd( const GLdouble * m);
    void glMultMatrixf( const GLfloat * m);
    Parameters

    m


    Points to 16 consecutive values that are used as the elements of
    a 4×4 column-major matrix.
    Last edited by tabstop; 12-11-2010 at 01:29 PM.

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ok, there seems to be some kind of misconception betwean row-major and column-major or something because from what I can see we both agree that OGL uses column-major matrix. OpenGL from the standard (as seen http://www.opengl.org/documentation/...1/glspec21.pdf) has this matrix layout:
    Code:
    a01 a05 a09 a13
    a02 a06 a10 a14
    a03 a07 a11 a15
    a04 a08 a12 a16
    and a13 a14 a15 makes up the translation of x y and z with the diagonal set to 1.

    The standard also makes a note as
    This differs from the standard row-major C ordering for matrix elements. If the
    standard ordering is used, all of the subsequent transformation equations are transposed, and the columns representing vectors become rows.
    so there you have it folks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 09:40 AM
  2. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM