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 › PGraphics alpha channel / VSync / OpenGL
Page Index Toggle Pages: 1
PGraphics alpha channel / VSync / OpenGL (Read 811 times)
PGraphics alpha channel / VSync / OpenGL
Jul 27th, 2009, 5:41pm
 
Is it possible to have a per pixel alpha channel on a PGraphics that, when blitted to the main PGraphics (using image()) is honored?  I am trying to blit a PGraphics with a perlin noise alpha channel over a background image on the main / default PGraphics surface.  It doesn't seem to be working though.  Sorry if this isn't clear, here is a code example of what I am doing:

Code:
void setup () {
size(320, 240);
}
void draw () {
background(color(0,255,0));
PGraphics test = createGraphics(width, height, P2D);
test.beginDraw();
color fillColor = color(255,0,0);
for (int x=0; x < width; ++x) {
for (int y=0; y < height; ++y) {
test.stroke(fillColor, noise(x/100f,y/100f)*255);
test.point(x,y);
}
}
test.endDraw();
image(test,0,0);
}


Also, why am I seeing a flash of the green background before the new image is placed over it?  is there anyway to have all drawing be done in one atom, so to speak?  Is this become it's using immediate mode OpenGL?  Do people have a practice for drawing to an offscreen buffer then swapping them?  I have noticed I have some VSync problems in other applets (progress tearing of animations), is there anything I can do about that?
Re: PGraphics alpha channel / VSync / OpenGL
Reply #1 - Jul 27th, 2009, 6:07pm
 
Looks like I can get something like what I want by doing this.  My aim is to pregenerate a bunch of 'sprites' using the drawing commands and have them have alpha channels, doing it like this, essentially drawing the image twice, once as an alpha channel map and once as a color map seems kind of laborious though, especially since most of processings drawing commands seem to have alpha effects as part of their function.

Code:
void setup () {
 size(320, 240);
}
void draw () {
 background(color(0,255,0));
 PGraphics testAlpha = createGraphics(width, height, P2D);
 PGraphics test = createGraphics(width, height, P2D);
 testAlpha.beginDraw();
 color fillColor = color(255,0,0);
 for (int x=0; x < width; ++x) {
   for (int y=0; y < height; ++y) {
     testAlpha.stroke(noise(x/100f,y/100f)*255);
     testAlpha.point(x,y);
   }
 }
 testAlpha.endDraw();
 test.background(color(255,0,0));
 test.mask(testAlpha);
 image(test,0,0);
}
Re: PGraphics alpha channel / VSync / OpenGL
Reply #2 - Jul 28th, 2009, 2:24am
 
AFAIK, you have transparency in a PGraphics only in JAVA2D mode (at least it doesn't work with P2D -- just tested, works fine and fast in P3D). Which is unfortunate as the JAVA2D way of running your sketch is much slower. Probably because you use point(), which is slow.
About the green flash: for some reason, background() acts immediately on the screen (while other drawings wait for completion of draw()). If you move the call just before the image() call, you don't see the green flash (but instead see the default gray background... Sad).

If you use pixels array instead of point(), you have a more reactive sketch:
Code:
void draw () {
PGraphics test = createGraphics(width, height, JAVA2D);
test.loadPixels();
for (int x=0; x < width; ++x) {
for (int y=0; y < height; ++y) {
color c = color(255, 0, 0, noise(x/100f,y/100f)*255);
test.pixels[x + y * width] = c;
}
}
test.updatePixels();
background(0,255,0);
image(test,0,0);
}
Page Index Toggle Pages: 1