V

Avatar

1d to 2d

At some point you might end up needing to convert a 1d index value to access a 2d array. Here’s how you can do it.


// Texture is POT, NxN
int x = index % N;
int y = index / N;

As noted by Diogo Teixeira, one way to get rid of the mod/div operations would be:


int precalc_a = ( N - 1 );
int precalc_b = (int)log2( N );
int x = index  & precalc_a;
int y = index >> precalc_b;

.