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

Re : How to use a fragment shader on a PGraphics in Processing 2.0xx?

$
0
0
The filer(PShader) function should work with a PGraphics surface, as long as it is created as P2D or P3D:
  1. PShader edges;
  2. boolean applyFilter = true;
  3. PGraphics pg;

  4. void setup() {
  5.   size(640, 360, P3D);
  6.   edges = loadShader("edges.glsl");
  7.   pg = createGraphics(640, 360, P3D);  
  8.   noStroke(); 
  9. }

  10. void draw() {
  11.   pg.beginDraw();
  12.   pg.noStroke();
  13.   pg.background(0);
  14.   pg.lights();
  15.   
  16.   pg.translate(width/2, height/2);  
  17.   pg.pushMatrix();
  18.   pg.rotateX(frameCount * 0.01);  
  19.   pg.rotateY(frameCount * 0.01);
  20.   pg.box(120);
  21.   pg.popMatrix();
  22.     
  23.   if (applyFilter == true) {
  24.     pg.filter(edges);
  25.   }
  26.   
  27.   // The sphere doesn't have the edge detection applied 
  28.   // on it because it is drawn after filter() is called.
  29.   pg.rotateY(frameCount * 0.02);
  30.   pg.translate(150, 0);
  31.   pg.sphere(40);
  32.   pg.endDraw();
  33.   
  34.   image(pg, 0, 0, width, height);
  35. }

  36. void mousePressed() {
  37.   applyFilter = !applyFilter;
  38. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles