diff options
author | Sven Gothel <[email protected]> | 2012-10-10 15:09:09 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-10-10 15:09:09 +0200 |
commit | 6ac1c8c8995458671cf603e46bff89fcaefd8146 (patch) | |
tree | ea20f2d9eaa16919b4a271933b3803a5274d45e9 /src | |
parent | 7587cce91c3fda7dcff4e36e1aa0edf53bf34e00 (diff) |
FloatUtil/PMVMatrix/GLUniformData: Move impl. of FloatBuffer matrix toString(..) from PMVMatrix to FloatUtil and make it more generic; GLUniformData toString() also dumps it's matrices.
Diffstat (limited to 'src')
4 files changed, 181 insertions, 64 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/FloatUtil.java b/src/jogl/classes/com/jogamp/opengl/FloatUtil.java index 93543eaf6..297cec285 100644 --- a/src/jogl/classes/com/jogamp/opengl/FloatUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/FloatUtil.java @@ -29,6 +29,8 @@ package com.jogamp.opengl; import java.nio.FloatBuffer; +import com.jogamp.common.os.Platform; + /** * Basic Float math utility functions. * <p> @@ -291,4 +293,84 @@ public class FloatUtil { } } + /** + * @param sb optional passed StringBuilder instance to be used + * @param f the format string of one floating point, i.e. "%10.5f", see {@link java.util.Formatter} + * @param a mxn matrix (rows x columns) + * @param aOffset offset to <code>a</code>'s current position + * @param rows + * @param columns + * @param rowMajorOrder if true floats are layed out in row-major-order, otherwise column-major-order (OpenGL) + * @param row row number to print + * @return matrix row string representation + */ + public static StringBuilder matrixRowToString(StringBuilder sb, String f, FloatBuffer a, int aOffset, int rows, int columns, boolean rowMajorOrder, int row) { + if(null == sb) { + sb = new StringBuilder(); + } + final int a0 = aOffset + a.position(); + if(rowMajorOrder) { + for(int c=0; c<columns; c++) { + sb.append( String.format( f+" ", a.get( a0 + row*columns + c ) ) ); + } + } else { + for(int r=0; r<columns; r++) { + sb.append( String.format( f+" ", a.get( a0 + row + r*rows ) ) ); + } + } + return sb; + } + + /** + * @param sb optional passed StringBuilder instance to be used + * @param rowPrefix optional prefix for each row + * @param f the format string of one floating point, i.e. "%10.5f", see {@link java.util.Formatter} + * @param a mxn matrix (rows x columns) + * @param aOffset offset to <code>a</code>'s current position + * @param rows + * @param columns + * @param rowMajorOrder if true floats are layed out in row-major-order, otherwise column-major-order (OpenGL) + * @return matrix string representation + */ + public static StringBuilder matrixToString(StringBuilder sb, String rowPrefix, String f, FloatBuffer a, int aOffset, int rows, int columns, boolean rowMajorOrder) { + if(null == sb) { + sb = new StringBuilder(); + } + final String prefix = ( null == rowPrefix ) ? "" : rowPrefix; + for(int i=0; i<rows; i++) { + sb.append(prefix).append("[ "); + matrixRowToString(sb, f, a, aOffset, rows, columns, rowMajorOrder, i); + sb.append("]").append(Platform.getNewline()); + } + return sb; + } + + /** + * @param sb optional passed StringBuilder instance to be used + * @param rowPrefix optional prefix for each row + * @param f the format string of one floating point, i.e. "%10.5f", see {@link java.util.Formatter} + * @param a 4x4 matrix in column major order (OpenGL) + * @param aOffset offset to <code>a</code>'s current position + * @param b 4x4 matrix in column major order (OpenGL) + * @param bOffset offset to <code>a</code>'s current position + * @param rows + * @param columns + * @param rowMajorOrder if true floats are layed out in row-major-order, otherwise column-major-order (OpenGL) + * @return side by side representation + */ + public static StringBuilder matrixToString(StringBuilder sb, String rowPrefix, String f, FloatBuffer a, int aOffset, FloatBuffer b, int bOffset, int rows, int columns, boolean rowMajorOrder) { + if(null == sb) { + sb = new StringBuilder(); + } + final String prefix = ( null == rowPrefix ) ? "" : rowPrefix; + for(int i=0; i<rows; i++) { + sb.append(prefix).append("[ "); + matrixRowToString(sb, f, a, aOffset, rows, columns, rowMajorOrder, i); + sb.append("=?= "); + matrixRowToString(sb, f, b, bOffset, rows, columns, rowMajorOrder, i); + sb.append("]").append(Platform.getNewline()); + } + return sb; + } + }
\ No newline at end of file diff --git a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java index 686dd3895..70c4d1e1b 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java +++ b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java @@ -169,55 +169,11 @@ public class PMVMatrix implements GLMatrixFunc { /** * @param sb optional passed StringBuilder instance to be used * @param f the format string of one floating point, i.e. "%10.5f", see {@link java.util.Formatter} - * @param row row number - * @param a 4x4 matrix in column major order (OpenGL) - * @return matrix row string representation - */ - public static StringBuilder matrixRowToString(StringBuilder sb, String f, int row, FloatBuffer a) { - if(null == sb) { - sb = new StringBuilder(); - } - final int a0 = a.position(); - sb.append( String.format("[ "+f+" "+f+" "+f+" "+f+" ]", - a.get(a0+row+0*4), a.get(a0+row+1*4), a.get(a0+row+2*4), a.get(a0+row+3*4) ) ); - return sb; - } - - /** - * @param sb optional passed StringBuilder instance to be used - * @param f the format string of one floating point, i.e. "%10.5f", see {@link java.util.Formatter} * @param a 4x4 matrix in column major order (OpenGL) * @return matrix string representation */ public static StringBuilder matrixToString(StringBuilder sb, String f, FloatBuffer a) { - if(null == sb) { - sb = new StringBuilder(); - } - matrixRowToString(sb, f, 0, a).append(Platform.getNewline()); - matrixRowToString(sb, f, 1, a).append(Platform.getNewline()); - matrixRowToString(sb, f, 2, a).append(Platform.getNewline()); - matrixRowToString(sb, f, 3, a).append(Platform.getNewline()); - return sb; - } - - /** - * @param sb optional passed StringBuilder instance to be used - * @param f the format string of one floating point, i.e. "%10.5f", see {@link java.util.Formatter} - * @param row row number - * @param a 4x4 matrix in column major order (OpenGL) - * @param b 4x4 matrix in column major order (OpenGL) - * @return matrix row string representation side by side - */ - public static StringBuilder matrixRowToString(StringBuilder sb, String f, int row, FloatBuffer a, FloatBuffer b) { - if(null == sb) { - sb = new StringBuilder(); - } - final int a0 = a.position(); - final int b0 = b.position(); - sb.append( String.format("[ "+f+" "+f+" "+f+" "+f+" =?= "+f+" "+f+" "+f+" "+f+" ]", - a.get(a0+row+0*4), a.get(a0+row+1*4), a.get(a0+row+2*4), a.get(a0+row+3*4), - b.get(b0+row+0*4), b.get(b0+row+1*4), b.get(b0+row+2*4), b.get(b0+row+3*4) ) ); - return sb; + return FloatUtil.matrixToString(sb, null, f, a, 0, 4, 4, false); } /** @@ -228,14 +184,7 @@ public class PMVMatrix implements GLMatrixFunc { * @return side by side representation */ public static StringBuilder matrixToString(StringBuilder sb, String f, FloatBuffer a, FloatBuffer b) { - if(null == sb) { - sb = new StringBuilder(); - } - matrixRowToString(sb, f, 0, a, b).append(Platform.getNewline()); - matrixRowToString(sb, f, 1, a, b).append(Platform.getNewline()); - matrixRowToString(sb, f, 2, a, b).append(Platform.getNewline()); - matrixRowToString(sb, f, 3, a, b).append(Platform.getNewline()); - return sb; + return FloatUtil.matrixToString(sb, null, f, a, 0, b, 0, 4, 4, false); } /** diff --git a/src/jogl/classes/javax/media/opengl/GLUniformData.java b/src/jogl/classes/javax/media/opengl/GLUniformData.java index 475ff4546..095115ff8 100644 --- a/src/jogl/classes/javax/media/opengl/GLUniformData.java +++ b/src/jogl/classes/javax/media/opengl/GLUniformData.java @@ -3,6 +3,9 @@ package javax.media.opengl; import java.nio.*; +import com.jogamp.common.nio.Buffers; +import com.jogamp.opengl.FloatUtil; + public class GLUniformData { /** @@ -69,14 +72,33 @@ public class GLUniformData { public IntBuffer intBufferValue() { return (IntBuffer)data; }; public FloatBuffer floatBufferValue() { return (FloatBuffer)data; }; + public StringBuilder toString(StringBuilder sb) { + if(null == sb) { + sb = new StringBuilder(); + } + sb.append("GLUniformData[name ").append(name). + append(", location ").append(location). + append(", size ").append(rows).append("x").append(columns). + append(", count ").append(count). + append(", data "); + if(isMatrix() && data instanceof FloatBuffer) { + sb.append("\n"); + final FloatBuffer fb = (FloatBuffer)getBuffer(); + for(int i=0; i<count; i++) { + FloatUtil.matrixToString(sb, i+": ", "%10.5f", fb, 0, rows, columns, false); + sb.append(",\n"); + } + } else if(isBuffer()) { + Buffers.toString(sb, getBuffer()); + } else { + sb.append(data); + } + sb.append("]"); + return sb; + } + public String toString() { - return "GLUniformData[name "+name+ - ", location "+location+ - ", size "+rows+"*"+columns+ - ", count "+count+ - ", matrix "+isMatrix+ - ", data "+data+ - "]"; + return toString(null).toString(); } private void init(String name, int rows, int columns, Object data) { diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestPMVMatrix01NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestPMVMatrix01NEWT.java index 18e9df5a0..c44a82a20 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestPMVMatrix01NEWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestPMVMatrix01NEWT.java @@ -44,6 +44,7 @@ import org.junit.BeforeClass; import org.junit.Test; import com.jogamp.common.os.Platform; +import com.jogamp.opengl.FloatUtil; import com.jogamp.opengl.test.junit.util.MiscUtils; import com.jogamp.opengl.test.junit.util.UITestCase; import com.jogamp.opengl.util.PMVMatrix; @@ -51,8 +52,33 @@ import com.jogamp.opengl.util.PMVMatrix; public class TestPMVMatrix01NEWT extends UITestCase { static final float epsilon = 0.00001f; - // Translated xyz 123 - Normal - In column major order ! - static FloatBuffer translated123N = FloatBuffer.wrap( new float[] { 1.0f, 0.0f, 0.0f, 0.0f, + + // matrix 2 rows x 3 columns - In row major order + static FloatBuffer matrix2x3R = FloatBuffer.wrap( new float[] { 1.0f, 2.0f, 3.0f, + 4.0f, 5.0f, 6.0f } ); + + // matrix 2 rows x 3 columns - In column major order + static FloatBuffer matrix2x3C = FloatBuffer.wrap( new float[] { 1.0f, 4.0f, + 2.0f, 5.0f, + 3.0f, 6.0f } ); + + // matrix 3 rows x 2 columns - In row major order + static FloatBuffer matrix3x2R = FloatBuffer.wrap( new float[] { 1.0f, 2.0f, + 3.0f, 4.0f, + 5.0f, 6.0f } ); + + // matrix 3 rows x 2 columns - In column major order + static FloatBuffer matrix3x2C = FloatBuffer.wrap( new float[] { 1.0f, 3.0f, 5.0f, + 2.0f, 4.0f, 6.0f } ); + + // Translated xyz 123 - Row - In row major order ! + static FloatBuffer translated123R = FloatBuffer.wrap( new float[] { 1.0f, 0.0f, 0.0f, 1.0f, + 0.0f, 1.0f, 0.0f, 2.0f, + 0.0f, 0.0f, 1.0f, 3.0f, + 0.0f, 0.0f, 0.0f, 1.0f } ); + + // Translated xyz 123 - Column - In column major order ! + static FloatBuffer translated123C = FloatBuffer.wrap( new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 2.0f, 3.0f, 1.0f } ); @@ -69,6 +95,44 @@ public class TestPMVMatrix01NEWT extends UITestCase { 0.0f, 0.0f, 1.0f, -3.0f, 0.0f, 0.0f, 0.0f, 1.0f } ); + @Test + public void test00MatrixToString() { + final String s4x4Cpmv = PMVMatrix.matrixToString(null, "%10.5f", translated123C).toString(); + final String s4x4Cflu = FloatUtil.matrixToString(null, null, "%10.5f", translated123C, 0, 4, 4, false).toString(); + final String s4x4Rflu = FloatUtil.matrixToString(null, null, "%10.5f", translated123R, 0, 4, 4, true).toString(); + System.err.println("PMV-C-O 4x4: "); + System.err.println(s4x4Cpmv); + System.err.println(); + System.err.println("FLU-C-O 4x4: "); + System.err.println(s4x4Cflu); + System.err.println(); + System.err.println("FLU-R-O 4x4: "); + System.err.println(s4x4Rflu); + System.err.println(); + Assert.assertEquals(s4x4Cpmv, s4x4Cflu); + Assert.assertEquals(s4x4Cflu, s4x4Rflu); + + final String s2x3Rflu = FloatUtil.matrixToString(null, null, "%10.5f", matrix2x3R, 0, 2, 3, true).toString(); + final String s2x3Cflu = FloatUtil.matrixToString(null, null, "%10.5f", matrix2x3C, 0, 2, 3, false).toString(); + System.err.println("FLU-R-O 2x3: "); + System.err.println(s2x3Rflu); + System.err.println(); + System.err.println("FLU-C-O 2x3: "); + System.err.println(s2x3Cflu); + System.err.println(); + Assert.assertEquals(s2x3Cflu, s2x3Rflu); + + final String s3x2Rflu = FloatUtil.matrixToString(null, null, "%10.5f", matrix3x2R, 0, 3, 2, true).toString(); + final String s3x2Cflu = FloatUtil.matrixToString(null, null, "%10.5f", matrix3x2C, 0, 3, 2, false).toString(); + System.err.println("FLU-R-O 3x2: "); + System.err.println(s3x2Rflu); + System.err.println(); + System.err.println("FLU-C-O 3x2: "); + System.err.println(s3x2Cflu); + System.err.println(); + Assert.assertEquals(s3x2Cflu, s3x2Rflu); + } + /** * Test using traditional access workflow, i.e. 1) operation 2) get-matrix references * <p> @@ -123,7 +187,7 @@ public class TestPMVMatrix01NEWT extends UITestCase { p = pmv.glGetPMatrixf(); MiscUtils.assertFloatBufferEquals("P not identity, "+pmv.toString(), ident, p, epsilon); mv = pmv.glGetMvMatrixf(); - MiscUtils.assertFloatBufferEquals("Mv not translated123, "+pmv.toString(), translated123N, mv, epsilon); + MiscUtils.assertFloatBufferEquals("Mv not translated123, "+pmv.toString(), translated123C, mv, epsilon); mvi = pmv.glGetMviMatrixf(); MiscUtils.assertFloatBufferEquals("Mvi not translated123, "+pmv.toString(), translated123I, mvi, epsilon); Assert.assertEquals("Remaining dirty bits not Mvit, "+pmv.toString(), PMVMatrix.DIRTY_INVERSE_TRANSPOSED_MODELVIEW, pmv.getDirtyBits()); @@ -220,7 +284,7 @@ public class TestPMVMatrix01NEWT extends UITestCase { Assert.assertTrue("Modified bits zero", 0 != pmv.getModifiedBits(true)); // clear & test Assert.assertTrue("Dirty bits clean, "+pmv.toString(), 0 != pmv.getDirtyBits()); MiscUtils.assertFloatBufferEquals("P not identity, "+pmv.toString()+pmv.toString(), ident, p, epsilon); - MiscUtils.assertFloatBufferEquals("Mv not translated123, "+pmv.toString()+pmv.toString(), translated123N, mv, epsilon); + MiscUtils.assertFloatBufferEquals("Mv not translated123, "+pmv.toString()+pmv.toString(), translated123C, mv, epsilon); MiscUtils.assertFloatBufferNotEqual("Mvi already translated123 w/o update, "+pmv.toString()+pmv.toString(), translated123I, mvi, epsilon); MiscUtils.assertFloatBufferNotEqual("Mvit already translated123 w/o update, "+pmv.toString()+pmv.toString(), translated123IT, mvit, epsilon); MiscUtils.assertFloatBufferEquals("Mvi not identity, "+pmv.toString()+pmv.toString(), ident, mvi, epsilon); |