How to use a 3D Texture to instance cubes

I am trying to create a 3d cellular automata, largely inspired the methods used by Evan Wallace’s 3d cellular automata project.

They mention using two 3d textures which ping-pong off each other, and is visualized by using “instanced cubes with one instance per grid cell”.

My question is how do I use a 3D texture to instance these cubes?

I understand how to instance objects, using glDrawElementsInstanced() and calling gl_InstanceID with an array of data, but I’m just struggling to figure out how to access texture data using that method.

  • Do I store positions alongside cell state in the texture?
  • Or do I still send in an array of transformations and use that?

I am still rather new to OpenGL, so apologies if there is a large gap in my understanding of how all of this works.

uniform isampler3d tex;
ivec3d size = textureSize(tex, 0);
int x = gl_InstanceID % size.x;
int y = (gl_InstanceID / size.x) % size.y;
int z = (gl_InstanceID / (size.x * size.y));
ivec4 texel = texelFetch(tex, ivec3(x,y,z), 0);

Thank you so much! I’ve never used texels before, but it works like a charm!