为 2D 纹理数组生成 mip 级别

发布于 2024-12-07 09:06:31 字数 1424 浏览 3 评论 0原文

我有一个关于 OpenGL 纹理数组的简单(我认为)问题。我使用如下内容创建纹理数组:

glTexImage3D(   GL_TEXTURE_2D_ARRAY,
        0,
        formatGL,
        width,
        height,
        slices,
        0,
        GL_RGBA, 
        GL_UNSIGNED_BYTE, 
        nullptr);

请注意,我还不想实际将任何图像加载到其中。我只是创建稍后将填充数据的结构。

所以我的问题是:

  1. 如何为纹理数组的每个“切片”生成 mip 链?我不想物理生成 mip,我只想通过告诉它有关 mip 的信息来完成纹理。我可以使用 2D 纹理来完成此操作,只需使用 mip 级别、大小并传入 nullptr 调用 glTexImage2D 函数即可。由于没有“切片”参数,我不知道如何使用 glTexImage3D 函数来执行此操作。 OpenGL 超级示例仅将 .bmp 加载到数组中每个纹理的第一个 mip 级别。使用 glTexSubImage3D 并传入 nullptr,将为数组中的给定纹理创建我想要的 mip 级别,这样说是否正确?

  2. khronos.org 上有关 glGenerateMipMap 的文档仅指定 GL_TEXTURE_2D 或 GL_TEXTURE_CUBE_MAP。那么,我不能对 2D 纹理数组使用此方法是否正确?

以下是我使用 2D 纹理创建 mip 链的方法:

    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'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 with data later on.

So my questions are:

  1. 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?

  2. 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++;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-12-14 09:06:31

我可以使用 2D 纹理来完成此操作,只需使用 mip 级别、大小并传入 nullptr 调用 glTexImage2D 函数即可。由于没有“切片”参数,我不知道如何使用 glTexImage3D 函数执行此操作。

为什么不呢? glTexImage3D 采用 mipmap 级别,就像 glTexImage2D 一样。它需要一个大小,在 2D 中是二维,但在 3D 中是三个维度。

事实上,这两个函数之间的唯一区别在于它们所采用的维度数(以及可能的目标参数)。并且有一个“slice”参数;您在向我们展示的通话中将其与 glTexImage3D 一起使用。在那里,它被称为深度

3D 纹理和 2D 数组纹理之间的区别在于,2D 数组的较低 mipmap 中的深度不会改变。数组中的每个 mipmap 都具有与其他每个 mipmap 相同数量的“切片”。

khronos.org 上的文档

如果位于“khronos.org”而不是“opengl.org”,则几乎可以肯定是在谈论 OpenGL ES,它没有数组纹理(稍后注意: ES 3.0 添加了数组纹理)。


老实说,如果不实际为您编写代码,我不知道如何更清楚。

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++;
}

另外,您需要检查 mipmap 大小的最小值是否为 1。因为 (1 >> 1) == 0

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文