Login  Register

Re: Transparent Texture?

Posted by Demoscene Passivist on Aug 03, 2010; 9:15pm
URL: https://forum.jogamp.org/Transparent-Texture-tp1020455p1020524.html

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);
 }