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
January 19, 2011, 10:48pm
2
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);
}
mobeen
January 22, 2011, 8:34pm
4
MrUNOwen:
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?
mobeen
January 22, 2011, 11:54pm
6
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);
mobeen
January 23, 2011, 1:01am
8
MrUNOwen:
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.
dalemac
January 23, 2011, 1:36pm
10
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
dalemac
January 23, 2011, 5:15pm
12
The following link should answer all your texturing questions:
http://glprogramming.com/red/chapter09.html
mobeen
January 23, 2011, 7:49pm
13
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?
mobeen
January 23, 2011, 9:04pm
15
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?
mobeen
January 23, 2011, 11:18pm
17
yes after call to glTexImage2D, you can delete the image data ptr.