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:
- import java.nio.*;
- void setup() {
- size(640, 360, P3D);
- fill(204);
- }
- void draw() {
- lights();
- background(0);
- camera(30.0, 300, 220.0,
- 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0);
- noStroke();
- box(90);
- stroke(255);
- line(-100, 0, 0, 100, 0, 0);
- line(0, -100, 0, 0, 100, 0);
- line(0, 0, -100, 0, 0, 100);
- PGraphicsOpenGL pg = (PGraphicsOpenGL) g;
- PGL pgl = pg.beginPGL();
- FloatBuffer zbuff = FloatBuffer.allocate(1);
- pgl.readPixels(mouseX, height - mouseY - 1, 1, 1, PGL.DEPTH_COMPONENT, PGL.FLOAT, zbuff);
- float z = zbuff.get();
- pg.endPGL();
- println(z);
- }
- import java.nio.*;
- import javax.media.opengl.GL;
- import javax.media.opengl.GL2;
- void setup() {
- size(640, 360, P3D);
- fill(204);
- }
- void draw() {
- lights();
- background(0);
- camera(30.0, 300, 220.0,
- 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0);
- noStroke();
- box(90);
- stroke(255);
- line(-100, 0, 0, 100, 0, 0);
- line(0, -100, 0, 0, 100, 0);
- line(0, 0, -100, 0, 0, 100);
- PGraphicsOpenGL pg = (PGraphicsOpenGL) g;
- PGL pgl = pg.beginPGL();
- FloatBuffer zbuff = FloatBuffer.allocate(1);
- pgl.gl.glReadPixels(mouseX, height - mouseY - 1, 1, 1, GL2.GL_DEPTH_COMPONENT, GL.GL_FLOAT, zbuff);
- float z = zbuff.get();
- pg.endPGL();
- println(z);
- }