Thanks GoToLoop, amnon.owed. both your solutions do the job. Since I was looking for a GPU solution to avoid using loadPixels, I came up with the following shader which does what I need to the frameBuffer I am using. It also works fine with sending the frameBuffer via Syphon to another application, here the code I use (the shader is at the bottom of the sketch):
- // import codeanticode.syphon.SyphonServer;
- PGraphics tex;
- PShader opaque;
- //SyphonServer syphonserver;
- void setup() {
- size(1024, 768, P3D);
- // syphonserver = new SyphonServer( this, "Continuum Renderer" );
- tex = createGraphics(width, height, P3D );
- opaque = loadShader( "opaque.glsl" );
- }
- String renderMethod = "";
- void draw() {
- background(0);
- if (keyPressed) {
- renderMethod = "image(), rendering texture";
- image(tex, 0, 0 );
- }
- else {
- renderMethod = "directly drawing shapes to screen";
- drawThings(g);
- }
- tex.beginDraw();
- drawThings( tex );
- tex.filter(opaque);
- tex.endDraw();
- fill( 255 );
- text( renderMethod , 400 , 40 );
- //syphonserver.sendImage( tex );
- }
- void drawThings(PGraphics g) {
- g.fill(0, mouseX, 0);
- g.rect(0, 0, width, height);
- g.noStroke();
- for (int i=0;i<20;i++) {
- g.fill(255, 0, 0, i * 12.5);
- float c = 150;
- g.ellipse( 200 + (i*100) % 500, 200 + (i/5)*100, c, c );
- }
- for (int i=0;i<10;i++) {
- g.fill(255, i*25);
- float c = 100;
- g.ellipse( i * 100, +i*100, (sin(frameCount*0.1) * 100) + c+i*20, c+i*20 );
- }
- for (int i=0;i<10;i++) {
- g.fill(0, 0, 255, i*25);
- float c = 100;
- g.ellipse( 200 + i * 100, i*100, (sin(frameCount*0.1) * 100) + c+i*20, c+i*20 );
- }
- g.fill(255,255,0,mouseY);
- g.ellipse(100,100,200,200);
- }
- // opaque.glsl
- // A shader to make transparent pixels in a frambuffer opaque.
- // What happanes here:
- // - a gl_FragColor's alpha value is set to 1.0.
- // - the rgb value is adjusted based on alpha value (one_minus_source_alpha).
- // a slight alpha deviation remains in the mid-ranges
- /*
- #define PROCESSING_TEXTURE_SHADER
- uniform sampler2D texture;
- varying vec4 vertTexCoord;
- void main(void) {
- vec4 rgba = texture2D(texture, vertTexCoord.st);
- gl_FragColor = vec4(rgba.rgb + ( ( 1.0 - rgba.a ) ) * rgba.rgb, 1.0 );
- }
- */
andreas schlegel, http://www.sojamo.de