diff options
author | Sven Gothel <[email protected]> | 2012-04-06 03:47:09 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-04-06 03:47:09 +0200 |
commit | c6738c828bab2ca1572635f7ba90a7a374bbdef2 (patch) | |
tree | 3961ad6faac794c37c365c8016bbe893c2a130bf /src/jogl/classes | |
parent | aeeddc61cd50742ad2bceb2ce91c707ddfb69d88 (diff) |
PMVMatrix: Add convenient ProjectFloat calls of gluLookAt, gluProject, gluUnProject and gluPickMatrix
Diffstat (limited to 'src/jogl/classes')
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java | 93 | ||||
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/util/ProjectFloat.java | 151 |
2 files changed, 192 insertions, 52 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java index 1db3f132f..929eb3d28 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java +++ b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java @@ -72,6 +72,8 @@ public class PMVMatrix implements GLMatrixFunc { * this flag shall be set to <code>true</code> or <code>false</code></p>. */ public PMVMatrix(boolean useBackingArray) { + this.usesBackingArray = useBackingArray; + // I Identity // T Texture // P Projection @@ -131,6 +133,8 @@ public class PMVMatrix implements GLMatrixFunc { update(); } + public final boolean usesBackingArray() { return usesBackingArray; } + public void destroy() { if(null!=projectFloat) { projectFloat.destroy(); projectFloat=null; @@ -321,14 +325,6 @@ public class PMVMatrix implements GLMatrixFunc { } } - public final void gluPerspective(final float fovy, final float aspect, final float zNear, final float zFar) { - float top=(float)Math.tan(fovy*((float)Math.PI)/360.0f)*zNear; - float bottom=-1.0f*top; - float left=aspect*bottom; - float right=aspect*top; - glFrustumf(left, right, bottom, top, zNear, zFar); - } - // // MatrixIf // @@ -573,6 +569,14 @@ public class PMVMatrix implements GLMatrixFunc { glMultMatrixf(matrixOrtho, 0); } + public final void gluPerspective(final float fovy, final float aspect, final float zNear, final float zFar) { + float top=(float)Math.tan(fovy*((float)Math.PI)/360.0f)*zNear; + float bottom=-1.0f*top; + float left=aspect*bottom; + float right=aspect*top; + glFrustumf(left, right, bottom, top, zNear, zFar); + } + public final void glFrustumf(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar) { if(zNear<=0.0f||zFar<0.0f) { throw new GLException("GL_INVALID_VALUE: zNear and zFar must be positive, and zNear>0"); @@ -607,6 +611,78 @@ public class PMVMatrix implements GLMatrixFunc { glMultMatrixf(matrixFrustum, 0); } + public void gluLookAt(float eyex, float eyey, float eyez, + float centerx, float centery, float centerz, + float upx, float upy, float upz) { + projectFloat.gluLookAt(this, eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz); + } + + /** + * Uses this instance {@link #glGetMvMatrixf()} and {@link #glGetPMatrixf()} + * + * @param objx + * @param objy + * @param objz + * @param viewport + * @param viewport_offset + * @param win_pos + * @param win_pos_offset + * @return + */ + public boolean gluProject(float objx, float objy, float objz, + int[] viewport, int viewport_offset, + float[] win_pos, int win_pos_offset ) { + if(usesBackingArray) { + return projectFloat.gluProject(objx, objy, objz, + matrixMv.array(), 0, + matrixP.array(), 0, + viewport, viewport_offset, + win_pos, win_pos_offset); + } else { + return projectFloat.gluProject(objx, objy, objz, + matrixMv, + matrixP, + viewport, viewport_offset, + win_pos, win_pos_offset); + } + } + + /** + * Uses this instance {@link #glGetMvMatrixf()} and {@link #glGetPMatrixf()} + * + * @param winx + * @param winy + * @param winz + * @param viewport + * @param viewport_offset + * @param obj_pos + * @param obj_pos_offset + * @return + */ + public boolean gluUnProject(float winx, float winy, float winz, + int[] viewport, int viewport_offset, + float[] obj_pos, int obj_pos_offset) { + if(usesBackingArray) { + return projectFloat.gluUnProject(winx, winy, winz, + matrixMv.array(), 0, + matrixP.array(), 0, + viewport, viewport_offset, + obj_pos, obj_pos_offset); + } else { + return projectFloat.gluUnProject(winx, winy, winz, + matrixMv, + matrixP, + viewport, viewport_offset, + obj_pos, obj_pos_offset); + } + } + + public void gluPickMatrix(float x, float y, + float deltaX, float deltaY, + int[] viewport, int viewport_offset) { + projectFloat.gluPickMatrix(this, x, y, deltaX, deltaY, viewport, viewport_offset); + } + // // private // @@ -659,6 +735,7 @@ public class PMVMatrix implements GLMatrixFunc { } } + protected final boolean usesBackingArray; protected Buffer matrixBuffer; protected FloatBuffer matrixIdent, matrixPMvMvit, matrixPMvMvi, matrixPMv, matrixP, matrixTex, matrixMv, matrixMvi, matrixMvit; protected float[] matrixMult, matrixTrans, matrixRot, matrixScale, matrixOrtho, matrixFrustum, vec3f; diff --git a/src/jogl/classes/com/jogamp/opengl/util/ProjectFloat.java b/src/jogl/classes/com/jogamp/opengl/util/ProjectFloat.java index aa1258c34..97915502d 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/ProjectFloat.java +++ b/src/jogl/classes/com/jogamp/opengl/util/ProjectFloat.java @@ -703,15 +703,9 @@ public class ProjectFloat { * @param upz */ public void gluLookAt(GLMatrixFunc gl, - float eyex, - float eyey, - float eyez, - float centerx, - float centery, - float centerz, - float upx, - float upy, - float upz) { + float eyex, float eyey, float eyez, + float centerx, float centery, float centerz, + float upx, float upy, float upz) { FloatBuffer forward = this.forwardBuf; FloatBuffer side = this.sideBuf; FloatBuffer up = this.upBuf; @@ -763,17 +757,11 @@ public class ProjectFloat { * * @return */ - public boolean gluProject(float objx, - float objy, - float objz, - float[] modelMatrix, - int modelMatrix_offset, - float[] projMatrix, - int projMatrix_offset, - int[] viewport, - int viewport_offset, - float[] win_pos, - int win_pos_offset ) { + public boolean gluProject(float objx, float objy, float objz, + float[] modelMatrix, int modelMatrix_offset, + float[] projMatrix, int projMatrix_offset, + int[] viewport, int viewport_offset, + float[] win_pos, int win_pos_offset ) { float[] in = this.in; float[] out = this.out; @@ -786,8 +774,9 @@ public class ProjectFloat { multMatrixVecf(modelMatrix, modelMatrix_offset, in, 0, out); multMatrixVecf(projMatrix, projMatrix_offset, out, 0, in); - if (in[3] == 0.0f) + if (in[3] == 0.0f) { return false; + } in[3] = (1.0f / in[3]) * 0.5f; @@ -804,6 +793,42 @@ public class ProjectFloat { return true; } + public boolean gluProject(float objx, float objy, float objz, + FloatBuffer modelMatrix, + FloatBuffer projMatrix, + int[] viewport, int viewport_offset, + float[] win_pos, int win_pos_offset ) { + + FloatBuffer in = this.inBuf; + FloatBuffer out = this.outBuf; + + in.put(0, objx); + in.put(1, objy); + in.put(2, objz); + in.put(3, 1.0f); + + multMatrixVecf(modelMatrix, in, out); + multMatrixVecf(projMatrix, out, in); + + if (in.get(3) == 0.0f) { + return false; + } + + in.put(3, (1.0f / in.get(3)) * 0.5f); + + // Map x, y and z to range 0-1 + in.put(0, in.get(0) * in.get(3) + 0.5f); + in.put(1, in.get(1) * in.get(3) + 0.5f); + in.put(2, in.get(2) * in.get(3) + 0.5f); + + // Map x,y to viewport + win_pos[0+win_pos_offset] = in.get(0) * viewport[2+viewport_offset] + viewport[0+viewport_offset]; + win_pos[1+win_pos_offset] = in.get(1) * viewport[3+viewport_offset] + viewport[1+viewport_offset]; + win_pos[2+win_pos_offset] = in.get(2); + + return true; + } + /** * Method gluProject * @@ -817,9 +842,7 @@ public class ProjectFloat { * * @return */ - public boolean gluProject(float objx, - float objy, - float objz, + public boolean gluProject(float objx, float objy, float objz, FloatBuffer modelMatrix, FloatBuffer projMatrix, IntBuffer viewport, @@ -836,8 +859,9 @@ public class ProjectFloat { multMatrixVecf(modelMatrix, in, out); multMatrixVecf(projMatrix, out, in); - if (in.get(3) == 0.0f) + if (in.get(3) == 0.0f) { return false; + } in.put(3, (1.0f / in.get(3)) * 0.5f); @@ -870,24 +894,19 @@ public class ProjectFloat { * * @return */ - public boolean gluUnProject(float winx, - float winy, - float winz, - float[] modelMatrix, - int modelMatrix_offset, - float[] projMatrix, - int projMatrix_offset, - int[] viewport, - int viewport_offset, - float[] obj_pos, - int obj_pos_offset) { + public boolean gluUnProject(float winx, float winy, float winz, + float[] modelMatrix, int modelMatrix_offset, + float[] projMatrix, int projMatrix_offset, + int[] viewport, int viewport_offset, + float[] obj_pos, int obj_pos_offset) { float[] in = this.in; float[] out = this.out; multMatrixf(modelMatrix, modelMatrix_offset, projMatrix, projMatrix_offset, matrix, 0); - if (!gluInvertMatrixf(matrix, 0, matrix, 0)) + if (!gluInvertMatrixf(matrix, 0, matrix, 0)) { return false; + } in[0] = winx; in[1] = winy; @@ -905,8 +924,9 @@ public class ProjectFloat { multMatrixVecf(matrix, in, out); - if (out[3] == 0.0) + if (out[3] == 0.0) { return false; + } out[3] = 1.0f / out[3]; @@ -918,6 +938,49 @@ public class ProjectFloat { } + public boolean gluUnProject(float winx, float winy, float winz, + FloatBuffer modelMatrix, + FloatBuffer projMatrix, + int[] viewport, int viewport_offset, + float[] obj_pos, int obj_pos_offset) { + FloatBuffer in = this.inBuf; + FloatBuffer out = this.outBuf; + + multMatrixf(modelMatrix, projMatrix, matrixBuf); + + if (!gluInvertMatrixf(matrixBuf, matrixBuf)) { + return false; + } + + in.put(0, winx); + in.put(1, winy); + in.put(2, winz); + in.put(3, 1.0f); + + // Map x and y from window coordinates + in.put(0, (in.get(0) - viewport[0+viewport_offset]) / viewport[2+viewport_offset]); + in.put(1, (in.get(1) - viewport[1+viewport_offset]) / viewport[3+viewport_offset]); + + // Map to range -1 to 1 + in.put(0, in.get(0) * 2 - 1); + in.put(1, in.get(1) * 2 - 1); + in.put(2, in.get(2) * 2 - 1); + + multMatrixVecf(matrixBuf, in, out); + + if (out.get(3) == 0.0f) { + return false; + } + + out.put(3, 1.0f / out.get(3)); + + obj_pos[0+obj_pos_offset] = out.get(0) * out.get(3); + obj_pos[1+obj_pos_offset] = out.get(1) * out.get(3); + obj_pos[2+obj_pos_offset] = out.get(2) * out.get(3); + + return true; + } + /** * Method gluUnproject * @@ -931,10 +994,8 @@ public class ProjectFloat { * * @return */ - public boolean gluUnProject(float winx, - float winy, - float winz, - FloatBuffer modelMatrix, + public boolean gluUnProject(float winx, float winy, float winz, + FloatBuffer modelMatrix, FloatBuffer projMatrix, IntBuffer viewport, FloatBuffer obj_pos) { @@ -943,8 +1004,9 @@ public class ProjectFloat { multMatrixf(modelMatrix, projMatrix, matrixBuf); - if (!gluInvertMatrixf(matrixBuf, matrixBuf)) + if (!gluInvertMatrixf(matrixBuf, matrixBuf)) { return false; + } in.put(0, winx); in.put(1, winy); @@ -964,8 +1026,9 @@ public class ProjectFloat { multMatrixVecf(matrixBuf, in, out); - if (out.get(3) == 0.0f) + if (out.get(3) == 0.0f) { return false; + } out.put(3, 1.0f / out.get(3)); |