diff options
Diffstat (limited to 'src/jogl/classes')
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/FloatUtil.java | 289 | ||||
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java | 47 | ||||
-rw-r--r-- | src/jogl/classes/jogamp/opengl/ProjectFloat.java (renamed from src/jogl/classes/com/jogamp/opengl/util/ProjectFloat.java) | 357 |
3 files changed, 349 insertions, 344 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/FloatUtil.java b/src/jogl/classes/com/jogamp/opengl/FloatUtil.java new file mode 100644 index 000000000..54fac46e6 --- /dev/null +++ b/src/jogl/classes/com/jogamp/opengl/FloatUtil.java @@ -0,0 +1,289 @@ +/** + * Copyright 2010 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; + +import java.nio.FloatBuffer; + +/** + * Basic Float math utility functions. + * <p> + * Derived from ProjectFloat.java - Created 11-jan-2004 + * </p> + * + * @author Erik Duijs + * @author Kenneth Russell + * @author Sven Gothel + */ +public class FloatUtil { + private static final float[] IDENTITY_MATRIX = + 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, + 0.0f, 0.0f, 0.0f, 1.0f }; + + private static final float[] ZERO_MATRIX = + new float[] { + 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f }; + + /** + * Make matrix an identity matrix + */ + public static final void makeIdentityf(float[] m, int offset) { + for (int i = 0; i < 16; i++) { + m[i+offset] = IDENTITY_MATRIX[i]; + } + } + + /** + * Make matrix an identity matrix + */ + public static final void makeIdentityf(FloatBuffer m) { + int oldPos = m.position(); + m.put(IDENTITY_MATRIX); + m.position(oldPos); + } + + /** + * Make matrix an zero matrix + */ + public static final void makeZero(float[] m, int offset) { + for (int i = 0; i < 16; i++) { + m[i+offset] = 0; + } + } + + /** + * Make matrix an zero matrix + */ + public static final void makeZero(FloatBuffer m) { + int oldPos = m.position(); + m.put(ZERO_MATRIX); + m.position(oldPos); + } + + /** + * @param a + * @param b + * @param d result a*b + */ + 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]; + 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 + */ + 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]; + 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] ); + d.put(dP+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 + */ + 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); + 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] ); + d.put(dP+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 + */ + 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); + 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) ); + d.put(dP+i+3*4 , ai0 * b.get(bP+0+3*4) + ai1 * b.get(bP+1+3*4) + ai2 * b.get(bP+2+3*4) + ai3 * b.get(bP+3+3*4) ); + } + } + + + /** + * Normalize vector + * + * @param v makes len(v)==1 + */ + public static final void normalize(float[] v) { + float r = (float) Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); + + if ( r == 0.0 || r == 1.0) { + return; + } + + r = 1.0f / r; + + v[0] *= r; + v[1] *= r; + v[2] *= r; + } + + /** + * Normalize vector + * + * @param v makes len(v)==1 + */ + public static final void normalize(FloatBuffer v) { + final int vPos = v.position(); + + float r = (float) Math.sqrt(v.get(0+vPos) * v.get(0+vPos) + + v.get(1+vPos) * v.get(1+vPos) + + v.get(2+vPos) * v.get(2+vPos)); + + if ( r == 0.0 || r == 1.0) { + return; + } + + r = 1.0f / r; + + v.put(0+vPos, v.get(0+vPos) * r); + v.put(1+vPos, v.get(1+vPos) * r); + v.put(2+vPos, v.get(2+vPos) * r); + } + + + /** + * Calculate cross-product + * + * @param v1 + * @param v2 + * @param result v1 X v2 + */ + public static final void cross(float[] v1, float[] v2, float[] result) { + result[0] = v1[1] * v2[2] - v1[2] * v2[1]; + result[1] = v1[2] * v2[0] - v1[0] * v2[2]; + result[2] = v1[0] * v2[1] - v1[1] * v2[0]; + } + + /** + * Calculate cross-product + * + * @param v1 + * @param v2 + * @param result v1 X v2 + */ + public static final void cross(FloatBuffer v1, FloatBuffer v2, FloatBuffer result) { + final int v1Pos = v1.position(); + final int v2Pos = v2.position(); + final int rPos = result.position(); + + result.put(0+rPos, v1.get(1+v1Pos) * v2.get(2+v2Pos) - v1.get(2+v1Pos) * v2.get(1+v2Pos)); + result.put(1+rPos, v1.get(2+v1Pos) * v2.get(0+v2Pos) - v1.get(0+v1Pos) * v2.get(2+v2Pos)); + result.put(2+rPos, v1.get(0+v1Pos) * v2.get(1+v2Pos) - v1.get(1+v1Pos) * v2.get(0+v2Pos)); + } + + /** + * Method __gluMultMatrixVecf + * + * @param m_in + * @param m_in_off + * @param v_in + * @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++) { + 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] + + v_in[2+v_in_off] * m_in[2*4+i+m_in_off] + + v_in[3+v_in_off] * m_in[3*4+i+m_in_off]; + } + } + + /** + * Method __gluMultMatrixVecf + * + * @param m_in + * @param m_in_off + * @param v_in + * @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++) { + v_out[i] = + v_in[0] * m_in[0*4+i] + + v_in[1] * m_in[1*4+i] + + v_in[2] * m_in[2*4+i] + + v_in[3] * m_in[3*4+i]; + } + } + + /** + * Method __gluMultMatrixVecf + * + * @param m_in + * @param v_in + * @param v_out m_in * v_in + */ + public static final void multMatrixVecf(FloatBuffer m_in, FloatBuffer v_in, FloatBuffer v_out) { + int inPos = v_in.position(); + int outPos = v_out.position(); + int matrixPos = m_in.position(); + for (int i = 0; i < 4; i++) { + 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) + + v_in.get(2+inPos) * m_in.get(2*4+i+matrixPos) + + v_in.get(3+inPos) * m_in.get(3*4+i+matrixPos)); + } + } + +}
\ 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 929eb3d28..fc6ef3ede 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java +++ b/src/jogl/classes/com/jogamp/opengl/util/PMVMatrix.java @@ -44,6 +44,9 @@ import javax.media.opengl.GL; import javax.media.opengl.GLException; import javax.media.opengl.fixedfunc.GLMatrixFunc; +import jogamp.opengl.ProjectFloat; + +import com.jogamp.opengl.FloatUtil; import com.jogamp.common.nio.Buffers; public class PMVMatrix implements GLMatrixFunc { @@ -89,22 +92,22 @@ public class PMVMatrix implements GLMatrixFunc { matrixBuffer.mark(); } - matrixIdent = ProjectFloat.slice2Float(matrixBuffer, matrixBufferArray, 0*16, 1*16); // I - matrixTex = ProjectFloat.slice2Float(matrixBuffer, matrixBufferArray, 1*16, 1*16); // T - matrixPMvMvit = ProjectFloat.slice2Float(matrixBuffer, matrixBufferArray, 2*16, 4*16); // P + Mv + Mvi + Mvit - matrixPMvMvi = ProjectFloat.slice2Float(matrixBuffer, matrixBufferArray, 2*16, 3*16); // P + Mv + Mvi - matrixPMv = ProjectFloat.slice2Float(matrixBuffer, matrixBufferArray, 2*16, 2*16); // P + Mv - matrixP = ProjectFloat.slice2Float(matrixBuffer, matrixBufferArray, 2*16, 1*16); // P - matrixMv = ProjectFloat.slice2Float(matrixBuffer, matrixBufferArray, 3*16, 1*16); // Mv - matrixMvi = ProjectFloat.slice2Float(matrixBuffer, matrixBufferArray, 4*16, 1*16); // Mvi - matrixMvit = ProjectFloat.slice2Float(matrixBuffer, matrixBufferArray, 5*16, 1*16); // Mvit + matrixIdent = Buffers.slice2Float(matrixBuffer, matrixBufferArray, 0*16, 1*16); // I + matrixTex = Buffers.slice2Float(matrixBuffer, matrixBufferArray, 1*16, 1*16); // T + matrixPMvMvit = Buffers.slice2Float(matrixBuffer, matrixBufferArray, 2*16, 4*16); // P + Mv + Mvi + Mvit + matrixPMvMvi = Buffers.slice2Float(matrixBuffer, matrixBufferArray, 2*16, 3*16); // P + Mv + Mvi + matrixPMv = Buffers.slice2Float(matrixBuffer, matrixBufferArray, 2*16, 2*16); // P + Mv + matrixP = Buffers.slice2Float(matrixBuffer, matrixBufferArray, 2*16, 1*16); // P + matrixMv = Buffers.slice2Float(matrixBuffer, matrixBufferArray, 3*16, 1*16); // Mv + matrixMvi = Buffers.slice2Float(matrixBuffer, matrixBufferArray, 4*16, 1*16); // Mvi + matrixMvit = Buffers.slice2Float(matrixBuffer, matrixBufferArray, 5*16, 1*16); // Mvit projectFloat = new ProjectFloat(matrixBuffer, matrixBufferArray, 6*16); if(null != matrixBuffer) { matrixBuffer.reset(); } - ProjectFloat.makeIdentityf(matrixIdent); + FloatUtil.makeIdentityf(matrixIdent); vec3f = new float[3]; matrixMult = new float[16]; @@ -113,11 +116,11 @@ public class PMVMatrix implements GLMatrixFunc { matrixScale = new float[16]; matrixOrtho = new float[16]; matrixFrustum = new float[16]; - ProjectFloat.makeIdentityf(matrixTrans, 0); - ProjectFloat.makeIdentityf(matrixRot, 0); - ProjectFloat.makeIdentityf(matrixScale, 0); - ProjectFloat.makeIdentityf(matrixOrtho, 0); - ProjectFloat.makeZero(matrixFrustum, 0); + FloatUtil.makeIdentityf(matrixTrans, 0); + FloatUtil.makeIdentityf(matrixRot, 0); + FloatUtil.makeIdentityf(matrixScale, 0); + FloatUtil.makeIdentityf(matrixOrtho, 0); + FloatUtil.makeZero(matrixFrustum, 0); matrixPStack = new ArrayList<float[]>(); matrixMvStack= new ArrayList<float[]>(); @@ -461,26 +464,26 @@ public class PMVMatrix implements GLMatrixFunc { public final void glMultMatrixf(final FloatBuffer m) { if(matrixMode==GL_MODELVIEW) { - ProjectFloat.multMatrixf(matrixMv, m, matrixMv); + FloatUtil.multMatrixf(matrixMv, m, matrixMv); modified |= DIRTY_MODELVIEW ; } else if(matrixMode==GL_PROJECTION) { - ProjectFloat.multMatrixf(matrixP, m, matrixP); + FloatUtil.multMatrixf(matrixP, m, matrixP); modified |= DIRTY_PROJECTION ; } else if(matrixMode==GL.GL_TEXTURE) { - ProjectFloat.multMatrixf(matrixTex, m, matrixTex); + FloatUtil.multMatrixf(matrixTex, m, matrixTex); modified |= DIRTY_TEXTURE ; } } public void glMultMatrixf(float[] m, int m_offset) { if(matrixMode==GL_MODELVIEW) { - ProjectFloat.multMatrixf(matrixMv, m, m_offset, matrixMv); + FloatUtil.multMatrixf(matrixMv, m, m_offset, matrixMv); modified |= DIRTY_MODELVIEW ; } else if(matrixMode==GL_PROJECTION) { - ProjectFloat.multMatrixf(matrixP, m, m_offset, matrixP); + FloatUtil.multMatrixf(matrixP, m, m_offset, matrixP); modified |= DIRTY_PROJECTION ; } else if(matrixMode==GL.GL_TEXTURE) { - ProjectFloat.multMatrixf(matrixTex, m, m_offset, matrixTex); + FloatUtil.multMatrixf(matrixTex, m, m_offset, matrixTex); modified |= DIRTY_TEXTURE ; } } @@ -504,7 +507,7 @@ public class PMVMatrix implements GLMatrixFunc { final float s = (float)Math.sin(angrad); vec3f[0]=x; vec3f[1]=y; vec3f[2]=z; - ProjectFloat.normalize(vec3f); + FloatUtil.normalize(vec3f); x = vec3f[0]; y = vec3f[1]; z = vec3f[2]; // Rotation matrix: diff --git a/src/jogl/classes/com/jogamp/opengl/util/ProjectFloat.java b/src/jogl/classes/jogamp/opengl/ProjectFloat.java index 97915502d..bce3f3cfc 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/ProjectFloat.java +++ b/src/jogl/classes/jogamp/opengl/ProjectFloat.java @@ -113,315 +113,28 @@ * in the design, construction, operation or maintenance of any nuclear * facility. */ -package com.jogamp.opengl.util; +package jogamp.opengl; import java.nio.Buffer; -import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.media.opengl.fixedfunc.GLMatrixFunc; import com.jogamp.common.nio.Buffers; +import com.jogamp.opengl.FloatUtil; /** * ProjectFloat.java - * <p/> - * <p/> + * <p> * Created 11-jan-2004 + * </p> * * @author Erik Duijs * @author Kenneth Russell * @author Sven Gothel */ public class ProjectFloat { - private static final float[] IDENTITY_MATRIX = - 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, - 0.0f, 0.0f, 0.0f, 1.0f }; - - private static final float[] ZERO_MATRIX = - new float[] { - 0.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 0.0f }; - - /** - * Make matrix an identity matrix - */ - public static void makeIdentityf(float[] m, int offset) { - for (int i = 0; i < 16; i++) { - m[i+offset] = IDENTITY_MATRIX[i]; - } - } - - /** - * Make matrix an identity matrix - */ - public static void makeIdentityf(FloatBuffer m) { - int oldPos = m.position(); - m.put(IDENTITY_MATRIX); - m.position(oldPos); - } - - /** - * Make matrix an zero matrix - */ - public static void makeZero(float[] m, int offset) { - for (int i = 0; i < 16; i++) { - m[i+offset] = 0; - } - } - - /** - * Make matrix an zero matrix - */ - public static void makeZero(FloatBuffer m) { - int oldPos = m.position(); - m.put(ZERO_MATRIX); - m.position(oldPos); - } - - /** - * @param a - * @param b - * @param d result a*b - */ - 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]; - 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 - */ - 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]; - 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] ); - d.put(dP+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 - */ - 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); - 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] ); - d.put(dP+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 - */ - 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); - 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) ); - d.put(dP+i+3*4 , ai0 * b.get(bP+0+3*4) + ai1 * b.get(bP+1+3*4) + ai2 * b.get(bP+2+3*4) + ai3 * b.get(bP+3+3*4) ); - } - } - - - /** - * Normalize vector - * - * @param v makes len(v)==1 - */ - public static void normalize(float[] v) { - float r = (float) Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); - - if ( r == 0.0 || r == 1.0) { - return; - } - - r = 1.0f / r; - - v[0] *= r; - v[1] *= r; - v[2] *= r; - } - - /** - * Normalize vector - * - * @param v makes len(v)==1 - */ - public static void normalize(FloatBuffer v) { - final int vPos = v.position(); - - float r = (float) Math.sqrt(v.get(0+vPos) * v.get(0+vPos) + - v.get(1+vPos) * v.get(1+vPos) + - v.get(2+vPos) * v.get(2+vPos)); - - if ( r == 0.0 || r == 1.0) { - return; - } - - r = 1.0f / r; - - v.put(0+vPos, v.get(0+vPos) * r); - v.put(1+vPos, v.get(1+vPos) * r); - v.put(2+vPos, v.get(2+vPos) * r); - } - - - /** - * Calculate cross-product - * - * @param v1 - * @param v2 - * @param result v1 X v2 - */ - public static void cross(float[] v1, float[] v2, float[] result) { - result[0] = v1[1] * v2[2] - v1[2] * v2[1]; - result[1] = v1[2] * v2[0] - v1[0] * v2[2]; - result[2] = v1[0] * v2[1] - v1[1] * v2[0]; - } - - /** - * Calculate cross-product - * - * @param v1 - * @param v2 - * @param result v1 X v2 - */ - public static void cross(FloatBuffer v1, FloatBuffer v2, FloatBuffer result) { - final int v1Pos = v1.position(); - final int v2Pos = v2.position(); - final int rPos = result.position(); - - result.put(0+rPos, v1.get(1+v1Pos) * v2.get(2+v2Pos) - v1.get(2+v1Pos) * v2.get(1+v2Pos)); - result.put(1+rPos, v1.get(2+v1Pos) * v2.get(0+v2Pos) - v1.get(0+v1Pos) * v2.get(2+v2Pos)); - result.put(2+rPos, v1.get(0+v1Pos) * v2.get(1+v2Pos) - v1.get(1+v1Pos) * v2.get(0+v2Pos)); - } - - /** - * Method __gluMultMatrixVecf - * - * @param m_in - * @param m_in_off - * @param v_in - * @param v_out m_in * v_in - */ - public static 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++) { - 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] + - v_in[2+v_in_off] * m_in[2*4+i+m_in_off] + - v_in[3+v_in_off] * m_in[3*4+i+m_in_off]; - } - } - - /** - * Method __gluMultMatrixVecf - * - * @param m_in - * @param m_in_off - * @param v_in - * @param v_out m_in * v_in - */ - public static void multMatrixVecf(float[] m_in, float[] v_in, float[] v_out) { - for (int i = 0; i < 4; i++) { - v_out[i] = - v_in[0] * m_in[0*4+i] + - v_in[1] * m_in[1*4+i] + - v_in[2] * m_in[2*4+i] + - v_in[3] * m_in[3*4+i]; - } - } - - /** - * Method __gluMultMatrixVecf - * - * @param m_in - * @param v_in - * @param v_out m_in * v_in - */ - public static void multMatrixVecf(FloatBuffer m_in, FloatBuffer v_in, FloatBuffer v_out) { - int inPos = v_in.position(); - int outPos = v_out.position(); - int matrixPos = m_in.position(); - for (int i = 0; i < 4; i++) { - 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) + - v_in.get(2+inPos) * m_in.get(2*4+i+matrixPos) + - v_in.get(3+inPos) * m_in.get(3*4+i+matrixPos)); - } - } - - /** - * Slices a ByteBuffer or a primitive float array to a FloatBuffer at the given position with the given size - * in float-space. - * <p> - * Using a ByteBuffer as the source guarantees - * keeping the source native order programmatically. - * This works around <a href="http://code.google.com/p/android/issues/detail?id=16434">Honeycomb / Android 3.0 Issue 16434</a>. - * This bug is resolved at least in Android 3.2. - * </p> - * - * @param buf source Buffer, maybe ByteBuffer (recommended) or FloatBuffer or <code>null</code>. - * Buffer's position is ignored and floatPos is being used. - * @param backing source float array or <code>null</code> - * @param floatPos {@link Buffers#SIZEOF_FLOAT} position - * @param floatSize {@link Buffers#SIZEOF_FLOAT} size - * @return FloatBuffer w/ native byte order as given ByteBuffer - */ - public static FloatBuffer slice2Float(Buffer buf, float[] backing, int floatPos, int floatSize) { - if(buf instanceof ByteBuffer) { - ByteBuffer bb = (ByteBuffer) buf; - bb.position( floatPos * Buffers.SIZEOF_FLOAT ); - bb.limit( (floatPos + floatSize) * Buffers.SIZEOF_FLOAT ); - FloatBuffer fb = bb.slice().order(bb.order()).asFloatBuffer(); // slice and duplicate may change byte order - fb.mark(); - return fb; - } else if(null != backing) { - FloatBuffer fb = FloatBuffer.wrap(backing, floatPos, floatSize); - fb.mark(); - return fb; - } else if(buf instanceof FloatBuffer) { - FloatBuffer fb = (FloatBuffer) buf; - fb.position( floatPos ); - fb.limit( floatPos + floatSize ); - FloatBuffer fb0 = fb.slice(); // slice and duplicate may change byte order - fb0.mark(); - return fb0; - } else { - throw new InternalError("XXX"); - } - } - public static final int getRequiredFloatBufferSize() { return 2*16+2*4+3*3; } // Note that we have cloned parts of the implementation in order to @@ -471,21 +184,21 @@ public class ProjectFloat { public ProjectFloat(Buffer floatBuffer, float[] floatArray, int floatOffset) { int floatPos = floatOffset; int floatSize = 16; - matrixBuf = slice2Float(floatBuffer, floatArray, floatPos, floatSize); + matrixBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); floatPos += floatSize; - tempInvertMatrixBuf = slice2Float(floatBuffer, floatArray, floatPos, floatSize); + tempInvertMatrixBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); floatPos += floatSize; floatSize = 4; - inBuf = slice2Float(floatBuffer, floatArray, floatPos, floatSize); + inBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); floatPos += floatSize; - outBuf = slice2Float(floatBuffer, floatArray, floatPos, floatSize); + outBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); floatPos += floatSize; floatSize = 3; - forwardBuf = slice2Float(floatBuffer, floatArray, floatPos, floatSize); + forwardBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); floatPos += floatSize; - sideBuf = slice2Float(floatBuffer, floatArray, floatPos, floatSize); + sideBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); floatPos += floatSize; - upBuf = slice2Float(floatBuffer, floatArray, floatPos, floatSize); + upBuf = Buffers.slice2Float(floatBuffer, floatArray, floatPos, floatSize); } public void destroy() { @@ -515,7 +228,7 @@ public class ProjectFloat { temp[i][j] = src[i*4+j+srcOffset]; } } - makeIdentityf(inverse, inverseOffset); + FloatUtil.makeIdentityf(inverse, inverseOffset); for (i = 0; i < 4; i++) { // @@ -589,7 +302,7 @@ public class ProjectFloat { temp.put(i*4+j, src.get(i*4+j + srcPos)); } } - makeIdentityf(inverse); + FloatUtil.makeIdentityf(inverse); for (i = 0; i < 4; i++) { // @@ -677,7 +390,7 @@ public class ProjectFloat { cotangent = (float) Math.cos(radians) / sine; - makeIdentityf(matrixBuf); + FloatUtil.makeIdentityf(matrixBuf); matrixBuf.put(0 * 4 + 0, cotangent / aspect); matrixBuf.put(1 * 4 + 1, cotangent); @@ -718,16 +431,16 @@ public class ProjectFloat { up.put(1, upy); up.put(2, upz); - normalize(forward); + FloatUtil.normalize(forward); /* Side = forward x up */ - cross(forward, up, side); - normalize(side); + FloatUtil.cross(forward, up, side); + FloatUtil.normalize(side); /* Recompute up as: up = side x forward */ - cross(side, forward, up); + FloatUtil.cross(side, forward, up); - makeIdentityf(matrixBuf); + FloatUtil.makeIdentityf(matrixBuf); matrixBuf.put(0 * 4 + 0, side.get(0)); matrixBuf.put(1 * 4 + 0, side.get(1)); matrixBuf.put(2 * 4 + 0, side.get(2)); @@ -771,8 +484,8 @@ public class ProjectFloat { in[2] = objz; in[3] = 1.0f; - multMatrixVecf(modelMatrix, modelMatrix_offset, in, 0, out); - multMatrixVecf(projMatrix, projMatrix_offset, out, 0, in); + FloatUtil.multMatrixVecf(modelMatrix, modelMatrix_offset, in, 0, out); + FloatUtil.multMatrixVecf(projMatrix, projMatrix_offset, out, 0, in); if (in[3] == 0.0f) { return false; @@ -807,8 +520,8 @@ public class ProjectFloat { in.put(2, objz); in.put(3, 1.0f); - multMatrixVecf(modelMatrix, in, out); - multMatrixVecf(projMatrix, out, in); + FloatUtil.multMatrixVecf(modelMatrix, in, out); + FloatUtil.multMatrixVecf(projMatrix, out, in); if (in.get(3) == 0.0f) { return false; @@ -856,8 +569,8 @@ public class ProjectFloat { in.put(2, objz); in.put(3, 1.0f); - multMatrixVecf(modelMatrix, in, out); - multMatrixVecf(projMatrix, out, in); + FloatUtil.multMatrixVecf(modelMatrix, in, out); + FloatUtil.multMatrixVecf(projMatrix, out, in); if (in.get(3) == 0.0f) { return false; @@ -902,7 +615,7 @@ public class ProjectFloat { float[] in = this.in; float[] out = this.out; - multMatrixf(modelMatrix, modelMatrix_offset, projMatrix, projMatrix_offset, matrix, 0); + FloatUtil.multMatrixf(modelMatrix, modelMatrix_offset, projMatrix, projMatrix_offset, matrix, 0); if (!gluInvertMatrixf(matrix, 0, matrix, 0)) { return false; @@ -922,7 +635,7 @@ public class ProjectFloat { in[1] = in[1] * 2 - 1; in[2] = in[2] * 2 - 1; - multMatrixVecf(matrix, in, out); + FloatUtil.multMatrixVecf(matrix, in, out); if (out[3] == 0.0) { return false; @@ -946,7 +659,7 @@ public class ProjectFloat { FloatBuffer in = this.inBuf; FloatBuffer out = this.outBuf; - multMatrixf(modelMatrix, projMatrix, matrixBuf); + FloatUtil.multMatrixf(modelMatrix, projMatrix, matrixBuf); if (!gluInvertMatrixf(matrixBuf, matrixBuf)) { return false; @@ -966,7 +679,7 @@ public class ProjectFloat { in.put(1, in.get(1) * 2 - 1); in.put(2, in.get(2) * 2 - 1); - multMatrixVecf(matrixBuf, in, out); + FloatUtil.multMatrixVecf(matrixBuf, in, out); if (out.get(3) == 0.0f) { return false; @@ -1002,7 +715,7 @@ public class ProjectFloat { FloatBuffer in = this.inBuf; FloatBuffer out = this.outBuf; - multMatrixf(modelMatrix, projMatrix, matrixBuf); + FloatUtil.multMatrixf(modelMatrix, projMatrix, matrixBuf); if (!gluInvertMatrixf(matrixBuf, matrixBuf)) { return false; @@ -1024,7 +737,7 @@ public class ProjectFloat { in.put(1, in.get(1) * 2 - 1); in.put(2, in.get(2) * 2 - 1); - multMatrixVecf(matrixBuf, in, out); + FloatUtil.multMatrixVecf(matrixBuf, in, out); if (out.get(3) == 0.0f) { return false; @@ -1073,7 +786,7 @@ public class ProjectFloat { float[] in = this.in; float[] out = this.out; - multMatrixf(modelMatrix, modelMatrix_offset, projMatrix, projMatrix_offset, matrix, 0); + FloatUtil.multMatrixf(modelMatrix, modelMatrix_offset, projMatrix, projMatrix_offset, matrix, 0); if (!gluInvertMatrixf(matrix, 0, matrix, 0)) return false; @@ -1093,7 +806,7 @@ public class ProjectFloat { in[1] = in[1] * 2 - 1; in[2] = in[2] * 2 - 1; - multMatrixVecf(matrix, in, out); + FloatUtil.multMatrixVecf(matrix, in, out); if (out[3] == 0.0f) return false; @@ -1134,7 +847,7 @@ public class ProjectFloat { FloatBuffer in = this.inBuf; FloatBuffer out = this.outBuf; - multMatrixf(modelMatrix, projMatrix, matrixBuf); + FloatUtil.multMatrixf(modelMatrix, projMatrix, matrixBuf); if (!gluInvertMatrixf(matrixBuf, matrixBuf)) return false; @@ -1155,7 +868,7 @@ public class ProjectFloat { in.put(1, in.get(1) * 2 - 1); in.put(2, in.get(2) * 2 - 1); - multMatrixVecf(matrixBuf, in, out); + FloatUtil.multMatrixVecf(matrixBuf, in, out); if (out.get(3) == 0.0f) return false; |