在此纹理初始化之后,我在每个 gl 调用上都会遇到访问冲突(实际上最后一个 GLCALL(glBindTexture(m_Target,bound));
也会导致访问冲突,因此顶部的代码是可能是什么原因造成的):
Texture2D::Texture2D(unsigned int format, unsigned int width, unsigned int height, unsigned int unit, unsigned int mimapLevels, unsigned int layers)
: Texture(GL_TEXTURE_2D_ARRAY, unit)
{
unsigned int internalFormat;
if (format == GL_DEPTH_COMPONENT)
{
internalFormat = GL_DEPTH_COMPONENT32;
}
else
{
internalFormat = format;
}
m_Format = format;
m_Width = width;
m_Height = height;
unsigned int bound = 0;
glGetIntegerv(GL_TEXTURE_BINDING_2D_ARRAY, (int*)&bound);
GLCALL(glGenTextures(1, &m_ID));
GLCALL(glActiveTexture(GL_TEXTURE0 + m_Unit));
GLCALL(glBindTexture(m_Target, m_ID));
GLCALL(glTexParameteri(m_Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GLCALL(glTexParameteri(m_Target, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GLCALL(glTexStorage3D(m_Target, mimapLevels, internalFormat, width, height, layers));
for (size_t i = 0; i < layers; i++)
{
glTexSubImage3D(m_Target, 0, 0, 0, i, m_Width, m_Height, 1, m_Format, s_FormatTypeMap[internalFormat], NULL);
}
GLCALL(glBindTexture(m_Target, bound));
}
OGL指针在程序开始时用glad初始化:
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
这只发生在 GL_TEXTURE_2D_ARRAY
中,即使这是我代码的第一行(当然是在初始化之后),示例代码:
auto t = Texture2D(GL_DEPTH_COMPONENT, 1024, 1024, 10, 1, 4);
知道是什么原因造成的吗?
提前致谢!
最佳答案
您正在为 glTexSubImage3D
的最后一个参数传递 NULL
,但 OpenGL 不允许这样做:
TexSubImage*D and TextureSubImage*D arguments width, height, depth, format, type, and data match the corresponding arguments to the corresponding TexImage*D command (where those arguments exist), meaning that they accept the same values, and have the same meanings. The exception is that a
NULL
data pointer does not represent unspecified image contents.
...并且没有允许 NULL
指针的文本,因此您不能传递 NULL
。
目前尚不清楚您想要通过这些 glTexSubImage3D
调用实现什么目的。由于您使用的是不可变纹理 (glTexStorage3D
),因此您无需执行任何额外操作。相反,如果您想清除纹理,则可以使用 glClearTexSubImage
,它接受NULL
作为数据,表示'用零清除。
关于c++ - 创建 GL_TEXTURE_2D_ARRAY 后访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61198229/