New base classes Vector and Matrix

This commit is contained in:
Caspian Rychlik-Prince 2002-08-26 15:46:39 +00:00
parent 71b1090163
commit eea62c9e0f
8 changed files with 628 additions and 66 deletions

View File

@ -0,0 +1,132 @@
/*
* Copyright (c) 2002 Light Weight Java Game Library Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'Light Weight Java Game Library' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.vector;
import java.nio.FloatBuffer;
/**
* $Id$
*
* Base class for matrices.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
public abstract class Matrix {
/**
* Constructor for Matrix.
*/
public Matrix() {
super();
}
/**
* Set this matrix to be the identity matrix.
* @return this
*/
public abstract Matrix identity();
/**
* Invert this matrix
* @return this
*/
public abstract Matrix invert();
/**
* Load from a float buffer. The buffer stores the matrix in column major
* (OpenGL) order.
*
* @param buf A float buffer to read from
* @return this
*/
public abstract Matrix load(FloatBuffer buf);
/**
* Load from a float buffer. The buffer stores the matrix in row major
* (mathematical) order.
*
* @param buf A float buffer to read from
* @return this
*/
public abstract Matrix loadTranspose(FloatBuffer buf);
/**
* Negate this matrix
* @return this
*/
public abstract Matrix negate();
/**
* Store this matrix in a float buffer. The matrix is stored in column
* major (openGL) order.
* @param buf The buffer to store this matrix in
* @return this
*/
public abstract Matrix store(FloatBuffer buf);
/**
* Store this matrix in a float buffer. The matrix is stored in row
* major (maths) order.
* @param buf The buffer to store this matrix in
* @return this
*/
public abstract Matrix storeTranspose(FloatBuffer buf);
/**
* Transpose this matrix
* @return this
*/
public abstract Matrix transpose();
/**
* Set this matrix to 0.
* @return this
*/
public abstract Matrix zero();
/**
* @return the determinant of the matrix
*/
public abstract float determinant();
}

View File

