Build a LookAt matrix from a model matrix - Android java.lang

Android examples for java.lang:Math Matrix

Description

Build a LookAt matrix from a model matrix

Demo Code


import android.opengl.Matrix;

public class Main{
    /**//from w  ww .  j  a v a 2  s  .  co m
     * Cache matrix
     */
    private final static float[] sTemp = new float[32];
    /**
     * Cache lookat vector
     */
    private final static float[] lookAtTemp = new float[8];
    /**
     * Build a LookAt matrix from a model matrix
     * 
     * @param matrix The matrix to set
     * @param matrixOffset The offset in the view matrix
     * @param nodeModel The matrix model
     * @param nodeModelOffset The matrix model offset
     * @param xUp The camera X scale factor
     * @param yUp The camera Y scale factor
     * @param zUp The camera Z scale factor
     */
    public static final void setLookAtMM(final float[] matrix,
            final int matrixOffset, final float[] nodeModel,
            final int nodeModelOffset, final float xUp, final float yUp,
            final float zUp) {
        //android.util.Log.d(TAG,"setLookAtMM()");
        synchronized (lookAtTemp) {
            //eye
            lookAtTemp[0] = lookAtTemp[1] = lookAtTemp[2] = 0;
            lookAtTemp[3] = 1;
            //Direction
            lookAtTemp[4] = lookAtTemp[5] = lookAtTemp[7] = 0;
            lookAtTemp[6] = -1;
            MatrixUtils.multiplyMV(lookAtTemp, 0, nodeModel,
                    nodeModelOffset, lookAtTemp, 0);
            MatrixUtils.multiplyMV(lookAtTemp, 4, nodeModel,
                    nodeModelOffset, lookAtTemp, 4);
            Matrix.setLookAtM(matrix, matrixOffset, lookAtTemp[0],
                    lookAtTemp[1], lookAtTemp[2], lookAtTemp[0]
                            + lookAtTemp[4], lookAtTemp[1] + lookAtTemp[5],
                    lookAtTemp[2] + lookAtTemp[6], xUp, yUp, zUp);
        }
    }
    /**
     * Vector Matrix multiplication (Java version is far faster than JNI one !!!)
     * 
     * @param result The float array that holds the result.
     * @param resultOffset The offset into result array
     * @param lhs The float array that holds the left-hand-side matrix.
     * @param lhsOffset The offset into the lhs array where the lhs is stored
     * @param rhs The float array that holds the right-hand-side matrix
     * @param rhsOffset The offset into the rhs array where the rhs is stored.
     */
    public static void multiplyMV(final float[] result,
            final int resultOffset, final float[] lhs, final int lhsOffset,
            final float[] rhs, final int rhsOffset) {
        //android.util.Log.d(TAG,"multiplyMV()");
        synchronized (sTemp) {
            sTemp[0] = rhs[rhsOffset];
            sTemp[1] = rhs[rhsOffset + 1];
            sTemp[2] = rhs[rhsOffset + 2];
            sTemp[3] = rhs[rhsOffset + 3];
            System.arraycopy(lhs, lhsOffset, sTemp, 4, 16);
            result[3 + resultOffset] = (sTemp[7] * sTemp[0])
                    + (sTemp[11] * sTemp[1]) + (sTemp[15] * sTemp[2])
                    + (sTemp[19] * sTemp[3]);
            result[2 + resultOffset] = (sTemp[6] * sTemp[0])
                    + (sTemp[10] * sTemp[1]) + (sTemp[14] * sTemp[2])
                    + (sTemp[18] * sTemp[3]);
            result[1 + resultOffset] = (sTemp[5] * sTemp[0])
                    + (sTemp[9] * sTemp[1]) + (sTemp[13] * sTemp[2])
                    + (sTemp[17] * sTemp[3]);
            result[0 + resultOffset] = (sTemp[4] * sTemp[0])
                    + (sTemp[8] * sTemp[1]) + (sTemp[12] * sTemp[2])
                    + (sTemp[16] * sTemp[3]);
        }
    }
}

Related Tutorials