0

I'm using LWJGL for its OpenGL bindings and I'm trying to draw a quad with a texture. I have done so successfully, but there is a strange side-effect and I'm unsure why it's presenting itself.

The following code executes when first loading the texture (before the first render). I save the value of textureID for when I bind the texture/draw the quad. The following set-up code is taken from an SO answer found here.

int textureID = GL11.glGenTextures(); // Generate texture ID

GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID); // Bind texture ID

// Setup wrap mode
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);

// Setup texture scaling filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);

// Send texel data to OpenGL
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

In the rendering loop, I draw the quad with the following code:

int texture = TextureLoader.loadTexture(TextureLoader.loadImage("/pc_run/run3.png"));

GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);

GL11.glBegin(GL11.GL_QUADS);
{
    GL11.glTexCoord2f(0.0f, 0.0f);
    GL11.glVertex2f(-hw, -hh);

    GL11.glTexCoord2f(1.0f, 0.0f);
    GL11.glVertex2f(+hw, -hh);

    GL11.glTexCoord2f(1.0f, 1.0f);
    GL11.glVertex2f(+hw, +hh);

    GL11.glTexCoord2f(0.0f, 1.0f);
    GL11.glVertex2f(-hw, +hh);
}
GL11.glEnd();

So far, this works (and is also wrapped between glPushMatrix()/glPopMatrix() calls). However, whatever I have drawn to the GLContext so far is not displayed. Removing the call to GL11.glTexImage2D in the initialization code, the sprite is not displayed, but everything else (non-textured) is displayed correctly.

Community
  • 1
  • 1
efritz
  • 5,125
  • 4
  • 24
  • 33
  • Why are you loading the texture anew for each frame? This will eat up your memory in no time. textures are not garbage collected. Make the generated texture ID a class member variable and load the texture only once! – datenwolf Aug 05 '12 at 09:26
  • I'm only creating the texture when I initialize GL. – efritz Aug 05 '12 at 20:46
  • I was just asking, because you have the the call of TextureLoader.loadTexture in the code you described as rendering code. – datenwolf Aug 05 '12 at 21:32

1 Answers1

2

You need to disable texturing (glDisable(GL_TEXTURE_2D)) for objects that are not supposed to be textured.

Tim
  • 35,413
  • 11
  • 95
  • 121