matrix.java
字号:
public final double m24() { return this.m24; } public final double m31() { return this.m31; } public final double m32() { return this.m32; } public final double m33() { return this.m33; } public final double m34() { return this.m34; } public final double m41() { return this.m41; } public final double m42() { return this.m42; } public final double m43() { return this.m43; } public final double m44() { return this.m44; } // ============== Factory Functions ======================= // // ============== Factory Functions ======================= // // ============== Factory Functions ======================= // public static Matrix fromAxisAngle(Angle angle, Vec4 axis) { if (angle == null) { String msg = Logging.getMessage("nullValue.AngleIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } if (axis == null) { String msg = Logging.getMessage("nullValue.Vec4IsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } return fromAxisAngle(angle, axis.x, axis.y, axis.z, true); } public static Matrix fromAxisAngle(Angle angle, double axisX, double axisY, double axisZ) { if (angle == null) { String msg = Logging.getMessage("nullValue.AngleIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } return fromAxisAngle(angle, axisX, axisY, axisZ, true); } private static Matrix fromAxisAngle(Angle angle, double axisX, double axisY, double axisZ, boolean normalize) { if (angle == null) { String msg = Logging.getMessage("nullValue.AngleIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } if (normalize) { double length = Math.sqrt((axisX * axisX) + (axisY * axisY) + (axisZ * axisZ)); if (!isZero(length) && (length != 1.0)) { axisX /= length; axisY /= length; axisZ /= length; } } double c = angle.cos(); double s = angle.sin(); double one_minus_c = 1.0 - c; return new Matrix( // Row 1 c + (one_minus_c * axisX * axisX), (one_minus_c * axisX * axisY) - (s * axisZ), (one_minus_c * axisX * axisZ) + (s * axisY), 0.0, // Row 2 (one_minus_c * axisX * axisY) + (s * axisZ), c + (one_minus_c * axisY * axisY), (one_minus_c * axisY * axisZ) - (s * axisX), 0.0, // Row 3 (one_minus_c * axisX * axisZ) - (s * axisY), (one_minus_c * axisY * axisZ) + (s * axisX), c + (one_minus_c * axisZ * axisZ), 0.0, // Row 4 0.0, 0.0, 0.0, 1.0, // Rotation matrices are orthogonal, 3D transforms. true); } public static Matrix fromQuaternion(Quaternion quaternion) { if (quaternion == null) { String msg = Logging.getMessage("nullValue.QuaternionIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } return fromQuaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w, true); } private static Matrix fromQuaternion(double x, double y, double z, double w, boolean normalize) { if (normalize) { double length = Math.sqrt((x * x) + (y * y) + (z * z)+ (w * w)); if (!isZero(length) && (length != 1.0)) { x /= length; y /= length; z /= length; w /= length; } } return new Matrix( // Row 1 1.0 - (2.0 * y * y) - (2.0 * z * z), (2.0 * x * y) - (2.0 * z * w), (2.0 * x * z) + (2.0 * y * w), 0.0, // Row 2 (2.0 * x * y) + (2.0 * z * w), 1.0 - (2.0 * x * x) - (2.0 * z * z), (2.0 * y * z) - (2.0 * x * w), 0.0, // Row 3 (2.0 * x * z) - (2.0 * y * w), (2.0 * y * z) + (2.0 * x * w), 1.0 - (2.0 * x * x) - (2.0 * y * y), 0.0, // Row 4 0.0, 0.0, 0.0, 1.0, // Rotation matrices are orthogonal, 3D transforms. true); } public static Matrix fromRotationXYZ(Angle xRotation, Angle yRotation, Angle zRotation) { if ((xRotation == null) || (yRotation == null) || (zRotation == null)) { String msg = Logging.getMessage("nullValue.AngleIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } double cx = xRotation.cos(); double cy = yRotation.cos(); double cz = zRotation.cos(); double sx = xRotation.sin(); double sy = yRotation.sin(); double sz = zRotation.sin(); return new Matrix( cy * cz, -cy * sz, sy, 0.0, (sx * sy * cz) + (cx * sz), -(sx * sy * sz) + (cx * cz), -sx * cy, 0.0, -(cx * sy * cz) + (sx * sz), (cx * sy * sz) + (sx * cz), cx * cy, 0.0, 0.0, 0.0, 0.0, 1.0, // Rotation matrices are orthogonal, 3D transforms. true); } public static Matrix fromRotationX(Angle angle) { if (angle == null) { String msg = Logging.getMessage("nullValue.AngleIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } double c = angle.cos(); double s = angle.sin(); return new Matrix( 1.0, 0.0, 0.0, 0.0, 0.0, c, -s, 0.0, 0.0, s, c, 0.0, 0.0, 0.0, 0.0, 1.0, // Rotation matrices are orthogonal, 3D transforms. true); } public static Matrix fromRotationY(Angle angle) { if (angle == null) { String msg = Logging.getMessage("nullValue.AngleIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } double c = angle.cos(); double s = angle.sin(); return new Matrix( c, 0.0, s, 0.0, 0.0, 1.0, 0.0, 0.0, -s, 0.0, c, 0.0, 0.0, 0.0, 0.0, 1.0, // Rotation matrices are orthogonal, 3D transforms. true); } public static Matrix fromRotationZ(Angle angle) { if (angle == null) { String msg = Logging.getMessage("nullValue.AngleIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } double c = angle.cos(); double s = angle.sin(); return new Matrix( c, -s, 0.0, 0.0, s, c, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, // Rotation matrices are orthogonal, 3D transforms. true); } public static Matrix fromScale(double scale) { return fromScale(scale, scale, scale); } public static Matrix fromScale(Vec4 scale) { if (scale == null) { String msg = Logging.getMessage("nullValue.Vec4IsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } return fromScale(scale.x, scale.y, scale.z); } public static Matrix fromScale(double scaleX, double scaleY, double scaleZ) { return new Matrix( scaleX, 0.0, 0.0, 0.0, 0.0, scaleY, 0.0, 0.0, 0.0, 0.0, scaleZ, 0.0, 0.0, 0.0, 0.0, 1.0, // Scale matrices are non-orthogonal, 3D transforms. false); } public static Matrix fromTranslation(Vec4 translation) { if (translation == null) { String msg = Logging.getMessage("nullValue.Vec4IsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } return fromTranslation(translation.x, translation.y, translation.z); } public static Matrix fromTranslation(double x, double y, double z) { return new Matrix( 1.0, 0.0, 0.0, x, 0.0, 1.0, 0.0, y, 0.0, 0.0, 1.0, z, 0.0, 0.0, 0.0, 1.0, // Translation matrices are orthogonal, 3D transforms. true); } public static Matrix fromViewLookAt(Vec4 eye, Vec4 center, Vec4 up) { if (eye == null || center == null || up == null) { String msg = Logging.getMessage("nullValue.Vec4IsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } Vec4 forward = center.subtract3(eye); Vec4 f = forward.normalize3(); Vec4 s = f.cross3(up); s = s.normalize3(); Vec4 u = s.cross3(f); u = u.normalize3(); Matrix mAxes = new Matrix( s.x, s.y, s.z, 0.0, u.x, u.y, u.z, 0.0, -f.x, -f.y, -f.z, 0.0, 0.0, 0.0, 0.0, 1.0, true); Matrix mEye = Matrix.fromTranslation( -eye.x, -eye.y, -eye.z); return mAxes.multiply(mEye); } public static Matrix fromModelLookAt(Vec4 eye, Vec4 center, Vec4 up) { if (eye == null || center == null || up == null) { String msg = Logging.getMessage("nullValue.Vec4IsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } Vec4 forward = center.subtract3(eye); Vec4 f = forward.normalize3(); Vec4 s = up.cross3(f); s = s.normalize3(); Vec4 u = f.cross3(s); u = u.normalize3(); Matrix mAxes = new Matrix( s.x, u.x, f.x, 0.0, s.y, u.y, f.y, 0.0, s.z, u.z, f.z, 0.0, 0.0, 0.0, 0.0, 1.0, true); Matrix mEye = Matrix.fromTranslation( eye.x, eye.y, eye.z); return mEye.multiply(mAxes); } public static Matrix fromPerspective(Angle horizontalFieldOfView, double viewportWidth, double viewportHeight, double near, double far) { if (horizontalFieldOfView == null) { String msg = Logging.getMessage("nullValue.AngleIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } double fovX = horizontalFieldOfView.degrees; if (fovX <= 0.0 || fovX > 180.0) { String msg = Logging.getMessage("generic.ArgumentOutOfRange", "horizontalFieldOfView=" + fovX); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } if (viewportWidth <= 0.0) { String msg = Logging.getMessage("generic.ArgumentOutOfRange", "viewportWidth=" + viewportWidth); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } if (viewportHeight <= 0.0) { String msg = Logging.getMessage("generic.ArgumentOutOfRange", "viewportHeight=" + viewportHeight); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } if (near <= 0.0) { String msg = Logging.getMessage("generic.ArgumentOutOfRange", "near=" + near); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } if (far <= 0.0) { String msg = Logging.getMessage("generic.ArgumentOutOfRange", "far=" + far); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } if (far <= near) { String msg = Logging.getMessage("generic.ArgumentOutOfRange", "far=" + far + ",near=" + near); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } double f = 1.0 / horizontalFieldOfView.tanHalfAngle(); // We are using *horizontal* field-of-view here. This results in a different matrix than documented in sources // using vertical field-of-view. return new Matrix( f, 0.0, 0.0, 0.0, 0.0, (f * viewportWidth) / viewportHeight, 0.0, 0.0, 0.0, 0.0, -(far + near) / (far - near), -(2.0 * far * near) / (far - near), 0.0, 0.0, -1.0, 0.0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -