因此,我目前正在尝试用2D纹理数组替换旧的纹理图集缝合器,以便在以后使用各向异性过滤和贪婪网格化来简化生活。
我正在用stb加载png文件,我知道缓冲区已正确填充,因为如果我在上传之前导出即将成为图册的每一层,那么它就是正确的png文件。
我的设置如下:
我正在使用stb加载我的jar文件中的每个纹理,并使用它创建一个对象,该对象存储宽度,高度,图层和pixelData。
加载每个纹理时,我会寻找最大的纹理,并将每个较小的纹理缩放到与最大纹理相同的大小,因为我知道只有每个图层中的每一个具有相同的大小,2D纹理数组才有效。
然后我像这样初始化2d纹理数组:
public void init(int layerCount, boolean supportsAlpha, int textureSize) {
this.textureId = glGenTextures();
this.maxLayer = layerCount;
int internalFormat = supportsAlpha ? GL_RGBA8 : GL_RGB8;
this.format = supportsAlpha ? GL_RGBA : GL_RGB;
glBindTexture(GL_TEXTURE_2D_ARRAY, this.textureId);
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, internalFormat, textureSize, textureSize, layerCount, 0, this.format, GL_UNSIGNED_BYTE, 0);
}
之后,我浏览了textureLayer对象的地图,并按照以下方式上传每一个对象:
public void upload(ITextureLayer textureLayer) {
if (textureLayer.getLayer() >= this.maxLayer) {
LOGGER.error("Tried uploading a texture with a too big layer.");
return;
} else if (this.textureId == 0) {
LOGGER.error("Tried uploading texture layer to uninitialized texture array.");
return;
}
glBindTexture(GL_TEXTURE_2D_ARRAY, this.textureId);
// Tell openGL how to unpack the RGBA bytes
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Tell openGL to not blur the texture when it is stretched
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Upload the texture data
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, textureLayer.getLayer(), textureLayer.getWidth(), textureLayer.getHeight(), 0, this.format, GL_UNSIGNED_BYTE, textureLayer.getPixels());
int errorCode = glGetError();
if (errorCode != 0) LOGGER.error("Error while uploading texture layer {} to graphics card. {}", textureLayer.getLayer(), GLHelper.errorToString(errorCode));
}
我的每一个图层的错误代码都是0,所以我认为一切都很顺利。但是当我使用RenderDoc调试游戏时,我可以看到每个单独的每一层都是0,因此它只是一个具有正确宽度和高度的透明纹理。
我无法弄清楚我做错了什么,因为openGL告诉我一切顺利。对我来说很重要的是我只使用openGL 3.3和更低,因为我希望游戏可以在旧PC上播放,所以预先用glTexStorage3D
分配内存不是一种选择。
qazxsw poi的第8个参数应为1(qazxsw poi)。
不,层的大小是glTexSubImage3D
,depth
,textureLayer.getWidth()
:
textureLayer.getHeight()
将1
,glTexSubImage3D(
GL_TEXTURE_2D_ARRAY, 0, 0, 0, textureLayer.getLayer(),
textureLayer.getWidth(), textureLayer.getHeight(), 1, // depth is 1
this.format, GL_UNSIGNED_BYTE, textureLayer.getPixels());
或width
传递给height
并不是错误,但它对纹理对象数据存储没有任何影响。