diff options
author | Sven Gothel <[email protected]> | 2012-10-31 16:41:28 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-10-31 16:41:28 +0100 |
commit | f56e36bd4a497643756ab737422eb356b963b27d (patch) | |
tree | da937ab36243241eb9057ef62be87f6bbeb9ee54 /src | |
parent | 7dff066bb823dddb5d6e0e7672f5599afa5a43b9 (diff) |
Fix ProjectFloat (Bug 633): Adding missing offset of sliced buffer usage; reduce buffer usage (performance) in favor of float[].
Thomas De Bodt reported this error and provided the unit test.
Diffstat (limited to 'src')
3 files changed, 303 insertions, 181 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/FloatUtil.java b/src/jogl/classes/com/jogamp/opengl/FloatUtil.java index 297cec285..ffd2344a6 100644 --- a/src/jogl/classes/com/jogamp/opengl/FloatUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/FloatUtil.java @@ -73,7 +73,7 @@ public class FloatUtil { * Make matrix an identity matrix */ public static final void makeIdentityf(FloatBuffer m) { - int oldPos = m.position(); + final int oldPos = m.position(); m.put(IDENTITY_MATRIX); m.position(oldPos); } @@ -91,7 +91,7 @@ public class FloatUtil { * Make matrix an zero matrix */ public static final void makeZero(FloatBuffer m) { - int oldPos = m.position(); + final int oldPos = m.position(); m.put(ZERO_MATRIX); m.position(oldPos); } @@ -246,10 +246,10 @@ public class FloatUtil { * @param v_in 4-component column-vector * @param v_out m_in * v_in */ - public static final void multMatrixVecf(float[] m_in, int m_in_off, float[] v_in, int v_in_off, float[] v_out) { + public static final void multMatrixVecf(float[] m_in, int m_in_off, float[] v_in, int v_in_off, float[] v_out, int v_out_off) { for (int i = 0; i < 4; i++) { // (one matrix row in column-major order) X (column vector) - v_out[i] = + v_out[i + v_out_off] = v_in[0+v_in_off] * m_in[0*4+i+m_in_off] + v_in[1+v_in_off] * m_in[1*4+i+m_in_off] + v_in[2+v_in_off] * m_in[2*4+i+m_in_off] + @@ -279,10 +279,44 @@ public class FloatUtil { * @param v_in 4-component column-vector * @param v_out m_in * v_in */ + public static final void multMatrixVecf(FloatBuffer m_in, float[] v_in, int v_in_off, float[] v_out, int v_out_off) { + final int matrixPos = m_in.position(); + for (int i = 0; i < 4; i++) { + // (one matrix row in column-major order) X (column vector) + v_out[i+v_out_off] = + v_in[0+v_in_off] * m_in.get(0*4+i+matrixPos) + + v_in[1+v_in_off] * m_in.get(1*4+i+matrixPos) + + v_in[2+v_in_off] * m_in.get(2*4+i+matrixPos) + + v_in[3+v_in_off] * m_in.get(3*4+i+matrixPos); + } + } + + /** + * @param m_in 4x4 matrix in column-major order + * @param v_in 4-component column-vector + * @param v_out m_in * v_in + */ + public static final void multMatrixVecf(FloatBuffer m_in, float[] v_in, float[] v_out) { + final int matrixPos = m_in.position(); + for (int i = 0; i < 4; i++) { + // (one matrix row in column-major order) X (column vector) + v_out[i] = + v_in[0] * m_in.get(0*4+i+matrixPos) + + v_in[1] * m_in.get(1*4+i+matrixPos) + + v_in[2] * m_in.get(2*4+i+matrixPos) + + v_in[3] * m_in.get(3*4+i+matrixPos); + } + } + + /** + * @param m_in 4x4 matrix in column-major order + * @param v_in 4-component column-vector + * @param v_out m_in * v_in + */ public static final void multMatrixVecf(FloatBuffer m_in, FloatBuffer v_in, FloatBuffer v_out) { - int inPos = v_in.position(); - int outPos = v_out.position(); - int matrixPos = m_in.position(); + final int inPos = v_in.position(); + final int outPos = v_out.position(); + final int matrixPos = m_in.position(); for (int i = 0; i < 4; i++) { // (one matrix row in column-major order) X (column vector) v_out.put(i + outPos, diff --git a/src/jogl/classes/jogamp/opengl/ProjectFloat.java b/src/jogl/classes/jogamp/opengl/ProjectFloat.java index b1ba1f89c..18fe3e77e 100644 --- a/src/jogl/classes/jogamp/opengl/ProjectFloat.java +++ b/src/jogl/classes/jogamp/opengl/ProjectFloat.java @@ -135,7 +135,7 @@ import com.jogamp.opengl.FloatUtil; * @author Sven Gothel */ public class ProjectFloat { - public static final int getRequiredFloatBufferSize() { return 2*16+2*4+3*3; } + public static final int getRequiredFloatBufferSize() { return 1*16; } // Note that we have cloned parts of the implementation in order to // support incoming Buffers. The reason for this is to avoid loading @@ -153,18 +153,14 @@ public class ProjectFloat { private final float[] out = new float[4]; // Buffer-based implementation - private FloatBuffer matrixBuf; - private FloatBuffer tempInvertMatrixBuf; + private FloatBuffer matrixBuf; // 4x4 - private FloatBuffer inBuf; - private FloatBuffer outBuf; - - private FloatBuffer forwardBuf; - private FloatBuffer sideBuf; - private FloatBuffer upBuf; + private final float[] forward = new float[3]; // 3 + private final float[] side = new float[3]; // 3 + private final float[] up = new float[3]; // 3 public ProjectFloat() { - this(false); + this(true); } public ProjectFloat(boolean useBackingArray) { @@ -182,33 +178,11 @@ public class ProjectFloat { * @param floatOffset Offset for either of the given sources (buffer or array) */ public ProjectFloat(Buffer floatBuffer, float[] floatArray, int floatOffset) { - int floatPos = floatOffset; - int floatSize = 16; - matrixBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); - floatPos += floatSize; - tempInvertMatrixBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); - floatPos += floatSize; - floatSize = 4; - inBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); - floatPos += floatSize; - outBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); - floatPos += floatSize; - floatSize = 3; - forwardBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); - floatPos += floatSize; - sideBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); - floatPos += floatSize; - upBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); + matrixBuf = Buffers.slice2Float(floatBuffer, floatArray, floatOffset, 16); } public void destroy() { matrixBuf = null; - tempInvertMatrixBuf = null; - inBuf = null; - outBuf = null; - forwardBuf = null; - sideBuf = null; - upBuf = null; } /** @@ -221,7 +195,7 @@ public class ProjectFloat { public boolean gluInvertMatrixf(float[] src, int srcOffset, float[] inverse, int inverseOffset) { int i, j, k, swap; float t; - float[][] temp = tempInvertMatrix; + final float[][] temp = tempInvertMatrix; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { @@ -292,14 +266,14 @@ public class ProjectFloat { int i, j, k, swap; float t; - int srcPos = src.position(); - int invPos = inverse.position(); - - FloatBuffer temp = tempInvertMatrixBuf; + final int srcPos = src.position(); + final int invPos = inverse.position(); + + final float[][] temp = tempInvertMatrix; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { - temp.put(i*4+j, src.get(i*4+j + srcPos)); + temp[i][j] = src.get(i*4+j + srcPos); } } FloatUtil.makeIdentityf(inverse); @@ -310,7 +284,7 @@ public class ProjectFloat { // swap = i; for (j = i + 1; j < 4; j++) { - if (Math.abs(temp.get(j*4+i)) > Math.abs(temp.get(i*4+i))) { + if (Math.abs(temp[j][i]) > Math.abs(temp[i][i])) { swap = j; } } @@ -320,17 +294,17 @@ public class ProjectFloat { // Swap rows. // for (k = 0; k < 4; k++) { - t = temp.get(i*4+k); - temp.put(i*4+k, temp.get(swap*4+k)); - temp.put(swap*4+k, t); - + t = temp[i][k]; + temp[i][k] = temp[swap][k]; + temp[swap][k] = t; + t = inverse.get(i*4+k + invPos); inverse.put(i*4+k + invPos, inverse.get(swap*4+k + invPos)); inverse.put(swap*4+k + invPos, t); } } - if (temp.get(i*4+i) == 0) { + if (temp[i][i] == 0) { // // No non-zero pivot. The matrix is singular, which shouldn't // happen. This means the user gave us a bad matrix. @@ -338,17 +312,19 @@ public class ProjectFloat { return false; } - t = temp.get(i*4+i); + t = temp[i][i]; for (k = 0; k < 4; k++) { - temp.put(i*4+k, temp.get(i*4+k) / t); - inverse.put(i*4+k + invPos, inverse.get(i*4+k + invPos) / t); + temp[i][k] /= t; + final int z = i*4+k + invPos; + inverse.put(z, inverse.get(z) / t); } for (j = 0; j < 4; j++) { if (j != i) { - t = temp.get(j*4+i); + t = temp[j][i]; for (k = 0; k < 4; k++) { - temp.put(j*4+k, temp.get(j*4+k) - temp.get(i*4+k) * t); - inverse.put(j*4+k + invPos, inverse.get(j*4+k + invPos) - inverse.get(i*4+k + invPos) * t); + temp[j][k] -= temp[i][k] * t; + final int z = j*4+k + invPos; + inverse.put(z, inverse.get(z) - inverse.get(i*4+k + invPos) * t); } } } @@ -378,8 +354,8 @@ public class ProjectFloat { * @param zFar */ public void gluPerspective(GLMatrixFunc gl, float fovy, float aspect, float zNear, float zFar) { + final float radians = fovy / 2 * (float) Math.PI / 180; float sine, cotangent, deltaZ; - float radians = fovy / 2 * (float) Math.PI / 180; deltaZ = zFar - zNear; sine = (float) Math.sin(radians); @@ -391,13 +367,13 @@ public class ProjectFloat { cotangent = (float) Math.cos(radians) / sine; FloatUtil.makeIdentityf(matrixBuf); - - matrixBuf.put(0 * 4 + 0, cotangent / aspect); - matrixBuf.put(1 * 4 + 1, cotangent); - matrixBuf.put(2 * 4 + 2, - (zFar + zNear) / deltaZ); - matrixBuf.put(2 * 4 + 3, -1); - matrixBuf.put(3 * 4 + 2, -2 * zNear * zFar / deltaZ); - matrixBuf.put(3 * 4 + 3, 0); + final int mPos = matrixBuf.position(); + matrixBuf.put(0 * 4 + 0 + mPos, cotangent / aspect); + matrixBuf.put(1 * 4 + 1 + mPos, cotangent); + matrixBuf.put(2 * 4 + 2 + mPos, - (zFar + zNear) / deltaZ); + matrixBuf.put(2 * 4 + 3 + mPos, -1); + matrixBuf.put(3 * 4 + 2 + mPos, -2 * zNear * zFar / deltaZ); + matrixBuf.put(3 * 4 + 3 + mPos, 0); gl.glMultMatrixf(matrixBuf); } @@ -419,17 +395,17 @@ public class ProjectFloat { 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; + final float[] forward = this.forward; + final float[] side = this.side; + final float[] up = this.up; - forward.put(0, centerx - eyex); - forward.put(1, centery - eyey); - forward.put(2, centerz - eyez); + forward[0] = centerx - eyex; + forward[1] = centery - eyey; + forward[2] = centerz - eyez; - up.put(0, upx); - up.put(1, upy); - up.put(2, upz); + up[0] = upx; + up[1] = upy; + up[2] = upz; FloatUtil.normalize(forward); @@ -441,17 +417,18 @@ public class ProjectFloat { FloatUtil.cross(side, forward, up); FloatUtil.makeIdentityf(matrixBuf); - matrixBuf.put(0 * 4 + 0, side.get(0)); - matrixBuf.put(1 * 4 + 0, side.get(1)); - matrixBuf.put(2 * 4 + 0, side.get(2)); + final int mPos = matrixBuf.position(); + matrixBuf.put(0 * 4 + 0 + mPos, side[0]); + matrixBuf.put(1 * 4 + 0 + mPos, side[1]); + matrixBuf.put(2 * 4 + 0 + mPos, side[2]); - matrixBuf.put(0 * 4 + 1, up.get(0)); - matrixBuf.put(1 * 4 + 1, up.get(1)); - matrixBuf.put(2 * 4 + 1, up.get(2)); + matrixBuf.put(0 * 4 + 1 + mPos, up[0]); + matrixBuf.put(1 * 4 + 1 + mPos, up[1]); + matrixBuf.put(2 * 4 + 1 + mPos, up[2]); - matrixBuf.put(0 * 4 + 2, -forward.get(0)); - matrixBuf.put(1 * 4 + 2, -forward.get(1)); - matrixBuf.put(2 * 4 + 2, -forward.get(2)); + matrixBuf.put(0 * 4 + 2 + mPos, -forward[0]); + matrixBuf.put(1 * 4 + 2 + mPos, -forward[1]); + matrixBuf.put(2 * 4 + 2 + mPos, -forward[2]); gl.glMultMatrixf(matrixBuf); gl.glTranslatef(-eyex, -eyey, -eyez); @@ -476,16 +453,16 @@ public class ProjectFloat { int[] viewport, int viewport_offset, float[] win_pos, int win_pos_offset ) { - float[] in = this.in; - float[] out = this.out; + final float[] in = this.in; + final float[] out = this.out; in[0] = objx; in[1] = objy; in[2] = objz; in[3] = 1.0f; - FloatUtil.multMatrixVecf(modelMatrix, modelMatrix_offset, in, 0, out); - FloatUtil.multMatrixVecf(projMatrix, projMatrix_offset, out, 0, in); + FloatUtil.multMatrixVecf(modelMatrix, modelMatrix_offset, in, 0, out, 0); + FloatUtil.multMatrixVecf(projMatrix, projMatrix_offset, out, 0, in, 0); if (in[3] == 0.0f) { return false; @@ -515,33 +492,33 @@ public class ProjectFloat { int[] viewport, int viewport_offset, float[] win_pos, int win_pos_offset ) { - FloatBuffer in = this.inBuf; - FloatBuffer out = this.outBuf; + final float[] in = this.in; + final float[] out = this.out; - in.put(0, objx); - in.put(1, objy); - in.put(2, objz); - in.put(3, 1.0f); + in[0] = objx; + in[1] = objy; + in[2] = objz; + in[3] = 1.0f; FloatUtil.multMatrixVecf(modelMatrix, in, out); FloatUtil.multMatrixVecf(projMatrix, out, in); - if (in.get(3) == 0.0f) { + if (in[3] == 0.0f) { return false; } - in.put(3, (1.0f / in.get(3)) * 0.5f); + in[3] = (1.0f / in[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); + in[0] = in[0] * in[3] + 0.5f; + in[1] = in[1] * in[3] + 0.5f; + in[2] = in[2] * in[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); - + win_pos[0+win_pos_offset] = in[0] * viewport[2+viewport_offset] + viewport[0+viewport_offset]; + win_pos[1+win_pos_offset] = in[1] * viewport[3+viewport_offset] + viewport[1+viewport_offset]; + win_pos[2+win_pos_offset] = in[2]; + return true; } @@ -564,34 +541,34 @@ public class ProjectFloat { IntBuffer viewport, FloatBuffer win_pos) { - FloatBuffer in = this.inBuf; - FloatBuffer out = this.outBuf; + final float[] in = this.in; + final float[] out = this.out; - in.put(0, objx); - in.put(1, objy); - in.put(2, objz); - in.put(3, 1.0f); + in[0] = objx; + in[1] = objy; + in[2] = objz; + in[3] = 1.0f; FloatUtil.multMatrixVecf(modelMatrix, in, out); FloatUtil.multMatrixVecf(projMatrix, out, in); - if (in.get(3) == 0.0f) { + if (in[3] == 0.0f) { return false; } - in.put(3, (1.0f / in.get(3)) * 0.5f); + in[3] = (1.0f / in[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); + in[0] = in[0] * in[3] + 0.5f; + in[1] = in[1] * in[3] + 0.5f; + in[2] = in[2] * in[3] + 0.5f; // Map x,y to viewport - int vPos = viewport.position(); - int wPos = win_pos.position(); - win_pos.put(0+wPos, in.get(0) * viewport.get(2+vPos) + viewport.get(0+vPos)); - win_pos.put(1+wPos, in.get(1) * viewport.get(3+vPos) + viewport.get(1+vPos)); - win_pos.put(2+wPos, in.get(2)); + final int vPos = viewport.position(); + final int wPos = win_pos.position(); + win_pos.put(0+wPos, in[0] * viewport.get(2+vPos) + viewport.get(0+vPos)); + win_pos.put(1+wPos, in[1] * viewport.get(3+vPos) + viewport.get(1+vPos)); + win_pos.put(2+wPos, in[2]); return true; } @@ -615,8 +592,8 @@ public class ProjectFloat { 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; + final float[] in = this.in; + final float[] out = this.out; FloatUtil.multMatrixf(projMatrix, projMatrix_offset, modelMatrix, modelMatrix_offset, matrix, 0); @@ -673,8 +650,8 @@ public class ProjectFloat { FloatBuffer projMatrix, int[] viewport, int viewport_offset, float[] obj_pos, int obj_pos_offset) { - FloatBuffer in = this.inBuf; - FloatBuffer out = this.outBuf; + final float[] in = this.in; + final float[] out = this.out; FloatUtil.multMatrixf(projMatrix, modelMatrix, matrixBuf); @@ -682,31 +659,31 @@ public class ProjectFloat { return false; } - in.put(0, winx); - in.put(1, winy); - in.put(2, winz); - in.put(3, 1.0f); + in[0] = winx; + in[1] = winy; + in[2] = winz; + in[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]); - + in[0] = (in[0] - viewport[0+viewport_offset]) / viewport[2+viewport_offset]; + in[1] = (in[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); + in[0] = in[0] * 2 - 1; + in[1] = in[1] * 2 - 1; + in[2] = in[2] * 2 - 1; FloatUtil.multMatrixVecf(matrixBuf, in, out); - - if (out.get(3) == 0.0f) { + + if (out[3] == 0.0) { return false; } - out.put(3, 1.0f / out.get(3)); + out[3] = 1.0f / out[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); + obj_pos[0+obj_pos_offset] = out[0] * out[3]; + obj_pos[1+obj_pos_offset] = out[1] * out[3]; + obj_pos[2+obj_pos_offset] = out[2] * out[3]; return true; } @@ -729,8 +706,8 @@ public class ProjectFloat { FloatBuffer projMatrix, IntBuffer viewport, FloatBuffer obj_pos) { - FloatBuffer in = this.inBuf; - FloatBuffer out = this.outBuf; + final float[] in = this.in; + final float[] out = this.out; FloatUtil.multMatrixf(projMatrix, modelMatrix, matrixBuf); @@ -738,33 +715,33 @@ public class ProjectFloat { return false; } - in.put(0, winx); - in.put(1, winy); - in.put(2, winz); - in.put(3, 1.0f); + in[0] = winx; + in[1] = winy; + in[2] = winz; + in[3] = 1.0f; // Map x and y from window coordinates - int vPos = viewport.position(); - int oPos = obj_pos.position(); - in.put(0, (in.get(0) - viewport.get(0+vPos)) / viewport.get(2+vPos)); - in.put(1, (in.get(1) - viewport.get(1+vPos)) / viewport.get(3+vPos)); + final int vPos = viewport.position(); + final int oPos = obj_pos.position(); + in[0] = (in[0] - viewport.get(0+vPos)) / viewport.get(2+vPos); + in[1] = (in[1] - viewport.get(1+vPos)) / viewport.get(3+vPos); // 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); + in[0] = in[0] * 2 - 1; + in[1] = in[1] * 2 - 1; + in[2] = in[2] * 2 - 1; FloatUtil.multMatrixVecf(matrixBuf, in, out); - if (out.get(3) == 0.0f) { + if (out[3] == 0.0) { return false; } - out.put(3, 1.0f / out.get(3)); + out[3] = 1.0f / out[3]; - obj_pos.put(0+oPos, out.get(0) * out.get(3)); - obj_pos.put(1+oPos, out.get(1) * out.get(3)); - obj_pos.put(2+oPos, out.get(2) * out.get(3)); + obj_pos.put(0+oPos, out[0] * out[3]); + obj_pos.put(1+oPos, out[1] * out[3]); + obj_pos.put(2+oPos, out[2] * out[3]); return true; } @@ -800,8 +777,8 @@ public class ProjectFloat { float far, float[] obj_pos, int obj_pos_offset ) { - float[] in = this.in; - float[] out = this.out; + final float[] in = this.in; + final float[] out = this.out; FloatUtil.multMatrixf(projMatrix, projMatrix_offset, modelMatrix, modelMatrix_offset, matrix, 0); @@ -825,8 +802,9 @@ public class ProjectFloat { FloatUtil.multMatrixVecf(matrix, in, out); - if (out[3] == 0.0f) + if (out[3] == 0.0f) { return false; + } obj_pos[0+obj_pos_offset] = out[0]; obj_pos[1+obj_pos_offset] = out[1]; @@ -861,40 +839,41 @@ public class ProjectFloat { float near, float far, FloatBuffer obj_pos) { - FloatBuffer in = this.inBuf; - FloatBuffer out = this.outBuf; + final float[] in = this.in; + final float[] out = this.out; FloatUtil.multMatrixf(projMatrix, modelMatrix, matrixBuf); if (!gluInvertMatrixf(matrixBuf, matrixBuf)) return false; - in.put(0, winx); - in.put(1, winy); - in.put(2, winz); - in.put(3, clipw); + in[0] = winx; + in[1] = winy; + in[2] = winz; + in[3] = clipw; // Map x and y from window coordinates - int vPos = viewport.position(); - in.put(0, (in.get(0) - viewport.get(0+vPos)) / viewport.get(2+vPos)); - in.put(1, (in.get(1) - viewport.get(1+vPos)) / viewport.get(3+vPos)); - in.put(2, (in.get(2) - near) / (far - near)); - + final int vPos = viewport.position(); + in[0] = (in[0] - viewport.get(0+vPos)) / viewport.get(2+vPos); + in[1] = (in[1] - viewport.get(1+vPos)) / viewport.get(3+vPos); + in[2] = (in[2] - near) / (far - near); + // 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); + in[0] = in[0] * 2 - 1; + in[1] = in[1] * 2 - 1; + in[2] = in[2] * 2 - 1; FloatUtil.multMatrixVecf(matrixBuf, in, out); - if (out.get(3) == 0.0f) + if (out[3] == 0.0f) { return false; + } - int oPos = obj_pos.position(); - obj_pos.put(0+oPos, out.get(0)); - obj_pos.put(1+oPos, out.get(1)); - obj_pos.put(2+oPos, out.get(2)); - obj_pos.put(3+oPos, out.get(3)); + final int oPos = obj_pos.position(); + obj_pos.put(0+oPos, out[0]); + obj_pos.put(1+oPos, out[1]); + obj_pos.put(2+oPos, out[2]); + obj_pos.put(3+oPos, out[3]); return true; } @@ -919,7 +898,7 @@ public class ProjectFloat { } /* Translate and scale the picked region to the entire window */ - int vPos = viewport.position(); + final int vPos = viewport.position(); gl.glTranslatef((viewport.get(2+vPos) - 2 * (x - viewport.get(0+vPos))) / deltaX, (viewport.get(3+vPos) - 2 * (y - viewport.get(1+vPos))) / deltaY, 0); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestPMVMatrix02NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestPMVMatrix02NEWT.java new file mode 100644 index 000000000..4e959d460 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestPMVMatrix02NEWT.java @@ -0,0 +1,109 @@ +/**
+ * Copyright 2012 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+
+package com.jogamp.opengl.test.junit.jogl.acore;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.jogamp.opengl.util.PMVMatrix;
+
+import javax.media.opengl.fixedfunc.GLMatrixFunc;
+import java.nio.FloatBuffer;
+
+import static org.junit.Assert.assertArrayEquals;
+
+/**
+ * @author Thomas De Bodt
+ */
+public class TestPMVMatrix02NEWT {
+
+ private PMVMatrix fMat;
+
+ @Before
+ public void setUp() throws Exception {
+ fMat = new PMVMatrix();
+ }
+
+ @Test
+ public void testLookAtNegZIsNoOp() throws Exception {
+ fMat.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+ // Look towards -z
+ fMat.gluLookAt(
+ 0, 0, 0,
+ 0, 0, -1,
+ 0, 1, 0
+ );
+ FloatBuffer mvMatrix = fMat.glGetMvMatrixf();
+ float[] mvMatrixArr = new float[16];
+ mvMatrix.asReadOnlyBuffer().get(mvMatrixArr);
+ assertArrayEquals(
+ /**
+ * The 3 rows of the matrix (= the 3 columns of the array/buffer) should be: side, up, -forward.
+ */
+ new float[] {
+ 1, 0, 0, 0,
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1
+ },
+ mvMatrixArr,
+ 1e-6f
+ );
+ }
+ @Test
+ public void testLookAtPosY() throws Exception {
+ fMat.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+ // Look towards +y
+ fMat.gluLookAt(
+ 0, 0, 0,
+ 0, 1, 0,
+ 0, 0, 1
+ );
+ FloatBuffer mvMatrix = fMat.glGetMvMatrixf();
+ float[] mvMatrixArr = new float[16];
+ mvMatrix.asReadOnlyBuffer().get(mvMatrixArr);
+ assertArrayEquals(
+ /**
+ * The 3 rows of the matrix (= the 3 columns of the array/buffer) should be: side, up, -forward.
+ */
+ new float[] {
+ 1, 0, 0, 0,
+ 0, 0, -1, 0,
+ 0, 1, 0, 0,
+ 0, 0, 0, 1
+ },
+ mvMatrixArr,
+ 1e-6f
+ );
+ }
+
+ public static void main(String args[]) {
+ org.junit.runner.JUnitCore.main(TestPMVMatrix02NEWT.class.getName());
+ }
+}
|