TexSubImage2D produces a garbled texture for specific images only
我正在尝试将一个纹理渲染到另一个纹理的顶部,如下图所示:
但是,只有该图像可以正确渲染。我的其他图像出现乱码并"扭曲"。如果仔细看,就好像行已移位:
在上面的示例中,我在背景中使用了完全相同的猫图片。除了一张特殊图片外,由于某种原因,这张猫图片和我生成的所有其他图片最终都会出现乱码。我查看过EXIF数据,除了它不使用sRGB之外,它的格式与其他格式完全相同。它具有alpha通道和所有内容。
我认为,考虑到行的移动方式,它与像素对齐有关,但是我已经尝试了对齐的所有可能组合,但到目前为止没有任何效果。这是我的代码:
1 2 3 4 5 6 7 8 9 | int height, width = 512; m_pSubImage = SOIL_load_image("sample.png", &width, &height, 0, SOIL_LOAD_RGBA); glGenTextures(1, &m_textureObj); glBindTexture(m_textureTarget, m_textureObj); ... glActiveTexture(TextureUnit); glBindTexture(m_textureTarget, m_textureObj); glTexSubImage2D(GL_TEXTURE_2D, 0, 20, 10, 100, 100, GL_RGBA, GL_UNSIGNED_BYTE, m_pSubImage); |
用于加载背景图像的代码是相似的,除了它使用此调用而不是glTexSubImage2D:
1 | glTexImage2D(m_textureTarget, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_pImage); |
您似乎没有正确将宽度和高度传递给
"逻辑"宽度与"存储"宽度之间的差异将在每条扫描线上留下一些填充像素,这些填充像素将被解释为下一条扫描线的最左侧像素,并在向下移动图像时累积。这会产生您观察到的倾斜效果。
您似乎并没有检查失败。
GL_INVALID_VALUE is generated ifxoffset < 0 ,xoffset + width > w ,yoffset < 0 ,yoffset + height > h , wherew is the width andh is the height of the texture image being modified.
GL_INVALID_VALUE is generated if width or height is less than 0.
GL_INVALID_OPERATION is generated if the texture array has not been defined by a previousglTexImage2D orglCopyTexImage2D operation whoseinternalformat matches the format ofglTexSubImage2D .