OpenGL - 2D Texture Mapping
Solution 1
have you tried calling
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, texture);
before
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Solution 2
Try using gDEBugger gDEBugger. It will let you see all the textures that have been loaded into OpenGL and provides other debugging tools as well.
Solution 3
Are all these in the same file?
static GLuint Texture;
static void LoadTextures()
{
Texture = LoadPNG("filename");
}
static void glRenderTest()
{
glRectF rect = {20, 20, 64, 64};
glDrawTexturedQuad(rect, Texture);
}
If they aren't in the same file, well static
variables will have independent copies in each compilation unit, individually initialized to zero when the program starts. Which would explain why you're binding texture #0 after the loader returned 1
.
TheMonster
Updated on June 04, 2022Comments
-
TheMonster almost 2 years
I'm trying to render a simple texture(64x64) to a 64x64 quad. The quad itself is rendering, but, not the texture. (It's rendering a blank white 64x64 quad.)
I'm using SOIL to load the image.
static GLuint LoadPNG(char* filename) { GLuint texture = SOIL_load_OGL_texture ( filename, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT ); if (texture == 0) Log("Texture Load Error: " + string(filename)); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); return texture; }
This is my rendering code, I may not be mapping it correctly, so that could be the issue too.
// Draw Textured Quad static void glDrawTexturedQuad(glRectF rect, GLuint tex) { // Bind Texture glBindTexture (GL_TEXTURE_2D, tex); // Render Settings glEnable(GL_TEXTURE_2D); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glColor3ub(255,255,255); glBegin(GL_QUADS); // Top Left glTexCoord2f(0, 1); glVertex2f(rect.X, rect.Y); // Top Right glTexCoord2f(1, 1); glVertex2f(rect.X + rect.Width, rect.Y); // Bottom Right glTexCoord2f(1, 0); glVertex2f(rect.X + rect.Width, rect.Y + rect.Height); // Bottom Left glTexCoord2f(0, 0); glVertex2f(rect.X, rect.Y + rect.Height); glEnd(); }
Here is the rest of the relevant code. (This is really just temp code, to glue it all together for testing, I'll come up with a better solution after I get it working.)
static GLuint Texture; static void LoadTextures() { Texture = LoadPNG("filename"); } static void glRenderTest() { glRectF rect = {20, 20, 64, 64}; glDrawTexturedQuad(rect, Texture); }
I've also followed all the suggestions found here It's still not displaying my texture.
I swapped out LodePNG for SOIL(Simple OpenGL Image Library), it's a bit easier to use, but still isn't working.
I added glTexEnv as suggested in the answer below, but I'm still just getting a white box, I'll try some more settings, but I don't think that was it. (Edit: Tried various flags, nothing, still just a white quad.)
-
TheMonster almost 13 yearsYeah, it's all in the same file, and the number is correct across all functions now. That was due to the game engine I'm hacking dumping the textures during startup(for whatever reason). (I am now using a keypress to trigger my code after the game is fully loaded.)
-
TheMonster almost 13 yearsWell, that probably helped, but it wasn't the issue. I tried various flags, all of them gave the same result, a blank white quad.
-
lb. about 12 yearsI had this issue and you helped me resolve it thanks. I was calling BindTexture close to glBegin(), I needed to move the glTextParameterf() calls AFTER glBindTexture. State machines :).
-
2013Asker almost 10 years+1 I was following a tutorial and they hadn't mentioned the call to
glEnable(GL_TEXTURE_2D);
. Thanks to your answer my problem is solved.