Reputation: 768
I'm trying to get a texture to be rendered on top of another one, like in the image below:
However, only that image gets rendered properly. My other images get garbled and "twisted". If you look carefully, it's as if the rows were shifted:
In the above example, I used the very same cat picture in the background. Both this cat picture, and all other images I generate end up garbled, except that one special picture, for some reason. I have looked at EXIF data, and other than the fact that it doesn't use sRGB, it is in the exact same format as the others. It has an alpha channel and everything.
I believe it has something to do with pixel alignment, given how the rows are shifted, but I have tried literally every possible combination of alignment and nothing as worked so far. Here is my code:
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);
The code for loading the background image is similar, except that it uses this call instead of glTexSubImage2D:
glTexImage2D(m_textureTarget, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_pImage);
Upvotes: 0
Views: 995
Reputation: 283614
You don't appear to be checking for failures. The following failure modes of glTexSubImage2D
are especially relevant here:
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
.
Upvotes: 0
Reputation: 283614
It appears that you aren't passing the width and height correctly to glTexSubImage2D
. Note that you need the number of pixels stored per scanline, which is often not exactly the "logical" width of the image, but rounded up to a multiple of 4.
The difference between the "logical" and "storage" width will leave a few padding pixels left over on each scan line, which will be interpreted as the leftmost pixels of the next scanline, and accumulate as you move down the image. That creates the slant effect you observe.
Upvotes: 1