Help!!! Single Color Transparency PNG

Hi! :smiley:

I’ve got several PNG images, lets say for example one of them has a ball in the center (of blue color) and the rest of the PNG is light-purple colored. How would I go about telling JOGL that I only want the blue ball rendered onto the quad / triangle strip and not the light-purple.

I’ve tried glBlendFunc() and enabling it, And I tried just about every combination of values (ex: GL_SRC_ALPHA, GL_SRC_COLOR, GL_ONE, etc) Doing that I get results like no blending, or a texture which is semi transparent.

I’m NOT looking for a semi transparent texture. I want a PNG texture which has a color for transparency.

Do I have to use multi-texturing or a mask (are they the same?). Thanks for your help (in advance of course :D).

ah… the eternal question, transparency in jogl…

I’ve posted endless questions on this and after numerous (and numerous more) tries finally implemented it correctly. It’s easy in theory but often requires alot of cover-up functions. Basically, you do this:

(pseudocode)
BufferedImage img = readPNG(filename); //filename is a String, get the readPNG method off NeHe if you don’t already have it, same with some other methods I call here
if(img == null) complain

turn image transparent however you want here:
the easiest way is to make a new BufferedImage of type 4BYTE_ABGR and draw in your old image pixel by pixel, with getRGB() and setRGB() methods. If getRGB() returns an int with the same number as the RGB value as the color you want transparent, setRGB() in the new image with 0x00000000 (fully transparent black, in case for some reason you don’t know RGB encoding numbers) instead of the same color as the old image.
The new image is now the one you’re using.
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D, some number which will refer to this texture, in int form);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAX_FILTER, GL.GL_NEAREST);

ByteBuffer dest = a ByteBuffer filled with data from your image, if you don’t know how to get this check one of my posts in this forum or ask me to paste the code that finally worked, sry im not pasting it right now bc its on a different computer
if(you want it mipmapped)
glu.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, GL.GL_RGBA, width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, dest);
else
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, dest);

then disable blending and textures until you need to use them; when you do:
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
//this is the line you were wondering about, this is what you want bc src alpha is the color of your image, which you want to depend on alpha value, and destination color is the color already in the frame buffer.
gl.glEnable(GL.GL_BLEND);
//i think you need those lines in that order
gl.glBindTexture(GL.GL_TEXTURE_2D, the id number you gave that texture);

whew, and thats only the very basics… I suggest you make some sort of TextureHandler class as I did to hide all that gunk

hope this helped, if it didnt i have actual (non-pseudo) code if you want it

Any chance you would still be interested in posting that code?

ok, what method do you want to see?

here is a shortened version of the one that turns one color in a png transparent:

public BufferedImage makeTransparent(BufferedImage image, Color col){
int width = image.getWidth();
int height = image.getHeight();
Color temp = null;
BufferedImage newImage = new BufferedImage(width,
height, BufferedImage.TYPE_4BYTE_ABGR);
int color;
if(col != null) color = col.getRGB();
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
int k = image.getRGB(i, j);
if(col != null){
if(k == color){
newImage.setRGB(i, j, 0x00000000);
}
else{
newImage.setRGB(i, j, k);
}
}
else newImage.setRGB(i, j, k);
}
}
return newImage;
}