I've got a simple (I think) question about OpenGL texture arrays. I create my texture array with something like the following:
glTexImage3D( GL_TEXTURE_2D_ARRAY,
0,
formatGL,
width,
height,
slices,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
nullptr);
Note that I don't want to actually load any images into it yet. I'm just creating the structure that I'll be filling wit开发者_开发百科h data later on.
So my questions are:
How do I generate the mip chain for each "slice" of the texture array? I don't want to physically generate the mip, I just want to complete the texture by telling it about the mip. I can do this with 2D textures simply by calling the glTexImage2D function with the mip level, size and also passing in a nullptr. I can't see how to do this with the glTexImage3D function given there's no "slice" parameter. The OpenGL superbible example only loads a .bmp into the first mip level for each texture in the array. Is it correct to say that using glTexSubImage3D, passing in a nullptr, will create the mip level I want for a given texture in the array?
Documentation at khronos.org on glGenerateMipMap specifies GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP only. So is it correct to say I can't use this method for 2D texture arrays?
Here's what I do with 2D textures to create the mip chain:
size_t i = 0, mipSizeX = width, mipSizeY = height;
while(mipSizeX >= 1 || mipSizeY >= 1)
{
// Generate image for this mip level
glTexImage2D( GL_TEXTURE_2D,
i,
formatGL,
mipSizeX,
mipSizeY,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
nullptr);
// Go to the next mip size.
mipSizeX >>= 1;
mipSizeY >>= 1;
// and the next mipmap level.
i++;
}
I can do this with 2D textures simply by calling the glTexImage2D function with the mip level, size and also passing in a nullptr. I can't see how to do this with the glTexImage3D function given there's no "slice" parameter.
Why not? glTexImage3D takes a mipmap level, just like glTexImage2D. It takes a size, which in 2D is two dimensions, but in 3D is three dimensions.
Indeed, the only difference between the two functions is the number of dimensions that they take (and therefore the possible target
parameters). And there is a "slice" parameter; you used it with glTexImage3D in the call you showed us. There, it's called depth
.
The difference between 3D textures and 2D array textures is that the depth
does not change in lower mipmaps for 2D arrays. Each mipmap of the array has the same number of "slices" as every other mipmap.
Documentation at khronos.org
If it's at "khronos.org" and not "opengl.org", then it's almost certainly talking about OpenGL ES, which does not have array textures (later note: ES 3.0 added array textures).
I honestly don't know how to be any clearer without actually writing your code for you.
size_t i = 0, mipSizeX = width, mipSizeY = height;
while(mipSizeX >= 1 || mipSizeY >= 1)
{
// Generate image for this mip level
glTexImage3D( GL_TEXTURE_2D_ARRAY,
i,
formatGL,
mipSizeX,
mipSizeY,
arrayCount,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
nullptr);
// Go to the next mip size.
mipSizeX >>= 1;
mipSizeY >>= 1;
// and the next mipmap level.
i++;
}
Also, you need to check that the mipmap sizes have a minimum value of 1. Because (1 >> 1) == 0
.
精彩评论