Git Refactoring

This commit is contained in:
MrDev023 2016-09-15 12:41:15 +02:00
parent c700911190
commit 55fcb82edd
35 changed files with 2338 additions and 2361 deletions

View file

@ -0,0 +1,110 @@
package fr.technicalgames.math;
import static org.lwjgl.opengl.GL11.*;
public class Color4f {
public static final Color4f
RED = new Color4f(1,0,0,1),
BLUE = new Color4f(0,0,1,1),
GREEN = new Color4f(0,1,0,1),
YELLOW = new Color4f(1,1,0,1),
PURPLE = new Color4f(1,0,1,1),
CYAN = new Color4f(0,1,1,1),
BLACK = new Color4f(0,0,0,1),
WHITE = new Color4f(1,1,1,1);
public float r,g,b,a;
public Color4f(float r,float g,float b,float a){
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public static Color4f mul (Color4f a, float b){
return new Color4f(a.r * b,a.g * b,a.b * b,a.a * b);
}
public static Color4f mul (float o,Color4f... a){
float r = 0;
float b = 0;
float g = 0;
float al = 0;
for(Color4f c : a){
r += c.r;
g += c.g;
b += c.b;
al += c.a;
}
r /= a.length;
g /= a.length;
b /= a.length;
al /= a.length;
return new Color4f(r * o,g * o,b * o,al * o);
}
public static Color4f mul (Color4f... a){
float r = 0;
float b = 0;
float g = 0;
float al = 0;
for(Color4f c : a){
r += c.r;
g += c.g;
b += c.b;
al += c.a;
}
r /= a.length;
g /= a.length;
b /= a.length;
al /= a.length;
return new Color4f(r,g,b,al);
}
public Color4f() {
}
public float getR() {
return r;
}
public void setR(float r) {
this.r = r;
}
public float getG() {
return g;
}
public void setG(float g) {
this.g = g;
}
public float getB() {
return b;
}
public void setB(float b) {
this.b = b;
}
public float getA() {
return a;
}
public void setA(float a) {
this.a = a;
}
public void bind(){
glColor4f(r,g,b,a);
}
public void unbind(){
BLACK.bind();
}
}

View file

@ -0,0 +1,67 @@
package fr.technicalgames.math;
public class Mathf {
public static final float PI = 3.14159265358979323846f;
public static final float EPSILON = 1.401298e-45f;
public static float cos(float angle){
return (float)Math.cos(angle);
}
public static float acos(float angle){
return (float)Math.acos(angle);
}
public static float sin(float angle){
return (float)Math.sin(angle);
}
public static float asin(float angle){
return (float)Math.asin(angle);
}
public static float toRadians(float angle){
return (float)Math.toRadians(angle);
}
public static float toDegrees(float angle){
return (float)Math.toDegrees(angle);
}
public static float atan2(float a,float b){
return (float)Math.atan2(a,b);
}
public static float cut(float nbre,float a){
return (float)((int)(nbre*Math.pow(10, a))/Math.pow(10, a));
}
public static boolean equals(float a,float b,float tolerance){
return (a + tolerance >= b) && (a - tolerance <= b);
}
public static float sqrt(float a){
return (float)Math.sqrt(a);
}
public static float exp(float a){
return (float)Math.sqrt(a);
}
public static float log(float a){
return (float)Math.log(a);
}
public static float clamp(float value, float min, float max) {
if(value < min){
value = min;
}
if(value > max){
value = max;
}
return value;
}
}

View file

@ -0,0 +1,189 @@
package fr.technicalgames.math;
import java.nio.*;
import java.util.*;
import org.lwjgl.*;
public class Matrix4f {
public float[][] m = null;
public Matrix4f(){
m = new float[][]{
{1,0,0,0},
{0,1,0,0},
{0,0,1,0},
{0,0,0,1}
};
}
public Matrix4f(float[][] m){
this.m = m;
}
public Matrix4f loadIdentity(){
m = new float[][]{
{1,0,0,0},
{0,1,0,0},
{0,0,1,0},
{0,0,0,1}
};
return this;
}
public Matrix4f rotate(Quaternion q){
Matrix4f rot = q.toMatrixRotation();
m = mul(rot).getM();
return this;
}
public void rotate(float x,float y,float z){
x = Mathf.toRadians(x);
y = Mathf.toRadians(y);
z = Mathf.toRadians(z);
Matrix4f rx = new Matrix4f(new float[][]{
{1,0,0,0},
{0,Mathf.cos(x),-Mathf.sin(x),0},
{0,Mathf.sin(x),Mathf.cos(x),0},
{0,0,0,1}
});
Matrix4f ry = new Matrix4f(new float[][]{
{Mathf.cos(y),0,Mathf.sin(y),0},
{0,1,0,0},
{-Mathf.sin(y),0,Mathf.cos(y),0},
{0,0,0,1}
});
Matrix4f rz = new Matrix4f(new float[][]{
{Mathf.cos(z),-Mathf.sin(z),0,0},
{Mathf.sin(z),Mathf.cos(z),0,0},
{0,0,1,0},
{0,0,0,1}
});
Matrix4f m1 = (rz.mul(ry.mul(rx)));
m = mul(m1).getM();
}
public static Matrix4f rotate(Vector3f forward, Vector3f up, Vector3f right)
{
Matrix4f mat = new Matrix4f(new float[][]{
{right.getX(), right.getY(), right.getZ() ,0},
{up.getX(), up.getY(), up.getZ() ,0},
{forward.getX(),forward.getY(), forward.getZ() ,0},
{0,0,0,1}
});
return mat;
}
public Matrix4f tranlate(float x,float y,float z){
Matrix4f mat = new Matrix4f(new float[][]{
{1,0,0,x},
{0,1,0,y},
{0,0,1,z},
{0,0,0,1}
});
m = mul(mat).getM();
return this;
}
public Matrix4f scale(float x,float y,float z){
Matrix4f mat = new Matrix4f(new float[][]{
{x,0,0,0},
{0,y,0,0},
{0,0,z,0},
{0,0,0,1}
});
m = mul(mat).getM();
return this;
}
public Matrix4f mul(Matrix4f mat){
Matrix4f ma = new Matrix4f();
for(int i = 0;i < 4;i++){
for(int j = 0;j < 4;j++){
ma.m[i][j] = m[i][0] * mat.m[0][j] +
m[i][1] * mat.m[1][j] +
m[i][2] * mat.m[2][j] +
m[i][3] * mat.m[3][j];
}
}
return ma;
}
public Matrix4f Ortho2D(float left, float right, float bottom, float top, float near, float far)
{
float width = right - left;
float height = top - bottom;
float depth = far - near;
m = new float[][]{
{2/width,0,0,-(right + left)/width},
{0,2/height,0,-(top + bottom)/height},
{0,0,-2/depth,-(far + near)/depth},
{0,0,0,1}
};
return this;
}
public Matrix4f perspective(float fov, float aspectRatio, float zNear, float zFar)
{
float f = fov;
fov = Mathf.toRadians(f);
float tanHalfFOV = (float)Math.tan(fov / 2);
float zRange = zNear - zFar;
m = new float[][]{
{1.0f / (tanHalfFOV * aspectRatio),0,0,0},
{0,1.0f / tanHalfFOV,0,0},
{0,0,(-zNear -zFar)/zRange,2.0f * zFar * zNear / zRange},
{0,0,1,0}
};
return this;
}
public FloatBuffer getBuffer(){
FloatBuffer buffer = BufferUtils.createFloatBuffer(4 * 4);
for(int i = 0;i < 4;i++){
buffer.put(m[i]);
}
buffer.flip();
return buffer;
}
public String toString(){
int size = 3;
int max = 10;
StringJoiner st = new StringJoiner("\n","--------Mat4-Begin--------\n","\n--------Mat4-End----------");
for(int i = 0;i < 4;i++){
StringJoiner st2 = new StringJoiner(" | ");
for(int j = 0;j < 4;j++){
String value = Mathf.cut(m[i][j], size) + "";
for(int k = value.length();k < max;k++){
value += " ";
}
st2.add(value);
}
st.add(st2.toString());
}
return st.toString();
}
public float[][] getM() {
return m;
}
public void setM(float[][] m) {
this.m = m;
}
public Matrix4f copy(){
return new Matrix4f(this.getM());
}
}

View file

@ -0,0 +1,132 @@
package fr.technicalgames.math;
public class Quaternion {
public float x,y,z,w;
public Quaternion(){
x = 0;
y = 0;
z = 0;
w = 0;
}
public Quaternion(Vector3f axis,float angle){
float sin = Mathf.sin(Mathf.toRadians(angle/2.0f));
float cos = Mathf.cos(Mathf.toRadians(angle/2.0f));
x = axis.getX() * sin;
y = axis.getY() * sin;
z = axis.getZ() * sin;
w = cos;
}
public Quaternion(Vector3f rot){
this(rot.x,rot.y,rot.z);
}
public Quaternion (float yaw, float roll, float pitch) {
yaw = Mathf.toRadians(yaw);
roll = Mathf.toRadians(roll);
pitch = Mathf.toRadians(pitch);
float angle;
float sinRoll, sinPitch, sinYaw, cosRoll, cosPitch, cosYaw;
angle = pitch * 0.5f;
sinPitch = Mathf.sin(angle);
cosPitch = Mathf.cos(angle);
angle = roll * 0.5f;
sinRoll = Mathf.sin(angle);
cosRoll = Mathf.cos(angle);
angle = yaw * 0.5f;
sinYaw = Mathf.sin(angle);
cosYaw = Mathf.cos(angle);
// variables used to reduce multiplication calls.
float cosRollXcosPitch = cosRoll * cosPitch;
float sinRollXsinPitch = sinRoll * sinPitch;
float cosRollXsinPitch = cosRoll * sinPitch;
float sinRollXcosPitch = sinRoll * cosPitch;
w = (cosRollXcosPitch * cosYaw - sinRollXsinPitch * sinYaw);
x = (cosRollXcosPitch * sinYaw + sinRollXsinPitch * cosYaw);
y = (sinRollXcosPitch * cosYaw + cosRollXsinPitch * sinYaw);
z = (cosRollXsinPitch * cosYaw - sinRollXcosPitch * sinYaw);
normalize();
}
public void normalize(){
float n = (float)(1.0/Math.sqrt(norm()));
x *= n;
y *= n;
z *= n;
w *= n;
}
public float norm(){
return w * w + x * x + y * y + z * z;
}
public Quaternion Euler(Vector3f rot) {
x = Mathf.toRadians(rot.x);
y = Mathf.toRadians(rot.y);
z = Mathf.toRadians(rot.z);
float c1 = Mathf.cos(y/2);
float s1 = Mathf.sin(y/2);
float c2 = Mathf.cos(z/2);
float s2 = Mathf.sin(z/2);
float c3 = Mathf.cos(x/2);
float s3 = Mathf.sin(x/2);
float c1c2 = c1*c2;
float s1s2 = s1*s2;
this.w =c1c2*c3 - s1s2*s3;
this.x =c1c2*s3 + s1s2*c3;
this.y =s1*c2*c3 + c1*s2*s3;
this.z =c1*s2*c3 - s1*c2*s3;
return new Quaternion(x, y, z, w);
}
public Vector3f toEulerAngles(){
Vector3f euler = new Vector3f();
float sqw = w * w;
float sqx = x * x;
float sqy = y * y;
float sqz = z * z;
float unit = sqx + sqy + sqz + sqw; // if normalized is one, otherwise
// is correction factor
float test = x * y + z * w;
if (test > 0.499 * unit) { // singularity at north pole
euler.y = 2 * Mathf.atan2(x, w);
euler.z = Mathf.PI/2.0f;
euler.x = 0;
} else if (test < -0.499 * unit) { // singularity at south pole
euler.y = -2 * Mathf.atan2(x, w);
euler.z = -Mathf.PI/2.0f;
euler.x = 0;
} else {
euler.y = Mathf.atan2(2 * y * w - 2 * x * z, sqx - sqy - sqz + sqw); // roll or heading
euler.z = Mathf.asin(2 * test / unit); // pitch or attitude
euler.x = Mathf.atan2(2 * x * w - 2 * y * z, -sqx + sqy - sqz + sqw); // yaw or bank
}
return euler.toDegrees();
}
public Quaternion(float axisX,float axisY,float axisZ,float angle){
float sin = Mathf.sin(Mathf.toRadians(angle/2.0f));
float cos = Mathf.cos(Mathf.toRadians(angle/2.0f));
x = axisX * sin;
y = axisY * sin;
z = axisZ * sin;
w = cos;
}
public Matrix4f toMatrixRotation(){
Vector3f forward = new Vector3f(2.0f * (x * z - w * y), 2.0f * (y * z + w * x), 1.0f - 2.0f * (x * x + y * y));
Vector3f up = new Vector3f(2.0f * (x * y + w * z), 1.0f - 2.0f * (x * x + z * z), 2.0f * (y * z - w * x));
Vector3f right = new Vector3f(1.0f - 2.0f * (y * y + z * z), 2.0f * (x * y - w * z), 2.0f * (x * z + w * y));
return Matrix4f.rotate(forward, up, right);
}
}

View file

@ -0,0 +1,42 @@
package fr.technicalgames.math;
import java.util.*;
public class Vector2f {
public float x,y;
public Vector2f(){
x = 0;
y = 0;
}
public Vector2f(float x,float y){
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public String toString(){
StringJoiner st = new StringJoiner(",","vec2(",")");
st.add("" + x);
st.add("" + y);
return st.toString();
}
}

View file

@ -0,0 +1,104 @@
package fr.technicalgames.math;
import java.util.*;
public class Vector3f {
public float x,y,z;
public Vector3f(){
x = 0;
y = 0;
z = 0;
}
public Vector3f(float x,float y,float z){
this.x = x;
this.y = y;
this.z = z;
}
public Vector3f(Vector2f vec,float z){
this(vec.x,vec.y,z);
}
public Vector3f(Vector3f vec){
this(vec.x,vec.y,vec.z);
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getZ() {
return z;
}
public void setZ(float z) {
this.z = z;
}
public float length(){
return Mathf.sqrt(x * x + y * y + z * z);
}
public Vector3f lookAt(Vector3f d){
Vector3f rot = new Vector3f();
float x1 = d.x - x;
float y1 = d.y - y;
float z1 = d.z - z;
return rot;
}
public Vector3f normalize(){
float length = length();
x /= length;
y /= length;
z /= length;
return this;
}
public Vector3f mul(float m){
x *= m;
y *= m;
z *= m;
return this;
}
public String toString(){
StringJoiner st = new StringJoiner(",","vec3(",")");
st.add("" + x);
st.add("" + y);
st.add("" + z);
return st.toString();
}
public Vector3f toRadians() {
x = Mathf.toRadians(x);
y = Mathf.toRadians(y);
z = Mathf.toRadians(z);
return this;
}
public Vector3f toDegrees() {
x = Mathf.toDegrees(x);
y = Mathf.toDegrees(y);
z = Mathf.toDegrees(z);
return this;
}
}

View file

@ -0,0 +1,56 @@
package fr.technicalgames.math;
public class Vector4f {
public float x,y,z,w;
public Vector4f(float x,float y,float z,float w){
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public Vector4f(Vector3f v,float w){
this.x = v.x;
this.y = v.y;
this.z = v.z;
this.w = w;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getZ() {
return z;
}
public void setZ(float z) {
this.z = z;
}
public float getW() {
return w;
}
public void setW(float w) {
this.w = w;
}
}