To specify correct mipmaps for a 3D texture, you need to reduce the size of each level in all 3 directions:
for (int level = 0; level < LevelCount; level++){
GL.TexImage3D(glTarget, level, glInternalFormat, width, height, depth, 0,
glFormat, glType, IntPtr.Zero);
width = width >> 1;
height = height >> 1;
depth = depth >> 1;
}
You can picture a 3D texture as a full generalization of a 2D texture into 3 dimensions. Each mipmap is half the size of the previous mipmap in all 3 dimensions. The typical use of a 3D texture is for volumetric data that can be described by a 3D grid of texels, which are then sampled using 3 texture coordinates.
As already suggested in a comment, what you are trying to use sounds much more like a 2D texture array. As the name suggests, this is an array of separate 2D textures, where the depth specifies the number of 2D textures in the array.
Now, if you picture the texture array as a bunch of 2D textures stacked on top of each other, it looks very similar to a 3D texture. In both cases, you end up with a 3D grid of texels. And in fact, the differences are fairly subtle as long as you are not using mipmaps.
With mipmaps in play, there are very substantial differences:
- In a 3D texture, each mipmap is for the entire 3D texture. Its shape is a box with half the size in each direction.
- In a 2D texture array, each texture in the array has its own mipmaps. If you picture the images of a given mipmap level for the entire array being stacked on top of each other again, the result still has the same number of images as the original texture array (i.e. the same depth as the original texture data), but the size of each image (i.e. its width and height) is half that of the size of the previous level.
It clearly looks like you were going for the second case. All you really need to do is change the texture target. The texture target is GL_TEXTURE_2D_ARRAY in the C bindings. If the C# bindings follow the same pattern, it should be:
glTarget = TextureTarget.Texture2DArray;
Related to the different sizes of the mipmaps between 3D textures and 2D texture arrays , there are also differences in how they are sampled. For a 3D texture, there is mipmapping in all 3 directions. So linear sampling can use 8 texels each from two different mipmap levels. With a 2D texture array, sampling will always use only texels from a single layer, meaning that 4 texels each from 2 mipmap levels are used.