Having trouble with textures in HUD

For some odd reason I can’t get my texture to show up on my HUD. Instead I just get a solid color. Does anyone see what’s going wrong? Any configurations I could be missing? It works perfectly fine when I render the environment, but it’s just not working on the HUD.

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, gGameData.mHUD->mTexID);
glTexImage2D(GL_TEXTURE_2D, 0, 4, gGameData.mHUD->mW, gGameData.mHUD->mH,
0, GL_RGBA, GL_UNSIGNED_BYTE, gGameData.mHUD->mData);

glBegin( GL_QUADS );

glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0); glVertex2d(1.0,0.0);
glTexCoord2d(1.0,1.0); glVertex2d(1.0,1.0);
glTexCoord2d(0.0,1.0); glVertex2d(0.0,1.0);
glEnd();
glFlush();

change this line


glTexImage2D(GL_TEXTURE_2D, 0, 4, gGameData.mHUD->mW, gGameData.mHUD->mH,
0, GL_RGBA, GL_UNSIGNED_BYTE, gGameData.mHUD->mData);

to this


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gGameData.mHUD->mW, gGameData.mHUD->mH,
0, GL_RGBA, GL_UNSIGNED_BYTE, gGameData.mHUD->mData);

Also is your depth test enabled? if so disable depth test before rendering the HUD.

Still giving me trouble and yes depth test is enabled…

Also… I noticed that the first thing I call to be drawn is at the front rather than the back? Anything wrong with the following?

void initGame::setupGUI(int x, int y, int w, int h)

{

glViewport(x, y, w, h);

glMatrixMode(GL_PROJECTION);

glPushMatrix();

glLoadIdentity();

gluOrtho2D(-(float)w/(float)h, (float)w/(float)h, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW);

glPushMatrix();

glLoadIdentity();

initGame::enableGUI();

}

void initGame::takeDownGUI()

{

glMatrixMode(GL_PROJECTION);

glPopMatrix();

glMatrixMode(GL_MODELVIEW);

glPopMatrix();

}

void initGame::enableGUI()

{

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

}

In this code snippet, i dont see where u disable the depth test. It should be called before you are about to render the HUD. Could u post a minimal glut code so it would be better to know what u r trying to do?

Ooooops, sorry skimmed that last phrase… thought you said it had to be enabled.

Anyway, the texture is still not showing up, but now it draws in order. So where am I going wrong in terms of the texture?

Could u post the code where u create the texture. Have u called


glGenTextures(GL_TEXTURE_2D, &gGameData.mHUD->mTexID);

because from the code snippet u posted earlier i did not see this call.
And one more thing, have u restored the viewport again since the setupGUI function is setting it.

This is where I don’t have a full understanding of textures. I never made that call and as to how I went about getting the texture, I read in a BMP file and in the case of mTexID, its value is 0 as set by the second argument of my function loadMyTexBMP().

  gGameData.mHUD = loadMyTexBMP("./content/hud/imgBlueBrick.bmp", 0);

  glBindTexture(GL_TEXTURE_2D, gGameData.mHUD->mTexID);

  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);



  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gGameData.mHUD->mW, gGameData.mHUD->mH,

               0, GL_RGBA, GL_UNSIGNED_BYTE, gGameData.mHUD->mData);

Hmm thats where ur problem is. You have to ask opengl to generate a texture id for u by making a call to


GLuint texID;
glGenTextures(1, &texID);

Then u will pass this texID to glBindTexture and do the other stuff u showed earlier. So to make ur code to work do this.
Add a new global variable texID like this,

GLuint texID;

and then make ur calls as follows


glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texID);
gGameData.mHUD = loadMyTexBMP("./content/hud/imgBlueBrick.bmp", texID);

glBindTexture(GL_TEXTURE_2D, gGameData.mHUD->mTexID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gGameData.mHUD->mW, gGameData.mHUD->mH,0, GL_RGBA, GL_UNSIGNED_BYTE, gGameData.mHUD->mData); 

See if this helps.
Regards,
Mobeen

Still doesn’t work, I get a solid color still. Other than having texture disabled, what else would cause a texture not to not show up.

Can you post your most recent code?

Dale

It’s fragmented into a number of files.

So… could someone give me a check list that I can go through for the following…

>What things need to be set-up/enabled for textures to work in general
>What functions need to be called when reading in images
>What functions need to be called before drawing

Thanks

:slight_smile:

The following link should answer all your texturing questions:

http://glprogramming.com/red/chapter09.html

One more thing that often happens is that the image is not there. Could u double check that the image is loaded properly.

YAY!!! I think* I found the problem… does entering game mode cause everything to reset, including textures?

Yes since a new context is created when u leave the game mode, the textures are deleted. Check this thread http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=46094

If only I knew that earlier… so many hours lost bashing my head on a desk…

Anyway, one last question… after calling glTexImage2D() can I delete the allocated space I made to read in the BMP file?

yes after call to glTexImage2D, you can delete the image data ptr.