Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparency when using ‘drawPixel’ on Pixmap?

Tags:

java

libgdx

Background

I am creating an application where I manually lay out a texture atlas. The whole point is that the image itself isn’t supposed to be transparent—it has a consistent background color that is then supposed to be removed in the code.

The problem I’m having is that I can’t seem to be able to remove pixels—make them transparent—from the Pixmap I’m working on. I’ve made sure to use RGBA_8888 for my Pixmap’s Format, which is sure to support transparency.

Problem

Here’s the piece of code I’m having trouble with:

pixmap.drawPixel(x, y, 0x00000000);

Quite obviously, 0x00000000 would be zero—even if converted from hex to base ten—seriously, it’s the same number!

From my observations, I’ve noticed that the drawPixel method draws a pixel onto the current one. What I want is for it to become transparent. Using the aforementioned piece of code, it draws transparency onto something that isn’t transparent—ending up changing nothing. As if you add nothing to something, the something remains.

Research

As usual, I’ve done some research on my own. Here on Stackoverflow I haven’t managed to find anything of use. The only things I know is the things mentioned above that I’ve managed to find out by observing the Pixmap and its drawPixel method.

Moreover

Perhaps someone can point me in the right direction as to how you’d make a pixel of a Pixmap transparent?

like image 670
D. Ataro Avatar asked Jan 20 '17 03:01

D. Ataro


1 Answers

As you already discovered, you can't draw a blended transparent pixel over the existing pixel, because you can't see anything. So first you need to disable Pixmap blending.

Pixmap.setBlending(Pixmap.Blending.None); // before you start drawing pixels.

pixmap.drawPixel(x, y, 0x00000000);
// And all pixels you want to draw

Pixmap.setBlending(Pixmap.Blending.SourceOver); // if you want to go back to blending

If you want to maintain the RGB of the existing pixel, you have to get the current pixel color and manually black out the alpha component only:

pixmap.drawPixel(x, y, pixmap.getPixel(x, y) & 0xffffff00);
like image 60
Tenfour04 Avatar answered Nov 19 '22 16:11

Tenfour04