Created
April 12, 2012 20:39
-
-
Save clausd/2370833 to your computer and use it in GitHub Desktop.
Find the line corresponding to a single projected point in Processing (P3D assumed)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Find the 3D coordinates at depth z that project onto planar coordinates (x,y). | |
Screen position is at z-1 in the screen coordinate system, so z should be less than -1 | |
(basically inverse for screenX/screenY) | |
*/ | |
PVector screenToDepth(float x, float y, float z) { | |
// first get the transforms in place | |
PMatrix3D M = (PMatrix3D) g.getMatrix(); | |
PMatrix3D P = (PMatrix3D) ((PGraphics3D) g).getProjection(); | |
P.apply(M); | |
// compute scaling variables for this z-depth | |
// not sure how general this is for arbitrary M and P - but works for default camera setup | |
float[] in = new float[4]; | |
in[0] = 0; | |
in[1] = 0; | |
in[2] = z; | |
in[3] = 1; | |
float[] out = new float[4]; | |
P.mult(in,out); | |
// setup input for projection inverse | |
x = x*2/width-1; | |
y = y*2/height-1; | |
float fw = out[3]; | |
float fz = out[2]; | |
in[0] = x*fw; | |
in[1] = y*fw; | |
in[2] = fz; | |
in[3] = fw; | |
// and the actual computation | |
P.invert(); | |
P.mult(in, out); | |
return new PVector(out[0], out[1], out[2]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment