Why isn't texture showing? OpenGL

I got a function that reads the bitmap data and then I want to use the bitmap to texture a rectangle, but I can't get the texture to show.

Anyone know what wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();

			glColor3f(0.0, 0.0, 0.0);

			GLuint texture = LoadBMP("water.bmp");
			glEnable(GL_TEXTURE_2D);

			glBindTexture(GL_TEXTURE_2D, texture);
			glBegin(GL_QUADS);
				glTexCoord2i(0, 0); glVertex2i(0, 0);
				glTexCoord2i(0, 1); glVertex2i(0, 64);
				glTexCoord2i(1, 1); glVertex2i(64, 64);
				glTexCoord2i(1, 0); glVertex2i(64, 0);
			glEnd();

			glDisable(GL_TEXTURE_2D);


Loads bitmap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
Two things I notice:

1) OpenGL textures must be a power of 2 size

2) You probably should be enabling 2D textures before attempting to create/load one, but I don't know if that will matter.
I'm loading a bitmap which is 64x64, so that power of 2?

And tried enabling 2D textures before and still just displaying a black rectangle
according to my experience´╝îafter you set the texture parameter ,you should bind it again.
Since you define LoadBMP as GLuint LoadBMP(const char *fileName), it should return texture; upon completion.

I looked at a few examples and glTexParameteri() calls are made before the call to glTexImage2D(), which is usually made last. I don't know if that could be an issue. Also, I see you do not make any call to glTexEnvf(). Try making the calls in this order:

1
2
3
4
5
6
7
8
//LoadBMP:
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;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Main code:

			GLuint texture = LoadBMP("water.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, 64);
				glTexCoord2i(1, 1); glVertex2i(64, 64);
				glTexCoord2i(1, 0); glVertex2i(64, 0);
			glEnd();

			glDisable(GL_TEXTURE_2D);
Last edited on
Thanks Ogoyant, it loads now :)

But there is one more issue, it not really a problem just a bit annoying. Because how Bitmaps work it loads the image a bit mirror, I changed the colour from RGB to BGR but is there a way to make the image load so it doesn't have it mirror?
Bitmaps are [usually] stored upside-down (bottom row first). OpenGL expects them to be top row first.

Only way to fix is to flip the rows upon loading.

I just went through all this with someone else recently. Here's the thread:
http://cplusplus.com/forum/windows/78897/
Topic archived. No new replies allowed.