We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Cleaning a PGraphics Object
Page Index Toggle Pages: 1
Cleaning a PGraphics Object (Read 2747 times)
Cleaning a PGraphics Object
Jun 15th, 2010, 5:10pm
 
Hi there
I wanted to know if it is possible to clean a PGraphics buffer object... for example for creating a buffer for a foreground which gets drawn over another buffer that stores a background... both buffers have to be redrawn each frame, but background(0) does not work because it kills the transparence...
I tried to clean the PGraphics object by creating a new one using createGraphics() but it makes the sketch suffer greatly on it's performance... so, how can i wipe out a PGraphics object to leave a clean alpha channel?
Re: Cleaning a PGraphics Object
Reply #1 - Jun 15th, 2010, 7:29pm
 
Hmmm.  Well, you could try something like:

Code:
PGraphics pg; // Foreground PGraphics object

// ...

pg.loadPixels();

for (int i = 0; i < pg.pixels.length; ++i) {
pg.pixels[i] = pg.pixels[i] & 0xff000000;
}

pg.updatePixels();

I think alpha is stored in the first byte of a pixel, so if I've written this correctly, it should leave the alpha of each pixel alone while setting the second, third, and fourth bytes (R/G/B, I think) to zero.

Also, this is more likely to work for the P2D or P3D renderers than for Java or OpenGL.  (Not that it's necessarily going to work in any case.)

Re: Cleaning a PGraphics Object
Reply #2 - Jun 15th, 2010, 11:04pm
 
A background(0, 0); might work (might depend on renderer, otherwise the manual way of Smitty should work, but it is faster just to assign 0xFF000000 or just use Arrays.fill()).
Re: Cleaning a PGraphics Object
Reply #3 - Jun 16th, 2010, 7:55am
 
PhiLho  wrote on Jun 15th, 2010, 11:04pm:
...it is faster just to assign 0xFF000000 or just use Arrays.fill()).

Ah, yes.  When I first read this, I thought fdevant wanted to keep the data in the alpha channel but clean out the color data (i.e., preserving areas of greater and lesser transparency from frame to frame) but now that I'm looking at his question again, he wants a "clean" alpha channel, which is (probably) much easier.


Re: Cleaning a PGraphics Object
Reply #4 - Jun 16th, 2010, 6:20pm
 
Thank you guys, gonna check it out
Page Index Toggle Pages: 1