@ -42,7 +42,7 @@ import java.nio.FloatBuffer;
* @version $Revision$
*/
public class Matrix2f {
public class Matrix2f extends Matrix {
public float m00 = 1.0f, m01, m10, m11 = 1.0f;
@ -81,7 +81,7 @@ public class Matrix2f {
* @param buf A float buffer to read from
* @return this
*/
public Matrix2f load(FloatBuffer buf) {
public Matrix load(FloatBuffer buf) {
m00 = buf.get();
m10 = buf.get();
@ -91,18 +91,49 @@ public class Matrix2f {
return this;
}
/**
* Load from a float buffer. The buffer stores the matrix in row major
* (mathematical) order.
*
* @param buf A float buffer to read from
* @return this
*/
public Matrix loadTranspose(FloatBuffer buf) {
m00 = buf.get();
m01 = buf.get();
m10 = buf.get();
m11 = buf.get();
return this;
}
/**
* Store this matrix in a float buffer. The matrix is stored in column
* major (openGL) order.
* @param buf The buffer to store this matrix in
*/
public void store(FloatBuffer buf) {
public Matrix store(FloatBuffer buf) {
buf.put(m00);
buf.put(m10);
buf.put(m01);
buf.put(m11);
return this;
}
/**
* Store this matrix in a float buffer. The matrix is stored in row
* major (maths) order.
* @param buf The buffer to store this matrix in
*/
public Matrix storeTranspose(FloatBuffer buf) {
buf.put(m00);
buf.put(m01);
buf.put(m10);
buf.put(m11);
return this;
}
/**
@ -228,7 +259,7 @@ public class Matrix2f {
* Transpose this matrix
* @return this
*/
public Matrix2f transpose() {
public Matrix transpose() {
float temp;
temp = m01;
@ -261,7 +292,7 @@ public class Matrix2f {
* Invert this matrix
* @return this
*/
public Matrix2f invert() {
public Matrix invert() {
return this;
}
@ -269,7 +300,7 @@ public class Matrix2f {
* Negate this matrix
* @return this
*/
public Matrix2f negate() {
public Matrix negate() {
m00 = -m00;
m01 = -m01;
@ -301,7 +332,7 @@ public class Matrix2f {
* Set this matrix to be the identity matrix.
* @return this
*/
public Matrix2f identity() {
public Matrix identity() {
m00 = 1.0f;
m01 = 0.0f;
m10 = 0.0f;
@ -313,7 +344,7 @@ public class Matrix2f {
* Set this matrix to 0.
* @return this
*/
public Matrix2f zero() {
public Matrix zero() {
m00 = 0.0f;
m01 = 0.0f;
m10 = 0.0f;
@ -321,4 +352,11 @@ public class Matrix2f {
return this;
}
/* (non-Javadoc)
* @see org.lwjgl.vector.Matrix#determinant()
*/
public float determinant() {
return 0;
}
}

View File

@ -42,7 +42,7 @@ import java.nio.FloatBuffer;
* @version $Revision$
*/
public class Matrix3f {
public class Matrix3f extends Matrix {
public float m00 = 1.0f,
m01,
@ -87,7 +87,7 @@ public class Matrix3f {
* @param buf A float buffer to read from
* @return this
*/
public Matrix3f load(FloatBuffer buf) {
public Matrix load(FloatBuffer buf) {
m00 = buf.get();
m10 = buf.get();
@ -102,12 +102,34 @@ public class Matrix3f {
return this;
}
/**
* Load from a float buffer. The buffer stores the matrix in row major
* (maths) order.
*
* @param buf A float buffer to read from
* @return this
*/
public Matrix loadTranspose(FloatBuffer buf) {
m00 = buf.get();
m01 = buf.get();
m02 = buf.get();
m10 = buf.get();
m11 = buf.get();
m12 = buf.get();
m20 = buf.get();
m21 = buf.get();
m22 = buf.get();
return this;
}
/**
* Store this matrix in a float buffer. The matrix is stored in column
* major (openGL) order.
* @param buf The buffer to store this matrix in
*/
public void store(FloatBuffer buf) {
public Matrix store(FloatBuffer buf) {
buf.put(m00);
buf.put(m10);
buf.put(m20);
@ -117,8 +139,27 @@ public class Matrix3f {
buf.put(m02);
buf.put(m12);
buf.put(m22);
return this;
}
/**
* Store this matrix in a float buffer. The matrix is stored in row
* major (maths) order.
* @param buf The buffer to store this matrix in
*/
public Matrix storeTranspose(FloatBuffer buf) {
buf.put(m00);
buf.put(m01);
buf.put(m02);
buf.put(m10);
buf.put(m11);
buf.put(m12);
buf.put(m20);
buf.put(m21);
buf.put(m22);
return this;
}
/**
@ -269,7 +310,7 @@ public class Matrix3f {
* Transpose this matrix
* @return this
*/
public Matrix3f transpose() {
public Matrix transpose() {
float f = m10;
m10 = m01;
m01 = f;
@ -324,7 +365,7 @@ public class Matrix3f {
* Invert this matrix
* @return this
*/
public Matrix3f invert() {
public Matrix invert() {
return this;
}
@ -332,7 +373,7 @@ public class Matrix3f {
* Negate this matrix
* @return this
*/
public Matrix3f negate() {
public Matrix negate() {
m00 = -m00;
m01 = -m02;
m02 = -m01;

View File

@ -38,7 +38,7 @@ import java.nio.FloatBuffer;
*
* @author foo
*/
public class Matrix4f {
public class Matrix4f extends Matrix {
/**
* Set this matrix to be the identity matrix.
* @return this
@ -156,6 +156,35 @@ public class Matrix4f {
return this;
}
/**
* Load from a float buffer. The buffer stores the matrix in row major
* (maths) order.
*
* @param buf A float buffer to read from
* @return this
*/
public Matrix4f loadTranspose(FloatBuffer buf) {
m00 = buf.get();
m01 = buf.get();
m02 = buf.get();
m03 = buf.get();
m10 = buf.get();
m11 = buf.get();
m12 = buf.get();
m13 = buf.get();
m20 = buf.get();
m21 = buf.get();
m22 = buf.get();
m23 = buf.get();
m30 = buf.get();
m31 = buf.get();
m32 = buf.get();
m33 = buf.get();
return this;
}
/**
* Store this matrix in a float buffer. The matrix is stored in column
* major (openGL) order.
@ -180,6 +209,30 @@ public class Matrix4f {
buf.put(m33);
}
/**
* Store this matrix in a float buffer. The matrix is stored in row
* major (maths) order.
* @param buf The buffer to store this matrix in
*/
public void storeTranspose(FloatBuffer buf) {
buf.put(m00);
buf.put(m01);
buf.put(m02);
buf.put(m03);
buf.put(m10);
buf.put(m11);
buf.put(m12);
buf.put(m13);
buf.put(m20);
buf.put(m21);
buf.put(m22);
buf.put(m23);
buf.put(m30);
buf.put(m31);
buf.put(m32);
buf.put(m33);
}
/**
* Add two matrices together and place the result in a third matrix.

View File

@ -0,0 +1,109 @@
/*
* Copyright (c) 2002 Light Weight Java Game Library Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'Light Weight Java Game Library' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.vector;
import java.nio.FloatBuffer;
import org.lwjgl.Math;
/**
* $Id$
*
* Base class for vectors.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
public abstract class Vector {
/**
* Constructor for Vector.
*/
public Vector() {
super();
}
/**
* @return the length of the vector
*/
public final float length() {
return Math.sqrt(lengthSquared());
}
/**
* @return the length squared of the vector
*/
public abstract float lengthSquared();
/**
* Load this vector from a FloatBuffer
* @param buf The buffer to load it from, at the current position
* @return this
*/
public abstract Vector load(FloatBuffer buf);
/**
* Negate a vector
* @return this
*/
public abstract Vector negate();
/**
* Normalise this vector
* @return this
*/
public final Vector normalise() {
float l = 1.0f / length();
return scale(l);
}
/**
* Store this vector in a FloatBuffer
* @param buf The buffer to store it in, at the current position
* @return this
*/
public abstract Vector store(FloatBuffer buf);
/**
* Scale this vector
* @param scale The scale factor
* @return this
*/
public abstract Vector scale(float scale);
}

View File

@ -31,6 +31,8 @@
*/
package org.lwjgl.vector;
import java.nio.FloatBuffer;
import org.lwjgl.Math;
/**
@ -42,7 +44,7 @@ import org.lwjgl.Math;
* @version $Revision$
*/
public class Vector2f {
public class Vector2f extends Vector {
public float x, y;
@ -88,13 +90,6 @@ public class Vector2f {
return this;
}
/**
* @return the length of the vector
*/
public float length() {
return Math.sqrt(lengthSquared());
}
/**
* @return the length squared of the vector
*/
@ -118,23 +113,26 @@ public class Vector2f {
* Negate a vector
* @return this
*/
public Vector2f negate() {
public Vector negate() {
x = -x;
y = -y;
return this;
}
/**
* Normalise this vector
* @return this
* Negate a vector and place the result in a destination vector.
* @param dest The destination vector or null if a new vector is to be created
* @return the negated vector
*/
public Vector2f normalise() {
float l = 1.0f / length();
x *= l;
y *= l;
return this;
public Vector2f negate(Vector2f dest) {
if (dest == null)
dest = new Vector2f();
dest.x = -x;
dest.y = -y;
return dest;
}
/**
* 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
@ -176,5 +174,70 @@ public class Vector2f {
dls = 1.0f;
return Math.acos(dls);
}
/**
* Add a vector to another vector and place the result in a destination
* vector.
* @param left The LHS vector
* @param right The RHS vector
* @param dest The destination vector, or null if a new vector is to be created
* @return the sum of left and right in dest
*/
public static Vector2f add(Vector2f left, Vector2f right, Vector2f dest) {
if (dest == null)
return new Vector2f(left.x + right.x, left.y + right.y);
else {
return dest.set(left.x + right.x, left.y + right.y);
}
}
/**
* Subtract a vector from another vector and place the result in a destination
* vector.
* @param left The LHS vector
* @param right The RHS vector
* @param dest The destination vector, or null if a new vector is to be created
* @return left minus right in dest
*/
public static Vector2f sub(Vector2f left, Vector2f right, Vector2f dest) {
if (dest == null)
return new Vector2f(left.x - right.x, left.y - right.y);
else {
return dest.set(left.x - right.x, left.y - right.y);
}
}
/**
* Store this vector in a FloatBuffer
* @param buf The buffer to store it in, at the current position
* @return this
*/
public Vector store(FloatBuffer buf) {
buf.put(x);
buf.put(y);
return this;
}
/**
* Load this vector from a FloatBuffer
* @param buf The buffer to load it from, at the current position
* @return this
*/
public Vector load(FloatBuffer buf) {
x = buf.get();
y = buf.get();
return this;
}
/* (non-Javadoc)
* @see org.lwjgl.vector.Vector#scale(float)
*/
public Vector scale(float scale) {
x *= scale;
y *= scale;
return this;
}
}

View File

@ -31,6 +31,8 @@
*/
package org.lwjgl.vector;
import java.nio.FloatBuffer;
import org.lwjgl.Math;
/**
@ -42,7 +44,7 @@ import org.lwjgl.Math;
* @version $Revision$
*/
public class Vector3f {
public class Vector3f extends Vector {
public float x, y, z;
@ -90,13 +92,6 @@ public class Vector3f {
return this;
}
/**
* @return the length of the vector
*/
public float length() {
return Math.sqrt(lengthSquared());
}
/**
* @return the length squared of the vector
*/
@ -117,11 +112,45 @@ public class Vector3f {
return this;
}
/**
* Add a vector to another vector and place the result in a destination
* vector.
* @param left The LHS vector
* @param right The RHS vector
* @param dest The destination vector, or null if a new vector is to be created
* @return the sum of left and right in dest
*/
public static Vector3f add(Vector3f left, Vector3f right, Vector3f dest) {
if (dest == null)
return new Vector3f(left.x + right.x, left.y + right.y, left.z + right.z);
else {
return dest.set(left.x + right.x, left.y + right.y, left.z + right.z);
}
}
/**
* Subtract a vector from another vector and place the result in a destination
* vector.
* @param left The LHS vector
* @param right The RHS vector
* @param dest The destination vector, or null if a new vector is to be created
* @return left minus right in dest
*/
public static Vector3f sub(Vector3f left, Vector3f right, Vector3f dest) {
if (dest == null)
return new Vector3f(left.x - right.x, left.y - right.y, left.z - right.z);
else {
return dest.set(left.x - right.x, left.y - right.y, left.z - right.z);
}
}
/**
* Negate a vector
* @return this
*/
public Vector3f negate() {
public Vector negate() {
x = -x;
y = -y;
z = -z;
@ -129,17 +158,20 @@ public class Vector3f {
}
/**
* Normalise this vector
* @return this
* Negate a vector and place the result in a destination vector.
* @param dest The destination vector or null if a new vector is to be created
* @return the negated vector
*/
public Vector3f normalise() {
float l = 1.0f / length();
x *= l;
y *= l;
z *= l;
return this;
public Vector3f negate(Vector3f dest) {
if (dest == null)
dest = new Vector3f();
dest.x = -x;
dest.y = -y;
dest.z = -z;
return dest;
}
/**
* 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
@ -182,4 +214,36 @@ public class Vector3f {
return Math.acos(dls);
}
/* (non-Javadoc)
* @see org.lwjgl.vector.Vector#load(FloatBuffer)
*/
public Vector load(FloatBuffer buf) {
return null;
}
/* (non-Javadoc)
* @see org.lwjgl.vector.Vector#scale(float)
*/
public Vector scale(float scale) {
x *= scale;
y *= scale;
z *= scale;
return this;
}
/* (non-Javadoc)
* @see org.lwjgl.vector.Vector#store(FloatBuffer)
*/
public Vector store(FloatBuffer buf) {
buf.put(x);
buf.put(y);
buf.put(z);
return this;
}
}

View File

@ -31,6 +31,8 @@
*/
package org.lwjgl.vector;
import java.nio.FloatBuffer;
import org.lwjgl.Math;
/**
@ -42,7 +44,7 @@ import org.lwjgl.Math;
* @version $Revision$
*/
public class Vector4f {
public class Vector4f extends Vector {
public float x, y, z, w;
@ -92,13 +94,6 @@ public class Vector4f {
return this;
}
/**
* @return the length of the vector
*/
public float length() {
return Math.sqrt(lengthSquared());
}
/**
* @return the length squared of the vector
*/
@ -120,11 +115,44 @@ public class Vector4f {
return this;
}
/**
* Add a vector to another vector and place the result in a destination
* vector.
* @param left The LHS vector
* @param right The RHS vector
* @param dest The destination vector, or null if a new vector is to be created
* @return the sum of left and right in dest
*/
public static Vector4f add(Vector4f left, Vector4f right, Vector4f dest) {
if (dest == null)
return new Vector4f(left.x + right.x, left.y + right.y, left.z + right.z, left.w + right.w);
else {
return dest.set(left.x + right.x, left.y + right.y, left.z + right.z, left.w + right.w);
}
}
/**
* Subtract a vector from another vector and place the result in a destination
* vector.
* @param left The LHS vector
* @param right The RHS vector
* @param dest The destination vector, or null if a new vector is to be created
* @return left minus right in dest
*/
public static Vector4f sub(Vector4f left, Vector4f right, Vector4f dest) {
if (dest == null)
return new Vector4f(left.x - right.x, left.y - right.y, left.z - right.z, left.w - right.w);
else {
return dest.set(left.x - right.x, left.y - right.y, left.z - right.z, left.w - right.w);
}
}
/**
* Negate a vector
* @return this
*/
public Vector4f negate() {
public Vector negate() {
x = -x;
y = -y;
z = -z;
@ -133,18 +161,21 @@ public class Vector4f {
}
/**
* Normalise this vector
* @return this
* Negate a vector and place the result in a destination vector.
* @param dest The destination vector or null if a new vector is to be created
* @return the negated vector
*/
public Vector4f normalise() {
float l = 1.0f / length();
x *= l;
y *= l;
z *= l;
w *= l;
return this;
public Vector4f negate(Vector4f dest) {
if (dest == null)
dest = new Vector4f();
dest.x = -x;
dest.y = -y;
dest.z = -z;
dest.w = -w;
return dest;
}
/**
* 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
@ -187,4 +218,35 @@ public class Vector4f {
return Math.acos(dls);
}
/* (non-Javadoc)
* @see org.lwjgl.vector.Vector#load(FloatBuffer)
*/
public Vector load(FloatBuffer buf) {
return null;
}
/* (non-Javadoc)
* @see org.lwjgl.vector.Vector#scale(float)
*/
public Vector scale(float scale) {
x *= scale;
y *= scale;
z *= scale;
w *= scale;
return this;
}
/* (non-Javadoc)
* @see org.lwjgl.vector.Vector#store(FloatBuffer)
*/
public Vector store(FloatBuffer buf) {
buf.put(x);
buf.put(y);
buf.put(z);
buf.put(w);
return this;
}
}