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

[2.0xx] How to set P2D/P3D PGraphics pixels to transparent via the pixel array?

$
0
0
Kudos to the Processing team for working towards the big 2.0. I've been testing Processing 2.0b7 for the past few days and have been running into some issues in the combination of PGraphics and transparency. I am unsure if these are due to my own coding errors, changes in Processing, remaining bugs or hardware incompatibilities. So I'm using the forum first to see if filing an issue is necessary.

I am trying to set pixels in a P2D/P3D PGraphics to transparent. The pixels also need to stay transparent when another begin-endDraw is called on the PGraphics. In the example below, setting the color to red (or any color) is possible. However setting it to transparent doesn't work. Instead it's set to grey. Note that the code does work when it's a JAVA2D PGraphics.

Can others reproduce this behavior?
And if so...
How can I set the pixels in a P2D/P3D PGraphics to transparent?

Code Example
  1. PGraphics pg;
  2.  
  3. void setup() {
  4.   size(500, 500, P2D);
  5.   pg = createGraphics(width/2, height/2, P2D);
  6.   noFill();
  7. }
  8.  
  9. void draw() {
  10.   setTransparent();
  11.   background(0, 255, 0);
  12.   image(pg, width/4, height/4);
  13.   rect(width/4, height/4, width/2, height/2);
  14. }
  15.  
  16. void setTransparent() {
  17.   color transparent = color(0, 0);
  18.   color red = color(255, 0, 0);
  19.  
  20.   pg.beginDraw();
  21.   pg.loadPixels();
  22.   for (int y=0; y<pg.height; y++) {
  23.     for (int x=0; x<pg.width; x++) {
  24.       if (x>pg.width/2) {
  25.         pg.pixels[x+y*pg.width] = transparent; // doesn't work with P2D/P3D
  26.       } else {
  27.         pg.pixels[x+y*pg.width] = red; // works
  28.       }
  29.     }
  30.   }
  31.   pg.updatePixels();
  32.   pg.endDraw();
  33. }

Re : Processing 2.0 - 1,000,000 points in OpenGL using Vertex Arrays

$
0
0
Andres, I see that you added this to the beginPGL() in r10729 - awesome.   That will fix tons of issues I was dealing with.  Do you foresee changing this into a shader?  I expect it would be backward compatible, as well as forward compatible.

In any case, I'm glad to see it tied directly into the beginPGL().

Re : 2.0b7 Skybox - not working

$
0
0


dosn't work for me - are you in 2.0b7 ?

Re : 2.0b7 Skybox - not working

$
0
0
Ya, I got it working in 2.0b7, but had to include the matrix transforms.  The original code works with the latest source code of Processing if you build it yourself as Andres add this to the beginPGL().

frameRate() can crash 2.0b7

$
0
0
If you use advanced OpenGL, frameRate can crash the sketch.  Wanted to post here before I submit a bug report.  Maybe it's something simple I'm not doing.  I put the frameRate change on the keyEvent.
  1. import javax.media.opengl.GL2;
  2.  
  3. float a;
  4. GL2 gl;
  5. PGraphicsOpenGL pgl;
  6. float[] projMatrix;
  7. float[] mvMatrix;
  8.  
  9. void setup() {
  10.   size(800, 600, OPENGL);
  11.   pgl = (PGraphicsOpenGL) g;  // g may change
  12.   gl = pgl.beginPGL().gl.getGL2();
  13.   projMatrix = new float[16];
  14.   mvMatrix = new float[16];
  15. }
  16.  
  17. void draw() {
  18.  
  19. background(255);
  20.  
  21.   pgl.beginPGL();
  22.  
  23.   gl.glColor4f(0.7, 0.7, 0.7, 0.8);
  24.   gl.glTranslatef(width/2, height/2, 0);
  25.   gl.glRotatef(a, 1, 0, 0);
  26.   gl.glRotatef(a*2, 0, 1, 0);
  27.   gl.glRectf(-200, -200, 200, 200);
  28.   gl.glRotatef(90, 1, 0, 0);
  29.   gl.glRectf(-200, -200, 200, 200);
  30.  
  31.   pgl.endPGL();
  32.   loadMatrix();
  33.   a += 0.5;
  34. }
  35.  
  36. void loadMatrix() {
  37.  
  38.   gl.glMatrixMode(GL2.GL_PROJECTION);
  39.   projMatrix[0] = pgl.projection.m00;
  40.   projMatrix[1] = pgl.projection.m10;
  41.   projMatrix[2] = pgl.projection.m20;
  42.   projMatrix[3] = pgl.projection.m30;
  43.  
  44.   projMatrix[4] = pgl.projection.m01;
  45.   projMatrix[5] = pgl.projection.m11;
  46.   projMatrix[6] = pgl.projection.m21;
  47.   projMatrix[7] = pgl.projection.m31;
  48.  
  49.   projMatrix[8] = pgl.projection.m02;
  50.   projMatrix[9] = pgl.projection.m12;
  51.   projMatrix[10] = pgl.projection.m22;
  52.   projMatrix[11] = pgl.projection.m32;
  53.  
  54.   projMatrix[12] = pgl.projection.m03;
  55.   projMatrix[13] = pgl.projection.m13;
  56.   projMatrix[14] = pgl.projection.m23;
  57.   projMatrix[15] = pgl.projection.m33;
  58.  
  59.   gl.glLoadMatrixf(projMatrix, 0);
  60.  
  61.   gl.glMatrixMode(GL2.GL_MODELVIEW);
  62.   mvMatrix[0] = pgl.modelview.m00;
  63.   mvMatrix[1] = pgl.modelview.m10;
  64.   mvMatrix[2] = pgl.modelview.m20;
  65.   mvMatrix[3] = pgl.modelview.m30;
  66.  
  67.   mvMatrix[4] = pgl.modelview.m01;
  68.   mvMatrix[5] = pgl.modelview.m11;
  69.   mvMatrix[6] = pgl.modelview.m21;
  70.   mvMatrix[7] = pgl.modelview.m31;
  71.  
  72.   mvMatrix[8] = pgl.modelview.m02;
  73.   mvMatrix[9] = pgl.modelview.m12;
  74.   mvMatrix[10] = pgl.modelview.m22;
  75.   mvMatrix[11] = pgl.modelview.m32;
  76.  
  77.   mvMatrix[12] = pgl.modelview.m03;
  78.   mvMatrix[13] = pgl.modelview.m13;
  79.   mvMatrix[14] = pgl.modelview.m23;
  80.   mvMatrix[15] = pgl.modelview.m33;
  81.   gl.glLoadMatrixf(mvMatrix, 0);
  82. }
  83. void keyPressed() {
  84.   frameRate(2000);
  85. }

Re : frameRate() can crash 2.0b7

$
0
0
No crash for me... (2.0b7, win8)
Why are you using a framerate of 2000?

Re : [2.0xx] How to set P2D/P3D PGraphics pixels to transparent via the pixel array?

$
0
0
As you certainly know, P2D and P3D are now OpenGL. And I saw lot of people wondering about transparency in OpenGL. I don't recall a solution, but I might have missed it.

Re : [2.0xx] How to set P2D/P3D PGraphics pixels to transparent via the pixel array?

$
0
0
This issue is due to how the pixels array is composited into the offscreen buffer when using multisampling. If you initialize your pg object w/out smooth, then the transparency will be ok. Just committed the fix to the repo, so the next release won't have this problem. For the time being, a workaround is to call pg.background(0, 0) right after pg.beginDraw() (or use pg with noSmooth).

Re : Processing 2.0 - 1,000,000 points in OpenGL using Vertex Arrays

$
0
0
Processing automatically sends all the required matrices to the shaders, provided that they define the appropriate uniform variables. See how it is done in the Demos|Graphics|LowLevelGL example. This example assumes no fixed-function pipeline, so the GL matrix-related functions (glMatrixMode, glLoadMatrix, etc) are not used at all.

Re : [2.0xx] How to set P2D/P3D PGraphics pixels to transparent via the pixel array?

$
0
0
Thanks andres. Good to see this problem will be fixed.

Unfortunately both your workarounds cause other issues themselves. noSmooth() solves the immediate issue, but this result is unusable because any subsequent call to beginDraw will clear it again. Using background(0, 0) does not allow accurate creation of pixels because of a mirror issue (already filed on googlecode). See below for details and examples.

noSmooth()
Using noSmooth will reset the PGraphics to fully transparent on each begin-endDraw call even when no background call is made. See the example code below. Without noSmooth() the drawing is cumulative (which is the expected behavior). With noSmooth() you only see one ellipse, because apparantly the PGraphics is cleared.
  1. PGraphics pg;
  2.  
  3. void setup() {
  4.   size(500, 500, P2D);
  5.   pg = createGraphics(width/2, height/2, P2D);
  6.   pg.beginDraw();
  7.   pg.noSmooth(); // comment out to see 'normal' cumulative drawing
  8.   pg.endDraw();
  9.   noFill();
  10. }
  11.  
  12. void draw() {
  13.   pg.beginDraw();
  14.   pg.ellipse((float)mouseX/width*pg.width, (float)mouseY/height*pg.height, 50, 50);
  15.   pg.endDraw();
  16.   background(0, 255, 0);
  17.   image(pg, width/4, height/4);
  18.   rect(width/4, height/4, width/2, height/2);
  19. }

Background(0, 0)
Once background is used with full transparency on a PGraphics, mirror pixels will be created in the upper half of the PGraphics with the given greyscale color. I have already filed an issue for this here. Here is another example in which the top half is erroneously black.
  1. PGraphics pg;
  2.  
  3. void setup() {
  4.   size(500, 500, P2D);
  5.   pg = createGraphics(width/2, height/2, P2D);
  6.   noFill();
  7. }
  8.  
  9. void draw() {
  10.   setTransparent();
  11.   background(0, 255, 0);
  12.   image(pg, width/4, height/4);
  13.   rect(width/4, height/4, width/2, height/2);
  14. }
  15.  
  16. void setTransparent() {
  17.   color transparent = color(0, 0);
  18.   color red = color(255, 0, 0);
  19.  
  20.   pg.beginDraw();
  21.   pg.background(0, 0);
  22.   pg.loadPixels();
  23.   for (int y=0; y<pg.height; y++) {
  24.     for (int x=0; x<pg.width; x++) {
  25.       if (y>pg.height/2) {
  26.         pg.pixels[x+y*pg.width] = red;
  27.       }
  28.     }
  29.   }
  30.   pg.updatePixels();
  31.   pg.endDraw();
  32. }

Re : animation is not responding to increased frameRate

$
0
0
Thanks, Amnon!

I added a scaler variable to vary the speed of animation as you suggested:

  1.     import peasy.*; // camera library
        PeasyCam cam;

        int shift = 0;
        int scaler = 3;

        PShape s1,s2,s3;

        void setup(){
          size(1000,1000,OPENGL);
          smooth(8);
          frameRate(60);

          cam = new PeasyCam(this, 1000);

          s1 = createShape(RECT, 0,0, 300,-300);
          s1.fill(255,0,0);
          s1.end();

          s2 = createShape(RECT, 0,0, 300,-300);
          s2.fill(0,255,0);
          s2.end();

          s3 = createShape(RECT, 0,0, 300,-300);
          s3.fill(0,0,255);
          s3.end();
        }

        void draw(){
          background(0);
        
          pushMatrix();
          translate(0,0,shift);
          shape(s1, 0,0);
          popMatrix();

          pushMatrix();
          translate(shift,0,0);
          rotateY(PI/(-2));
          shape(s2, 0,0);
          popMatrix();
        
          pushMatrix();
          translate(0,-shift,0);
          rotateX(PI/(-2));
          shape(s3, 0,0);
          popMatrix();
        
          shift += scaler;
          if (shift>300){shift=0;}
        
        }

Re : [2.0xx] How to set P2D/P3D PGraphics pixels to transparent via the pixel array?

$
0
0
Thank you for yet another fix andres!

For the above noSmooth example I am seeing different behavior between my own computers (Radeon 4800 = not cumulative vs NVIDIA GTX 570M = cumulative). No errors/warnings, just different behavior. From now on I will make sure to test on both before reporting issues.

Re : [2.0xx] How to set P2D/P3D PGraphics pixels to transparent via the pixel array?

$
0
0
The problem with cumulative drawing and the Radeon happens on OSX or Windows?

Re : [2.0xx] How to set P2D/P3D PGraphics pixels to transparent via the pixel array?

$
0
0
On Windows XP x32 SP3 with Radeon HD 4800 using the latest available drivers AMD Catalyst Display Driver 12.6 (release date: 24/07/2012).

Re : frameRate() can crash 2.0b7

$
0
0
It causes an NPE on the GL2 object here. I found that if you do gl = pgl.beginPGL().gl.getGL2(); again on draw when switching frameRate it works.

Re : frameRate() can crash 2.0b7

$
0
0
I usually include it with "gl.setSwapInterval(0);" to unsync it.  I do this to test the performance, so that I can more efficiently render more stuff.

Re : frameRate() can crash 2.0b7

$
0
0
rbrauer, ya, that's what I'm getting as well.

Re : frameRate() can crash 2.0b7

$
0
0
No crash on Windows 7 x64 + Processing 2.0b7 x64 + NVIDIA GTX 570M.

When I press the key for the first time, I see an almost unnoticeable flicker. The frameRate before and after key press remains 60 fps (so perhaps it's capped by some setting elsewhere on my computer).

Re : frameRate() can crash 2.0b7

$
0
0
Should have mentioned that my testing was on a Mac OSX 10.8.2

Re : frameRate() can crash 2.0b7

$
0
0
I have the impression that JOGL started to cap the framerate at 60fps at some point recently, because a while ago you could set higher values with the frameRate() function and Processing would try to render at that rate.
Viewing all 1768 articles
Browse latest View live