aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2012-05-11 22:38:56 +0200
committerSven Gothel <[email protected]>2012-05-11 22:38:56 +0200
commitcbc77718f01a8190e1a8aa0e9afdc2a3a3403358 (patch)
tree5db389ae484b24965e5a0feb3448b1c5563be163 /src
parentd887fa4c36c97676d845d2d95c33025ad8f9f006 (diff)
Fix regression of commit de2b129a56335262a44a05541a3ab2e35668cc6e: ProjectFloat Matrix Multiplication of gluUnProject(..) impl.
ProjectFloat's previous gluMultMatricesf(..) used row-major order, but the replacement multMatrixf(..) uses column-major order (like OpenGL, ..). Note: The replaced 'gluMultMatrixVecf' by multMatrixVecf() already used column-major order. Fix: Reverse the arguments of matrix multiplication m1 x m2 -> m2 x m1 Added proper API documentation in FloatUtil -> Column Major Order of Linear Matrix Layout
Diffstat (limited to 'src')
-rw-r--r--src/jogl/classes/com/jogamp/opengl/FloatUtil.java75
-rw-r--r--src/jogl/classes/jogamp/opengl/ProjectFloat.java10
-rwxr-xr-xsrc/test/com/jogamp/opengl/test/junit/jogl/acore/TestFloatUtil01MatrixMatrixMultNOUI.java113
3 files changed, 158 insertions, 40 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/FloatUtil.java b/src/jogl/classes/com/jogamp/opengl/FloatUtil.java
index 54fac46e6..93543eaf6 100644
--- a/src/jogl/classes/com/jogamp/opengl/FloatUtil.java
+++ b/src/jogl/classes/com/jogamp/opengl/FloatUtil.java
@@ -32,6 +32,10 @@ import java.nio.FloatBuffer;
/**
* Basic Float math utility functions.
* <p>
+ * Implementation assumes linear matrix layout in column-major order
+ * matching OpenGL's implementation.
+ * </p>
+ * <p>
* Derived from ProjectFloat.java - Created 11-jan-2004
* </p>
*
@@ -91,29 +95,31 @@ public class FloatUtil {
}
/**
- * @param a
- * @param b
- * @param d result a*b
+ * @param a 4x4 matrix in column-major order
+ * @param b 4x4 matrix in column-major order
+ * @param d result a*b in column-major order
*/
public static final void multMatrixf(final float[] a, int a_off, final float[] b, int b_off, float[] d, int d_off) {
for (int i = 0; i < 4; i++) {
- final float ai0=a[a_off+i+0*4], ai1=a[a_off+i+1*4], ai2=a[a_off+i+2*4], ai3=a[a_off+i+3*4];
+ // one row in column-major order
+ final float ai0=a[a_off+i+0*4], ai1=a[a_off+i+1*4], ai2=a[a_off+i+2*4], ai3=a[a_off+i+3*4]; // row-i of a
d[d_off+i+0*4] = ai0 * b[b_off+0+0*4] + ai1 * b[b_off+1+0*4] + ai2 * b[b_off+2+0*4] + ai3 * b[b_off+3+0*4] ;
d[d_off+i+1*4] = ai0 * b[b_off+0+1*4] + ai1 * b[b_off+1+1*4] + ai2 * b[b_off+2+1*4] + ai3 * b[b_off+3+1*4] ;
d[d_off+i+2*4] = ai0 * b[b_off+0+2*4] + ai1 * b[b_off+1+2*4] + ai2 * b[b_off+2+2*4] + ai3 * b[b_off+3+2*4] ;
d[d_off+i+3*4] = ai0 * b[b_off+0+3*4] + ai1 * b[b_off+1+3*4] + ai2 * b[b_off+2+3*4] + ai3 * b[b_off+3+3*4] ;
}
}
-
+
/**
- * @param a
- * @param b
- * @param d result a*b
+ * @param a 4x4 matrix in column-major order
+ * @param b 4x4 matrix in column-major order
+ * @param d result a*b in column-major order
*/
public static final void multMatrixf(final float[] a, int a_off, final float[] b, int b_off, FloatBuffer d) {
final int dP = d.position();
for (int i = 0; i < 4; i++) {
- final float ai0=a[a_off+i+0*4], ai1=a[a_off+i+1*4], ai2=a[a_off+i+2*4], ai3=a[a_off+i+3*4];
+ // one row in column-major order
+ final float ai0=a[a_off+i+0*4], ai1=a[a_off+i+1*4], ai2=a[a_off+i+2*4], ai3=a[a_off+i+3*4]; // row-i of a
d.put(dP+i+0*4 , ai0 * b[b_off+0+0*4] + ai1 * b[b_off+1+0*4] + ai2 * b[b_off+2+0*4] + ai3 * b[b_off+3+0*4] );
d.put(dP+i+1*4 , ai0 * b[b_off+0+1*4] + ai1 * b[b_off+1+1*4] + ai2 * b[b_off+2+1*4] + ai3 * b[b_off+3+1*4] );
d.put(dP+i+2*4 , ai0 * b[b_off+0+2*4] + ai1 * b[b_off+1+2*4] + ai2 * b[b_off+2+2*4] + ai3 * b[b_off+3+2*4] );
@@ -122,15 +128,16 @@ public class FloatUtil {
}
/**
- * @param a
- * @param b
- * @param d result a*b
+ * @param a 4x4 matrix in column-major order
+ * @param b 4x4 matrix in column-major order
+ * @param d result a*b in column-major order
*/
public static final void multMatrixf(final FloatBuffer a, final float[] b, int b_off, FloatBuffer d) {
final int aP = a.position();
final int dP = d.position();
for (int i = 0; i < 4; i++) {
- final float ai0=a.get(aP+i+0*4), ai1=a.get(aP+i+1*4), ai2=a.get(aP+i+2*4), ai3=a.get(aP+i+3*4);
+ // one row in column-major order
+ final float ai0=a.get(aP+i+0*4), ai1=a.get(aP+i+1*4), ai2=a.get(aP+i+2*4), ai3=a.get(aP+i+3*4); // row-i of a
d.put(dP+i+0*4 , ai0 * b[b_off+0+0*4] + ai1 * b[b_off+1+0*4] + ai2 * b[b_off+2+0*4] + ai3 * b[b_off+3+0*4] );
d.put(dP+i+1*4 , ai0 * b[b_off+0+1*4] + ai1 * b[b_off+1+1*4] + ai2 * b[b_off+2+1*4] + ai3 * b[b_off+3+1*4] );
d.put(dP+i+2*4 , ai0 * b[b_off+0+2*4] + ai1 * b[b_off+1+2*4] + ai2 * b[b_off+2+2*4] + ai3 * b[b_off+3+2*4] );
@@ -139,16 +146,17 @@ public class FloatUtil {
}
/**
- * @param a
- * @param b
- * @param d result a*b
+ * @param a 4x4 matrix in column-major order
+ * @param b 4x4 matrix in column-major order
+ * @param d result a*b in column-major order
*/
public static final void multMatrixf(final FloatBuffer a, final FloatBuffer b, FloatBuffer d) {
final int aP = a.position();
final int bP = b.position();
final int dP = d.position();
for (int i = 0; i < 4; i++) {
- final float ai0=a.get(aP+i+0*4), ai1=a.get(aP+i+1*4), ai2=a.get(aP+i+2*4), ai3=a.get(aP+i+3*4);
+ // one row in column-major order
+ final float ai0=a.get(aP+i+0*4), ai1=a.get(aP+i+1*4), ai2=a.get(aP+i+2*4), ai3=a.get(aP+i+3*4); // row-i of a
d.put(dP+i+0*4 , ai0 * b.get(bP+0+0*4) + ai1 * b.get(bP+1+0*4) + ai2 * b.get(bP+2+0*4) + ai3 * b.get(bP+3+0*4) );
d.put(dP+i+1*4 , ai0 * b.get(bP+0+1*4) + ai1 * b.get(bP+1+1*4) + ai2 * b.get(bP+2+1*4) + ai3 * b.get(bP+3+1*4) );
d.put(dP+i+2*4 , ai0 * b.get(bP+0+2*4) + ai1 * b.get(bP+1+2*4) + ai2 * b.get(bP+2+2*4) + ai3 * b.get(bP+3+2*4) );
@@ -201,10 +209,10 @@ public class FloatUtil {
/**
- * Calculate cross-product
+ * Calculate cross-product of 2 vector
*
- * @param v1
- * @param v2
+ * @param v1 3-component vector
+ * @param v2 3-component vector
* @param result v1 X v2
*/
public static final void cross(float[] v1, float[] v2, float[] result) {
@@ -214,10 +222,10 @@ public class FloatUtil {
}
/**
- * Calculate cross-product
+ * Calculate cross-product of 2 vector
*
- * @param v1
- * @param v2
+ * @param v1 3-component vector
+ * @param v2 3-component vector
* @param result v1 X v2
*/
public static final void cross(FloatBuffer v1, FloatBuffer v2, FloatBuffer result) {
@@ -231,15 +239,14 @@ public class FloatUtil {
}
/**
- * Method __gluMultMatrixVecf
- *
- * @param m_in
+ * @param m_in 4x4 matrix in column-major order
* @param m_in_off
- * @param v_in
+ * @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) {
for (int i = 0; i < 4; i++) {
+ // (one matrix row in column-major order) X (column vector)
v_out[i] =
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] +
@@ -249,15 +256,14 @@ public class FloatUtil {
}
/**
- * Method __gluMultMatrixVecf
- *
- * @param m_in
+ * @param m_in 4x4 matrix in column-major order
* @param m_in_off
- * @param v_in
+ * @param v_in 4-component column-vector
* @param v_out m_in * v_in
*/
public static final void multMatrixVecf(float[] m_in, float[] v_in, float[] v_out) {
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[0*4+i] +
v_in[1] * m_in[1*4+i] +
@@ -267,10 +273,8 @@ public class FloatUtil {
}
/**
- * Method __gluMultMatrixVecf
- *
- * @param m_in
- * @param v_in
+ * @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) {
@@ -278,6 +282,7 @@ public class FloatUtil {
int outPos = v_out.position();
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,
v_in.get(0+inPos) * m_in.get(0*4+i+matrixPos) +
v_in.get(1+inPos) * m_in.get(1*4+i+matrixPos) +
diff --git a/src/jogl/classes/jogamp/opengl/ProjectFloat.java b/src/jogl/classes/jogamp/opengl/ProjectFloat.java
index bce3f3cfc..ce8405f74 100644
--- a/src/jogl/classes/jogamp/opengl/ProjectFloat.java
+++ b/src/jogl/classes/jogamp/opengl/ProjectFloat.java
@@ -615,7 +615,7 @@ public class ProjectFloat {
float[] in = this.in;
float[] out = this.out;
- FloatUtil.multMatrixf(modelMatrix, modelMatrix_offset, projMatrix, projMatrix_offset, matrix, 0);
+ FloatUtil.multMatrixf(projMatrix, projMatrix_offset, modelMatrix, modelMatrix_offset, matrix, 0);
if (!gluInvertMatrixf(matrix, 0, matrix, 0)) {
return false;
@@ -659,7 +659,7 @@ public class ProjectFloat {
FloatBuffer in = this.inBuf;
FloatBuffer out = this.outBuf;
- FloatUtil.multMatrixf(modelMatrix, projMatrix, matrixBuf);
+ FloatUtil.multMatrixf(projMatrix, modelMatrix, matrixBuf);
if (!gluInvertMatrixf(matrixBuf, matrixBuf)) {
return false;
@@ -715,7 +715,7 @@ public class ProjectFloat {
FloatBuffer in = this.inBuf;
FloatBuffer out = this.outBuf;
- FloatUtil.multMatrixf(modelMatrix, projMatrix, matrixBuf);
+ FloatUtil.multMatrixf(projMatrix, modelMatrix, matrixBuf);
if (!gluInvertMatrixf(matrixBuf, matrixBuf)) {
return false;
@@ -786,7 +786,7 @@ public class ProjectFloat {
float[] in = this.in;
float[] out = this.out;
- FloatUtil.multMatrixf(modelMatrix, modelMatrix_offset, projMatrix, projMatrix_offset, matrix, 0);
+ FloatUtil.multMatrixf(projMatrix, projMatrix_offset, modelMatrix, modelMatrix_offset, matrix, 0);
if (!gluInvertMatrixf(matrix, 0, matrix, 0))
return false;
@@ -847,7 +847,7 @@ public class ProjectFloat {
FloatBuffer in = this.inBuf;
FloatBuffer out = this.outBuf;
- FloatUtil.multMatrixf(modelMatrix, projMatrix, matrixBuf);
+ FloatUtil.multMatrixf(projMatrix, modelMatrix, matrixBuf);
if (!gluInvertMatrixf(matrixBuf, matrixBuf))
return false;
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestFloatUtil01MatrixMatrixMultNOUI.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestFloatUtil01MatrixMatrixMultNOUI.java
new file mode 100755
index 000000000..42b5972bb
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestFloatUtil01MatrixMatrixMultNOUI.java
@@ -0,0 +1,113 @@
+/**
+ * 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.Assert;
+import org.junit.Test;
+
+import com.jogamp.opengl.FloatUtil;
+
+public class TestFloatUtil01MatrixMatrixMultNOUI {
+
+ final float[] m1 = new float[]{ 1, 3, 4, 0,
+ 6, 7, 8, 5,
+ 98, 7, 6, 9,
+ 54, 3, 2, 5 };
+
+ final float[] m2 = new float[]{ 1, 6, 98, 54,
+ 3, 7, 7, 3,
+ 4, 8, 6, 2,
+ 0, 5, 9, 5 };
+
+ final float[] m1xm2_RM = // m2xm1_CM
+ new float[]{ 26, 59, 143, 71,
+ 59, 174, 730, 386,
+ 143, 730, 9770, 5370,
+ 71, 386, 5370, 2954 };
+
+ final float[] m2xm1_RM = // m1xm2_CM
+ new float[]{12557, 893, 748, 1182,
+ 893, 116, 116, 113,
+ 748, 116, 120, 104,
+ 1182, 113, 104, 131 };
+
+ public static final void multMatrixf_RM(final float[] a, int a_off, final float[] b, int b_off, float[] d, int d_off) {
+ for (int i = 0; i < 4; i++) {
+ final float ai0=a[a_off+i*4+0], ai1=a[a_off+i*4+1], ai2=a[a_off+i*4+2], ai3=a[a_off+i*4+3];
+ d[d_off+i*4+0] = ai0 * b[b_off+0*4+0] + ai1 * b[b_off+1*4+0] + ai2 * b[b_off+2*4+0] + ai3 * b[b_off+3*4+0] ;
+ d[d_off+i*4+1] = ai0 * b[b_off+0*4+1] + ai1 * b[b_off+1*4+1] + ai2 * b[b_off+2*4+1] + ai3 * b[b_off+3*4+1] ;
+ d[d_off+i*4+2] = ai0 * b[b_off+0*4+2] + ai1 * b[b_off+1*4+2] + ai2 * b[b_off+2*4+2] + ai3 * b[b_off+3*4+2] ;
+ d[d_off+i*4+3] = ai0 * b[b_off+0*4+3] + ai1 * b[b_off+1*4+3] + ai2 * b[b_off+2*4+3] + ai3 * b[b_off+3*4+3] ;
+ }
+ }
+
+ @Test
+ public void testCM_m1xm2(){
+
+ float[] r = new float[16];
+
+ FloatUtil.multMatrixf(m1, 0, m2, 0, r, 0);
+
+ Assert.assertArrayEquals(m2xm1_RM, r, 0f);
+ }
+
+ @Test
+ public void testCM_m2xm1(){
+
+ float[] r = new float[16];
+
+ FloatUtil.multMatrixf(m2, 0, m1, 0, r, 0);
+
+ Assert.assertArrayEquals(m1xm2_RM, r, 0f);
+ }
+
+ @Test
+ public void testRM_m1xm2(){
+
+ float[] r = new float[16];
+
+ multMatrixf_RM(m1, 0, m2, 0, r, 0);
+
+ Assert.assertArrayEquals(m1xm2_RM, r, 0f);
+ }
+
+ @Test
+ public void testRM_m2xm1(){
+
+ float[] r = new float[16];
+
+ multMatrixf_RM(m2, 0, m1, 0, r, 0);
+
+ Assert.assertArrayEquals(m2xm1_RM, r, 0f);
+ }
+
+ public static void main(String args[]) {
+ org.junit.runner.JUnitCore.main(TestFloatUtil01MatrixMatrixMultNOUI.class.getName());
+ }
+}