diff options
author | Sven Gothel <sgothel@jausoft.com> | 2023-04-07 08:46:18 +0200 |
---|---|---|
committer | Sven Gothel <sgothel@jausoft.com> | 2023-04-07 08:46:18 +0200 |
commit | 84a6d63205ec49ddfb36b57fe2888425ecda3a5a (patch) | |
tree | 354cec2ac14a8932a01122c5234926e774ef874e /src/jogl/classes/com/jogamp/opengl/math | |
parent | 10b60e10ece3cbc3e0b8a68ac73229371530e0ba (diff) |
PMVMatrix rewrite using Matrix4f, providing SyncMatrix4f* for GLUniformData; Utilize Vec3f, Recti, .. throughout API (Matrix4f, AABBox, .. Graph*)
Big Easter Cleanup
- Net -214 lines of code, despite new classes.
- GLUniformData buffer can be synced w/ underlying data via SyncAction/SyncBuffer, e.g. SyncMatrix4f + SyncMatrices4f
- PMVMatrix rewrite using Matrix4f and providing SyncMatrix4f/Matrices4f to sync w/ GLUniformData
- Additional SyncMatrix4f16 + SyncMatrices4f16 covering Matrix4f sync w/ GLUniformData w/o PMVMatrix
- Utilize Vec3f, Recti, .. throughout API (Matrix4f, AABBox, .. Graph*)
- Moved FloatUtil -> Matrix4f, kept a few basic matrix ops for ProjectFloat
- Most, if not all, float[] and int[] should have been moved to proper classes
- int[] -> Recti for viewport rectangle
- Matrix4f and PMVMatrix is covered by math unit tests (as was FloatUtil before) -> save
Passed all unit tests on AMD64 GNU/Linux
Diffstat (limited to 'src/jogl/classes/com/jogamp/opengl/math')
10 files changed, 527 insertions, 1036 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java b/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java index 9ffa3bba6..9ef09d8c5 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java @@ -35,8 +35,6 @@ import com.jogamp.opengl.GLException; import jogamp.opengl.Debug; import com.jogamp.common.os.Platform; -import com.jogamp.opengl.math.geom.AABBox; -import com.jogamp.opengl.math.geom.Frustum; /** * Basic Float math utility functions. @@ -87,40 +85,12 @@ public final class FloatUtil { // // Matrix Ops + // Only a subset will remain here, try using Matrix4f and perhaps PMVMatrix, SyncMatrix4f16 or SyncMatrices4f16 // /** * Make matrix an identity matrix * @param m 4x4 matrix in column-major order (also result) - * @param m_offset offset in given array <i>m</i>, i.e. start of the 4x4 matrix - * @return given matrix for chaining - */ - public static float[] makeIdentity(final float[] m, final int m_offset) { - m[m_offset+0+4*0] = 1f; - m[m_offset+1+4*0] = 0f; - m[m_offset+2+4*0] = 0f; - m[m_offset+3+4*0] = 0f; - - m[m_offset+0+4*1] = 0f; - m[m_offset+1+4*1] = 1f; - m[m_offset+2+4*1] = 0f; - m[m_offset+3+4*1] = 0f; - - m[m_offset+0+4*2] = 0f; - m[m_offset+1+4*2] = 0f; - m[m_offset+2+4*2] = 1f; - m[m_offset+3+4*2] = 0f; - - m[m_offset+0+4*3] = 0f; - m[m_offset+1+4*3] = 0f; - m[m_offset+2+4*3] = 0f; - m[m_offset+3+4*3] = 1f; - return m; - } - - /** - * Make matrix an identity matrix - * @param m 4x4 matrix in column-major order (also result) * @return given matrix for chaining */ public static float[] makeIdentity(final float[] m) { @@ -159,43 +129,6 @@ public final class FloatUtil { * All matrix fields are only set if <code>initM</code> is <code>true</code>. * </p> * @param m 4x4 matrix in column-major order (also result) - * @param m_offset offset in given array <i>m</i>, i.e. start of the 4x4 matrix - * @param initM if true, given matrix will be initialized w/ identity matrix, - * otherwise only the diagonal and last-row is set. - * The latter can be utilized to share a once {@link #makeIdentity(float[], int) identity set} matrix - * for {@link #makeScale(float[], int, boolean, float, float, float) scaling} - * and {@link #makeTranslation(float[], int, boolean, float, float, float) translation}, - * while leaving the other fields untouched for performance reasons. - * @return given matrix for chaining - */ - public static float[] makeTranslation(final float[] m, final int m_offset, final boolean initM, final float tx, final float ty, final float tz) { - if( initM ) { - makeIdentity(m, m_offset); - } else { - m[m_offset+0+4*0] = 1; - m[m_offset+1+4*1] = 1; - m[m_offset+2+4*2] = 1; - m[m_offset+3+4*3] = 1; - } - m[m_offset+0+4*3] = tx; - m[m_offset+1+4*3] = ty; - m[m_offset+2+4*3] = tz; - return m; - } - - /** - * Make a translation matrix in column-major order from the given axis deltas - * <pre> - Translation matrix (Column Order): - 1 0 0 0 - 0 1 0 0 - 0 0 1 0 - x y z 1 - * </pre> - * <p> - * All matrix fields are only set if <code>initM</code> is <code>true</code>. - * </p> - * @param m 4x4 matrix in column-major order (also result) * @param initM if true, given matrix will be initialized w/ identity matrix, * otherwise only the diagonal and last-row is set. * The latter can be utilized to share a once {@link #makeIdentity(float[], int) identity set} matrix @@ -232,43 +165,6 @@ public final class FloatUtil { * All matrix fields are only set if <code>initM</code> is <code>true</code>. * </p> * @param m 4x4 matrix in column-major order (also result) - * @param m_offset offset in given array <i>m</i>, i.e. start of the 4x4 matrix - * @param initM if true, given matrix will be initialized w/ identity matrix, - * otherwise only the diagonal and last-row is set. - * The latter can be utilized to share a once {@link #makeIdentity(float[], int) identity set} matrix - * for {@link #makeScale(float[], int, boolean, float, float, float) scaling} - * and {@link #makeTranslation(float[], int, boolean, float, float, float) translation}, - * while leaving the other fields untouched for performance reasons. - * @return given matrix for chaining - */ - public static float[] makeScale(final float[] m, final int m_offset, final boolean initM, final float sx, final float sy, final float sz) { - if( initM ) { - makeIdentity(m, m_offset); - } else { - m[m_offset+0+4*3] = 0; - m[m_offset+1+4*3] = 0; - m[m_offset+2+4*3] = 0; - m[m_offset+3+4*3] = 1; - } - m[m_offset+0+4*0] = sx; - m[m_offset+1+4*1] = sy; - m[m_offset+2+4*2] = sz; - return m; - } - - /** - * Make a scale matrix in column-major order from the given axis factors - * <pre> - Scale matrix (Any Order): - x 0 0 0 - 0 y 0 0 - 0 0 z 0 - 0 0 0 1 - * </pre> - * <p> - * All matrix fields are only set if <code>initM</code> is <code>true</code>. - * </p> - * @param m 4x4 matrix in column-major order (also result) * @param initM if true, given matrix will be initialized w/ identity matrix, * otherwise only the diagonal and last-row is set. * The latter can be utilized to share a once {@link #makeIdentity(float[], int) identity set} matrix @@ -293,189 +189,6 @@ public final class FloatUtil { } /** - * Make a rotation matrix from the given axis and angle in radians. - * <pre> - Rotation matrix (Column Order): - xx(1-c)+c xy(1-c)+zs xz(1-c)-ys 0 - xy(1-c)-zs yy(1-c)+c yz(1-c)+xs 0 - xz(1-c)+ys yz(1-c)-xs zz(1-c)+c 0 - 0 0 0 1 - * </pre> - * <p> - * All matrix fields are set. - * </p> - * @see <a href="http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q38">Matrix-FAQ Q38</a> - * @param m 4x4 matrix in column-major order (also result) - * @param m_offset offset in given array <i>m</i>, i.e. start of the 4x4 matrix - * @return given matrix for chaining - */ - public static float[] makeRotationAxis(final float[] m, final int m_offset, final float angrad, float x, float y, float z, final float[] tmpVec3f) { - final float c = cos(angrad); - final float ic= 1.0f - c; - final float s = sin(angrad); - - tmpVec3f[0]=x; tmpVec3f[1]=y; tmpVec3f[2]=z; - VectorUtil.normalizeVec3(tmpVec3f); - x = tmpVec3f[0]; y = tmpVec3f[1]; z = tmpVec3f[2]; - - final float xy = x*y; - final float xz = x*z; - final float xs = x*s; - final float ys = y*s; - final float yz = y*z; - final float zs = z*s; - m[0+0*4+m_offset] = x*x*ic+c; - m[1+0*4+m_offset] = xy*ic+zs; - m[2+0*4+m_offset] = xz*ic-ys; - m[3+0*4+m_offset] = 0; - - m[0+1*4+m_offset] = xy*ic-zs; - m[1+1*4+m_offset] = y*y*ic+c; - m[2+1*4+m_offset] = yz*ic+xs; - m[3+1*4+m_offset] = 0; - - m[0+2*4+m_offset] = xz*ic+ys; - m[1+2*4+m_offset] = yz*ic-xs; - m[2+2*4+m_offset] = z*z*ic+c; - m[3+2*4+m_offset] = 0; - - m[0+3*4+m_offset] = 0f; - m[1+3*4+m_offset] = 0f; - m[2+3*4+m_offset] = 0f; - m[3+3*4+m_offset] = 1f; - - return m; - } - - /** - * Make a concatenated rotation matrix in column-major order from the given Euler rotation angles in radians. - * <p> - * The rotations are applied in the given order: - * <ul> - * <li>y - heading</li> - * <li>z - attitude</li> - * <li>x - bank</li> - * </ul> - * </p> - * <p> - * All matrix fields are set. - * </p> - * @param m 4x4 matrix in column-major order (also result) - * @param m_offset offset in given array <i>m</i>, i.e. start of the 4x4 matrix - * @param bankX the Euler pitch angle in radians. (rotation about the X axis) - * @param headingY the Euler yaw angle in radians. (rotation about the Y axis) - * @param attitudeZ the Euler roll angle in radians. (rotation about the Z axis) - * @return given matrix for chaining - * <p> - * Implementation does not use Quaternion and hence is exposed to - * <a href="http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q34">Gimbal-Lock</a> - * </p> - * @see <a href="http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q36">Matrix-FAQ Q36</a> - * @see <a href="http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToMatrix/index.htm">euclideanspace.com-eulerToMatrix</a> - */ - public static float[] makeRotationEuler(final float[] m, final int m_offset, final float bankX, final float headingY, final float attitudeZ) { - // Assuming the angles are in radians. - final float ch = cos(headingY); - final float sh = sin(headingY); - final float ca = cos(attitudeZ); - final float sa = sin(attitudeZ); - final float cb = cos(bankX); - final float sb = sin(bankX); - - m[0+0*4+m_offset] = ch*ca; - m[1+0*4+m_offset] = sa; - m[2+0*4+m_offset] = -sh*ca; - m[3+0*4+m_offset] = 0; - - m[0+1*4+m_offset] = sh*sb - ch*sa*cb; - m[1+1*4+m_offset] = ca*cb; - m[2+1*4+m_offset] = sh*sa*cb + ch*sb; - m[3+1*4+m_offset] = 0; - - m[0+2*4+m_offset] = ch*sa*sb + sh*cb; - m[1+2*4+m_offset] = -ca*sb; - m[2+2*4+m_offset] = -sh*sa*sb + ch*cb; - m[3+2*4+m_offset] = 0; - - m[0+3*4+m_offset] = 0; - m[1+3*4+m_offset] = 0; - m[2+3*4+m_offset] = 0; - m[3+3*4+m_offset] = 1; - - return m; - } - - /** - * Make given matrix the orthogonal matrix based on given parameters. - * <pre> - Ortho matrix (Column Order): - 2/dx 0 0 0 - 0 2/dy 0 0 - 0 0 2/dz 0 - tx ty tz 1 - * </pre> - * <p> - * All matrix fields are only set if <code>initM</code> is <code>true</code>. - * </p> - * @param m 4x4 matrix in column-major order (also result) - * @param m_offset offset in given array <i>m</i>, i.e. start of the 4x4 matrix - * @param initM if true, given matrix will be initialized w/ identity matrix, - * otherwise only the orthogonal fields are set. - * @param left - * @param right - * @param bottom - * @param top - * @param zNear - * @param zFar - * @return given matrix for chaining - */ - public static float[] makeOrtho(final float[] m, final int m_offset, final boolean initM, - final float left, final float right, - final float bottom, final float top, - final float zNear, final float zFar) { - if( initM ) { - // m[m_offset+0+4*0] = 1f; - m[m_offset+1+4*0] = 0f; - m[m_offset+2+4*0] = 0f; - m[m_offset+3+4*0] = 0f; - - m[m_offset+0+4*1] = 0f; - // m[m_offset+1+4*1] = 1f; - m[m_offset+2+4*1] = 0f; - m[m_offset+3+4*1] = 0f; - - m[m_offset+0+4*2] = 0f; - m[m_offset+1+4*2] = 0f; - // m[m_offset+2+4*2] = 1f; - m[m_offset+3+4*2] = 0f; - - // m[m_offset+0+4*3] = 0f; - // m[m_offset+1+4*3] = 0f; - // m[m_offset+2+4*3] = 0f; - // m[m_offset+3+4*3] = 1f; - } - final float dx=right-left; - final float dy=top-bottom; - final float dz=zFar-zNear; - final float tx=-1.0f*(right+left)/dx; - final float ty=-1.0f*(top+bottom)/dy; - final float tz=-1.0f*(zFar+zNear)/dz; - - m[m_offset+0+4*0] = 2.0f/dx; - - m[m_offset+1+4*1] = 2.0f/dy; - - m[m_offset+2+4*2] = -2.0f/dz; - - m[m_offset+0+4*3] = tx; - m[m_offset+1+4*3] = ty; - m[m_offset+2+4*3] = tz; - m[m_offset+3+4*3] = 1f; - - return m; - } - - /** * Make given matrix the frustum matrix based on given parameters. * <pre> Frustum matrix (Column Order): @@ -699,7 +412,7 @@ public final class FloatUtil { * @param mat4Tmp temp float[16] storage * @return given matrix <code>m</code> for chaining or <code>null</code> if either delta value is <= zero. */ - public static float[] makePick(final float[] m, final int m_offset, + public static float[] makePick(final float[] m, final float x, final float y, final float deltaX, final float deltaY, final int[] viewport, final int viewport_offset, @@ -709,13 +422,13 @@ public final class FloatUtil { } /* Translate and scale the picked region to the entire window */ - makeTranslation(m, m_offset, true, + makeTranslation(m, true, (viewport[2+viewport_offset] - 2 * (x - viewport[0+viewport_offset])) / deltaX, (viewport[3+viewport_offset] - 2 * (y - viewport[1+viewport_offset])) / deltaY, 0); makeScale(mat4Tmp, true, viewport[2+viewport_offset] / deltaX, viewport[3+viewport_offset] / deltaY, 1.0f); - multMatrix(m, m_offset, mat4Tmp, 0); + multMatrix(m, mat4Tmp); return m; } @@ -723,42 +436,6 @@ public final class FloatUtil { * Transpose the given matrix. * * @param msrc 4x4 matrix in column-major order, the source - * @param msrc_offset offset in given array <i>msrc</i>, i.e. start of the 4x4 matrix - * @param mres 4x4 matrix in column-major order, the result - * @param mres_offset offset in given array <i>mres</i>, i.e. start of the 4x4 matrix - * @return given result matrix <i>mres</i> for chaining - */ - public static float[] transposeMatrix(final float[] msrc, final int msrc_offset, final float[] mres, final int mres_offset) { - mres[mres_offset+0] = msrc[msrc_offset+0*4]; - mres[mres_offset+1] = msrc[msrc_offset+1*4]; - mres[mres_offset+2] = msrc[msrc_offset+2*4]; - mres[mres_offset+3] = msrc[msrc_offset+3*4]; - - final int i4_1 = 1*4; - mres[mres_offset+0+i4_1] = msrc[msrc_offset+1+0*4]; - mres[mres_offset+1+i4_1] = msrc[msrc_offset+1+1*4]; - mres[mres_offset+2+i4_1] = msrc[msrc_offset+1+2*4]; - mres[mres_offset+3+i4_1] = msrc[msrc_offset+1+3*4]; - - final int i4_2 = 2*4; - mres[mres_offset+0+i4_2] = msrc[msrc_offset+2+0*4]; - mres[mres_offset+1+i4_2] = msrc[msrc_offset+2+1*4]; - mres[mres_offset+2+i4_2] = msrc[msrc_offset+2+2*4]; - mres[mres_offset+3+i4_2] = msrc[msrc_offset+2+3*4]; - - final int i4_3 = 3*4; - mres[mres_offset+0+i4_3] = msrc[msrc_offset+3+0*4]; - mres[mres_offset+1+i4_3] = msrc[msrc_offset+3+1*4]; - mres[mres_offset+2+i4_3] = msrc[msrc_offset+3+2*4]; - mres[mres_offset+3+i4_3] = msrc[msrc_offset+3+3*4]; - - return mres; - } - - /** - * Transpose the given matrix. - * - * @param msrc 4x4 matrix in column-major order, the source * @param mres 4x4 matrix in column-major order, the result * @return given result matrix <i>mres</i> for chaining */ @@ -792,40 +469,6 @@ public final class FloatUtil { /** * Returns the determinant of the given matrix * @param m 4x4 matrix in column-major order, the source - * @param m_offset offset in given array <i>m</i>, i.e. start of the 4x4 matrix - * @return the matrix determinant - */ - public static float matrixDeterminant(final float[] m, final int m_offset) { - float a11 = m[ 1+4*1 + m_offset ]; - float a21 = m[ 2+4*1 + m_offset ]; - float a31 = m[ 3+4*1 + m_offset ]; - float a12 = m[ 1+4*2 + m_offset ]; - float a22 = m[ 2+4*2 + m_offset ]; - float a32 = m[ 3+4*2 + m_offset ]; - float a13 = m[ 1+4*3 + m_offset ]; - float a23 = m[ 2+4*3 + m_offset ]; - float a33 = m[ 3+4*3 + m_offset ]; - - float ret = 0; - ret += m[ 0 + m_offset ] * ( + a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31)); - a11 = m[ 1+4*0 + m_offset ]; - a21 = m[ 2+4*0 + m_offset ]; - a31 = m[ 3+4*0 + m_offset ]; - ret -= m[ 0+4*1 + m_offset ] * ( + a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31)); - a12 = m[ 1+4*1 + m_offset ]; - a22 = m[ 2+4*1 + m_offset ]; - a32 = m[ 3+4*1 + m_offset ]; - ret += m[ 0+4*2 + m_offset ] * ( + a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31)); - a13 = m[ 1+4*2 + m_offset ]; - a23 = m[ 2+4*2 + m_offset ]; - a33 = m[ 3+4*2 + m_offset ]; - ret -= m[ 0+4*3 + m_offset ] * ( + a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31)); - return ret; - } - - /** - * Returns the determinant of the given matrix - * @param m 4x4 matrix in column-major order, the source * @return the matrix determinant */ public static float matrixDeterminant(final float[] m) { @@ -855,94 +498,6 @@ public final class FloatUtil { ret -= m[ 0+4*3 ] * ( + a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31)); return ret; } - - /** - * Invert the given matrix. - * <p> - * Returns <code>null</code> if inversion is not possible, - * e.g. matrix is singular due to a bad matrix. - * </p> - * - * @param msrc 4x4 matrix in column-major order, the source - * @param msrc_offset offset in given array <i>msrc</i>, i.e. start of the 4x4 matrix - * @param mres 4x4 matrix in column-major order, the result - may be <code>msrc</code> (in-place) - * @param mres_offset offset in given array <i>mres</i>, i.e. start of the 4x4 matrix - may be <code>msrc_offset</code> (in-place) - * @return given result matrix <i>mres</i> for chaining if successful, otherwise <code>null</code>. See above. - */ - public static float[] invertMatrix(final float[] msrc, final int msrc_offset, final float[] mres, final int mres_offset) { - final float scale; - { - float max = Math.abs(msrc[0]); - - for( int i = 1; i < 16; i++ ) { - final float a = Math.abs(msrc[i]); - if( a > max ) max = a; - } - if( 0 == max ) { - return null; - } - scale = 1.0f/max; - } - - final float a11 = msrc[0+4*0+msrc_offset]*scale; - final float a21 = msrc[1+4*0+msrc_offset]*scale; - final float a31 = msrc[2+4*0+msrc_offset]*scale; - final float a41 = msrc[3+4*0+msrc_offset]*scale; - final float a12 = msrc[0+4*1+msrc_offset]*scale; - final float a22 = msrc[1+4*1+msrc_offset]*scale; - final float a32 = msrc[2+4*1+msrc_offset]*scale; - final float a42 = msrc[3+4*1+msrc_offset]*scale; - final float a13 = msrc[0+4*2+msrc_offset]*scale; - final float a23 = msrc[1+4*2+msrc_offset]*scale; - final float a33 = msrc[2+4*2+msrc_offset]*scale; - final float a43 = msrc[3+4*2+msrc_offset]*scale; - final float a14 = msrc[0+4*3+msrc_offset]*scale; - final float a24 = msrc[1+4*3+msrc_offset]*scale; - final float a34 = msrc[2+4*3+msrc_offset]*scale; - final float a44 = msrc[3+4*3+msrc_offset]*scale; - - final float m11 = + a22*(a33*a44 - a34*a43) - a23*(a32*a44 - a34*a42) + a24*(a32*a43 - a33*a42); - final float m12 = -( + a21*(a33*a44 - a34*a43) - a23*(a31*a44 - a34*a41) + a24*(a31*a43 - a33*a41)); - final float m13 = + a21*(a32*a44 - a34*a42) - a22*(a31*a44 - a34*a41) + a24*(a31*a42 - a32*a41); - final float m14 = -( + a21*(a32*a43 - a33*a42) - a22*(a31*a43 - a33*a41) + a23*(a31*a42 - a32*a41)); - final float m21 = -( + a12*(a33*a44 - a34*a43) - a13*(a32*a44 - a34*a42) + a14*(a32*a43 - a33*a42)); - final float m22 = + a11*(a33*a44 - a34*a43) - a13*(a31*a44 - a34*a41) + a14*(a31*a43 - a33*a41); - final float m23 = -( + a11*(a32*a44 - a34*a42) - a12*(a31*a44 - a34*a41) + a14*(a31*a42 - a32*a41)); - final float m24 = + a11*(a32*a43 - a33*a42) - a12*(a31*a43 - a33*a41) + a13*(a31*a42 - a32*a41); - final float m31 = + a12*(a23*a44 - a24*a43) - a13*(a22*a44 - a24*a42) + a14*(a22*a43 - a23*a42); - final float m32 = -( + a11*(a23*a44 - a24*a43) - a13*(a21*a44 - a24*a41) + a14*(a21*a43 - a23*a41)); - final float m33 = + a11*(a22*a44 - a24*a42) - a12*(a21*a44 - a24*a41) + a14*(a21*a42 - a22*a41); - final float m34 = -( + a11*(a22*a43 - a23*a42) - a12*(a21*a43 - a23*a41) + a13*(a21*a42 - a22*a41)); - final float m41 = -( + a12*(a23*a34 - a24*a33) - a13*(a22*a34 - a24*a32) + a14*(a22*a33 - a23*a32)); - final float m42 = + a11*(a23*a34 - a24*a33) - a13*(a21*a34 - a24*a31) + a14*(a21*a33 - a23*a31); - final float m43 = -( + a11*(a22*a34 - a24*a32) - a12*(a21*a34 - a24*a31) + a14*(a21*a32 - a22*a31)); - final float m44 = + a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31); - - final float det = (a11*m11 + a12*m12 + a13*m13 + a14*m14)/scale; - if( 0 == det ) { - return null; - } - final float invdet = 1.0f / det; - - mres[0+4*0+mres_offset] = m11 * invdet; - mres[1+4*0+mres_offset] = m12 * invdet; - mres[2+4*0+mres_offset] = m13 * invdet; - mres[3+4*0+mres_offset] = m14 * invdet; - mres[0+4*1+mres_offset] = m21 * invdet; - mres[1+4*1+mres_offset] = m22 * invdet; - mres[2+4*1+mres_offset] = m23 * invdet; - mres[3+4*1+mres_offset] = m24 * invdet; - mres[0+4*2+mres_offset] = m31 * invdet; - mres[1+4*2+mres_offset] = m32 * invdet; - mres[2+4*2+mres_offset] = m33 * invdet; - mres[3+4*2+mres_offset] = m34 * invdet; - mres[0+4*3+mres_offset] = m41 * invdet; - mres[1+4*3+mres_offset] = m42 * invdet; - mres[2+4*3+mres_offset] = m43 * invdet; - mres[3+4*3+mres_offset] = m44 * invdet; - return mres; - } - /** * Invert the given matrix. * <p> @@ -1087,53 +642,6 @@ public final class FloatUtil { } /** - * Map object coordinates to window coordinates. - * <p> - * Traditional <code>gluProject</code> implementation. - * </p> - * - * @param objx - * @param objy - * @param objz - * @param mat4PMv [projection] x [modelview] matrix, i.e. P x Mv - * @param viewport 4 component viewport vector - * @param win_pos 3 component window coordinate, the result - * @param vec4Tmp1 4 component vector for temp storage - * @param vec4Tmp2 4 component vector for temp storage - * @return true if successful, otherwise false (z is 1) - */ - public static boolean mapObjToWin(final float objx, final float objy, final float objz, - final float[/*16*/] mat4PMv, - final int[] viewport, final float[] win_pos, - final float[/*4*/] vec4Tmp1, final float[/*4*/] vec4Tmp2) { - vec4Tmp2[0] = objx; - vec4Tmp2[1] = objy; - vec4Tmp2[2] = objz; - vec4Tmp2[3] = 1.0f; - - // vec4Tmp1 = P * Mv * o - multMatrixVec(mat4PMv, vec4Tmp2, vec4Tmp1); - - if (vec4Tmp1[3] == 0.0f) { - return false; - } - - vec4Tmp1[3] = (1.0f / vec4Tmp1[3]) * 0.5f; - - // Map x, y and z to range 0-1 - vec4Tmp1[0] = vec4Tmp1[0] * vec4Tmp1[3] + 0.5f; - vec4Tmp1[1] = vec4Tmp1[1] * vec4Tmp1[3] + 0.5f; - vec4Tmp1[2] = vec4Tmp1[2] * vec4Tmp1[3] + 0.5f; - - // Map x,y to viewport - win_pos[0] = vec4Tmp1[0] * viewport[2] + viewport[0]; - win_pos[1] = vec4Tmp1[1] * viewport[3] + viewport[1]; - win_pos[2] = vec4Tmp1[2]; - - return true; - } - - /** * Map window coordinates to object coordinates. * <p> * Traditional <code>gluUnProject</code> implementation. @@ -1201,134 +709,6 @@ public final class FloatUtil { /** * Map window coordinates to object coordinates. * <p> - * Traditional <code>gluUnProject</code> implementation. - * </p> - * - * @param winx - * @param winy - * @param winz - * @param mat4PMvI inverse [projection] x [modelview] matrix, i.e. Inv(P x Mv) - * @param viewport 4 component viewport vector - * @param viewport_offset - * @param obj_pos 3 component object coordinate, the result - * @param obj_pos_offset - * @param vec4Tmp1 4 component vector for temp storage - * @param vec4Tmp2 4 component vector for temp storage - * @return true if successful, otherwise false (failed to invert matrix, or becomes infinity due to zero z) - */ - public static boolean mapWinToObj(final float winx, final float winy, final float winz, - final float[/*16*/] mat4PMvI, - final int[] viewport, final int viewport_offset, - final float[] obj_pos, final int obj_pos_offset, - final float[/*4*/] vec4Tmp1, final float[/*4*/] vec4Tmp2) { - vec4Tmp1[0] = winx; - vec4Tmp1[1] = winy; - vec4Tmp1[2] = winz; - vec4Tmp1[3] = 1.0f; - - // Map x and y from window coordinates - vec4Tmp1[0] = (vec4Tmp1[0] - viewport[0+viewport_offset]) / viewport[2+viewport_offset]; - vec4Tmp1[1] = (vec4Tmp1[1] - viewport[1+viewport_offset]) / viewport[3+viewport_offset]; - - // Map to range -1 to 1 - vec4Tmp1[0] = vec4Tmp1[0] * 2 - 1; - vec4Tmp1[1] = vec4Tmp1[1] * 2 - 1; - vec4Tmp1[2] = vec4Tmp1[2] * 2 - 1; - - // object raw coords = Inv(P x Mv) * winPos -> mat4Tmp2 - multMatrixVec(mat4PMvI, vec4Tmp1, vec4Tmp2); - - if (vec4Tmp2[3] == 0.0) { - return false; - } - - vec4Tmp2[3] = 1.0f / vec4Tmp2[3]; - - obj_pos[0+obj_pos_offset] = vec4Tmp2[0] * vec4Tmp2[3]; - obj_pos[1+obj_pos_offset] = vec4Tmp2[1] * vec4Tmp2[3]; - obj_pos[2+obj_pos_offset] = vec4Tmp2[2] * vec4Tmp2[3]; - - return true; - } - - /** - * Map two window coordinates to two object coordinates, - * distinguished by their z component. - * - * @param winx - * @param winy - * @param winz1 - * @param winz2 - * @param mat4PMvI inverse [projection] x [modelview] matrix, i.e. Inv(P x Mv) - * @param viewport 4 component viewport vector - * @param viewport_offset - * @param obj1_pos 3 component object coordinate, the result for winz1 - * @param obj1_pos_offset - * @param obj2_pos 3 component object coordinate, the result for winz2 - * @param obj2_pos_offset - * @param vec4Tmp1 4 component vector for temp storage - * @param vec4Tmp2 4 component vector for temp storage - * @return true if successful, otherwise false (failed to invert matrix, or becomes infinity due to zero z) - */ - public static boolean mapWinToObj(final float winx, final float winy, final float winz1, final float winz2, - final float[/*16*/] mat4PMvI, final int[] viewport, - final Vec3f objPos1, final Vec3f objPos2, - final float[/*4*/] vec4Tmp1, final float[/*4*/] vec4Tmp2) { - vec4Tmp1[0] = winx; - vec4Tmp1[1] = winy; - vec4Tmp1[3] = 1.0f; - - // Map x and y from window coordinates - vec4Tmp1[0] = (vec4Tmp1[0] - viewport[0]) / viewport[2]; - vec4Tmp1[1] = (vec4Tmp1[1] - viewport[1]) / viewport[3]; - - // Map to range -1 to 1 - vec4Tmp1[0] = vec4Tmp1[0] * 2 - 1; - vec4Tmp1[1] = vec4Tmp1[1] * 2 - 1; - - // - // winz1 - // - vec4Tmp1[2] = winz1; - vec4Tmp1[2] = vec4Tmp1[2] * 2 - 1; - - // object raw coords = Inv(P x Mv) * winPos -> mat4Tmp2 - multMatrixVec(mat4PMvI, vec4Tmp1, vec4Tmp2); - - if (vec4Tmp2[3] == 0.0) { - return false; - } - - vec4Tmp2[3] = 1.0f / vec4Tmp2[3]; - - objPos1.set( vec4Tmp2[0] * vec4Tmp2[3], - vec4Tmp2[1] * vec4Tmp2[3], - vec4Tmp2[2] * vec4Tmp2[3] ); - - // - // winz2 - // - vec4Tmp1[2] = winz2 * 2 - 1; - - // object raw coords = Inv(P x Mv) * winPos -> mat4Tmp2 - multMatrixVec(mat4PMvI, vec4Tmp1, vec4Tmp2); - - if (vec4Tmp2[3] == 0.0) { - return false; - } - - vec4Tmp2[3] = 1.0f / vec4Tmp2[3]; - - objPos2.set( vec4Tmp2[0] * vec4Tmp2[3], - vec4Tmp2[1] * vec4Tmp2[3], - vec4Tmp2[2] * vec4Tmp2[3] ); - - return true; - } - - /** - * Map window coordinates to object coordinates. - * <p> * Traditional <code>gluUnProject4</code> implementation. * </p> * @@ -1396,57 +776,6 @@ public final class FloatUtil { return true; } - - /** - * Map two window coordinates w/ shared X/Y and distinctive Z - * to a {@link Ray}. The resulting {@link Ray} maybe used for <i>picking</i> - * using a {@link AABBox#getRayIntersection(Ray, float[]) bounding box}. - * <p> - * Notes for picking <i>winz0</i> and <i>winz1</i>: - * <ul> - * <li>see {@link #getZBufferEpsilon(int, float, float)}</li> - * <li>see {@link #getZBufferValue(int, float, float, float)}</li> - * <li>see {@link #getOrthoWinZ(float, float, float)}</li> - * </ul> - * </p> - * @param winx - * @param winy - * @param winz0 - * @param winz1 - * @param modelMatrix 4x4 modelview matrix - * @param modelMatrix_offset - * @param projMatrix 4x4 projection matrix - * @param projMatrix_offset - * @param viewport 4 component viewport vector - * @param viewport_offset - * @param ray storage for the resulting {@link Ray} - * @param mat4Tmp1 16 component matrix for temp storage - * @param mat4Tmp2 16 component matrix for temp storage - * @param vec4Tmp2 4 component vector for temp storage - * @return true if successful, otherwise false (failed to invert matrix, or becomes z is infinity) - */ - public static boolean mapWinToRay(final float winx, final float winy, final float winz0, final float winz1, - final float[] modelMatrix, final int modelMatrix_offset, - final float[] projMatrix, final int projMatrix_offset, - final int[] viewport, - final Ray ray, - final float[/*16*/] mat4Tmp1, final float[/*16*/] mat4Tmp2, final float[/*4*/] vec4Tmp2) { - // mat4Tmp1 = P x Mv - multMatrix(projMatrix, projMatrix_offset, modelMatrix, modelMatrix_offset, mat4Tmp1, 0); - - // mat4Tmp1 = Inv(P x Mv) - if ( null == invertMatrix(mat4Tmp1, mat4Tmp1) ) { - return false; - } - if( mapWinToObj(winx, winy, winz0, winz1, mat4Tmp1, viewport, - ray.orig, ray.dir, mat4Tmp2, vec4Tmp2) ) { - ray.dir.sub(ray.orig).normalize(); - return true; - } else { - return false; - } - } - /** * Multiply matrix: [d] = [a] x [b] * @param a 4x4 matrix in column-major order @@ -1840,36 +1169,6 @@ public final class FloatUtil { * @param v_out m_in * v_in, 3-component column-vector * @return given result vector <i>v_out</i> for chaining */ - public static float[] multMatrixVec3(final float[] m_in, final int m_in_off, - final float[] v_in, final float[] v_out) { - // (one matrix row in column-major order) X (column vector) - v_out[0] = v_in[0] * m_in[0*4+m_in_off ] + v_in[1] * m_in[1*4+m_in_off ] + - v_in[2] * m_in[2*4+m_in_off ] + 1f * m_in[3*4+m_in_off ]; - - final int m_in_off_1 = 1+m_in_off; - v_out[1] = v_in[0] * m_in[0*4+m_in_off_1] + v_in[1] * m_in[1*4+m_in_off_1] + - v_in[2] * m_in[2*4+m_in_off_1] + 1f * m_in[3*4+m_in_off_1]; - - final int m_in_off_2 = 2+m_in_off; - v_out[2] = v_in[0] * m_in[0*4+m_in_off_2] + v_in[1] * m_in[1*4+m_in_off_2] + - v_in[2] * m_in[2*4+m_in_off_2] + 1f * m_in[3*4+m_in_off_2]; - - return v_out; - } - - /** - * Affine 3f-vector transformation by 4x4 matrix - * - * 4x4 matrix multiplication with 3-component vector, - * using {@code 1} for for {@code v_in[3]} and dropping {@code v_out[3]}, - * which shall be {@code 1}. - * - * @param m_in 4x4 matrix in column-major order - * @param m_in_off - * @param v_in 3-component column-vector - * @param v_out m_in * v_in, 3-component column-vector - * @return given result vector <i>v_out</i> for chaining - */ public static float[] multMatrixVec3(final float[] m_in, final float[] v_in, final float[] v_out) { // (one matrix row in column-major order) X (column vector) v_out[0] = v_in[0] * m_in[0*4 ] + v_in[1] * m_in[1*4 ] + @@ -2000,66 +1299,6 @@ public final class FloatUtil { 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, final String rowPrefix, final String f, - final FloatBuffer a, final int aOffset, final FloatBuffer b, final int bOffset, - final int rows, final int columns, final 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; - } - - /** - * @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, final String rowPrefix, final String f, - final float[] a, final int aOffset, final float[] b, final int bOffset, - final int rows, final int columns, final 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; - } - // // Scalar Ops // diff --git a/src/jogl/classes/com/jogamp/opengl/math/Matrix4f.java b/src/jogl/classes/com/jogamp/opengl/math/Matrix4f.java index 6f4b2f38d..a06d5cefc 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Matrix4f.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Matrix4f.java @@ -121,10 +121,44 @@ public class Matrix4f { load(m, m_off); } + /** + * Creates a new matrix based on given {@link FloatBuffer} 4x4 column major order. + * @param m 4x4 matrix in column-major order + */ + public Matrix4f(final FloatBuffer m) { + load(m); + } + // - // Write to Matrix via load(..) + // Write to Matrix via set(..) or load(..) // + /** Sets the {@code i}th component with float {@code v} 0 <= i < 16 */ + public void set(final int i, final float v) { + switch (i) { + case 0+4*0: m00 = v; break; + case 1+4*0: m10 = v; break; + case 2+4*0: m20 = v; break; + case 3+4*0: m30 = v; break; + + case 0+4*1: m01 = v; break; + case 1+4*1: m11 = v; break; + case 2+4*1: m21 = v; break; + case 3+4*1: m31 = v; break; + + case 0+4*2: m02 = v; break; + case 1+4*2: m12 = v; break; + case 2+4*2: m22 = v; break; + case 3+4*2: m32 = v; break; + + case 0+4*3: m03 = v; break; + case 1+4*3: m13 = v; break; + case 2+4*3: m23 = v; break; + case 3+4*3: m33 = v; break; + default: throw new IndexOutOfBoundsException(); + } + } + /** * Set this matrix to identity. * <pre> @@ -242,7 +276,7 @@ public class Matrix4f { // Read out Matrix via get(..) // - /** Gets the ith component, 0 <= i < 16 */ + /** Gets the {@code i}th component, 0 <= i < 16 */ public float get(final int i) { switch (i) { case 0+4*0: return m00; @@ -686,10 +720,6 @@ public class Matrix4f { /** * Multiply matrix: [this] = [this] x [b] - * <p> - * Roughly 15% slower than {@link #mul(Matrix4f, Matrix4f)} - * Roughly 3% slower than {@link FloatUtil#multMatrix(float[], float[])} - * </p> * @param b 4x4 matrix * @return this matrix for chaining * @see #mul(Matrix4f, Matrix4f) @@ -736,10 +766,6 @@ public class Matrix4f { /** * Multiply matrix: [this] = [a] x [b] - * <p> - * Roughly 13% faster than {@link #mul(Matrix4f)} - * Roughly 11% faster than {@link FloatUtil#multMatrix(float[], float[])} - * </p> * @param a 4x4 matrix, can't be this matrix * @param b 4x4 matrix, can't be this matrix * @return this matrix for chaining @@ -778,21 +804,6 @@ public class Matrix4f { * @param v_out this * v_in * @returns v_out for chaining */ - public final float[] mulVec4f(final float[/*4*/] v_in, final float[/*4*/] v_out) { - // (one matrix row in column-major order) X (column vector) - final float x = v_in[0], y = v_in[1], z = v_in[2], w = v_in[3]; - v_out[0] = x * m00 + y * m01 + z * m02 + w * m03; - v_out[1] = x * m10 + y * m11 + z * m12 + w * m13; - v_out[2] = x * m20 + y * m21 + z * m22 + w * m23; - v_out[3] = x * m30 + y * m31 + z * m32 + w * m33; - return v_out; - } - - /** - * @param v_in 4-component column-vector - * @param v_out this * v_in - * @returns v_out for chaining - */ public final Vec4f mulVec4f(final Vec4f v_in, final Vec4f v_out) { // (one matrix row in column-major order) X (column vector) final float x = v_in.x(), y = v_in.y(), z = v_in.z(), w = v_in.w(); @@ -807,26 +818,6 @@ public class Matrix4f { * Affine 3f-vector transformation by 4x4 matrix * * 4x4 matrix multiplication with 3-component vector, - * using {@code 1} for for {@code v_in[3]} and dropping {@code v_out[3]}, - * which shall be {@code 1}. - * - * @param v_in 3-component column-vector - * @param v_out m_in * v_in, 3-component column-vector - * @returns v_out for chaining - */ - public final float[] mulVec3f(final float[/*3*/] v_in, final float[/*3*/] v_out) { - // (one matrix row in column-major order) X (column vector) - final float x = v_in[0], y = v_in[1], z = v_in[2]; - v_out[0] = x * m00 + y * m01 + z * m02 + 1f * m03; - v_out[1] = x * m10 + y * m11 + z * m12 + 1f * m13; - v_out[2] = x * m20 + y * m21 + z * m22 + 1f * m23; - return v_out; - } - - /** - * Affine 3f-vector transformation by 4x4 matrix - * - * 4x4 matrix multiplication with 3-component vector, * using {@code 1} for for {@code v_in.w()} and dropping {@code v_out.w()}, * which shall be {@code 1}. * @@ -916,6 +907,22 @@ public class Matrix4f { } /** + * Set this matrix to scale. + * <pre> + Scale matrix (Any Order): + x 0 0 0 + 0 y 0 0 + 0 0 z 0 + 0 0 0 1 + * </pre> + * @param s scale Vec3f + * @return this matrix for chaining + */ + public final Matrix4f setToScale(final Vec3f s) { + return setToScale(s.x(), s.y(), s.z()); + } + + /** * Set this matrix to rotation from the given axis and angle in radians. * <pre> Rotation matrix (Column Order): @@ -936,9 +943,8 @@ public class Matrix4f { final float ic= 1.0f - c; final float s = FloatUtil.sin(ang_rad); - final float[] tmpVec3f = { x, y, z }; - VectorUtil.normalizeVec3(tmpVec3f); - x = tmpVec3f[0]; y = tmpVec3f[1]; z = tmpVec3f[2]; + final Vec3f tmp = new Vec3f(x, y, z).normalize(); + x = tmp.x(); y = tmp.y(); z = tmp.z(); final float xy = x*y; final float xz = x*z; @@ -1043,6 +1049,31 @@ public class Matrix4f { } /** + * Set this matrix to rotation from the given Euler rotation angles in radians. + * <p> + * The rotations are applied in the given order: + * <ul> + * <li>y - heading</li> + * <li>z - attitude</li> + * <li>x - bank</li> + * </ul> + * </p> + * @param angradXYZ euler angle vector in radians holding x-bank, y-heading and z-attitude + * @return this quaternion for chaining. + * <p> + * Implementation does not use Quaternion and hence is exposed to + * <a href="http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q34">Gimbal-Lock</a>, + * consider using {@link #setToRotation(Quaternion)}. + * </p> + * @see <a href="http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q36">Matrix-FAQ Q36</a> + * @see <a href="http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToMatrix/index.htm">euclideanspace.com-eulerToMatrix</a> + * @see #setToRotation(Quaternion) + */ + public Matrix4f setToRotationEuler(final Vec3f angradXYZ) { + return setToRotationEuler(angradXYZ.x(), angradXYZ.y(), angradXYZ.z()); + } + + /** * Set this matrix to rotation using the given Quaternion. * <p> * Implementation Details: @@ -1055,7 +1086,7 @@ public class Matrix4f { * @param q the Quaternion representing the rotation * @return this matrix for chaining * @see <a href="http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q54">Matrix-FAQ Q54</a> - * @see Quaternion#toMatrix(float[], int) + * @see Quaternion#toMatrix(float[]) * @see #getRotation() */ public final Matrix4f setToRotation(final Quaternion q) { @@ -1255,7 +1286,7 @@ public class Matrix4f { * @return this matrix for chaining * @throws IllegalArgumentException if {@code zNear <= 0} or {@code zFar <= zNear} * @see #setToFrustum(float, float, float, float, float, float) - * @see Frustum#updateByFovDesc(float[], int, boolean, Frustum.FovDesc) + * @see Frustum#updateByFovDesc(Matrix4f, com.jogamp.opengl.math.geom.Frustum.FovDesc) */ public Matrix4f setToPerspective(final FovHVHalves fovhv, final float zNear, final float zFar) throws IllegalArgumentException { final FovHVHalves fovhvTan = fovhv.toTangents(); // use tangent of half-fov ! @@ -1268,11 +1299,14 @@ public class Matrix4f { /** * Calculate the frustum planes in world coordinates - * using the passed float[16] as premultiplied P*MV (column major order). + * using this premultiplied P*MV (column major order) matrix. * <p> * Frustum plane's normals will point to the inside of the viewing frustum, * as required by this class. * </p> + * <p> + * Usually called by {@link Frustum#updateFrustumPlanes(Matrix4f)}. + * </p> */ public void updateFrustumPlanes(final Frustum frustum) { // Left: a = m41 + m11, b = m42 + m12, c = m43 + m13, d = m44 + m14 - [1..4] column-major @@ -1352,7 +1386,7 @@ public class Matrix4f { } /** - * Make given matrix the <i>look-at</i> matrix based on given parameters. + * Set this matrix to the <i>look-at</i> matrix based on given parameters. * <p> * Consist out of two matrix multiplications: * <pre> @@ -1406,6 +1440,50 @@ public class Matrix4f { return mul( tmp.setToTranslation( -eye.x(), -eye.y(), -eye.z() ) ); } + /** + * Set this matrix to the <i>pick</i> matrix based on given parameters. + * <p> + * Traditional <code>gluPickMatrix</code> implementation. + * </p> + * <p> + * Consist out of two matrix multiplications: + * <pre> + * <b>R</b> = <b>T</b> x <b>S</b>, + * with <b>T</b> for viewport translation matrix and + * <b>S</b> for viewport scale matrix. + * + * Result <b>R</b> can be utilized for <i>projection</i> multiplication, i.e. + * <b>P</b> = <b>P</b> x <b>R</b>, + * with <b>P</b> being the <i>projection</i> matrix. + * </pre> + * </p> + * <p> + * To effectively use the generated pick matrix for picking, + * call {@link #setToPick(float, float, float, float, Recti, Matrix4f) setToPick(..)} + * and multiply a {@link #setToPerspective(float, float, float, float) custom perspective matrix} + * by this pick matrix. Then you may load the result onto the perspective matrix stack. + * </p> + * @param x the center x-component of a picking region in window coordinates + * @param y the center y-component of a picking region in window coordinates + * @param deltaX the width of the picking region in window coordinates. + * @param deltaY the height of the picking region in window coordinates. + * @param viewport Rect4i viewport + * @param mat4Tmp temp storage + * @return this matrix for chaining or {@code null} if either delta value is <= zero. + */ + public Matrix4f setToPick(final float x, final float y, final float deltaX, final float deltaY, + final Recti viewport, final Matrix4f mat4Tmp) { + if (deltaX <= 0 || deltaY <= 0) { + return null; + } + /* Translate and scale the picked region to the entire window */ + setToTranslation( ( viewport.width() - 2 * ( x - viewport.x() ) ) / deltaX, + ( viewport.height() - 2 * ( y - viewport.y() ) ) / deltaY, + 0); + mat4Tmp.setToScale( viewport.width() / deltaX, viewport.height() / deltaY, 1.0f ); + return mul(mat4Tmp); + } + // // Matrix affine operations using setTo..() // @@ -1587,12 +1665,12 @@ public class Matrix4f { * @param obj object position, 3 component vector * @param mMv modelview matrix * @param mP projection matrix - * @param viewport 4 component viewport vector + * @param viewport Rect4i viewport * @param winPos 3 component window coordinate, the result * @return true if successful, otherwise false (z is 1) */ public static boolean mapObjToWin(final Vec3f obj, final Matrix4f mMv, final Matrix4f mP, - final int[] viewport, final float[] winPos) + final Recti viewport, final Vec3f winPos) { final Vec4f vec4Tmp1 = new Vec4f(obj, 1f); @@ -1613,9 +1691,9 @@ public class Matrix4f { rawWinPos.scale(s).add(0.5f, 0.5f, 0.5f, 0f); // Map x,y to viewport - winPos[0] = rawWinPos.x() * viewport[2] + viewport[0]; - winPos[1] = rawWinPos.y() * viewport[3] + viewport[1]; - winPos[2] = rawWinPos.z(); + winPos.set( rawWinPos.x() * viewport.width() + viewport.x(), + rawWinPos.y() * viewport.height() + viewport.y(), + rawWinPos.z() ); return true; } @@ -1628,12 +1706,12 @@ public class Matrix4f { * * @param obj object position, 3 component vector * @param mPMv [projection] x [modelview] matrix, i.e. P x Mv - * @param viewport 4 component viewport vector + * @param viewport Rect4i viewport * @param winPos 3 component window coordinate, the result * @return true if successful, otherwise false (z is 1) */ public static boolean mapObjToWin(final Vec3f obj, final Matrix4f mPMv, - final int[] viewport, final float[] winPos) + final Recti viewport, final Vec3f winPos) { final Vec4f vec4Tmp2 = new Vec4f(obj, 1f); @@ -1650,9 +1728,9 @@ public class Matrix4f { rawWinPos.scale(s).add(0.5f, 0.5f, 0.5f, 0f); // Map x,y to viewport - winPos[0] = rawWinPos.x() * viewport[2] + viewport[0]; - winPos[1] = rawWinPos.y() * viewport[3] + viewport[1]; - winPos[2] = rawWinPos.z(); + winPos.set( rawWinPos.x() * viewport.width() + viewport.x(), + rawWinPos.y() * viewport.height() + viewport.y(), + rawWinPos.z() ); return true; } @@ -1668,14 +1746,14 @@ public class Matrix4f { * @param winz * @param mMv 4x4 modelview matrix * @param mP 4x4 projection matrix - * @param viewport 4 component viewport vector + * @param viewport Rect4i viewport * @param objPos 3 component object coordinate, the result * @param mat4Tmp 16 component matrix for temp storage * @return true if successful, otherwise false (failed to invert matrix, or becomes infinity due to zero z) */ public static boolean mapWinToObj(final float winx, final float winy, final float winz, final Matrix4f mMv, final Matrix4f mP, - final int[] viewport, + final Recti viewport, final Vec3f objPos, final Matrix4f mat4Tmp) { @@ -1688,7 +1766,7 @@ public class Matrix4f { final Vec4f winPos = new Vec4f(winx, winy, winz, 1f); // Map x and y from window coordinates - winPos.add(-viewport[0], -viewport[1], 0f, 0f).scale(1f/viewport[2], 1f/viewport[3], 1f, 1f); + winPos.add(-viewport.x(), -viewport.y(), 0f, 0f).scale(1f/viewport.width(), 1f/viewport.height(), 1f, 1f); // Map to range -1 to 1 winPos.scale(2f, 2f, 2f, 1f).add(-1f, -1f, -1f, 0f); @@ -1714,21 +1792,21 @@ public class Matrix4f { * @param winy * @param winz * @param invPMv inverse [projection] x [modelview] matrix, i.e. Inv(P x Mv) - * @param viewport 4 component viewport vector + * @param viewport Rect4i viewport * @param objPos 3 component object coordinate, the result * @param mat4Tmp 16 component matrix for temp storage * @return true if successful, otherwise false (failed to invert matrix, or becomes infinity due to zero z) */ public static boolean mapWinToObj(final float winx, final float winy, final float winz, final Matrix4f invPMv, - final int[] viewport, + final Recti viewport, final Vec3f objPos, final Matrix4f mat4Tmp) { final Vec4f winPos = new Vec4f(winx, winy, winz, 1f); // Map x and y from window coordinates - winPos.add(-viewport[0], -viewport[1], 0f, 0f).scale(1f/viewport[2], 1f/viewport[3], 1f, 1f); + winPos.add(-viewport.x(), -viewport.y(), 0f, 0f).scale(1f/viewport.width(), 1f/viewport.height(), 1f, 1f); // Map to range -1 to 1 winPos.scale(2f, 2f, 2f, 1f).add(-1f, -1f, -1f, 0f); @@ -1756,21 +1834,21 @@ public class Matrix4f { * @param winz1 * @param winz2 * @param invPMv inverse [projection] x [modelview] matrix, i.e. Inv(P x Mv) - * @param viewport 4 component viewport vector + * @param viewport Rect4i viewport vector * @param objPos1 3 component object coordinate, the result * @param mat4Tmp 16 component matrix for temp storage * @return true if successful, otherwise false (failed to invert matrix, or becomes infinity due to zero z) */ public static boolean mapWinToObj(final float winx, final float winy, final float winz1, final float winz2, final Matrix4f invPMv, - final int[] viewport, + final Recti viewport, final Vec3f objPos1, final Vec3f objPos2, final Matrix4f mat4Tmp) { final Vec4f winPos = new Vec4f(winx, winy, winz1, 1f); // Map x and y from window coordinates - winPos.add(-viewport[0], -viewport[1], 0f, 0f).scale(1f/viewport[2], 1f/viewport[3], 1f, 1f); + winPos.add(-viewport.x(), -viewport.y(), 0f, 0f).scale(1f/viewport.width(), 1f/viewport.height(), 1f, 1f); // Map to range -1 to 1 winPos.scale(2f, 2f, 2f, 1f).add(-1f, -1f, -1f, 0f); @@ -1812,7 +1890,7 @@ public class Matrix4f { * @param clipw * @param mMv 4x4 modelview matrix * @param mP 4x4 projection matrix - * @param viewport 4 component viewport vector + * @param viewport Rect4i viewport vector * @param near * @param far * @param obj_pos 4 component object coordinate, the result @@ -1821,7 +1899,7 @@ public class Matrix4f { */ public static boolean mapWinToObj4(final float winx, final float winy, final float winz, final float clipw, final Matrix4f mMv, final Matrix4f mP, - final int[] viewport, + final Recti viewport, final float near, final float far, final Vec4f objPos, final Matrix4f mat4Tmp) @@ -1835,7 +1913,7 @@ public class Matrix4f { final Vec4f winPos = new Vec4f(winx, winy, winz, clipw); // Map x and y from window coordinates - winPos.add(-viewport[0], -viewport[1], -near, 0f).scale(1f/viewport[2], 1f/viewport[3], 1f/(far-near), 1f); + winPos.add(-viewport.x(), -viewport.y(), -near, 0f).scale(1f/viewport.width(), 1f/viewport.height(), 1f/(far-near), 1f); // Map to range -1 to 1 winPos.scale(2f, 2f, 2f, 1f).add(-1f, -1f, -1f, 0f); @@ -1852,7 +1930,7 @@ public class Matrix4f { /** * Map two window coordinates w/ shared X/Y and distinctive Z * to a {@link Ray}. The resulting {@link Ray} maybe used for <i>picking</i> - * using a {@link AABBox#getRayIntersection(Ray, float[]) bounding box}. + * using a {@link AABBox#getRayIntersection(Vec3f, Ray, float, boolean)}. * <p> * Notes for picking <i>winz0</i> and <i>winz1</i>: * <ul> @@ -1867,7 +1945,7 @@ public class Matrix4f { * @param winz1 * @param mMv 4x4 modelview matrix * @param mP 4x4 projection matrix - * @param viewport 4 component viewport vector + * @param viewport Rect4i viewport * @param ray storage for the resulting {@link Ray} * @param mat4Tmp1 16 component matrix for temp storage * @param mat4Tmp2 16 component matrix for temp storage @@ -1876,7 +1954,7 @@ public class Matrix4f { public static boolean mapWinToRay(final float winx, final float winy, final float winz0, final float winz1, final Matrix4f mMv, final Matrix4f mP, - final int[] viewport, + final Recti viewport, final Ray ray, final Matrix4f mat4Tmp1, final Matrix4f mat4Tmp2) { // invPMv = Inv(P x Mv) diff --git a/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java b/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java index 2bb0f96c6..a285774f8 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java @@ -1000,18 +1000,17 @@ public class Quaternion { * </p> * * @param matrix float[16] store for the resulting normalized column matrix 4x4 - * @param mat_offset * @return the given matrix store * @see <a href="http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q54">Matrix-FAQ Q54</a> * @see #setFromMatrix(Matrix4f) * @see #setFromMatrix(float, float, float, float, float, float, float, float, float) */ - public final float[] toMatrix(final float[] matrix, final int mat_offset) { + public final float[] toMatrix(final float[] matrix) { // pre-multiply scaled-reciprocal-magnitude to reduce multiplications final float norm = magnitudeSquared(); if ( FloatUtil.isZero(norm, FloatUtil.EPSILON) ) { // identity matrix -> srecip = 0f - return FloatUtil.makeIdentity(matrix, mat_offset); + return FloatUtil.makeIdentity(matrix); } final float srecip; if ( FloatUtil.isEqual(1f, norm, FloatUtil.EPSILON) ) { @@ -1034,25 +1033,25 @@ public class Quaternion { final float zz = z * zs; final float zw = zs * w; - matrix[0+0*4+mat_offset] = 1f - ( yy + zz ); - matrix[0+1*4+mat_offset] = ( xy - zw ); - matrix[0+2*4+mat_offset] = ( xz + yw ); - matrix[0+3*4+mat_offset] = 0f; - - matrix[1+0*4+mat_offset] = ( xy + zw ); - matrix[1+1*4+mat_offset] = 1f - ( xx + zz ); - matrix[1+2*4+mat_offset] = ( yz - xw ); - matrix[1+3*4+mat_offset] = 0f; - - matrix[2+0*4+mat_offset] = ( xz - yw ); - matrix[2+1*4+mat_offset] = ( yz + xw ); - matrix[2+2*4+mat_offset] = 1f - ( xx + yy ); - matrix[2+3*4+mat_offset] = 0f; - - matrix[3+0*4+mat_offset] = 0f; - matrix[3+1*4+mat_offset] = 0f; - matrix[3+2*4+mat_offset] = 0f; - matrix[3+3*4+mat_offset] = 1f; + matrix[0+0*4] = 1f - ( yy + zz ); + matrix[0+1*4] = ( xy - zw ); + matrix[0+2*4] = ( xz + yw ); + matrix[0+3*4] = 0f; + + matrix[1+0*4] = ( xy + zw ); + matrix[1+1*4] = 1f - ( xx + zz ); + matrix[1+2*4] = ( yz - xw ); + matrix[1+3*4] = 0f; + + matrix[2+0*4] = ( xz - yw ); + matrix[2+1*4] = ( yz + xw ); + matrix[2+2*4] = 1f - ( xx + yy ); + matrix[2+3*4] = 0f; + + matrix[3+0*4] = 0f; + matrix[3+1*4] = 0f; + matrix[3+2*4] = 0f; + matrix[3+3*4] = 1f; return matrix; } diff --git a/src/jogl/classes/com/jogamp/opengl/math/Recti.java b/src/jogl/classes/com/jogamp/opengl/math/Recti.java new file mode 100644 index 000000000..58f5e5e77 --- /dev/null +++ b/src/jogl/classes/com/jogamp/opengl/math/Recti.java @@ -0,0 +1,134 @@ +/** + * Copyright 2022-2023 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.math; + +/** + * Rectangle with x, y, width and height integer components. + */ +public final class Recti { + private int x; + private int y; + private int width; + private int height; + + public Recti() {} + + public Recti(final Recti o) { + set(o); + } + + public Recti copy() { + return new Recti(this); + } + + public Recti(final int[/*4*/] xywh) { + set(xywh); + } + + public Recti(final int x, final int y, final int width, final int height) { + set(x, y, width, height); + } + + /** this = o, returns this. */ + public void set(final Recti o) { + this.x = o.x; + this.y = o.y; + this.width = o.width; + this.height= o.height; + } + + /** this = { x, y, width, height }, returns this. */ + public void set(final int x, final int y, final int width, final int height) { + this.x = x; + this.y = y; + this.width = width; + this.height= height; + } + + /** this = xywh, returns this. */ + public Recti set(final int[/*2*/] xywh) { + this.x = xywh[0]; + this.y = xywh[1]; + this.width = xywh[2]; + this.height= xywh[3]; + return this; + } + + /** xywh = this, returns xy. */ + public int[] get(final int[/*4*/] xywh) { + xywh[0] = this.x; + xywh[1] = this.y; + xywh[2] = this.width; + xywh[3] = this.height; + return xywh; + } + + public int x() { return x; } + public int y() { return y; } + public int width() { return width; } + public int height() { return height; } + + public void setX(final int x) { this.x = x; } + public void setY(final int y) { this.y = y; } + public void setWidth(final int width) { this.width = width; } + public void setHeight(final int height) { this.height = height; } + + /** Return true if all components are zero. */ + public boolean isZero() { + return 0 == x && 0 == y; + } + + /** + * Equals check. + * @param o comparison value + * @return true if all components are equal + */ + public boolean isEqual(final Recti o) { + if( this == o ) { + return true; + } else { + return x == o.x && y == o.y && + width == o.width && height == o.height; + } + } + + @Override + public boolean equals(final Object o) { + if( o instanceof Recti ) { + return isEqual((Recti)o); + } else { + return false; + } + } + + @Override + public String toString() { + return x + " / " + y + " " + width + " x " + height; + } +} diff --git a/src/jogl/classes/com/jogamp/opengl/math/Vec2f.java b/src/jogl/classes/com/jogamp/opengl/math/Vec2f.java index 0c7854216..616ba0f60 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Vec2f.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Vec2f.java @@ -301,13 +301,6 @@ public final class Vec2f { return new Vec2f(-y, x); } - public boolean intersects(final Vec2f o) { - if( Math.abs(x-o.x) >= FloatUtil.EPSILON || Math.abs(y-o.y) >= FloatUtil.EPSILON ) { - return false; - } - return true; - } - /** * Equals check using a given {@link FloatUtil#EPSILON} value and {@link FloatUtil#isEqual(float, float, float)}. * <p> diff --git a/src/jogl/classes/com/jogamp/opengl/math/Vec2i.java b/src/jogl/classes/com/jogamp/opengl/math/Vec2i.java new file mode 100644 index 000000000..9e70a502f --- /dev/null +++ b/src/jogl/classes/com/jogamp/opengl/math/Vec2i.java @@ -0,0 +1,153 @@ +/** + * Copyright 2022-2023 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.math; + +/** + * 2D Vector based upon two integer components. + */ +public final class Vec2i { + private int x; + private int y; + + public Vec2i() {} + + public Vec2i(final Vec2i o) { + set(o); + } + + public Vec2i copy() { + return new Vec2i(this); + } + + public Vec2i(final int[/*2*/] xy) { + set(xy); + } + + public Vec2i(final int x, final int y) { + set(x, y); + } + + /** this = o, returns this. */ + public void set(final Vec2i o) { + this.x = o.x; + this.y = o.y; + } + + /** this = { x, y }, returns this. */ + public void set(final int x, final int y) { + this.x = x; + this.y = y; + } + + /** this = xy, returns this. */ + public Vec2i set(final int[/*2*/] xy) { + this.x = xy[0]; + this.y = xy[1]; + return this; + } + + /** xy = this, returns xy. */ + public int[] get(final int[/*2*/] xy) { + xy[0] = this.x; + xy[1] = this.y; + return xy; + } + + public int x() { return x; } + public int y() { return y; } + + public void setX(final int x) { this.x = x; } + public void setY(final int y) { this.y = y; } + + /** Return true if all components are zero. */ + public boolean isZero() { + return 0 == x && 0 == y; + } + + /** + * Return the length of this vector, a.k.a the <i>norm</i> or <i>magnitude</i> + */ + public int length() { + return (int) Math.sqrt(lengthSq()); + } + + /** + * Return the squared length of this vector, a.k.a the squared <i>norm</i> or squared <i>magnitude</i> + */ + public int lengthSq() { + return x*x + y*y; + } + + /** + * Return the squared distance between this vector and the given one. + * <p> + * When comparing the relative distance between two points it is usually sufficient to compare the squared + * distances, thus avoiding an expensive square root operation. + * </p> + */ + public int distSq(final Vec2i o) { + final int dx = x - o.x; + final int dy = y - o.y; + return dx*dx + dy*dy; + } + + /** + * Return the distance between this vector and the given one. + */ + public int dist(final Vec2i o) { + return (int)Math.sqrt(distSq(o)); + } + + /** + * Equals check. + * @param o comparison value + * @return true if all components are equal + */ + public boolean isEqual(final Vec2i o) { + if( this == o ) { + return true; + } else { + return x == o.x && y == o.y; + } + } + + @Override + public boolean equals(final Object o) { + if( o instanceof Vec2i ) { + return isEqual((Vec2i)o); + } else { + return false; + } + } + + @Override + public String toString() { + return x + " / " + y; + } +} diff --git a/src/jogl/classes/com/jogamp/opengl/math/Vec3f.java b/src/jogl/classes/com/jogamp/opengl/math/Vec3f.java index d5c725ad7..9ef985b36 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Vec3f.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Vec3f.java @@ -312,14 +312,6 @@ public final class Vec3f { return (float) Math.acos( cosAngle(o) ); } - public boolean intersects(final Vec3f o) { - if( Math.abs(x-o.x) >= FloatUtil.EPSILON || Math.abs(y-o.y) >= FloatUtil.EPSILON || Math.abs(z-o.z) >= FloatUtil.EPSILON ) { - return false; - } else { - return true; - } - } - /** * Equals check using a given {@link FloatUtil#EPSILON} value and {@link FloatUtil#isEqual(float, float, float)}. * <p> diff --git a/src/jogl/classes/com/jogamp/opengl/math/Vec4f.java b/src/jogl/classes/com/jogamp/opengl/math/Vec4f.java index 1a20015a9..570b7b2b3 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Vec4f.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Vec4f.java @@ -284,14 +284,6 @@ public final class Vec4f { return (float) Math.acos( cosAngle(o) ); } - public boolean intersects(final Vec4f o) { - if( Math.abs(x-o.x) >= FloatUtil.EPSILON || Math.abs(y-o.y) >= FloatUtil.EPSILON || Math.abs(z-o.z) >= FloatUtil.EPSILON || - Math.abs(w-o.w) >= FloatUtil.EPSILON) { - return false; - } - return true; - } - /** * Equals check using a given {@link FloatUtil#EPSILON} value and {@link FloatUtil#isEqual(float, float, float)}. * <p> diff --git a/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java b/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java index 77e1bfc59..ad521de2a 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java +++ b/src/jogl/classes/com/jogamp/opengl/math/geom/AABBox.java @@ -32,6 +32,7 @@ import com.jogamp.opengl.math.FloatUtil; import com.jogamp.opengl.math.Matrix4f; import com.jogamp.opengl.math.Quaternion; import com.jogamp.opengl.math.Ray; +import com.jogamp.opengl.math.Recti; import com.jogamp.opengl.math.Vec3f; import com.jogamp.opengl.util.PMVMatrix; @@ -718,41 +719,34 @@ public class AABBox { throw new InternalError("hashCode not designed"); } - public AABBox transform(final AABBox result, final float[/*16*/] mat4, final int mat4_off, - final float[] vec3Tmp0, final float[] vec3Tmp1) { - result.reset(); - FloatUtil.multMatrixVec3(mat4, mat4_off, low.get(vec3Tmp0), vec3Tmp1); - result.resize(vec3Tmp1); - - FloatUtil.multMatrixVec3(mat4, mat4_off, high.get(vec3Tmp0), vec3Tmp1); - result.resize(vec3Tmp1); - - result.computeCenter(); - return result; - } - - public AABBox transformMv(final AABBox result, final PMVMatrix pmv, - final float[] vec3Tmp0, final float[] vec3Tmp1) { - result.reset(); - pmv.multMvMatVec3f(low.get(vec3Tmp0), vec3Tmp1); - result.resize(vec3Tmp1); - - pmv.multMvMatVec3f(high.get(vec3Tmp0), vec3Tmp1); - result.resize(vec3Tmp1); - - result.computeCenter(); - return result; + /** + * Transform this box using the given {@link Matrix4f} into {@code out} + * @param mat transformation {@link Matrix4f} + * @param out the resulting {@link AABBox} + * @return the resulting {@link AABBox} for chaining + */ + public AABBox transform(final Matrix4f mat, final AABBox out) { + final Vec3f tmp = new Vec3f(); + out.reset(); + out.resize( mat.mulVec3f(low, tmp) ); + out.resize( mat.mulVec3f(high, tmp) ); + out.computeCenter(); + return out; } - public AABBox transform(final AABBox result, final Matrix4f mat, - final Vec3f vec3Tmp) { - result.reset(); - result.resize( mat.mulVec3f(low, vec3Tmp) ); - - result.resize( mat.mulVec3f(high, vec3Tmp) ); - - result.computeCenter(); - return result; + /** + * Transform this box using the {@link PMVMatrix#getMvMat() modelview} of the given {@link PMVMatrix} into {@code out} + * @param pmv transformation {@link PMVMatrix} + * @param out the resulting {@link AABBox} + * @return the resulting {@link AABBox} for chaining + */ + public AABBox transformMv(final PMVMatrix pmv, final AABBox out) { + final Vec3f tmp = new Vec3f(); + out.reset(); + out.resize( pmv.mulMvMatVec3f(low, tmp) ); + out.resize( pmv.mulMvMatVec3f(high, tmp) ); + out.computeCenter(); + return out; } /** @@ -771,56 +765,51 @@ public class AABBox { * | | * .y() ------ [3] * </pre> - * @param mat4PMv P x Mv matrix - * @param view + * @param mat4PMv [projection] x [modelview] matrix, i.e. P x Mv + * @param viewport viewport rectangle * @param useCenterZ * @param vec3Tmp0 3 component vector for temp storage * @param vec4Tmp1 4 component vector for temp storage * @param vec4Tmp2 4 component vector for temp storage * @return */ - public AABBox mapToWindow(final AABBox result, final float[/*16*/] mat4PMv, final int[] view, final boolean useCenterZ, - final float[] vec3Tmp0, final float[] vec4Tmp1, final float[] vec4Tmp2) { + public AABBox mapToWindow(final AABBox result, final Matrix4f mat4PMv, final Recti viewport, final boolean useCenterZ) { + final Vec3f tmp = new Vec3f(); + final Vec3f winPos = new Vec3f(); { - // System.err.printf("AABBox.mapToWindow.0: view[%d, %d, %d, %d], this %s%n", view.x(), view.y(), view.z(), view[3], toString()); final float objZ = useCenterZ ? center.z() : getMinZ(); - FloatUtil.mapObjToWin(getMinX(), getMinY(), objZ, mat4PMv, view, vec3Tmp0, vec4Tmp1, vec4Tmp2); - // System.err.printf("AABBox.mapToWindow.p1: %f, %f, %f -> %f, %f, %f%n", getMinX(), getMinY(), objZ, vec3Tmp0.x(), vec3Tmp0.y(), vec3Tmp0.z()); - // System.err.println("AABBox.mapToWindow.p1:"); - // System.err.println(FloatUtil.matrixToString(null, " mat4PMv", "%10.5f", mat4PMv, 0, 4, 4, false /* rowMajorOrder */)); - result.reset(); - result.resize(vec3Tmp0); - FloatUtil.mapObjToWin(getMinX(), getMaxY(), objZ, mat4PMv, view, vec3Tmp0, vec4Tmp1, vec4Tmp2); - // System.err.printf("AABBox.mapToWindow.p2: %f, %f, %f -> %f, %f, %f%n", getMinX(), getMaxY(), objZ, vec3Tmp0.x(), vec3Tmp0.y(), vec3Tmp0.z()); - result.resize(vec3Tmp0); + Matrix4f.mapObjToWin(tmp.set(getMinX(), getMinY(), objZ), mat4PMv, viewport, winPos); + result.resize(winPos); - FloatUtil.mapObjToWin(getMaxX(), getMinY(), objZ, mat4PMv, view, vec3Tmp0, vec4Tmp1, vec4Tmp2); - // System.err.printf("AABBox.mapToWindow.p3: %f, %f, %f -> %f, %f, %f%n", getMaxX(), getMinY(), objZ, vec3Tmp0.x(), vec3Tmp0.y(), vec3Tmp0.z()); - result.resize(vec3Tmp0); + Matrix4f.mapObjToWin(tmp.set(getMinX(), getMaxY(), objZ), mat4PMv, viewport, winPos); + result.resize(winPos); - FloatUtil.mapObjToWin(getMaxX(), getMaxY(), objZ, mat4PMv, view, vec3Tmp0, vec4Tmp1, vec4Tmp2); - // System.err.printf("AABBox.mapToWindow.p4: %f, %f, %f -> %f, %f, %f%n", getMaxX(), getMaxY(), objZ, vec3Tmp0.x(), vec3Tmp0.y(), vec3Tmp0.z()); - result.resize(vec3Tmp0); + Matrix4f.mapObjToWin(tmp.set(getMaxX(), getMaxY(), objZ), mat4PMv, viewport, winPos); + result.resize(winPos); + + Matrix4f.mapObjToWin(tmp.set(getMaxX(), getMinY(), objZ), mat4PMv, viewport, winPos); + result.resize(winPos); } if( !useCenterZ ) { final float objZ = getMaxZ(); - FloatUtil.mapObjToWin(getMinX(), getMinY(), objZ, mat4PMv, view, vec3Tmp0, vec4Tmp1, vec4Tmp2); - result.resize(vec3Tmp0); - FloatUtil.mapObjToWin(getMinX(), getMaxY(), objZ, mat4PMv, view, vec3Tmp0, vec4Tmp1, vec4Tmp2); - result.resize(vec3Tmp0); + Matrix4f.mapObjToWin(tmp.set(getMinX(), getMinY(), objZ), mat4PMv, viewport, winPos); + result.resize(winPos); + + Matrix4f.mapObjToWin(tmp.set(getMinX(), getMaxY(), objZ), mat4PMv, viewport, winPos); + result.resize(winPos); - FloatUtil.mapObjToWin(getMaxX(), getMinY(), objZ, mat4PMv, view, vec3Tmp0, vec4Tmp1, vec4Tmp2); - result.resize(vec3Tmp0); + Matrix4f.mapObjToWin(tmp.set(getMaxX(), getMaxY(), objZ), mat4PMv, viewport, winPos); + result.resize(winPos); - FloatUtil.mapObjToWin(getMaxX(), getMaxY(), objZ, mat4PMv, view, vec3Tmp0, vec4Tmp1, vec4Tmp2); - result.resize(vec3Tmp0); + Matrix4f.mapObjToWin(tmp.set(getMaxX(), getMinY(), objZ), mat4PMv, viewport, winPos); + result.resize(winPos); } if( DEBUG ) { - System.err.printf("AABBox.mapToWindow: view[%d, %d], this %s -> %s%n", view[0], view[1], toString(), result.toString()); + System.err.printf("AABBox.mapToWindow: view[%s], this %s -> %s%n", viewport, toString(), result.toString()); } return result; } diff --git a/src/jogl/classes/com/jogamp/opengl/math/geom/Frustum.java b/src/jogl/classes/com/jogamp/opengl/math/geom/Frustum.java index 4d098cb72..f72154827 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/geom/Frustum.java +++ b/src/jogl/classes/com/jogamp/opengl/math/geom/Frustum.java @@ -37,7 +37,7 @@ import com.jogamp.opengl.math.geom.Frustum.FovDesc; /** * Providing frustum {@link #getPlanes() planes} derived by different inputs - * ({@link #updateByPMV(float[], int) P*MV}, ..) used to classify objects + * ({@link #updateFrustumPlanes(float[], int) P*MV}, ..) used to classify objects * <ul> * <li> {@link #classifyPoint(float[]) point} </li> * <li> {@link #classifySphere(float[], float) sphere} </li> @@ -120,7 +120,7 @@ public class Frustum { * Use one of the <code>update(..)</code> methods to set the {@link #getPlanes() planes}. * </p> * @see #updateByPlanes(Plane[]) - * @see #updateByPMV(float[], int) + * @see #updateFrustumPlanes(float[], int) */ public Frustum() { for (int i = 0; i < 6; ++i) { @@ -162,11 +162,6 @@ public class Frustum { } /** Return distance of plane to given point, see {@link #distanceTo(float, float, float)}. */ - public final float distanceTo(final float[] p) { - return n.x() * p[0] + n.y() * p[1] + n.z() * p[2] + d; - } - - /** Return distance of plane to given point, see {@link #distanceTo(float, float, float)}. */ public final float distanceTo(final Vec3f p) { return n.x() * p.x() + n.y() * p.y() + n.z() * p.z() + d; } @@ -254,87 +249,14 @@ public class Frustum { /** * Calculate the frustum planes in world coordinates - * using the passed float[16] as premultiplied P*MV (column major order). + * using the passed premultiplied P*MV (column major order) matrix. * <p> * Frustum plane's normals will point to the inside of the viewing frustum, * as required by this class. * </p> */ - public void updateByPMV(final float[] pmv, final int pmv_off) { - // Left: a = m41 + m11, b = m42 + m12, c = m43 + m13, d = m44 + m14 - [1..4] column-major - // Left: a = m30 + m00, b = m31 + m01, c = m32 + m02, d = m33 + m03 - [0..3] column-major - { - final Plane p = planes[LEFT]; - final Vec3f p_n = p.n; - p_n.set( pmv[ pmv_off + 3 + 0 * 4 ] + pmv[ pmv_off + 0 + 0 * 4 ], - pmv[ pmv_off + 3 + 1 * 4 ] + pmv[ pmv_off + 0 + 1 * 4 ], - pmv[ pmv_off + 3 + 2 * 4 ] + pmv[ pmv_off + 0 + 2 * 4 ] ); - p.d = pmv[ pmv_off + 3 + 3 * 4 ] + pmv[ pmv_off + 0 + 3 * 4 ]; - } - - // Right: a = m41 - m11, b = m42 - m12, c = m43 - m13, d = m44 - m14 - [1..4] column-major - // Right: a = m30 - m00, b = m31 - m01, c = m32 - m02, d = m33 - m03 - [0..3] column-major - { - final Plane p = planes[RIGHT]; - final Vec3f p_n = p.n; - p_n.set( pmv[ pmv_off + 3 + 0 * 4 ] - pmv[ pmv_off + 0 + 0 * 4 ], - pmv[ pmv_off + 3 + 1 * 4 ] - pmv[ pmv_off + 0 + 1 * 4 ], - pmv[ pmv_off + 3 + 2 * 4 ] - pmv[ pmv_off + 0 + 2 * 4 ] ); - p.d = pmv[ pmv_off + 3 + 3 * 4 ] - pmv[ pmv_off + 0 + 3 * 4 ]; - } - - // Bottom: a = m41 + m21, b = m42 + m22, c = m43 + m23, d = m44 + m24 - [1..4] column-major - // Bottom: a = m30 + m10, b = m31 + m11, c = m32 + m12, d = m33 + m13 - [0..3] column-major - { - final Plane p = planes[BOTTOM]; - final Vec3f p_n = p.n; - p_n.set( pmv[ pmv_off + 3 + 0 * 4 ] + pmv[ pmv_off + 1 + 0 * 4 ], - pmv[ pmv_off + 3 + 1 * 4 ] + pmv[ pmv_off + 1 + 1 * 4 ], - pmv[ pmv_off + 3 + 2 * 4 ] + pmv[ pmv_off + 1 + 2 * 4 ] ); - p.d = pmv[ pmv_off + 3 + 3 * 4 ] + pmv[ pmv_off + 1 + 3 * 4 ]; - } - - // Top: a = m41 - m21, b = m42 - m22, c = m43 - m23, d = m44 - m24 - [1..4] column-major - // Top: a = m30 - m10, b = m31 - m11, c = m32 - m12, d = m33 - m13 - [0..3] column-major - { - final Plane p = planes[TOP]; - final Vec3f p_n = p.n; - p_n.set( pmv[ pmv_off + 3 + 0 * 4 ] - pmv[ pmv_off + 1 + 0 * 4 ], - pmv[ pmv_off + 3 + 1 * 4 ] - pmv[ pmv_off + 1 + 1 * 4 ], - pmv[ pmv_off + 3 + 2 * 4 ] - pmv[ pmv_off + 1 + 2 * 4 ] ); - p.d = pmv[ pmv_off + 3 + 3 * 4 ] - pmv[ pmv_off + 1 + 3 * 4 ]; - } - - // Near: a = m41 + m31, b = m42 + m32, c = m43 + m33, d = m44 + m34 - [1..4] column-major - // Near: a = m30 + m20, b = m31 + m21, c = m32 + m22, d = m33 + m23 - [0..3] column-major - { - final Plane p = planes[NEAR]; - final Vec3f p_n = p.n; - p_n.set( pmv[ pmv_off + 3 + 0 * 4 ] + pmv[ pmv_off + 2 + 0 * 4 ], - pmv[ pmv_off + 3 + 1 * 4 ] + pmv[ pmv_off + 2 + 1 * 4 ], - pmv[ pmv_off + 3 + 2 * 4 ] + pmv[ pmv_off + 2 + 2 * 4 ] ); - p.d = pmv[ pmv_off + 3 + 3 * 4 ] + pmv[ pmv_off + 2 + 3 * 4 ]; - } - - // Far: a = m41 - m31, b = m42 - m32, c = m43 - m33, d = m44 - m34 - [1..4] column-major - // Far: a = m30 - m20, b = m31 - m21, c = m32 + m22, d = m33 + m23 - [0..3] column-major - { - final Plane p = planes[FAR]; - final Vec3f p_n = p.n; - p_n.set( pmv[ pmv_off + 3 + 0 * 4 ] - pmv[ pmv_off + 2 + 0 * 4 ], - pmv[ pmv_off + 3 + 1 * 4 ] - pmv[ pmv_off + 2 + 1 * 4 ], - pmv[ pmv_off + 3 + 2 * 4 ] - pmv[ pmv_off + 2 + 2 * 4 ] ); - p.d = pmv[ pmv_off + 3 + 3 * 4 ] - pmv[ pmv_off + 2 + 3 * 4 ]; - } - - // Normalize all planes - for (int i = 0; i < 6; ++i) { - final Plane p = planes[i]; - final Vec3f p_n = p.n; - final float invLen = 1f / p_n.length(); - p_n.scale(invLen); - p.d *= invLen; - } + public void updateFrustumPlanes(final Matrix4f pmv) { + pmv.updateFrustumPlanes(this); } private static final boolean isOutsideImpl(final Plane p, final AABBox box) { @@ -380,7 +302,7 @@ public class Frustum { * @param p the point * @return {@link Location} of point related to frustum planes */ - public final Location classifyPoint(final float[] p) { + public final Location classifyPoint(final Vec3f p) { Location res = Location.INSIDE; for (int i = 0; i < 6; ++i) { @@ -400,7 +322,7 @@ public class Frustum { * @param p the point * @return true if outside of the frustum, otherwise inside or on a plane */ - public final boolean isPointOutside(final float[] p) { + public final boolean isPointOutside(final Vec3f p) { return Location.OUTSIDE == classifyPoint(p); } @@ -411,7 +333,7 @@ public class Frustum { * @param radius radius of the sphere * @return {@link Location} of point related to frustum planes */ - public final Location classifySphere(final float[] p, final float radius) { + public final Location classifySphere(final Vec3f p, final float radius) { Location res = Location.INSIDE; // fully inside for (int i = 0; i < 6; ++i) { @@ -434,7 +356,7 @@ public class Frustum { * @param radius radius of the sphere * @return true if outside of the frustum, otherwise inside or intersecting */ - public final boolean isSphereOutside(final float[] p, final float radius) { + public final boolean isSphereOutside(final Vec3f p, final float radius) { return Location.OUTSIDE == classifySphere(p, radius); } |