/*
* Copyright (C) 2014 MillerV
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package aspect.util;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector4f;
/**
*
* @author MillerV
*/
public class Matrix4x4 extends Matrix4f {
private Matrix4x4() {
}
public Matrix4x4(FloatBuffer buff) {
load((Matrix4f)load(buff), this);
}
public FloatBuffer getBuffer() {
FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
store(buffer);
buffer.flip();
return buffer;
}
public static Matrix4x4 identity() {
Matrix4x4 result = new Matrix4x4();
result.setIdentity();
return result;
}
public static Matrix4x4 zero() {
Matrix4x4 result = new Matrix4x4();
result.setZero();
return result;
}
public static Matrix4x4 camLookAt(Vector3 position, Vector3 at, Vector3 up) {
Matrix4x4 matrix = identity();
Vector3 f = position.minus(at).normalize();
Vector3 s = Vector3.cross(up, f).normalize();
Vector3 u = Vector3.cross(f, s).normalize();
matrix.m00 = s.x;
matrix.m10 = s.y;
matrix.m20 = s.z;
matrix.m01 = u.x;
matrix.m11 = u.y;
matrix.m21 = u.z;
matrix.m02 = f.x;
matrix.m12 = f.y;
matrix.m22 = f.z;
return matrix.translate(position.negate());
}
public static Matrix4x4 mdlLookAt(Vector3 position, Vector3 at, Vector3 up) {
return camLookAt(position, at, up).invert();
}
public static Matrix4x4 TRS(Vector3 translation, Angles rotation, Vector3 scale) {
Matrix4x4 m = identity();
m = m.translate(translation);
m = m.rotateY(rotation.yaw);
m = m.rotateX(rotation.pitch);
m = m.rotateZ(rotation.roll);
m = m.scale(scale);
return m;
}
public static Matrix4x4 ITRS(Vector3 translation, Angles rotation, Vector3 scale) {
Matrix4x4 m = identity();
m = m.scale(Vector3.divide(1, scale));
m = m.rotateZ(-rotation.roll);
m = m.rotateX(-rotation.pitch);
m = m.rotateY(-rotation.yaw);
m = m.translate(translation.negate());
return m;
}
public static Matrix4x4 add(Matrix4x4 lhs, Matrix4x4 rhs) {
Matrix4x4 result = new Matrix4x4();
add(lhs, rhs, result);
return result;
}
public static Matrix4x4 sub(Matrix4x4 lhs, Matrix4x4 rhs) {
Matrix4x4 result = new Matrix4x4();
sub(lhs, rhs, result);
return result;
}
public static Matrix4x4 mul(Matrix4x4 lhs, Matrix4x4 rhs) {
Matrix4x4 result = new Matrix4x4();
mul(lhs, rhs, result);
return result;
}
public static Vector3 transformPoint(Matrix4x4 mat, Vector3 vec) {
Vector4f vec4 = new Vector4f(vec.x, vec.y, vec.z, 1.0f);
Vector4f result = new Vector4f();
transform(mat, vec4, result);
return new Vector3(result.x, result.y, result.z);
}
public static Vector3 transformVector(Matrix4x4 mat, Vector3 vec) {
Vector4f vec4 = new Vector4f(vec.x, vec.y, vec.z, 0.0f);
Vector4f result = new Vector4f();
transform(mat, vec4, result);
return new Vector3(result.x, result.y, result.z);
}
public Matrix4x4 plus(Matrix4x4 rhs) {
return add(this, rhs);
}
public Matrix4x4 minus(Matrix4x4 rhs) {
return sub(this, rhs);
}
public Matrix4x4 times(Matrix4x4 rhs) {
return mul(this, rhs);
}
public static Matrix4x4 translate(Matrix4x4 mat, Vector3 trans) {
Matrix4x4 result = new Matrix4x4();
load(mat, result);
translate(trans, result, result);
return result;
}
public static Matrix4x4 scale(Matrix4x4 mat, Vector3 scale) {
Matrix4x4 result = new Matrix4x4();
load(mat, result);
scale(scale, result, result);
return result;
}
public static Matrix4x4 rotate(Matrix4x4 mat, Vector3 axis, float angle) {
Matrix4x4 result = new Matrix4x4();
load(mat, result);
rotate(angle, axis, result, result);
return result;
}
public static Matrix4x4 rotateX(Matrix4x4 mat, float angle) {
return rotate(mat, Vector3.xAxis(), angle);
}
public static Matrix4x4 rotateY(Matrix4x4 mat, float angle) {
return rotate(mat, Vector3.yAxis(), angle);
}
public static Matrix4x4 rotateZ(Matrix4x4 mat, float angle) {
return rotate(mat, Vector3.zAxis(), angle);
}
public Matrix4x4 translate(Vector3 trans) {
return translate(this, trans);
}
public Matrix4x4 scale(Vector3 scale) {
return scale(this, scale);
}
public Matrix4x4 rotate(Vector3 axis, float angle) {
return rotate(this, axis, angle);
}
public Matrix4x4 rotateX(float angle) {
return rotateX(this, angle);
}
public Matrix4x4 rotateY(float angle) {
return rotateY(this, angle);
}
public Matrix4x4 rotateZ(float angle) {
return rotateZ(this, angle);
}
@Override
public Matrix4x4 invert() {
Matrix4x4 result = new Matrix4x4();
invert(this, result);
return result;
}
@Override
public Matrix4x4 transpose() {
Matrix4x4 result = new Matrix4x4();
transpose(this, result);
return result;
}
@Override
public Matrix4x4 negate() {
Matrix4x4 result = new Matrix4x4();
negate(this, result);
return result;
}
public Vector3 transformPoint(Vector3 point) {
return transformPoint(this, point);
}
public Vector3 transformVector(Vector3 vector) {
return transformVector(this, vector);
}
}