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 issue
Page Index Toggle Pages: 1
PGraphics Alpha issue (Read 676 times)
PGraphics Alpha issue
Jun 15th, 2007, 2:30pm
 
I am trying to use a PGraphics object as a texture but i would like it to be semi transparant...
I don't seem to be able to resolve the problem, even after searching the forum... If anyone could tell me what i'm doing wrong i would be very grateful!!!

Here's an example of my code...

PGraphics myTexture;

void setup()
{
 frameRate(1);
 size(320,240);
 background(255);
 myTexture = createGraphics(160,120,JAVA2D);
}
 
void draw()
{
   
   myTexture.beginDraw();
   myTexture.background(255,0,0);
   myTexture.ellipse(80,60,10,10);
   myTexture.endDraw();
   myTexture.modified = true;
   
   myTexture.loadPixels();
   for (int i = 0; i < myTexture.width * myTexture.height; i++)
   {
     float r = red(color(myTexture.pixels[i]));
     float g = green(color(myTexture.pixels[i]));
     float b = blue(color(myTexture.pixels[i]));
     myTexture.pixels[i] = color(r, g, b,128);
   }
   myTexture.updatePixels();
   
   image(myTexture,20,20);
}
Re: PGraphics Alpha issue
Reply #1 - Jun 15th, 2007, 6:53pm
 
Your code is actually working, but because you're not clearing the background on each frame your transparent images are accumulating and quickly appear opaque.  To fix that just add a call to background() at the top of draw().

Also, just a tip, you can use tint() to set transparency instead of manipulating the pixels yourself:

Quote:

void draw()
{
   background(255);
   myTexture.beginDraw();
   myTexture.background(255,0,0);
   myTexture.ellipse(80,60,10,10);
   myTexture.endDraw();
   myTexture.modified = true;
   tint(255,128);
   image(myTexture,20,20);
}
Re: PGraphics Alpha issue
Reply #2 - Jun 15th, 2007, 8:43pm
 
Thank you Dave for your quick response... What a stupid mistake to make, but i just didn't see it.
About the tint() function. If I want to apply the texture to a shape, i don't immediately see a way to implicate it. The alpha thing (though not through bitwise operations, i'm not that comfortable using bit things) seems to work.
Thanx again!!!

PGraphics myTexture;

void setup()
{
 frameRate(1);
 size(320,240,P3D);
 background(255);
 myTexture = createGraphics(160,120,JAVA2D);
}

void draw()  
{  
   background(255);
   myTexture.beginDraw();  
   myTexture.background(255,0,0);  
   myTexture.ellipse(80,60,10,10);  
   myTexture.endDraw();  
   myTexture.modified = true;
   
   myTexture.loadPixels();
   for (int i = 0; i < myTexture.width * myTexture.height; i++)
   {
     float r = red(color(myTexture.pixels[i]));
     float g = green(color(myTexture.pixels[i]));
     float b = blue(color(myTexture.pixels[i]));
     myTexture.pixels[i] = color(r, g, b,128);
   }
   myTexture.updatePixels();
   
   image(myTexture,20,20);
   
   beginShape();
   texture(myTexture);
   vertex(10,10,0,0);
   vertex(100,10,160,0);
   vertex(100,120,160,120);
   vertex(10,120,0,120);
   endShape();
   
}
Page Index Toggle Pages: 1