Iterate matrices

Hi,

is there an efficient way to iterate all elements of a mat3? What I mainly wanted to do is sum up all elements of a matrix and was wondering if there is a better way to do it than just:

float sum = 0.;
for( int i = 0; i < 3; ++i )
{
  for( int j = 0; j < 3; ++j )
  {
    sum += m[i][j];
  }
}

thank you

I would do something like

sum = dot(mat[0]+mat[1]+mat[2],vec3(1.0))

^^

Awesome! Thank you very much - This looks somewhat cheaper.

I suppose there isn’t something equivalent for arrays of floats? Although the compiler will probably store these in sets of vec4 anyway? Or do you think a shader compiler will recognize a loop over an array which doesn’t anything more than just summing these elements. This could also be expressed as a set of vec4 dot-products with vec4(1.)…

I would probably need to use vec4[] instead of float[], don’t I? But that seems a bit uncomfortable :frowning:

Originally posted by Zengar:
[b] I would do something like

sum = dot(mat[0]+mat[1]+mat[2],vec3(1.0))

^^ [/b]
couldnt u do dot(mat,mat33(1.0))?
ive never tried but i think dot( … ) can take anytype

It does appear that way at first blush, but upon closer examination, it doesn’t.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.