Texture mapping not working?

I’m just getting started learning texture mapping and I can’t for the life of me figure out why my texture isn’t showing up on screen. Here are the calls I make after loading the bitmap into sprite1 (char[]):

	glBindTexture(GL_TEXTURE_2D, 1);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, sprite1[18], sprite1[22], 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, &sprite1[54]);

	glBindTexture(GL_TEXTURE_2D, 1);
	glBegin(GL_QUADS);
		glTexCoord2f (0.0, 0.0);
		glVertex2f (0.0, 0.0);
		glTexCoord2f (1.0, 0.0);
		glVertex2f (1.0, 0.0);
		glTexCoord2f (1.0, 1.0);
		glVertex2f (1.0, 1.0);
		glTexCoord2f (0.0, 1.0);
		glVertex2f (0.0, 1.0);
	glEnd();

I have done glEnable(GL_TEXTURE_2D).

This is how I set up my matrices before drawing:

	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1.0, 1.0, -1, 1, .1, 10);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

I think you gave the wrong internal format for glTexImage2D. Try with GL_RGBA8.

it could be any of a handful of problems.

#1 check if the polygon is being drawn, if it is it should be either white (depending a bit on the color you set) or textured.

#2 glBindTexture(GL_TEXTURE_2D, 1);.
While it might work sometimes, it is an undefined behavior according to the api, you need to generate a texturename to make sure it works.

#3 is your color set to white

#4 make sure your coordinates are within the z-near/z-far range, in your case i would set the z coordinate to 5.0f using glVertex3f instead of glVertex2f.

I hope this points you in the right direction

Thanks for the help, but I tried all of your advice and still no dice. The polygon is being drawn (you were right, I was drawing it white before), but now it’s not textured, just a solid color.

I use glGenTextures to get my tex name, but I notice it doesn’t change texName1; this GLuint is whatever I initialize it to, even after the call to glGenTextures…

Heres the new code:

	GLuint texName1 = 0;

	glGenTextures(1, &texName1);
	glBindTexture(GL_TEXTURE_2D, texName1);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, sprite1[18], sprite1[22], 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, &sprite1[54]);

        glColor3f(1, 1, 0);
	glBindTexture(GL_TEXTURE_2D, texName1);
	glBegin(GL_QUADS);
		glTexCoord2f (0.0, 0.0);
		glVertex3f (0.0, 0.0, -5.0f);
		glTexCoord2f (1.0, 0.0);
		glVertex3f (.5, 0.0, -5.0f);
		glTexCoord2f (1.0, 1.0);
		glVertex3f (.5, .5, -5.0f);
		glTexCoord2f (0.0, 1.0);
		glVertex3f (0.0, .5, -5.0f);
	glEnd();

Also, @trinitrotoluene, I have had success using GL_BGRA_EXT with glDrawPixels (same bitmap too), so I’m confident this is correct.

I have tried GL_BGRA_EXT as internal format for glTexImage2D and it doesn’t work with Mesa 7.4 software renderer. I have tried GL_BGRA_EXT for glDrawPixels and it work. So GL_BGRA_EXT don’t seem to work for internal format. Try this


glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8/*GL_BGRA_EXT*/,2,2,0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,&texData[0]);

Also, @trinitrotoluene, I have had success using GL_BGRA_EXT with glDrawPixels (same bitmap too), so I’m confident this is correct.

You are too much confident! :slight_smile:

Okay, maybe I’m too confident, but GL_RGBA8 still doesn’t work.

There must be something wrong with my glGenTextures call since it’s not assigning me a texture name.

Also, my texture bitmap is 29x20 pixels. The width and height are stored in the bitmap at sprite1[18] and sprite1[22] respectively (see Wikipedia entry for bitmap file format)

glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, sprite1[18], sprite1[22], 0, GL_BGRA, GL_UNSIGNED_BYTE, &sprite1[54]);

Try with procedurally-generated bitmaps first:


int size=32;
int *bitmap=new int[size*size];
for(int i=0;i<size*size;i++)bitmap[i]=0xCCBBAAFF;

I actually get GL_RGBA8:GL_BGRA:GL_UNSIGNED_BYTE to work fine for me (makes the driver shuffle bytes before upload).

Are you calling these functions in your drawing function ?


glGenTextures(1, &texName1);
	glBindTexture(GL_TEXTURE_2D, texName1);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, sprite1[18], sprite1[22], 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, &sprite1[54]);


If yes, move these in an “init” function to make sure you don’t call genTextures and TexImage2D multiple times for the same texture.

Try changing the bitmap to something like 32x32 or 64x64 (even if it is just to test) - some gfx cards prefer power of 2 textures, and while some will render anyway, I’m not sure all will.

I’m at a loss. Here’s everything I’ve tried:

  1. Turns out I was initializing my texture before OGL was initialized. The texture is initialized (glGenTextures->glTexImage2D) in a class constructor and drawn (glBegin->glEnd) in a member function that is called every frame. genTextures appears to be working correctly now and I’m getting a name of 1.

  2. Every possible combination of GL_RGBA8, GL_BGRA_EXT (GL_BGRA doesn’t work on my system; I need the _EXT), and I even removed the alpha channel from the bitmap and tried all combinations of GL_RGB, GL_BGR_EXT, etc etc. No luck.

  3. Tried procedurally creating a bitmap and using that

  4. Made sure GL_COLOR_MATERIAL isn’t enabled.

  5. Changed bitmap size to 32x32.

  6. Tried glTexEnvi instead of glTexEnvf.