Search code examples
javaopengllwjgl

How do I get rid of the opaque quad behind my texture? (lwjgl)


For this project, I am using LWJGL with Slick-Utils.

I am trying to render a small texture of a laser, with a bright center and fading transparent red around the edges. When I render it against my black background, it has a solid/opaque white quad behind it. I know that the quad is there because I'm using GL_QUADS to render the texture, but no matter where I look I can't seem to find out how to make the quad not render and just have the laser show up.

Here is the code I'm using to render the quad and texture:

public static void drawImage(Texture texture, float x, float y, float width, 
float height) {
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    texture.bind();
    glTranslatef(x + width / 2, y + height / 2, 0);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(-width / 2, -height / 2);
    glTexCoord2f(texture.getWidth(), 0);
    glVertex2f(width / 2, -height / 2);
    glTexCoord2f(texture.getWidth(), texture.getHeight());
    glVertex2f(width / 2, height / 2);
    glTexCoord2f(0, texture.getHeight());
    glVertex2f(-width / 2, height / 2);
    glEnd();
    glLoadIdentity();
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
}

I know the background quad is white because I have bound the color white by default, since I have found that binding any other color messes up the colors on my other textures.

EDIT: Thanks for the responses. ybungalobill got me on the right track. I ended up saving my texture as a 32 bit uncompressed TGA image and it now renders properly with the suggested change.


Solution

  • The default blending equation is GL_ONE, GL_ZERO. You have to set it properly. Judging by that you see a white quad 'behind', I suspect that you load a non-premultiplied alpha PNG and render it with the default blending equation. Instead do as follows to get correct additive blending (you need additive because lasers are light):

    glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
    //glBlendFunc(GL_SRC_ALPHA, GL_ONE); // if you don't care about destination alpha channel