Mac - 线性插值不起作用

Mac - Linear interpolation not working

我搜索了很多,似乎找不到关于这个主题的任何内容。我得到了一本关于 OpenGL 4.1 的 Mac 书。我创建了一个纹理:

glGenTextures(1, &heightMapTexture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, heightMapTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 13);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glUniform1i(program1->uniformLocation("heightMap"), 0);

glTexImage2D(GL_TEXTURE_2D, 0, GL_R16UI, heightMap.getWidth(), heightMap.getHeight(), 0, GL_RED_INTEGER, GL_UNSIGNED_SHORT, heightMap.getData().data());
glGenerateMipmap(GL_TEXTURE_2D);

这很好用,但实际上我想使用 GL_LINEAR。但是 GL_LINEAR 给了我一个全为零/黑色的纹理。

Mac 是不支持 GL_LINEAR 还是有扩展? 在官方文档中我找到了下面这段代码:

glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,
                GL_TEXTURE_MIN_FILTER, GL_LINEAR);

(https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html)

但我也无法使用扩展程序。

Mac 绝对支持 OpenGL 中的双线性和三线性过滤——很长一段时间以来,它一直是核心规范的一部分。您显示的用于上传纹理的代码:

glTexImage2D(GL_TEXTURE_2D, 0, GL_R16UI, heightMap.getWidth(), heightMap.getHeight(), 0, GL_RED_INTEGER, GL_UNSIGNED_SHORT, heightMap.getData().data());

使用 0 作为第二个参数,它对应于基础级图像(有时称为顶级 mipmap)。您没有显示任何代码来上传纹理的其他可能的 mip 级别,也没有使用您使用的是 glGenerateTextureMipmapglGenerateMipmap。在这种情况下,行为在 GL 规范中是未定义的,尽管大多数时候它只会导致黑色纹理样本。可能打开 GL_LINEAR 导致您对这些未定义的纹理级别进行采样,从而导致黑色样本。

您应该通过额外调用 glTexImage2D 来定义这些纹理级别,and/or 将纹理参数限制为您的纹理将包含的实际 mip 级别。

GL_R16UI 是整数纹理格式。 OpenGL 不支持整数格式的线性过滤。

来自 OpenGL 4.5 规范第 8.17 "Texture Completeness" 节,第 252 页:

Using the preceding definitions, a texture is complete unless any of the following conditions hold true:
[..] The internal format of the texture is integer [..] and either the magnification filter is not NEAREST, or the minification filter is neither NEAREST nor NEAREST_MIPMAP_NEAREST.

要使用每个组件精度为 16 位的纹理,您可以使用线性过滤进行采样,您需要使用 GL_R16 等规范化格式,或者使用 GL_R32F.