📄 matrix4f.java
字号:
m30 = matrix[3][0];
m31 = matrix[3][1];
m32 = matrix[3][2];
m33 = matrix[3][3];
}
/**
* <code>set</code> sets the values of this matrix from another matrix.
*
* @param matrix
* the matrix to read the value from.
*/
public Matrix4f set(Matrix4f matrix) {
m00 = matrix.m00; m01 = matrix.m01; m02 = matrix.m02; m03 = matrix.m03;
m10 = matrix.m10; m11 = matrix.m11; m12 = matrix.m12; m13 = matrix.m13;
m20 = matrix.m20; m21 = matrix.m21; m22 = matrix.m22; m23 = matrix.m23;
m30 = matrix.m30; m31 = matrix.m31; m32 = matrix.m32; m33 = matrix.m33;
return this;
}
/**
* <code>set</code> sets the values of this matrix from an array of
* values assuming that the data is rowMajor order;
*
* @param matrix
* the matrix to set the value to.
*/
public void set(float[] matrix) {
set(matrix, true);
}
/**
* <code>set</code> sets the values of this matrix from an array of
* values;
*
* @param matrix
* the matrix to set the value to.
* @param rowMajor
* whether the incoming data is in row or column major order.
*/
public void set(float[] matrix, boolean rowMajor) {
if (matrix.length != 16) throw new JmeException(
"Array must be of size 16.");
if (rowMajor) {
m00 = matrix[0];
m01 = matrix[1];
m02 = matrix[2];
m03 = matrix[3];
m10 = matrix[4];
m11 = matrix[5];
m12 = matrix[6];
m13 = matrix[7];
m20 = matrix[8];
m21 = matrix[9];
m22 = matrix[10];
m23 = matrix[11];
m30 = matrix[12];
m31 = matrix[13];
m32 = matrix[14];
m33 = matrix[15];
} else {
m00 = matrix[0];
m01 = matrix[4];
m02 = matrix[8];
m03 = matrix[12];
m10 = matrix[1];
m11 = matrix[5];
m12 = matrix[9];
m13 = matrix[13];
m20 = matrix[2];
m21 = matrix[6];
m22 = matrix[10];
m23 = matrix[14];
m30 = matrix[3];
m31 = matrix[7];
m32 = matrix[11];
m33 = matrix[15];
}
}
public Matrix4f transpose() {
float[] tmp = new float[16];
get(tmp, true);
Matrix4f mat = new Matrix4f(tmp);
return mat;
}
/**
* <code>transpose</code> locally transposes this Matrix.
*
* @return this object for chaining.
*/
public Matrix4f transposeLocal() {
float[] tmp = new float[16];
get(tmp, true);
set(tmp, false);
return this;
}
/**
* <code>toFloatBuffer</code> returns a FloatBuffer object that contains
* the matrix data.
*
* @return matrix data as a FloatBuffer.
*/
public FloatBuffer toFloatBuffer() {
return toFloatBuffer(false);
}
/**
* <code>toFloatBuffer</code> returns a FloatBuffer object that contains the
* matrix data.
*
* @param columnMajor
* if true, this buffer should be filled with column major data,
* otherwise it will be filled row major.
* @return matrix data as a FloatBuffer. The position is set to 0 for
* convenience.
*/
public FloatBuffer toFloatBuffer(boolean columnMajor) {
FloatBuffer fb = BufferUtils.createFloatBuffer(16);
fillFloatBuffer(fb, columnMajor);
fb.rewind();
return fb;
}
/**
* <code>fillFloatBuffer</code> fills a FloatBuffer object with
* the matrix data.
* @param fb the buffer to fill, must be correct size
* @return matrix data as a FloatBuffer.
*/
public FloatBuffer fillFloatBuffer(FloatBuffer fb) {
return fillFloatBuffer(fb, false);
}
/**
* <code>fillFloatBuffer</code> fills a FloatBuffer object with the matrix
* data.
*
* @param fb
* the buffer to fill, starting at current position. Must have
* room for 16 more floats.
* @param columnMajor
* if true, this buffer should be filled with column major data,
* otherwise it will be filled row major.
* @return matrix data as a FloatBuffer. (position is advanced by 16 and any
* limit set is not changed).
*/
public FloatBuffer fillFloatBuffer(FloatBuffer fb, boolean columnMajor) {
if(columnMajor) {
fb.put(m00).put(m10).put(m20).put(m30);
fb.put(m01).put(m11).put(m21).put(m31);
fb.put(m02).put(m12).put(m22).put(m32);
fb.put(m03).put(m13).put(m23).put(m33);
} else {
fb.put(m00).put(m01).put(m02).put(m03);
fb.put(m10).put(m11).put(m12).put(m13);
fb.put(m20).put(m21).put(m22).put(m23);
fb.put(m30).put(m31).put(m32).put(m33);
}
return fb;
}
/**
* <code>readFloatBuffer</code> reads value for this matrix from a FloatBuffer.
* @param fb the buffer to read from, must be correct size
* @return this data as a FloatBuffer.
*/
public Matrix4f readFloatBuffer(FloatBuffer fb) {
return readFloatBuffer(fb, false);
}
/**
* <code>readFloatBuffer</code> reads value for this matrix from a FloatBuffer.
* @param fb the buffer to read from, must be correct size
* @param columnMajor if true, this buffer should be filled with column
* major data, otherwise it will be filled row major.
* @return this data as a FloatBuffer.
*/
public Matrix4f readFloatBuffer(FloatBuffer fb, boolean columnMajor) {
if(columnMajor) {
m00 = fb.get(); m10 = fb.get(); m20 = fb.get(); m30 = fb.get();
m01 = fb.get(); m11 = fb.get(); m21 = fb.get(); m31 = fb.get();
m02 = fb.get(); m12 = fb.get(); m22 = fb.get(); m32 = fb.get();
m03 = fb.get(); m13 = fb.get(); m23 = fb.get(); m33 = fb.get();
} else {
m00 = fb.get(); m01 = fb.get(); m02 = fb.get(); m03 = fb.get();
m10 = fb.get(); m11 = fb.get(); m12 = fb.get(); m13 = fb.get();
m20 = fb.get(); m21 = fb.get(); m22 = fb.get(); m23 = fb.get();
m30 = fb.get(); m31 = fb.get(); m32 = fb.get(); m33 = fb.get();
}
return this;
}
/**
* <code>loadIdentity</code> sets this matrix to the identity matrix,
* namely all zeros with ones along the diagonal.
*
*/
public void loadIdentity() {
m01 = m02 = m03 = 0.0f;
m10 = m12 = m13 = 0.0f;
m20 = m21 = m23 = 0.0f;
m30 = m31 = m32 = 0.0f;
m00 = m11 = m22 = m33 = 1.0f;
}
/**
* <code>fromAngleAxis</code> sets this matrix4f to the values specified
* by an angle and an axis of rotation. This method creates an object, so
* use fromAngleNormalAxis if your axis is already normalized.
*
* @param angle
* the angle to rotate (in radians).
* @param axis
* the axis of rotation.
*/
public void fromAngleAxis(float angle, Vector3f axis) {
Vector3f normAxis = axis.normalize();
fromAngleNormalAxis(angle, normAxis);
}
/**
* <code>fromAngleNormalAxis</code> sets this matrix4f to the values
* specified by an angle and a normalized axis of rotation.
*
* @param angle
* the angle to rotate (in radians).
* @param axis
* the axis of rotation (already normalized).
*/
public void fromAngleNormalAxis(float angle, Vector3f axis) {
zero();
m33 = 1;
float fCos = FastMath.cos(angle);
float fSin = FastMath.sin(angle);
float fOneMinusCos = ((float)1.0)-fCos;
float fX2 = axis.x*axis.x;
float fY2 = axis.y*axis.y;
float fZ2 = axis.z*axis.z;
float fXYM = axis.x*axis.y*fOneMinusCos;
float fXZM = axis.x*axis.z*fOneMinusCos;
float fYZM = axis.y*axis.z*fOneMinusCos;
float fXSin = axis.x*fSin;
float fYSin = axis.y*fSin;
float fZSin = axis.z*fSin;
m00 = fX2*fOneMinusCos+fCos;
m01 = fXYM-fZSin;
m02 = fXZM+fYSin;
m10 = fXYM+fZSin;
m11 = fY2*fOneMinusCos+fCos;
m12 = fYZM-fXSin;
m20 = fXZM-fYSin;
m21 = fYZM+fXSin;
m22 = fZ2*fOneMinusCos+fCos;
}
/**
* <code>mult</code> multiplies this matrix by a scalar.
*
* @param scalar
* the scalar to multiply this matrix by.
*/
public void multLocal(float scalar) {
m00 *= scalar;
m01 *= scalar;
m02 *= scalar;
m03 *= scalar;
m10 *= scalar;
m11 *= scalar;
m12 *= scalar;
m13 *= scalar;
m20 *= scalar;
m21 *= scalar;
m22 *= scalar;
m23 *= scalar;
m30 *= scalar;
m31 *= scalar;
m32 *= scalar;
m33 *= scalar;
}
public Matrix4f mult(float scalar) {
Matrix4f out = new Matrix4f();
out.set(this);
out.multLocal(scalar);
return out;
}
public Matrix4f mult(float scalar, Matrix4f store) {
store.set(this);
store.multLocal(scalar);
return store;
}
/**
* <code>mult</code> multiplies this matrix with another matrix. The
* result matrix will then be returned. This matrix will be on the left hand
* side, while the parameter matrix will be on the right.
*
* @param in2
* the matrix to multiply this matrix by.
* @return the resultant matrix
*/
public Matrix4f mult(Matrix4f in2) {
return mult(in2, null);
}
/**
* <code>mult</code> multiplies this matrix with another matrix. The
* result matrix will then be returned. This matrix will be on the left hand
* side, while the parameter matrix will be on the right.
*
* @param in2
* the matrix to multiply this matrix by.
* @param store
* where to store the result. It is safe for in2 and store to be
* the same object.
* @return the resultant matrix
*/
public Matrix4f mult(Matrix4f in2, Matrix4f store) {
if (store == null) store = new Matrix4f();
float temp00, temp01, temp02, temp03;
float temp10, temp11, temp12, temp13;
float temp20, temp21, temp22, temp23;
float temp30, temp31, temp32, temp33;
temp00 = m00 * in2.m00 +
m01 * in2.m10 +
m02 * in2.m20 +
m03 * in2.m30;
temp01 = m00 * in2.m01 +
m01 * in2.m11 +
m02 * in2.m21 +
m03 * in2.m31;
temp02 = m00 * in2.m02 +
m01 * in2.m12 +
m02 * in2.m22 +
m03 * in2.m32;
temp03 = m00 * in2.m03 +
m01 * in2.m13 +
m02 * in2.m23 +
m03 * in2.m33;
temp10 = m10 * in2.m00 +
m11 * in2.m10 +
m12 * in2.m20 +
m13 * in2.m30;
temp11 = m10 * in2.m01 +
m11 * in2.m11 +
m12 * in2.m21 +
m13 * in2.m31;
temp12 = m10 * in2.m02 +
m11 * in2.m12 +
m12 * in2.m22 +
m13 * in2.m32;
temp13 = m10 * in2.m03 +
m11 * in2.m13 +
m12 * in2.m23 +
m13 * in2.m33;
temp20 = m20 * in2.m00 +
m21 * in2.m10 +
m22 * in2.m20 +
m23 * in2.m30;
temp21 = m20 * in2.m01 +
m21 * in2.m11 +
m22 * in2.m21 +
m23 * in2.m31;
temp22 = m20 * in2.m02 +
m21 * in2.m12 +
m22 * in2.m22 +
m23 * in2.m32;
temp23 = m20 * in2.m03 +
m21 * in2.m13 +
m22 * in2.m23 +
m23 * in2.m33;
temp30 = m30 * in2.m00 +
m31 * in2.m10 +
m32 * in2.m20 +
m33 * in2.m30;
temp31 = m30 * in2.m01 +
m31 * in2.m11 +
m32 * in2.m21 +
m33 * in2.m31;
temp32 = m30 * in2.m02 +
m31 * in2.m12 +
m32 * in2.m22 +
m33 * in2.m32;
temp33 = m30 * in2.m03 +
m31 * in2.m13 +
m32 * in2.m23 +
m33 * in2.m33;
store.m00 = temp00; store.m01 = temp01; store.m02 = temp02; store.m03 = temp03;
store.m10 = temp10; store.m11 = temp11; store.m12 = temp12; store.m13 = temp13;
store.m20 = temp20; store.m21 = temp21; store.m22 = temp22; store.m23 = temp23;
store.m30 = temp30; store.m31 = temp31; store.m32 = temp32; store.m33 = temp33;
return store;
}
/**
* <code>mult</code> multiplies this matrix with another matrix. The
* results are stored internally and a handle to this matrix will
* then be returned. This matrix will be on the left hand
* side, while the parameter matrix will be on the right.
*
* @param in2
* the matrix to multiply this matrix by.
* @return the resultant matrix
*/
public Matrix4f multLocal(Matrix4f in2) {
return mult(in2, this);
}
/**
* <code>mult</code> multiplies a vector about a rotation matrix. The
* resulting vector is returned as a new Vector3f.
*
* @param vec
* vec to multiply against.
* @return the rotated vector.
*/
public Vector3f mult(Vector3f vec) {
return mult(vec, null);
}
/**
* <code>mult</code> multiplies a vector about a rotation matrix and adds
* translation. The resulting vector is returned.
*
* @param vec
* vec to multiply against.
* @param store
* a vector to store the result in. Created if null is passed.
* @return the rotated vector.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -