glTexSubImage2D and buffer width


Recently I ran into a weird problem when I was attempting to use GL to generate a texture atlas of font characters using freetype2. The character widths would be odd numbers like 21 pixels, for example. I then created a temporary buffer to copy the character into while at the same time vertically flipping it and then used glTexSubImage2D to copy the temporary image buffer onto the main texture at the right position.

I began seeing skewing, like in this image:

At first I thought it was perhaps a freetype bug as even if I passed the freetype buffer through directly, it would be skewed in the same way (albeit upside-down). I noticed that the width of the character factored into whether or not it was skewed… a character which freetype rendered into an image buffer with a width that was a multiple of 4 was unskewed. Any other value produced skewing.

Eventually with help from some friendly users at I found that by padding the row width of my temporary image buffer up to the nearest multiple of 4, the skewing went away. In the case of the 21-pixel-wide character glyph I’d create a temporary image 24 pixels wide, then map the freetype glyph into that buffer. I resolved the issue more completely by creating my temporary image as RGBA instead (as previously I was working with 1-byte pixels).

So my question is, why? Does this have to do with the glPixelStore method? Or is it something more inherent to GL?

There’s no issue to resolve now, I have code which does what I want. I just like to try and understand the why. :smiley:


I assume you’ve done:

glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 ) ;
glPixelStorei ( GL_PACK_ALIGNMENT , 1 ) ;

It’s almost always what you want.

If it’s not that, it sounds like the widths of the freetype characters vary, but you’re assuming their all fixed-width.

Nope! That’s probably it then. That’s something I haven’t really looked into yet, so I guess that’s next! I certainly was assuming nothing about the widths of the freetype characters… I knew they’d be variable.

So I was on the right track, it’s in the pixel store method somehow. I just need to read up and understand how they affects GL exactly.


A good reference for unpack/pack alignment is section “7. Watch Your Pixel Store Alignment” of “Avoiding 16 Common OpenGL Pitfalls”