PDA

View Full Version : Having trouble with textures in HUD



MrUNOwen
01-19-2011, 09:57 PM
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();

mobeen
01-19-2011, 10:48 PM
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.

MrUNOwen
01-22-2011, 01:01 PM
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);

}

mobeen
01-22-2011, 08:34 PM
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?

MrUNOwen
01-22-2011, 11:17 PM
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?

mobeen
01-22-2011, 11:54 PM
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.

MrUNOwen
01-23-2011, 12:05 AM
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);

mobeen
01-23-2011, 01:01 AM
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

MrUNOwen
01-23-2011, 07:36 AM
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.

dalemac
01-23-2011, 01:36 PM
Can you post your most recent code?

Dale

MrUNOwen
01-23-2011, 04:17 PM
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

^_^

dalemac
01-23-2011, 05:15 PM
The following link should answer all your texturing questions:

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

mobeen
01-23-2011, 07:49 PM
One more thing that often happens is that the image is not there. Could u double check that the image is loaded properly.

MrUNOwen
01-23-2011, 08:40 PM
YAY!!!! I think* I found the problem.... does entering game mode cause everything to reset, including textures?

mobeen
01-23-2011, 09:04 PM
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

MrUNOwen
01-23-2011, 11:14 PM
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?

mobeen
01-23-2011, 11:18 PM
yes after call to glTexImage2D, you can delete the image data ptr.