我有一个用于从路径创建纹理的类,但是当我尝试加载具有 3 个通道(rgb)的纹理时,它在运行此行时给了我读取访问冲突异常
glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, m_DataFormat, GL_UNSIGNED_BYTE, data);
我试图更改 stbi 加载函数中的参数,但没有奏效。
当我加载图像时,它会设置正确的宽度、高度和通道数据,所以我不知道我在做什么错
指向数据的指针也不是 nullptr
OpenGLTexture2D::OpenGLTexture2D(const std::string& path)
{
RADIANT_PROFILE_FUNCTION();
m_Path = path;
stbi_set_flip_vertically_on_load(1);
int width, height, channels;
stbi_uc* data = stbi_load(path.c_str(), &width, &height, &channels, 0);
RADIANT_CORE_ASSERT(data, "Failed To Load Image");
m_Width = width;
m_Height = height;
if (channels == 4) {
m_InternalFormat = GL_RGBA8;
m_DataFormat = GL_RGBA;
}
else if (channels == 3) {
m_InternalFormat = GL_RGB8;
m_DataFormat = GL_RGB;
}
else {
RADIANT_CORE_ERROR("Texture Format Not Supported, Channels: {0})", channels);
}
glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);
glTextureStorage2D(m_RendererID, 1, m_InternalFormat, m_Width, m_Height);
glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, m_DataFormat, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}