Search code examples
c++matrixglm-math

GLM: How to create a matrix of 9 columns and 3 rows


I know only one way which is glm::mat4 matrix

I tried even float matrix[9][3] which didn't work, I need it to multiply it with glm::vec3

How to create?


Solution

  • You can compute that with 3x3 sub matrices and combine the outputs into final result. There are two options:

    1. 9 rows

       a a a       u'
       a a a       u'
       a a a       u'
       b b b   u   v'
       b b b * u = v'
       b b b   u   v'
       c c c       w'
       c c c       w'
       c c c       w'
      

      this is really simple:

       u' = a*u
       v' = b*u
       w' = c*u
      
    2. 9 columns

                           u
                           u
                           u
       a a a b b b c c c   v   u' 
       a a a b b b c c c * v = u'
       a a a b b b c c c   v   u'
                           w
                           w
                           w
      

    This is more complicated but not by much:

        u.x' = (a*u).x + (b*v).x + (c*w).x
        u.y' = (a*u).y + (b*v).y + (c*w).y
        u.z' = (a*u).z + (b*v).z + (c*w).z
    

    This is common way for expanding dimensionality in GLSL for example for purposes like these:

    [Edit1] 9x3 and 3x9 matrices version:

    +-----+                         +-----------------+
    |     |                         |     |     |     |
    |  A  |                         | A*U | A*V | A*W |
    |     |                         |     |     |     |
    |-----|   +-----------------+   |-----+-----+-----|
    |     |   |     |     |     |   |     |     |     |
    |  B  | * |  U  |  V  |  W  | = | B*U | B*V | B*W |
    |     |   |     |     |     |   |     |     |     |
    |-----|   +-----------------+   |-----+-----+-----|
    |     |                         |     |     |     |
    |  C  |                         | C*U | C*V | C*W |
    |     |                         |     |     |     |
    +-----+                         +-----------------+
    
                                   +--------+
                                   |u1 u2 u3|
                                   |u1 u2 u3|
                                   |u1 u2 u3|
    +--------------------------+   |--------|   +-----------------------------------------------------------------------------+
    |a1 a1 a1|b1 b1 b1|c1 c1 c1|   |v1 v2 v3|   | (a1.u1)+(b1.v1)+(c1.w1) | (a1.u2)+(b1.v2)+(c1.w2) | (a1.u3)+(b1.v3)+(c1.w3) |
    |a2 a2 a2|b2 b2 b2|c2 c2 c2| * |v1 v2 v3| = | (a2.u1)+(b2.v1)+(c2.w1) | (a2.u2)+(b2.v2)+(c2.w2) | (a2.u3)+(b2.v3)+(c2.w3) |
    |a3 a3 a3|b3 b3 b3|c3 c3 c3|   |v1 v2 v3|   | (a3.u1)+(b3.v1)+(c3.w1) | (a3.u2)+(b3.v2)+(c3.w2) | (a3.u3)+(b3.v3)+(c3.w3) |
    +--------------------------+   |--------|   +-----------------------------------------------------------------------------+
                                   |w1 w2 w3|
                                   |w1 w2 w3|
                                   |w1 w2 w3|
                                   +--------+
    

    where (a1.u1) means dot product between (a1,a1,a1) and (u1,u1,u1) and uppercase A means entire 3x3 submatrix