Scott Jordan
Scott Jordan

Reputation: 122

Transparency in texture not working in libgdx

I'm trying to draw a sprite in libgdx that has a transparent background to it, but the transparent background is filled in with white instead of showing what has already been rendered at that location.

transparency doesn't show up

The sprite (with the hat) is 64 by 64 with transparency around the edge and on the right. There should be two tiles with a 'C!' behind him but it's just filled in with white.

This is the code I am using to render the image.

public void draw(SpriteBatch sb, float parentAlpha){
    sb.enableBlending();
    sb.draw(texture, getX() + 32, getY());
}

Upvotes: 2

Views: 9367

Answers (4)

marcosleal
marcosleal

Reputation: 111

All the sprites are supposed to be blended? Maybe disabling the blend after you already drawn could solve your problem.

Maybe something like that:

public void draw(SpriteBatch sb, float parentAlpha){
   sb.enableBlending();
   sb.draw(texture, getX() + 32, getY());
   sb.disableBlending();
}

If this doesn't work you could try activating the blend in the GL options with something like that:

    Gdx.gl.glEnable(GL10.GL_BLEND);
    Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
            ...
            Your code Here
            ...
    Gdx.gl.glDisable(GL10.GL_BLEND);

Hope it helps :)

Upvotes: 3

Scott Jordan
Scott Jordan

Reputation: 122

I managed to fix the problem by changing my application to OpenGL 2.0 instead of 1.0. Still not sure what the problem was.

Upvotes: 2

P.T.
P.T.

Reputation: 25177

First, make sure your PNG has transparency information in it.

Second, make sure you're drawing things in the right order (you have to draw the background boxes before the sprite).

Upvotes: 2

Nicol Bolas
Nicol Bolas

Reputation: 474316

If you're going to enable blending, you also need to set the blend function with setBlendFunction. This defines exactly how you want the blending to work. Presumably, you want the classic GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA blending.

Upvotes: 7

Related Questions