I've used such code to load a .bmp file as a texture and I want to fill a rectangle(for example the one on the right wall with it)
GLuint LoadBMP(const char *fileName)
{
FILE *file;
unsigned char header[54];
unsigned int dataPos;
unsigned int size;
unsigned int width, height;
unsigned char *data;
file = fopen(fileName, "rb");
if (file == NULL)
{
//MessageBox(NULL, L"Error: Invaild file path!", L"Error", MB_OK);
return false;
}
if (fread(header, 1, 54, file) != 54)
{
//MessageBox(NULL, L"Error: Invaild file!", L"Error", MB_OK);
return false;
}
if (header[0] != 'B' || header[1] != 'M')
{
//MessageBox(NULL, L"Error: Invaild file!", L"Error", MB_OK);
return false;
}
dataPos = *(int*)&(header[0x0A]);
size = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if (size == NULL)
size = width * height * 3;
if (dataPos == NULL)
dataPos = 54;
data = new unsigned char[size];
fread(data, 1, size, file);
fclose(file);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
return texture;
}
and use it like this:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(0.0, 0.0, 0.0);
GLuint texture = LoadBMP("mina.bmp");
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(0, 0);
glTexCoord2i(0, 1); glVertex2i(0, 5);
glTexCoord2i(1, 1); glVertex2i(5, 5);
glTexCoord2i(1, 0); glVertex2i(5, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
but when I run it, it does nothing and when I comment out these 2 lines:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
the ouput is a black rectangle not a textured rectangle.
I don't know what is wrong!
Is it about the .bmp file that I use?
I changed format of a jpeg with microsoft paint to .bmp file. I even tried with a .bmp file created by visual studio.
here is the second output I said: