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

Re : Get zbuffer in Processing v2b6

$
0
0
first, one detail with the glReadPixels() call: you should use height - mouseY - 1 instead of mouseY, because of the inverted Y axis in Processing with respect to OpenGL.

Any of the two following code options are valid:
  1. import java.nio.*;
  2. void setup() {
  3.   size(640, 360, P3D);
  4.   fill(204);
  5. }
  6. void draw() {
  7.   lights();
  8.   background(0);
  9.   camera(30.0, 300, 220.0,
  10.          0.0, 0.0, 0.0,
  11.          0.0, 1.0, 0.0);
  12.   noStroke();
  13.   box(90);
  14.   stroke(255);
  15.   line(-100, 0, 0, 100, 0, 0);
  16.   line(0, -100, 0, 0, 100, 0);
  17.   line(0, 0, -100, 0, 0, 100);

  18.   PGraphicsOpenGL pg = (PGraphicsOpenGL) g;
  19.   PGL pgl = pg.beginPGL();
  20.   FloatBuffer zbuff = FloatBuffer.allocate(1);
  21.   pgl.readPixels(mouseX, height - mouseY - 1, 1, 1, PGL.DEPTH_COMPONENT, PGL.FLOAT, zbuff);
  22.   float z = zbuff.get();
  23.   pg.endPGL();

  24.   println(z);
  25. }
or
  1. import java.nio.*;
  2. import javax.media.opengl.GL;
  3. import javax.media.opengl.GL2;
  4. void setup() {
  5.   size(640, 360, P3D);
  6.   fill(204);
  7. }
  8. void draw() {
  9.   lights();
  10.   background(0);
  11.   camera(30.0, 300, 220.0,
  12.          0.0, 0.0, 0.0,
  13.          0.0, 1.0, 0.0);
  14.   noStroke();
  15.   box(90);
  16.   stroke(255);
  17.   line(-100, 0, 0, 100, 0, 0);
  18.   line(0, -100, 0, 0, 100, 0);
  19.   line(0, 0, -100, 0, 0, 100);

  20.   PGraphicsOpenGL pg = (PGraphicsOpenGL) g;
  21.   PGL pgl = pg.beginPGL();
  22.   FloatBuffer zbuff = FloatBuffer.allocate(1);
  23.   pgl.gl.glReadPixels(mouseX, height - mouseY - 1, 1, 1, GL2.GL_DEPTH_COMPONENT, GL.GL_FLOAT, zbuff);
  24.   float z = zbuff.get();
  25.   pg.endPGL();
  26.   
  27.   println(z);
  28. }
But testing both on 2.0b6 I get 0 for all positions on the screen, so there seems to be something off here. But running them on the latest revision from the repo gives non-zero values that appear to be correct. So the upcoming 2.0b7 should solve this issue.



Viewing all articles
Browse latest Browse all 1768

Trending Articles