Skip to content

Instantly share code, notes, and snippets.

@ucarion
Created April 10, 2013 05:44
Show Gist options
  • Save ucarion/5352105 to your computer and use it in GitHub Desktop.
Save ucarion/5352105 to your computer and use it in GitHub Desktop.
Java OpenGL matrix utility class ... write once, copypaste everwhere.
public class Matrix {
public static float[] toMatrix(
float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33) {
float[] f = {
m00, m10, m20, m30,
m01, m11, m21, m31,
m02, m12, m22, m32,
m03, m13, m23, m33
};
return f;
}
public static float[] identity() {
return toMatrix(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
}
public static float[] translate(float x, float y, float z) {
return toMatrix(
1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1
);
}
public static float[] scale(float x, float y, float z) {
return toMatrix(
x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1
);
}
public static float[] rotate(float[] axis, float angle) {
float c = (float) Math.cos(angle);
float s = (float) Math.sin(angle);
float t = 1 - c;
float x = axis[0], y = axis[1], z = axis[2];
float tx = t * x, ty = t * y;
return toMatrix(
tx * x + c, tx * y - s * z, tx * z + s * y, 0,
tx * y + s * z, ty * y + c, ty * z - s * x, 0,
tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
0, 0, 0, 1
);
}
public static float[] lookAt(float[] eye, float[] center, float[] up) {
float[] f = Vector.normalize(Vector.sub(center, eye));
float[] u = Vector.normalize(up);
float[] s = Vector.normalize(Vector.cross(f, u));
u = Vector.cross(s, f);
return toMatrix(
s[0], s[1], s[2], -Vector.dot(s, eye),
u[0], u[1], u[2], -Vector.dot(u, eye),
-f[0], -f[1], -f[2], Vector.dot(f, eye),
0, 0, 0, 1
);
}
public static float[] perspective(float fovy, float aspect, float zNear, float zFar) {
float halfFovyRadians = (float) Math.toRadians( (fovy / 2.0f));
float range = (float) Math.tan(halfFovyRadians) * zNear;
float left = -range * aspect;
float right = range * aspect;
float bottom = -range;
float top = range;
return toMatrix(
(2f * zNear) / (right - left), 0, 0, 0,
0, (2f * zNear) / (top - bottom), 0, 0,
0, 0, - (zFar + zNear) / (zFar - zNear), - (2f * zFar * zNear) / (zFar - zNear),
0, 0, -1, 0
);
}
}
public class Vector {
public static float[] toVector(float x, float y, float z) {
float[] f = { x, y, z };
return f;
}
public static float[] normalize(float[] v) {
float x = v[0];
float y = v[1];
float z = v[2];
float norm = (float) Math.sqrt(x * x + y * y + z * z);
return toVector(x / norm, y / norm, z / norm);
}
public static float[] add(float[] a, float[] b) {
return toVector(a[0] + b[0], a[1] + b[1], a[2] + b[2]);
}
public static float[] sub(float[] a, float[] b) {
return toVector(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
}
public static float dot(float[] a, float[] b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
public static float[] cross(float[] a, float[] b) {
return toVector(
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment