header image
[ # ] glTexImage2D doesn’t load sequential RGB data correctly
January 7th, 2011 under OpenGL

I wanted to render the image I had in memory with OpenGL. The image data are stored as

1
unsigned byte
unsigned byte
data without padding. The colors are represented by RGB (in this order). When I loaded the texture data with

1
2
3
4
5
6
7
8
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
             width, height, 0, 
             GL_RGB,  GL_UNSIGNED_BYTE, 
             imageData);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
             width, height, 0, 
             GL_RGB,  GL_UNSIGNED_BYTE, 
             imageData);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

the rendered image showed only a black and with image which was line-wise distorted. The reason for the distortion was that by image data was not correctly aligned. The

1
GL_UNPACK_ALIGNMENT
GL_UNPACK_ALIGNMENT
defines the alignment when uploading data. So, setting
1
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
solved my problem.