我现在已经敲了几个小时了。我无法弄清楚为什么glTexSubImage2D
在此代码中是无效的操作。
GLuint texture = 0;
int textureWidth = 512;
int textureHeight = 512;
// Create and bind
glCreateTextures(GL_TEXTURE_2D, 1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// Set wrapping and filtering.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Set storage type and allocate memory.
glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, textureWidth, textureHeight);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, textureWidth, textureHeight, GL_RED, GL_UNSIGNED_INT, NULL);
在调用glTexSubImage2D
之前,我在纹理上查询了一堆参数(大部分是针对mipmap级别0),并且所有结果似乎都表明纹理格式正确:
GL_TEXTURE_IMMUTABLE_FORMAT: 1
GL_TEXTURE_WIDTH: 512
GL_TEXTURE_HEIGHT: 512
GL_TEXTURE_DEPTH: 1
GL_TEXTURE_INTERNAL_FORMAT: 33334
GL_TEXTURE_RED_SIZE: 32
GL_TEXTURE_GREEN_SIZE: 0
GL_TEXTURE_BLUE_SIZE: 0
GL_TEXTURE_ALPHA_SIZE: 0
GL_TEXTURE_DEPTH_SIZE: 0
GL_TEXTURE_COMPRESSED: 0
GL_TEXTURE_BUFFER_OFFSET: 0
我也尝试通过一维向量数组设置实际数据而不是NULL
:
std::vector<GLuint> textureData(textureWidth * textureHeight, 0);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, textureWidth, textureHeight, GL_RED, GL_UNSIGNED_INT, textureData.data());
通过memcpy
:
GLuint *zeros = new GLuint[textureWidth * textureHeight];
memset(zeros, 0, textureWidth * textureHeight * 4);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, textureWidth, textureHeight, GL_RED, GL_UNSIGNED_INT, zeros);
delete[] zeros;
这些都不会改变错误。在我的片段着色器中,我(显然)找到了黑色纹理。