View Full Version : iterate matrices

02-05-2007, 05:03 AM

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

02-05-2007, 06:15 AM
I would do something like

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


02-06-2007, 12:15 AM
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 :(

02-06-2007, 10:14 AM
Originally posted by Zengar:
I would do something like

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

^^ couldnt u do dot(mat,mat33(1.0))?
ive never tried but i think dot( .. ) can take anytype

02-06-2007, 12:24 PM
It does appear that way at first blush, but upon closer examination, it doesn't.