Transparent Texture?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Transparent Texture?

Lathanda
atm I use jogl2 for 2D graphics.
I try to create moving sprit. As long as the sprit does not contain transparency everything works fine.
But atm i use transparent images the transparent parts are rendered in black.
I use the sequence
new Texture(new AWTTextureData(
                        GLProfile.get(GLProfile.GL2),
                        GL.GL_RGBA,
                        GL.GL_RGBA,
                        false,
                        img)
                );
which is stored for further use. img is a Bufferedimage containing a png image with transparency.
I inspected img, and the data looks fine.

For drawing i use... ( t is the previous created Texture.)
                t.enable();
                t.bind();
                gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_REPLACE);
                gl.glBegin(GL2.GL_QUADS);
                        gl.glTexCoord2d(0.0,0.0); gl.glVertex2d(x, y);
                        gl.glTexCoord2d(1.0,0.0); gl.glVertex2d(x+width, y);
                        gl.glTexCoord2d(1.0,1.0); gl.glVertex2d(x+width, y+height);
                        gl.glTexCoord2d(0.0,1.0); gl.glVertex2d(x, y+height);
                gl.glEnd();
                t.disable();

I also tried using a TextureRenderer, but the effect is the same i don't get transparency to work.

I believe I miss something really trivial, but I have no idea what it could be.
Reply | Threaded
Open this post in threaded view
|

Re: Transparent Texture?

Demoscene Passivist
Administrator
First of all u have to be real sure that ur BufferedImage really contains/preserved alpha channel information. Often this is not the case when u use the wrong image provider. If u are sure that u got the correct alpha information u can easily use the TextureRenderer to draw ur 2D sprite like this:

setup:
mTextureRenderer = new TextureRenderer(mBobImage.getWidth(), mBobImage.getHeight(), true);
Graphics2D tTextureGraphics2D = mTextureRenderer.createGraphics();
tTextureGraphics2D.drawImage(mBobImage,0,0,null);
tTextureGraphics2D.dispose();
mTextureRenderer.markDirty(0, 0, mBobImage.getWidth(), mBobImage.getHeight());

draw the sprite:
mTextureRenderer.beginOrthoRendering(tScreenWidth,tScreenHeight);
mTextureRenderer.drawOrthoRect(tXPos, tYPos, 0, 0, mTextureRenderer.getWidth(), mTextureRenderer.getHeight());
mTextureRenderer.endOrthoRendering();

If u wanna do it more "manually" using the buildin JOGL Texture class try this snippet:

private void renderBob(GL inGL, Texture inTexture, float inWidth, float inHeight) {
        TextureCoords tc = inTexture.getImageTexCoords();
        float tx1 = tc.left();
        float ty1 = tc.top();
        float tx2 = tc.right();
        float ty2 = tc.bottom();
        inGL.glEnable(GL_BLEND);
        inGL.glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
        inTexture.enable();
        inTexture.bind();
        inGL.glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        inGL.glBegin(GL_QUADS);
                inGL.glTexCoord2f(tx1, ty1); inGL.glVertex3f(-inWidth,  inHeight, 0);
                inGL.glTexCoord2f(tx2, ty1); inGL.glVertex3f( inWidth,  inHeight, 0);
                inGL.glTexCoord2f(tx2, ty2); inGL.glVertex3f( inWidth, -inHeight, 0);
                inGL.glTexCoord2f(tx1, ty2); inGL.glVertex3f(-inWidth, -inHeight, 0);
        inGL.glEnd();
        inTexture.disable();
        inGL.glDisable(GL_BLEND);
 }
Reply | Threaded
Open this post in threaded view
|

Re: Transparent Texture?

Lathanda
I used the more manuall way, the only way you learn how things work :)

I change my code to
  gl.glEnable(GL.GL_BLEND);
  gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);
  t.enable();
  t.bind();
  gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_REPLACE);
  gl.glBegin(GL2.GL_QUADS);
    gl.glTexCoord2d(0.0,0.0); gl.glVertex2d(x, y);
    gl.glTexCoord2d(1.0,0.0); gl.glVertex2d(x+width, y);
    gl.glTexCoord2d(1.0,1.0); gl.glVertex2d(x+width, y+height);
    gl.glTexCoord2d(0.0,1.0); gl.glVertex2d(x, y+height);
  gl.glEnd();
  t.disable();
  gl.glDisable(GL.GL_BLEND);
And now it's working as intended.
Thank you for your hint.