I'm using the following code to create and display a texture from an IplImage. It works about half the time but sometimes skews the image, I assume it has to do with the texture padding but I need help with a fix.
int loadTexture_Ipl(IplImage *image, GLuint *text) {
if (image==NULL) return -1;
glGenTextures(1, text);
glBindTexture( GL_TEXTURE_2D, *text );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->width, image->height,0, GL_BGR, GL_UNSIGNED_BYTE, image->imageData);
return 0;
}
This link is a screenshot of the output incase anyone has delt with a similar problem.
Your issue here is the fact that one row of your IplImage
in memory might be wider than the actual used image (this is due to performance reasons). You're on the right track using glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- just with the wrong value.
As a simple example, imagine an image being 3 pixels wide. So one row (RGB) essentially consists of 9 bytes (byte alignment; no spacing between rows), 10 bytes (even byte alignment; 1 byte space between rows), 12 bytes (word alignment; 3 bytes space between rows) or 16 bytes (double word alignment; 7 bytes space between rows).
Can't look it up right now, but I think the rows of an IplImage are aligned at word boundaries by default. Due to you using RGB (with 3 bytes per pixel; not 4 bytes) this is an issue. Just try different values, I think you'll need word alignment (glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
).