Java tutorial
//package com.java2s; public class Main { public static void setRotateM(float[] rm, float a, float x, float y, float z) { rm[3] = 0; rm[7] = 0; rm[11] = 0; rm[12] = 0; rm[13] = 0; rm[14] = 0; rm[15] = 1; a *= (float) (Math.PI / 180.0f); float s = (float) Math.sin(a); float c = (float) Math.cos(a); if (1.0f == x && 0.0f == y && 0.0f == z) { rm[5] = c; rm[10] = c; rm[6] = s; rm[9] = -s; rm[1] = 0; rm[2] = 0; rm[4] = 0; rm[8] = 0; rm[0] = 1; } else if (0.0f == x && 1.0f == y && 0.0f == z) { rm[0] = c; rm[10] = c; rm[8] = s; rm[2] = -s; rm[1] = 0; rm[4] = 0; rm[6] = 0; rm[9] = 0; rm[5] = 1; } else if (0.0f == x && 0.0f == y && 1.0f == z) { rm[0] = c; rm[5] = c; rm[1] = s; rm[4] = -s; rm[2] = 0; rm[6] = 0; rm[8] = 0; rm[9] = 0; rm[10] = 1; } else { float len = length(x, y, z); if (1.0f != len) { float recipLen = 1.0f / len; x *= recipLen; y *= recipLen; z *= recipLen; } float nc = 1.0f - c; float xy = x * y; float yz = y * z; float zx = z * x; float xs = x * s; float ys = y * s; float zs = z * s; rm[0] = x * x * nc + c; rm[4] = xy * nc - zs; rm[8] = zx * nc + ys; rm[1] = xy * nc + zs; rm[5] = y * y * nc + c; rm[9] = yz * nc - xs; rm[2] = zx * nc - ys; rm[6] = yz * nc + xs; rm[10] = z * z * nc + c; } } /** * Computes the length of a vector. * * @param x x coordinate of a vector * @param y y coordinate of a vector * @param z z coordinate of a vector * @return the length of a vector */ public static float length(float x, float y, float z) { return (float) Math.sqrt(x * x + y * y + z * z); } }