6

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.)

Community
  • 1
  • 1
TheMonster
  • 83
  • 1
  • 1
  • 6
  • hm.. I have cross-checked it with the according nehe tutorial (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06) but on the OpenGL calls I could not find anything awkward. Are you sure, the Image-loading function works correctly? – Constantinius May 14 '11 at 13:00
  • To verify for Constantinius, auto-generating a checkerboard tends to be the easiest thing. E.g. for(every x){ for(every y) { colour = ((x^y)&8) ? black : white; } } – Tommy May 14 '11 at 13:02
  • Well, my error handling looks correct, and it wouldn't render a checkerboard either, but, I may just have done it wrong too, I basically copy pasted it to get it done quickly. // http://www.csc.villanova.edu/~mdamian/graphics/notes/GLTextures/Checkerboard.htm – TheMonster May 14 '11 at 13:19
  • Could my quad be backwards, I'm hacking a game, so I don't know what kind of world system it's using, perhaps flipping the quad over? – TheMonster May 14 '11 at 13:44
  • If you say, that you can see your quad, but only without the texture, that would not change a thing I guess. Btw what you mean is back-face culling, which you can turn off, by typing `glDisable(GL_CULL_FACE);`. – Constantinius May 14 '11 at 13:48
  • I'll try that glDisable command, and what I meant, is, was it possible, that the quad was rendering with the backface, facing me, and the texture is on the side I can't see, and getting culled. I'm fairly certain that is possible. (Edit: glDisable changed nothing.) – TheMonster May 14 '11 at 13:50
  • I just tried a different PNG image, it wouldn't work either, I may try some different texture flags, etc,. So, unless it's a flag, or rendering wrong as I suggested in my last post, I'm stumped. – TheMonster May 14 '11 at 14:05
  • Is the quad just black? That would mean you have lighting on and do not set correct lighting conditions. Then you get black color multiplied by texture color, which is just balck. Try glDisable(GL_LIGHTING); or use glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); to just take the texture's color (which is probably what you want). – Christian Rau May 14 '11 at 14:17
  • No it's rendering white, the texture was just a smiley image I found on Google, I was basically just testing my rendering code. The actual goal is to create new GUI elements. (What kind of value does GL give to textures, I checked the value of the one I'm passing, and it was 0, does it use ID's like that? Or is that a sign it's going wrong?) – TheMonster May 14 '11 at 14:22
  • 0 is definitely wrong, it's a reserved ID, which means invalid texture. So I think there's something wrong with the glGenTextures (although I don't know what). – Christian Rau May 14 '11 at 14:41
  • I think we are on to something, I changed the code to use GLuint Texture[1], and now it stores a value of 1, but, by the time it reaches my quad rendering code, it's 0 again, so I'm pretty close to making it work I think, just need to get that value passed correctly. – TheMonster May 14 '11 at 14:53
  • But the way you use texture in your code, the GLuint texture was correct. – Christian Rau May 14 '11 at 14:59
  • Hmm, well, I was going by the link to the NeHe tutorial posted earlier. ? I switched it back, and checked my values, it is 1 after the image loading code, it is 1 after I store it to my static var, but 0 after passing it to my quad code. ??? – TheMonster May 14 '11 at 15:01
  • The behavior described above, is using the same code in my original post. I was checking the value in my quad code, that's where I was seeing that 0 I reported initially. (Btw, I tried forcing a 1 into the glBindTexture call right before I render, but it didn't work.) - I just checked the image dimensions, and they are correct after the load, so it has to be loading. – TheMonster May 14 '11 at 15:09
  • I'm hacking a game, and adding new features, could the game itself be dumping the texture from OpenGL? I may try adding my code to a key press, and seeing if it works after the game get's fully initialized, etc,. |Edit: The game was dumping my ID, but loading with a keypress still isn't working, though, I'm getting an ID of 28 across the board now, so I'm closer. :) – TheMonster May 14 '11 at 15:16

4 Answers4

7

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);
Pete
  • 4,784
  • 26
  • 33
  • I 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 :). – lb. Mar 06 '12 at 02:20
  • +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. – 2013Asker May 13 '14 at 04:56
1

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Yeah, 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 May 14 '11 at 19:11
1

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.

fintelia
  • 1,201
  • 6
  • 17
0

I don't see your setting a texture function. Try adding this after glBindTexture in the geometry drawing function:

glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); // texture function: GL_DECAL, GL_MODULATE, etc.

Try the different texture function to fit your need: GL_DECAL, GL_BLEND, GL_MODULATE, etc.

  • Well, that probably helped, but it wasn't the issue. I tried various flags, all of them gave the same result, a blank white quad. – TheMonster May 14 '11 at 19:12