efritz
efritz

Reputation: 5203

Calling glTexImage2D clears data buffer

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.

Upvotes: 0

Views: 874

Answers (1)

Tim
Tim

Reputation: 35943

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

Upvotes: 2

Related Questions