Search code examples
glslglsles

GLSL's transpose alternative in GLSL ES?


I was trying to convert GLSL to GLSL ES and got this problem: transpose function isn't available in GLSL ES.

mat3 yuv = transpose(yuv_matrix);

So how I can use it? Is there any alternative?


Solution

  • A 3x3 matrix can be transposed as follows:

    mat3 yuv = mat3(
        vec3(yuv_matrix[0].x, yuv_matrix[1].x, yuv_matrix[2].x),
        vec3(yuv_matrix[0].y, yuv_matrix[1].y, yuv_matrix[2].y),
        vec3(yuv_matrix[0].z, yuv_matrix[1].z, yuv_matrix[2].z));
    

    Note, the transposed matrix is a matrix which is flipped over its diagonal.

    (a  b  c) T    (a  d  g)
    (d  e  f)    = (b  e  h)
    (g  h  i)      (c  f  i)
    

    Matrices consist of column vectors. So a matrix can be initialized by vectors, e.g.:

    vec3 a, b, c;
    mat3 m = mat3(a, b, c); 
    

    And the vectors of the matrix can be accessed by the index operator, e.g.:

    mat3 m;
    vec3 v = m[1];