Bunch of matrix and vector code added
This commit is contained in:
parent
ba2877a75e
commit
a60f255e82
|
@ -36,7 +36,7 @@ import java.nio.FloatBuffer;
|
||||||
/**
|
/**
|
||||||
* $Id$
|
* $Id$
|
||||||
*
|
*
|
||||||
* Holds a 2x2 matrix.
|
* Holds a 2x2 matrix
|
||||||
*
|
*
|
||||||
* @author cix_foo <cix_foo@users.sourceforge.net>
|
* @author cix_foo <cix_foo@users.sourceforge.net>
|
||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
|
@ -44,7 +44,7 @@ import java.nio.FloatBuffer;
|
||||||
|
|
||||||
public class Matrix2f {
|
public class Matrix2f {
|
||||||
|
|
||||||
public float m00, m01, m10, m11;
|
public float m00 = 1.0f, m01, m10, m11 = 1.0f;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for Matrix2f.
|
* Constructor for Matrix2f.
|
||||||
|
@ -52,4 +52,242 @@ public class Matrix2f {
|
||||||
public Matrix2f() {
|
public Matrix2f() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public Matrix2f(Matrix2f src) {
|
||||||
|
load(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load from another matrix2f
|
||||||
|
* @param src The source matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix2f load(Matrix2f src) {
|
||||||
|
|
||||||
|
m00 = src.m00;
|
||||||
|
m01 = src.m01;
|
||||||
|
m10 = src.m10;
|
||||||
|
m11 = src.m11;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add two matrices together and place the result in a third matrix.
|
||||||
|
* @param left The left source matrix
|
||||||
|
* @param right The right source matrix
|
||||||
|
* @param dest The destination matrix, or null if a new one is to be created
|
||||||
|
* @return the destination matrix
|
||||||
|
*/
|
||||||
|
public static Matrix2f add(Matrix2f left, Matrix2f right, Matrix2f dest) {
|
||||||
|
|
||||||
|
Matrix2f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix2f();
|
||||||
|
else if (dest == left || dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Matrix2f();
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.m00 = left.m00 + right.m00;
|
||||||
|
dest.m01 = left.m01 + right.m01;
|
||||||
|
dest.m10 = left.m10 + right.m10;
|
||||||
|
dest.m11 = left.m11 + right.m11;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.load(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subtract the right matrix from the left and place the result in a third matrix.
|
||||||
|
* @param left The left source matrix
|
||||||
|
* @param right The right source matrix
|
||||||
|
* @param dest The destination matrix, or null if a new one is to be created
|
||||||
|
* @return the destination matrix
|
||||||
|
*/
|
||||||
|
public static Matrix2f sub(Matrix2f left, Matrix2f right, Matrix2f dest) {
|
||||||
|
|
||||||
|
Matrix2f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix2f();
|
||||||
|
else if (dest == left || dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Matrix2f();
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.m00 = left.m00 - right.m00;
|
||||||
|
dest.m01 = left.m01 - right.m01;
|
||||||
|
dest.m10 = left.m10 - right.m10;
|
||||||
|
dest.m11 = left.m11 - right.m11;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.load(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiply the right matrix by the left and place the result in a third matrix.
|
||||||
|
* @param left The left source matrix
|
||||||
|
* @param right The right source matrix
|
||||||
|
* @param dest The destination matrix, or null if a new one is to be created
|
||||||
|
* @return the destination matrix
|
||||||
|
*/
|
||||||
|
public static Matrix2f mul(Matrix2f left, Matrix2f right, Matrix2f dest) {
|
||||||
|
|
||||||
|
Matrix2f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix2f();
|
||||||
|
else if (dest == left || dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Matrix2f();
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.m00 = left.m00 * right.m00 + left.m10 * right.m01;
|
||||||
|
dest.m01 = left.m01 * right.m00 + left.m11 * right.m01;
|
||||||
|
dest.m10 = left.m00 * right.m10 + left.m10 * right.m11;
|
||||||
|
dest.m11 = left.m01 * right.m10 + left.m11 * right.m11;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.load(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiply a Vector by a matrix and return the result in a destination
|
||||||
|
* vector.
|
||||||
|
* @param left The left matrix
|
||||||
|
* @param right The right vector
|
||||||
|
* @param dest The destination vector, or null if a new one is to be created
|
||||||
|
* @return the destination vector
|
||||||
|
*/
|
||||||
|
public static Vector2f mul(Matrix2f left, Vector2f right, Vector2f dest) {
|
||||||
|
|
||||||
|
Vector2f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Vector2f();
|
||||||
|
else if (dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Vector2f();
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.x = left.m00 * right.x + left.m10 * right.y;
|
||||||
|
dest.y = left.m01 * right.x + left.m11 * right.y;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.set(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transpose this matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix2f transpose() {
|
||||||
|
float temp;
|
||||||
|
|
||||||
|
temp = m01;
|
||||||
|
m01 = m10;
|
||||||
|
m10 = temp;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transpose this matrix and place the result in another matrix.
|
||||||
|
* @param dest The destination matrix or null if a new matrix is to be created
|
||||||
|
* @return the transposed matrix
|
||||||
|
*/
|
||||||
|
public Matrix2f transpose(Matrix2f dest) {
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix2f();
|
||||||
|
|
||||||
|
if (dest == this)
|
||||||
|
transpose();
|
||||||
|
else {
|
||||||
|
dest.m01 = m10;
|
||||||
|
dest.m10 = m01;
|
||||||
|
}
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invert this matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix2f invert() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negate this matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix2f negate() {
|
||||||
|
|
||||||
|
m00 = -m00;
|
||||||
|
m01 = -m01;
|
||||||
|
m10 = -m10;
|
||||||
|
m11 = -m11;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negate this matrix and stash the result in another matrix.
|
||||||
|
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||||
|
* @return the negated matrix
|
||||||
|
*/
|
||||||
|
public Matrix2f negate(Matrix2f dest) {
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix2f();
|
||||||
|
|
||||||
|
dest.m00 = -m00;
|
||||||
|
dest.m01 = -m01;
|
||||||
|
dest.m10 = -m10;
|
||||||
|
dest.m11 = -m11;
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set this matrix to be the identity matrix.
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix2f identity() {
|
||||||
|
m00 = 1.0f;
|
||||||
|
m01 = 0.0f;
|
||||||
|
m10 = 0.0f;
|
||||||
|
m11 = 1.0f;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set this matrix to 0.
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix2f zero() {
|
||||||
|
m00 = 0.0f;
|
||||||
|
m01 = 0.0f;
|
||||||
|
m10 = 0.0f;
|
||||||
|
m11 = 0.0f;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,15 @@ package org.lwjgl.vector;
|
||||||
|
|
||||||
public class Matrix3f {
|
public class Matrix3f {
|
||||||
|
|
||||||
public float m00, m01, m02, m10, m11, m12, m20, m21, m22;
|
public float m00 = 1.0f,
|
||||||
|
m01,
|
||||||
|
m02,
|
||||||
|
m10,
|
||||||
|
m11 = 1.0f,
|
||||||
|
m12,
|
||||||
|
m20,
|
||||||
|
m21,
|
||||||
|
m22 = 1.0f;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for Matrix3f.
|
* Constructor for Matrix3f.
|
||||||
|
@ -50,5 +58,292 @@ public class Matrix3f {
|
||||||
public Matrix3f() {
|
public Matrix3f() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Load from another matrix3f
|
||||||
|
* @param src The source matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix3f load(Matrix3f src) {
|
||||||
|
|
||||||
|
m00 = src.m00;
|
||||||
|
m10 = src.m10;
|
||||||
|
m20 = src.m20;
|
||||||
|
m01 = src.m01;
|
||||||
|
m11 = src.m11;
|
||||||
|
m21 = src.m21;
|
||||||
|
m02 = src.m02;
|
||||||
|
m12 = src.m12;
|
||||||
|
m22 = src.m22;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add two matrices together and place the result in a third matrix.
|
||||||
|
* @param left The left source matrix
|
||||||
|
* @param right The right source matrix
|
||||||
|
* @param dest The destination matrix, or null if a new one is to be created
|
||||||
|
* @return the destination matrix
|
||||||
|
*/
|
||||||
|
public static Matrix3f add(Matrix3f left, Matrix3f right, Matrix3f dest) {
|
||||||
|
|
||||||
|
Matrix3f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix3f();
|
||||||
|
else if (dest == left || dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Matrix3f();
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.m00 = left.m00 + right.m00;
|
||||||
|
dest.m01 = left.m01 + right.m01;
|
||||||
|
dest.m02 = left.m02 + right.m02;
|
||||||
|
dest.m10 = left.m10 + right.m10;
|
||||||
|
dest.m11 = left.m11 + right.m11;
|
||||||
|
dest.m12 = left.m12 + right.m12;
|
||||||
|
dest.m20 = left.m20 + right.m20;
|
||||||
|
dest.m21 = left.m21 + right.m21;
|
||||||
|
dest.m22 = left.m22 + right.m22;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.load(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subtract the right matrix from the left and place the result in a third matrix.
|
||||||
|
* @param left The left source matrix
|
||||||
|
* @param right The right source matrix
|
||||||
|
* @param dest The destination matrix, or null if a new one is to be created
|
||||||
|
* @return the destination matrix
|
||||||
|
*/
|
||||||
|
public static Matrix3f sub(Matrix3f left, Matrix3f right, Matrix3f dest) {
|
||||||
|
|
||||||
|
Matrix3f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix3f();
|
||||||
|
else if (dest == left || dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Matrix3f();
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.m00 = left.m00 - right.m00;
|
||||||
|
dest.m01 = left.m01 - right.m01;
|
||||||
|
dest.m02 = left.m02 - right.m02;
|
||||||
|
dest.m10 = left.m10 - right.m10;
|
||||||
|
dest.m11 = left.m11 - right.m11;
|
||||||
|
dest.m12 = left.m12 - right.m12;
|
||||||
|
dest.m20 = left.m20 - right.m20;
|
||||||
|
dest.m21 = left.m21 - right.m21;
|
||||||
|
dest.m22 = left.m22 - right.m22;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.load(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiply the right matrix by the left and place the result in a third matrix.
|
||||||
|
* @param left The left source matrix
|
||||||
|
* @param right The right source matrix
|
||||||
|
* @param dest The destination matrix, or null if a new one is to be created
|
||||||
|
* @return the destination matrix
|
||||||
|
*/
|
||||||
|
public static Matrix3f mul(Matrix3f left, Matrix3f right, Matrix3f dest) {
|
||||||
|
|
||||||
|
Matrix3f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix3f();
|
||||||
|
else if (dest == left || dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Matrix3f();
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.m00 =
|
||||||
|
left.m00 * right.m00 + left.m10 * right.m01 + left.m20 * right.m02;
|
||||||
|
dest.m01 =
|
||||||
|
left.m01 * right.m00 + left.m11 * right.m01 + left.m21 * right.m02;
|
||||||
|
dest.m02 =
|
||||||
|
left.m02 * right.m00 + left.m12 * right.m01 + left.m22 * right.m02;
|
||||||
|
dest.m10 =
|
||||||
|
left.m00 * right.m10 + left.m10 * right.m11 + left.m20 * right.m12;
|
||||||
|
dest.m11 =
|
||||||
|
left.m01 * right.m10 + left.m11 * right.m11 + left.m21 * right.m12;
|
||||||
|
dest.m12 =
|
||||||
|
left.m02 * right.m10 + left.m12 * right.m11 + left.m22 * right.m12;
|
||||||
|
dest.m20 =
|
||||||
|
left.m00 * right.m20 + left.m10 * right.m21 + left.m20 * right.m22;
|
||||||
|
dest.m21 =
|
||||||
|
left.m01 * right.m20 + left.m11 * right.m21 + left.m21 * right.m22;
|
||||||
|
dest.m22 =
|
||||||
|
left.m02 * right.m20 + left.m12 * right.m21 + left.m22 * right.m22;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.load(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiply a Vector by a matrix and return the result in a destination
|
||||||
|
* vector.
|
||||||
|
* @param left The left matrix
|
||||||
|
* @param right The right vector
|
||||||
|
* @param dest The destination vector, or null if a new one is to be created
|
||||||
|
* @return the destination vector
|
||||||
|
*/
|
||||||
|
public static Vector3f mul(Matrix3f left, Vector3f right, Vector3f dest) {
|
||||||
|
|
||||||
|
Vector3f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Vector3f();
|
||||||
|
else if (dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Vector3f();
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.x = left.m00 * right.x + left.m10 * right.y + left.m20 * right.z;
|
||||||
|
dest.y = left.m01 * right.x + left.m11 * right.y + left.m21 * right.z;
|
||||||
|
dest.z = left.m02 * right.x + left.m12 * right.y + left.m22 * right.z;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.set(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transpose this matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix3f transpose() {
|
||||||
|
float f = m10;
|
||||||
|
m10 = m01;
|
||||||
|
m01 = f;
|
||||||
|
f = m20;
|
||||||
|
m20 = m02;
|
||||||
|
m02 = f;
|
||||||
|
f = m21;
|
||||||
|
m21 = m12;
|
||||||
|
m12 = f;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transpose this matrix and place the result in another matrix
|
||||||
|
* @param dest The destination matrix or null if a new matrix is to be created
|
||||||
|
* @return the transposed matrix
|
||||||
|
*/
|
||||||
|
public Matrix3f transpose(Matrix3f dest) {
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix3f();
|
||||||
|
|
||||||
|
if (this != dest) {
|
||||||
|
m00 = dest.m00;
|
||||||
|
m01 = dest.m10;
|
||||||
|
m02 = dest.m20;
|
||||||
|
m10 = dest.m01;
|
||||||
|
m11 = dest.m11;
|
||||||
|
m12 = dest.m21;
|
||||||
|
m20 = dest.m02;
|
||||||
|
m21 = dest.m12;
|
||||||
|
m22 = dest.m22;
|
||||||
|
} else
|
||||||
|
transpose();
|
||||||
|
|
||||||
|
return this;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invert this matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix3f invert() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negate this matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix3f negate() {
|
||||||
|
m00 = -m00;
|
||||||
|
m01 = -m02;
|
||||||
|
m02 = -m01;
|
||||||
|
m10 = -m10;
|
||||||
|
m11 = -m12;
|
||||||
|
m12 = -m11;
|
||||||
|
m20 = -m20;
|
||||||
|
m21 = -m22;
|
||||||
|
m22 = -m21;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negate this matrix and place the result in a destination matrix.
|
||||||
|
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||||
|
* @return the negated matrix
|
||||||
|
*/
|
||||||
|
public Matrix3f negate(Matrix3f dest) {
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix3f();
|
||||||
|
|
||||||
|
dest.m00 = -m00;
|
||||||
|
dest.m01 = -m02;
|
||||||
|
dest.m02 = -m01;
|
||||||
|
dest.m10 = -m10;
|
||||||
|
dest.m11 = -m12;
|
||||||
|
dest.m12 = -m11;
|
||||||
|
dest.m20 = -m20;
|
||||||
|
dest.m21 = -m22;
|
||||||
|
dest.m22 = -m21;
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set this matrix to be the identity matrix.
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix3f setIdentity() {
|
||||||
|
m00 = 1.0f;
|
||||||
|
m01 = 0.0f;
|
||||||
|
m02 = 0.0f;
|
||||||
|
m10 = 0.0f;
|
||||||
|
m11 = 1.0f;
|
||||||
|
m12 = 0.0f;
|
||||||
|
m20 = 0.0f;
|
||||||
|
m21 = 0.0f;
|
||||||
|
m22 = 1.0f;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set this matrix to 0.
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix3f setZero() {
|
||||||
|
m00 = 0.0f;
|
||||||
|
m01 = 0.0f;
|
||||||
|
m02 = 0.0f;
|
||||||
|
m10 = 0.0f;
|
||||||
|
m11 = 0.0f;
|
||||||
|
m12 = 0.0f;
|
||||||
|
m20 = 0.0f;
|
||||||
|
m21 = 0.0f;
|
||||||
|
m22 = 0.0f;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,8 +37,60 @@ package org.lwjgl.vector;
|
||||||
* @author foo
|
* @author foo
|
||||||
*/
|
*/
|
||||||
public class Matrix4f {
|
public class Matrix4f {
|
||||||
|
/**
|
||||||
|
* Set this matrix to be the identity matrix.
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix4f setIdentity() {
|
||||||
|
m00 = 1.0f;
|
||||||
|
m01 = 0.0f;
|
||||||
|
m02 = 0.0f;
|
||||||
|
m03 = 0.0f;
|
||||||
|
m10 = 0.0f;
|
||||||
|
m11 = 1.0f;
|
||||||
|
m12 = 0.0f;
|
||||||
|
m13 = 0.0f;
|
||||||
|
m20 = 0.0f;
|
||||||
|
m21 = 0.0f;
|
||||||
|
m22 = 1.0f;
|
||||||
|
m23 = 0.0f;
|
||||||
|
m30 = 0.0f;
|
||||||
|
m31 = 0.0f;
|
||||||
|
m32 = 0.0f;
|
||||||
|
m33 = 1.0f;
|
||||||
|
|
||||||
public float m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33;
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set this matrix to 0.
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix4f setZero() {
|
||||||
|
m00 = 0.0f;
|
||||||
|
m01 = 0.0f;
|
||||||
|
m02 = 0.0f;
|
||||||
|
m03 = 0.0f;
|
||||||
|
m10 = 0.0f;
|
||||||
|
m11 = 0.0f;
|
||||||
|
m12 = 0.0f;
|
||||||
|
m13 = 0.0f;
|
||||||
|
m20 = 0.0f;
|
||||||
|
m21 = 0.0f;
|
||||||
|
m22 = 0.0f;
|
||||||
|
m23 = 0.0f;
|
||||||
|
m30 = 0.0f;
|
||||||
|
m31 = 0.0f;
|
||||||
|
m32 = 0.0f;
|
||||||
|
m33 = 0.0f;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public float m00 = 1.0f, m01, m02, m03, m10, m11 = 1.0f, m12, m13, m20, m21, m22 = 1.0f, m23, m30, m31, m32, m33 = 1.0f;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a Matrix4f
|
* Construct a Matrix4f
|
||||||
|
@ -46,5 +98,310 @@ public class Matrix4f {
|
||||||
public Matrix4f() {
|
public Matrix4f() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Load from another matrix4f
|
||||||
|
* @param src The source matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix4f load(Matrix4f src) {
|
||||||
|
|
||||||
|
m00 = src.m00;
|
||||||
|
m01 = src.m01;
|
||||||
|
m02 = src.m02;
|
||||||
|
m03 = src.m03;
|
||||||
|
m10 = src.m10;
|
||||||
|
m11 = src.m11;
|
||||||
|
m12 = src.m12;
|
||||||
|
m13 = src.m13;
|
||||||
|
m20 = src.m20;
|
||||||
|
m21 = src.m21;
|
||||||
|
m22 = src.m22;
|
||||||
|
m23 = src.m23;
|
||||||
|
m30 = src.m30;
|
||||||
|
m31 = src.m31;
|
||||||
|
m32 = src.m32;
|
||||||
|
m33 = src.m33;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Add two matrices together and place the result in a third matrix.
|
||||||
|
* @param left The left source matrix
|
||||||
|
* @param right The right source matrix
|
||||||
|
* @param dest The destination matrix, or null if a new one is to be created
|
||||||
|
* @return the destination matrix
|
||||||
|
*/
|
||||||
|
public static Matrix4f add(Matrix4f left, Matrix4f right, Matrix4f dest) {
|
||||||
|
|
||||||
|
Matrix4f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix4f();
|
||||||
|
else if (dest == left || dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Matrix4f();
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.m00 = left.m00 + right.m00;
|
||||||
|
dest.m01 = left.m01 + right.m01;
|
||||||
|
dest.m02 = left.m02 + right.m02;
|
||||||
|
dest.m03 = left.m03 + right.m03;
|
||||||
|
dest.m10 = left.m10 + right.m10;
|
||||||
|
dest.m11 = left.m11 + right.m11;
|
||||||
|
dest.m12 = left.m12 + right.m12;
|
||||||
|
dest.m13 = left.m13 + right.m13;
|
||||||
|
dest.m20 = left.m20 + right.m20;
|
||||||
|
dest.m21 = left.m21 + right.m21;
|
||||||
|
dest.m22 = left.m22 + right.m22;
|
||||||
|
dest.m23 = left.m23 + right.m23;
|
||||||
|
dest.m30 = left.m30 + right.m30;
|
||||||
|
dest.m31 = left.m31 + right.m31;
|
||||||
|
dest.m32 = left.m32 + right.m32;
|
||||||
|
dest.m33 = left.m33 + right.m33;
|
||||||
|
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.load(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subtract the right matrix from the left and place the result in a third matrix.
|
||||||
|
* @param left The left source matrix
|
||||||
|
* @param right The right source matrix
|
||||||
|
* @param dest The destination matrix, or null if a new one is to be created
|
||||||
|
* @return the destination matrix
|
||||||
|
*/
|
||||||
|
public static Matrix4f sub(Matrix4f left, Matrix4f right, Matrix4f dest) {
|
||||||
|
|
||||||
|
Matrix4f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix4f();
|
||||||
|
else if (dest == left || dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Matrix4f();
|
||||||
|
}
|
||||||
|
|
||||||
|
dest.m00 = left.m00 - right.m00;
|
||||||
|
dest.m01 = left.m01 - right.m01;
|
||||||
|
dest.m02 = left.m02 - right.m02;
|
||||||
|
dest.m03 = left.m03 - right.m03;
|
||||||
|
dest.m10 = left.m10 - right.m10;
|
||||||
|
dest.m11 = left.m11 - right.m11;
|
||||||
|
dest.m12 = left.m12 - right.m12;
|
||||||
|
dest.m13 = left.m13 - right.m13;
|
||||||
|
dest.m20 = left.m20 - right.m20;
|
||||||
|
dest.m21 = left.m21 - right.m21;
|
||||||
|
dest.m22 = left.m22 - right.m22;
|
||||||
|
dest.m23 = left.m23 - right.m23;
|
||||||
|
dest.m30 = left.m30 - right.m30;
|
||||||
|
dest.m31 = left.m31 - right.m31;
|
||||||
|
dest.m32 = left.m32 - right.m32;
|
||||||
|
dest.m33 = left.m33 - right.m33;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.load(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiply the right matrix by the left and place the result in a third matrix.
|
||||||
|
* @param left The left source matrix
|
||||||
|
* @param right The right source matrix
|
||||||
|
* @param dest The destination matrix, or null if a new one is to be created
|
||||||
|
* @return the destination matrix
|
||||||
|
*/
|
||||||
|
public static Matrix4f mul(Matrix4f left, Matrix4f right, Matrix4f dest) {
|
||||||
|
|
||||||
|
Matrix4f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix4f();
|
||||||
|
else if (dest == left || dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Matrix4f();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
dest.m00 = left.m00 * right.m00 + left.m10 * right.m01 + left.m20 * right.m02 + left.m30 * right.m03;
|
||||||
|
dest.m01 = left.m01 * right.m00 + left.m11 * right.m01 + left.m21 * right.m02 + left.m31 * right.m03;
|
||||||
|
dest.m02 = left.m02 * right.m00 + left.m12 * right.m01 + left.m22 * right.m02 + left.m32 * right.m03;
|
||||||
|
dest.m03 = left.m03 * right.m00 + left.m13 * right.m01 + left.m23 * right.m02 + left.m33 * right.m03;
|
||||||
|
dest.m10 = left.m00 * right.m10 + left.m10 * right.m11 + left.m20 * right.m12 + left.m30 * right.m13;
|
||||||
|
dest.m11 = left.m01 * right.m10 + left.m11 * right.m11 + left.m21 * right.m12 + left.m31 * right.m13;
|
||||||
|
dest.m12 = left.m02 * right.m10 + left.m12 * right.m11 + left.m22 * right.m12 + left.m32 * right.m13;
|
||||||
|
dest.m13 = left.m03 * right.m10 + left.m13 * right.m11 + left.m23 * right.m12 + left.m33 * right.m13;
|
||||||
|
dest.m20 = left.m00 * right.m20 + left.m10 * right.m21 + left.m20 * right.m22 + left.m30 * right.m23;
|
||||||
|
dest.m21 = left.m01 * right.m20 + left.m11 * right.m21 + left.m21 * right.m22 + left.m31 * right.m23;
|
||||||
|
dest.m22 = left.m02 * right.m20 + left.m12 * right.m21 + left.m22 * right.m22 + left.m32 * right.m23;
|
||||||
|
dest.m23 = left.m03 * right.m20 + left.m13 * right.m21 + left.m23 * right.m22 + left.m33 * right.m23;
|
||||||
|
dest.m30 = left.m00 * right.m30 + left.m10 * right.m31 + left.m20 * right.m32 + left.m30 * right.m33;
|
||||||
|
dest.m31 = left.m01 * right.m30 + left.m11 * right.m31 + left.m21 * right.m32 + left.m31 * right.m33;
|
||||||
|
dest.m32 = left.m02 * right.m30 + left.m12 * right.m31 + left.m22 * right.m32 + left.m32 * right.m33;
|
||||||
|
dest.m33 = left.m03 * right.m30 + left.m13 * right.m31 + left.m23 * right.m32 + left.m33 * right.m33;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.load(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiply a Vector by a matrix and return the result in a destination
|
||||||
|
* vector.
|
||||||
|
* @param left The left matrix
|
||||||
|
* @param right The right vector
|
||||||
|
* @param dest The destination vector, or null if a new one is to be created
|
||||||
|
* @return the destination vector
|
||||||
|
*/
|
||||||
|
public static Vector4f mul(Matrix4f left, Vector4f right, Vector4f dest) {
|
||||||
|
|
||||||
|
Vector4f temp = null;
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Vector4f();
|
||||||
|
else if (dest == right) {
|
||||||
|
temp = dest;
|
||||||
|
dest = new Vector4f();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
dest.x = left.m00 * right.x + left.m10 * right.y + left.m20 * right.z + left.m30 * right.w;
|
||||||
|
dest.y = left.m01 * right.x + left.m11 * right.y + left.m21 * right.z + left.m31 * right.w;
|
||||||
|
dest.z = left.m02 * right.x + left.m12 * right.y + left.m22 * right.z + left.m32 * right.w;
|
||||||
|
dest.w = left.m03 * right.x + left.m13 * right.y + left.m23 * right.z + left.m33 * right.w;
|
||||||
|
|
||||||
|
if (temp != null) {
|
||||||
|
temp.set(dest);
|
||||||
|
return temp;
|
||||||
|
} else
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transpose this matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix4f transpose() {
|
||||||
|
|
||||||
|
float f = m10;
|
||||||
|
m10 = m01;
|
||||||
|
m01 = f;
|
||||||
|
f = m20;
|
||||||
|
m20 = m02;
|
||||||
|
m02 = f;
|
||||||
|
f = m30;
|
||||||
|
m30 = m03;
|
||||||
|
m03 = f;
|
||||||
|
f = m21;
|
||||||
|
m21 = m12;
|
||||||
|
m12 = f;
|
||||||
|
f = m31;
|
||||||
|
m31 = m13;
|
||||||
|
m13 = f;
|
||||||
|
f = m32;
|
||||||
|
m32 = m23;
|
||||||
|
m23 = f;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transpose this matrix and place the result in another matrix
|
||||||
|
* @param dest The destination matrix or null if a new matrix is to be created
|
||||||
|
* @return the transposed matrix
|
||||||
|
*/
|
||||||
|
public Matrix4f transpose(Matrix4f dest) {
|
||||||
|
|
||||||
|
if (this != dest) {
|
||||||
|
m00 = dest.m00;
|
||||||
|
m01 = dest.m10;
|
||||||
|
m02 = dest.m20;
|
||||||
|
m03 = dest.m30;
|
||||||
|
m10 = dest.m01;
|
||||||
|
m11 = dest.m11;
|
||||||
|
m12 = dest.m21;
|
||||||
|
m13 = dest.m31;
|
||||||
|
m20 = dest.m02;
|
||||||
|
m21 = dest.m12;
|
||||||
|
m22 = dest.m22;
|
||||||
|
m23 = dest.m32;
|
||||||
|
m30 = dest.m03;
|
||||||
|
m31 = dest.m13;
|
||||||
|
m32 = dest.m23;
|
||||||
|
m33 = dest.m33;
|
||||||
|
} else
|
||||||
|
transpose();
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invert this matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix4f invert() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negate this matrix
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Matrix4f negate() {
|
||||||
|
m00 = -m00;
|
||||||
|
m01 = -m01;
|
||||||
|
m02 = -m02;
|
||||||
|
m03 = -m03;
|
||||||
|
m10 = -m10;
|
||||||
|
m11 = -m11;
|
||||||
|
m12 = -m12;
|
||||||
|
m13 = -m13;
|
||||||
|
m20 = -m20;
|
||||||
|
m21 = -m21;
|
||||||
|
m22 = -m22;
|
||||||
|
m23 = -m23;
|
||||||
|
m30 = -m30;
|
||||||
|
m31 = -m31;
|
||||||
|
m32 = -m32;
|
||||||
|
m33 = -m33;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negate this matrix and place the result in a destination matrix.
|
||||||
|
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||||
|
* @return the negated matrix
|
||||||
|
*/
|
||||||
|
public Matrix4f negate(Matrix4f dest) {
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Matrix4f();
|
||||||
|
|
||||||
|
dest.m00 = -m00;
|
||||||
|
dest.m01 = -m01;
|
||||||
|
dest.m02 = -m02;
|
||||||
|
dest.m03 = -m03;
|
||||||
|
dest.m10 = -m10;
|
||||||
|
dest.m11 = -m11;
|
||||||
|
dest.m12 = -m12;
|
||||||
|
dest.m13 = -m13;
|
||||||
|
dest.m20 = -m20;
|
||||||
|
dest.m21 = -m21;
|
||||||
|
dest.m22 = -m22;
|
||||||
|
dest.m23 = -m23;
|
||||||
|
dest.m30 = -m30;
|
||||||
|
dest.m31 = -m31;
|
||||||
|
dest.m32 = -m32;
|
||||||
|
dest.m33 = -m33;
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,8 @@
|
||||||
*/
|
*/
|
||||||
package org.lwjgl.vector;
|
package org.lwjgl.vector;
|
||||||
|
|
||||||
|
import org.lwjgl.Math;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* $Id$
|
* $Id$
|
||||||
*
|
*
|
||||||
|
@ -45,10 +47,134 @@ public class Vector2f {
|
||||||
public float x, y;
|
public float x, y;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for Vector2f.
|
* Constructor for Vector3f.
|
||||||
*/
|
*/
|
||||||
public Vector2f() {
|
public Vector2f() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public Vector2f(Vector2f src) {
|
||||||
|
set(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public Vector2f(float x, float y) {
|
||||||
|
set(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set values
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector2f set(float x, float y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load from another Vector2f
|
||||||
|
* @param src The source vector
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector2f set(Vector2f src) {
|
||||||
|
x = src.x;
|
||||||
|
y = src.y;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the length of the vector
|
||||||
|
*/
|
||||||
|
public float length() {
|
||||||
|
return Math.sqrt(lengthSquared());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the length squared of the vector
|
||||||
|
*/
|
||||||
|
public float lengthSquared() {
|
||||||
|
return x * x + y * y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translate a vector
|
||||||
|
* @param x The translation in x
|
||||||
|
* @param y the translation in y
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector2f translate(float x, float y) {
|
||||||
|
this.x += x;
|
||||||
|
this.y += y;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negate a vector
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector2f negate() {
|
||||||
|
x = -x;
|
||||||
|
y = -y;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalise this vector
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector2f normalise() {
|
||||||
|
float l = 1.0f / length();
|
||||||
|
x *= l;
|
||||||
|
y *= l;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalise this vector and place the result in another vector.
|
||||||
|
* @param dest The destination vector, or null if a new vector is to be created
|
||||||
|
* @return the normalised vector
|
||||||
|
*/
|
||||||
|
public Vector2f normalise(Vector2f dest) {
|
||||||
|
float l = length();
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Vector2f(x / l, y / l);
|
||||||
|
else
|
||||||
|
dest.set(x / l, y / l);
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The dot product of two vectors is calculated as
|
||||||
|
* v1.x * v2.x + v1.y * v2.y
|
||||||
|
* @param left The LHS vector
|
||||||
|
* @param right The RHS vector
|
||||||
|
* @return left dot right
|
||||||
|
*/
|
||||||
|
public static float dot(Vector2f left, Vector2f right) {
|
||||||
|
return left.x * right.x + left.y * right.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the angle between two vectors, in degrees
|
||||||
|
* @param a A vector
|
||||||
|
* @param b The other vector
|
||||||
|
* @return the angle between the two vectors, in degrees
|
||||||
|
*/
|
||||||
|
public static float angle(Vector2f a, Vector2f b) {
|
||||||
|
float dls = dot(a, b) / (a.length() * b.length());
|
||||||
|
if (dls < -1f)
|
||||||
|
dls = -1f;
|
||||||
|
else if (dls > 1.0f)
|
||||||
|
dls = 1.0f;
|
||||||
|
return Math.acos(dls);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,8 @@
|
||||||
*/
|
*/
|
||||||
package org.lwjgl.vector;
|
package org.lwjgl.vector;
|
||||||
|
|
||||||
|
import org.lwjgl.Math;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* $Id$
|
* $Id$
|
||||||
*
|
*
|
||||||
|
@ -51,4 +53,133 @@ public class Vector3f {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public Vector3f(Vector3f src) {
|
||||||
|
set(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public Vector3f(float x, float y, float z) {
|
||||||
|
set(x, y, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set values
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector3f set(float x, float y, float z) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load from another Vector3f
|
||||||
|
* @param src The source vector
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector3f set(Vector3f src) {
|
||||||
|
x = src.x;
|
||||||
|
y = src.y;
|
||||||
|
z = src.z;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the length of the vector
|
||||||
|
*/
|
||||||
|
public float length() {
|
||||||
|
return Math.sqrt(lengthSquared());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the length squared of the vector
|
||||||
|
*/
|
||||||
|
public float lengthSquared() {
|
||||||
|
return x * x + y * y + z * z;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translate a vector
|
||||||
|
* @param x The translation in x
|
||||||
|
* @param y the translation in y
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector3f translate(float x, float y, float z) {
|
||||||
|
this.x += x;
|
||||||
|
this.y += y;
|
||||||
|
this.z += z;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negate a vector
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector3f negate() {
|
||||||
|
x = -x;
|
||||||
|
y = -y;
|
||||||
|
z = -z;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalise this vector
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector3f normalise() {
|
||||||
|
float l = 1.0f / length();
|
||||||
|
x *= l;
|
||||||
|
y *= l;
|
||||||
|
z *= l;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalise this vector and place the result in another vector.
|
||||||
|
* @param dest The destination vector, or null if a new vector is to be created
|
||||||
|
* @return the normalised vector
|
||||||
|
*/
|
||||||
|
public Vector3f normalise(Vector3f dest) {
|
||||||
|
float l = length();
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Vector3f(x / l, y / l, z / l);
|
||||||
|
else
|
||||||
|
dest.set(x / l, y / l, z / l);
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The dot product of two vectors is calculated as
|
||||||
|
* v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
|
||||||
|
* @param left The LHS vector
|
||||||
|
* @param right The RHS vector
|
||||||
|
* @return left dot right
|
||||||
|
*/
|
||||||
|
public static float dot(Vector3f left, Vector3f right) {
|
||||||
|
return left.x * right.x + left.y * right.y + left.z * right.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the angle between two vectors, in degrees
|
||||||
|
* @param a A vector
|
||||||
|
* @param b The other vector
|
||||||
|
* @return the angle between the two vectors, in degrees
|
||||||
|
*/
|
||||||
|
public static float angle(Vector3f a, Vector3f b) {
|
||||||
|
float dls = dot(a, b) / (a.length() * b.length());
|
||||||
|
if (dls < -1f)
|
||||||
|
dls = -1f;
|
||||||
|
else if (dls > 1.0f)
|
||||||
|
dls = 1.0f;
|
||||||
|
return Math.acos(dls);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,8 @@
|
||||||
*/
|
*/
|
||||||
package org.lwjgl.vector;
|
package org.lwjgl.vector;
|
||||||
|
|
||||||
|
import org.lwjgl.Math;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* $Id$
|
* $Id$
|
||||||
*
|
*
|
||||||
|
@ -51,4 +53,138 @@ public class Vector4f {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public Vector4f(Vector4f src) {
|
||||||
|
set(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public Vector4f(float x, float y, float z, float w) {
|
||||||
|
set(x, y, z, w);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set values
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector4f set(float x, float y, float z, float w) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
this.w = w;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load from another Vector4f
|
||||||
|
* @param src The source vector
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector4f set(Vector4f src) {
|
||||||
|
x = src.x;
|
||||||
|
y = src.y;
|
||||||
|
z = src.z;
|
||||||
|
w = src.w;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the length of the vector
|
||||||
|
*/
|
||||||
|
public float length() {
|
||||||
|
return Math.sqrt(lengthSquared());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the length squared of the vector
|
||||||
|
*/
|
||||||
|
public float lengthSquared() {
|
||||||
|
return x * x + y * y + z * z + w * w;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translate a vector
|
||||||
|
* @param x The translation in x
|
||||||
|
* @param y the translation in y
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector4f translate(float x, float y, float z, float w) {
|
||||||
|
this.x += x;
|
||||||
|
this.y += y;
|
||||||
|
this.z += z;
|
||||||
|
this.w += w;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negate a vector
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector4f negate() {
|
||||||
|
x = -x;
|
||||||
|
y = -y;
|
||||||
|
z = -z;
|
||||||
|
w = -w;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalise this vector
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Vector4f normalise() {
|
||||||
|
float l = 1.0f / length();
|
||||||
|
x *= l;
|
||||||
|
y *= l;
|
||||||
|
z *= l;
|
||||||
|
w *= l;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalise this vector and place the result in another vector.
|
||||||
|
* @param dest The destination vector, or null if a new vector is to be created
|
||||||
|
* @return the normalised vector
|
||||||
|
*/
|
||||||
|
public Vector4f normalise(Vector4f dest) {
|
||||||
|
float l = length();
|
||||||
|
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Vector4f(x / l, y / l, z / l, w / l);
|
||||||
|
else
|
||||||
|
dest.set(x / l, y / l, z / l, w / l);
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The dot product of two vectors is calculated as
|
||||||
|
* v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w
|
||||||
|
* @param left The LHS vector
|
||||||
|
* @param right The RHS vector
|
||||||
|
* @return left dot right
|
||||||
|
*/
|
||||||
|
public static float dot(Vector4f left, Vector4f right) {
|
||||||
|
return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the angle between two vectors, in degrees
|
||||||
|
* @param a A vector
|
||||||
|
* @param b The other vector
|
||||||
|
* @return the angle between the two vectors, in degrees
|
||||||
|
*/
|
||||||
|
public static float angle(Vector4f a, Vector4f b) {
|
||||||
|
float dls = dot(a, b) / (a.length() * b.length());
|
||||||
|
if (dls < -1f)
|
||||||
|
dls = -1f;
|
||||||
|
else if (dls > 1.0f)
|
||||||
|
dls = 1.0f;
|
||||||
|
return Math.acos(dls);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue