OpenGL 3.+中带有mipmap的数组纹理

Array texture in OpenGL 3.+ with mipmap

本文关键字:mipmap 数组 纹理 OpenGL      更新时间:2023-10-16

我已经阅读了本教程:数组纹理,但是我不想使用glTexStorage3D()函数(需要OpenGL 4.2)。首先,有人能检查一下我是否正确地实现了这段代码吗(我使用的是glTexImage3D而不是glTexStorage3D):

unsigned int nrTextures = 6;
GLsizei width = 256;
GLsizei height = 256;
GLuint arrayTextureID;
std::vector<unsigned char*> textures(nrTextures);
//textures: Load textures here...
glGenTextures(1, &arrayTextureID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, arrayTextureID);
//Gamma to linear color space for each texture.
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_SRGB, width, height, nrTextures, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
for(unsigned int i = 0; i < nrTextures; i++)
    glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, width, height, 1, GL_RGB, GL_UNSIGNED_BYTE, textures[i]);
/*glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);*/
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
/*glGenerateMipmap(GL_TEXTURE_2D_ARRAY);*/
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);

如何使用此实现实现mipmapping?

这基本上看起来不错。我能看到的唯一问题是GL_SRGB不是一个有效的内部纹理格式。因此glTexImage3D()调用需要使用GL_SRGB8

glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_SRGB8, width, height, nrTextures, 0,
             GL_RGB, GL_UNSIGNED_BYTE, NULL);

要生成mipmap,可以调用:

glGenerateMipmap(GL_TEXTURE_2D_ARRAY);

在使用glTexSubImage3D()调用用数据填充纹理之后。