Rotates matrix m by angle a (in degrees) around the axis (x, y, z) - Android java.lang

Android examples for java.lang:Math Matrix

Description

Rotates matrix m by angle a (in degrees) around the axis (x, y, z)

Demo Code


import android.opengl.Matrix;

public class Main{
    /**//ww w .jav  a 2 s .co m
     * Cache matrix
     */
    private final static float[] sTemp = new float[32];
    /**
     * Rotates matrix m by angle a (in degrees) around the axis (x, y, z)
     * @param rm returns the result
     * @param rmOffset index into rm where the result matrix starts
     * @param m source matrix
     * @param mOffset index into m where the source matrix starts
     * @param a angle to rotate in degrees
     * @param x scale factor x
     * @param y scale factor y
     * @param z scale factor z
     */
    public static void rotateM(float[] rm, int rmOffset, float[] m,
            int mOffset, float a, float x, float y, float z) {
        //android.util.Log.d(TAG,"rotateM()");
        synchronized (sTemp) {
            setRotateM(sTemp, 0, a, x, y, z);
            MatrixUtils.multiplyMM(rm, rmOffset, m, mOffset, sTemp, 0);
        }
    }
    /**
     * 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 multiplyMM(final float[] result,
            final int resultOffset, final float[] lhs, final int lhsOffset,
            final float[] rhs, final int rhsOffset) {
        //android.util.Log.d(TAG,"multiplyMM()");
        for (int i = 0; i < 4; i++) {
            final float rhs_i0 = rhs[rhsOffset + 4 * (i)];
            float ri0 = lhs[lhsOffset] * rhs_i0;
            float ri1 = lhs[lhsOffset + 1] * rhs_i0;
            float ri2 = lhs[lhsOffset + 2] * rhs_i0;
            float ri3 = lhs[lhsOffset + 3] * rhs_i0;
            for (int j = 1; j < 4; j++) {
                final float rhs_ij = rhs[rhsOffset + ((j) + 4 * (i))];
                ri0 += lhs[lhsOffset + 4 * (j)] * rhs_ij;
                ri1 += lhs[lhsOffset + ((1) + 4 * (j))] * rhs_ij;
                ri2 += lhs[lhsOffset + ((2) + 4 * (j))] * rhs_ij;
                ri3 += lhs[lhsOffset + ((3) + 4 * (j))] * rhs_ij;
            }
            result[resultOffset + 4 * (i)] = ri0;
            result[resultOffset + ((1) + 4 * (i))] = ri1;
            result[resultOffset + ((2) + 4 * (i))] = ri2;
            result[resultOffset + ((3) + 4 * (i))] = ri3;
        }
    }
}

Related Tutorials