Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : PGraphics3D, ignore alpha channel information when rendering to screen

$
0
0
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):

  1. // import codeanticode.syphon.SyphonServer;

  2. PGraphics tex;

  3. PShader opaque;

  4. //SyphonServer syphonserver;

  5. void setup() {
  6.   size(1024, 768, P3D);

  7.   // syphonserver = new SyphonServer( this, "Continuum Renderer" );

  8.   tex = createGraphics(width, height, P3D );

  9.   opaque = loadShader( "opaque.glsl" );

  10. }

  11. String renderMethod = "";

  12. void draw() {
  13.   background(0);

  14.   if (keyPressed) {
  15.     renderMethod = "image(), rendering texture";
  16.     image(tex, 0, 0 );
  17.   } 
  18.   else {
  19.     renderMethod = "directly drawing shapes to screen";
  20.     drawThings(g);
  21.   }
  22.   
  23.   tex.beginDraw();
  24.   drawThings( tex );
  25.   tex.filter(opaque);
  26.   tex.endDraw();
  27.   
  28.   fill( 255 );
  29.   text( renderMethod , 400 , 40 );
  30.   
  31.   //syphonserver.sendImage( tex );
  32. }

  33. void drawThings(PGraphics g) {
  34.   g.fill(0, mouseX, 0);
  35.   g.rect(0, 0, width, height);
  36.   g.noStroke();
  37.   for (int i=0;i<20;i++) {
  38.     g.fill(255, 0, 0, i * 12.5);
  39.     float c = 150;
  40.     g.ellipse( 200 + (i*100) % 500, 200 + (i/5)*100, c, c );
  41.   }
  42.   for (int i=0;i<10;i++) {
  43.     g.fill(255, i*25);
  44.     float c = 100;
  45.     g.ellipse( i * 100, +i*100, (sin(frameCount*0.1) * 100) + c+i*20, c+i*20 );
  46.   }
  47.   for (int i=0;i<10;i++) {
  48.     g.fill(0, 0, 255, i*25);
  49.     float c = 100;
  50.     g.ellipse( 200 + i * 100, i*100, (sin(frameCount*0.1) * 100) + c+i*20, c+i*20 );
  51.   }
  52.   g.fill(255,255,0,mouseY);
  53.   g.ellipse(100,100,200,200);
  54. }


  55. // opaque.glsl
  56. // A shader to make transparent pixels in a frambuffer opaque. 
  57. // What happanes here:
  58. // - a gl_FragColor's alpha value is set to 1.0.
  59. // - the rgb value is adjusted based on alpha value (one_minus_source_alpha).
  60. // a slight alpha deviation remains in the mid-ranges 
  61. /*

  62. #define PROCESSING_TEXTURE_SHADER

  63. uniform sampler2D texture;
  64. varying vec4 vertTexCoord;

  65. void main(void) {
  66.   vec4 rgba = texture2D(texture, vertTexCoord.st);
  67.   gl_FragColor = vec4(rgba.rgb + ( ( 1.0 - rgba.a ) ) * rgba.rgb, 1.0 );
  68. }

  69. */


andreas schlegel, http://www.sojamo.de

Viewing all articles
Browse latest Browse all 1768

Trending Articles