I am using OpenGL 2.1 in my academic work to create a 3D scene. I have a 24-bit BMP file, a texture for a cube side, that is loaded manually into the memory.
Straight to the problem: displayed texture has some excess pixels at the lower left corner. BMP file, of course, doesn't have these pixels, it has a full black border. Here's the result:
Seems like the data loaded from file is OK - pixels in a row after excess once are the same as in file, checked with another varicolored BMP. Here's the function I use to create texture from BMP:
GLuint CreateTextureFromBMP (const char path [])
{
unsigned char header [54];
unsigned int data_position, width, height, image_size;
unsigned char * data;
FILE * file = fopen (path, "rb");
fread (header, sizeof (char), 54, file);
data_position = *(int *)&(header [0x0A]);
image_size = *(int *)&(header [0x22]);
width = *(int *)&(header [0x12]);
height = *(int *)&(header [0x16]);
if (image_size == 0)
image_size = width * height * 3;
if (data_position == 0)
data_position = 54;
data = (unsigned char *) malloc (sizeof (unsigned char) * image_size);
fread (data, sizeof (char), image_size, file);
free (data);
fclose (file);
GLuint texture_id;
glGenTextures (1, &texture_id);
glBindTexture (GL_TEXTURE_2D, texture_id);
glTexImage2D (GL_TEXTURE_2D, 0, 3, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
return texture_id;
}
I tried using different parameters in glTexImage2D
and glTexParameteri
functions, but still it change nothing. Here's the render scene function:
void RenderScene (int arg)
{
static GLuint texture_id = CreateTextureFromBMP ("cube_blue.bmp");
glEnable (GL_TEXTURE_2D);
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glEnableClientState (GL_VERTEX_ARRAY);
glActiveTexture (GL_TEXTURE0);
GLfloat vertices [] = { ... };
GLfloat texcoords [] = { ... };
GLubyte indices [] = { ... };
glVertexPointer (3, GL_FLOAT, 0, vertices);
glTexCoordPointer (2, GL_FLOAT, 0, texcoords);
glPushMatrix ();
glBindTexture (GL_TEXTURE_2D, texture_id);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glColor3f (1.0f, 1.0f, 1.0f);
glDrawElements (GL_QUADS, 24, GL_UNSIGNED_BYTE, indices);
glPopMatrix ();
glDisable (GL_TEXTURE_2D);
glDisableClientState (GL_TEXTURE_COORD_ARRAY);
glDisableClientState (GL_VERTEX_ARRAY);
}
Any ideas where to look for the mistake?