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

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

$
0
0
When you use bitshifting and the pixel array directly, it doesn't seem to greatly impact the fps.

Code Example
  1. // Tested with Processing 2.0b8

  2. PGraphics pg;
  3.  
  4. void setup() {
  5.   size(500, 500, P2D);
  6.   pg = createGraphics(width, height, P2D);
  7.   pg.beginDraw();
  8.   pg.smooth(4);
  9.   pg.noStroke();
  10.   pg.endDraw();
  11.   noStroke();
  12.   fill(0, 255, 0);
  13. }
  14.  
  15. void draw() {
  16.   background(255); // white background
  17.   rect(0, 175, width, 150); // green rectangle
  18.   drawPG();
  19.   if (mousePressed) ignoreAlpha(pg);
  20.   image(pg, 0, 0);
  21.   frame.setTitle(int(frameRate) + " fps");
  22. }
  23.  
  24. void drawPG() {
  25.   pg.beginDraw();
  26.   pg.background(255, 0); // white 'invisible' background (also works with opaque white)
  27.   pg.fill(255, 0, 0); // red opaque ellipse
  28.   pg.ellipse(pg.width/2, pg.height/2, 250, 250);
  29.   pg.fill(0, 0, 255, 125); // blue transparent ellipse
  30.   pg.ellipse(mouseX, mouseY, 200, 200);
  31.   pg.endDraw();
  32. }
  33.  
  34. void ignoreAlpha(PGraphics pg) {
  35.   pg.beginDraw();
  36.   pg.loadPixels();
  37.   for (int i=0; i<pg.pixels.length; i++) {
  38.     color argb = pg.pixels[i];
  39.     int a = (argb >> 24) & 0xFF;
  40.     if (a!=255) {
  41.       int r = (argb >> 16) & 0xFF;
  42.       int g = (argb >> 8) & 0xFF;
  43.       int b = argb & 0xFF;
  44.       pg.pixels[i] = 255<<24 | r<<16 | g<<8 | b;
  45.     }
  46.   }
  47.   pg.updatePixels();
  48.   pg.endDraw();
  49. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles