From d624e3dc123c3730a438b7468c7ce5d49dbeb62f Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Mon, 27 May 2019 19:23:51 +0300 Subject: [PATCH] Projection matrices following OpenGL spec gitlab #6 Change-Id: Ia2fe79cb6beaeba65e51de1c5830427533441aed --- .../src/org/simantics/g3d/math/MathTools.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java b/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java index 73d5e606..63ac1ccc 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java +++ b/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java @@ -328,6 +328,19 @@ public class MathTools { } + public static void getMatrix(Quat4d quat, Matrix4d m) { + m.setZero(); + m.m00 = 1.0f - 2.0 * (quat.y * quat.y + quat.z * quat.z); + m.m01 = 2.0 * (quat.x * quat.y + quat.w * quat.z); + m.m02 = 2.0 * (quat.x * quat.z - quat.w * quat.y); + m.m10 = 2.0 * (quat.x * quat.y - quat.w * quat.z); + m.m11 = 1.0 - 2.0f * (quat.x * quat.x + quat.z * quat.z); + m.m12 = 2.0 * (quat.y * quat.z + quat.w * quat.x); + m.m20 = 2.0 * (quat.x * quat.z + quat.w * quat.y); + m.m21 = 2.0 * (quat.y * quat.z - quat.w * quat.x); + m.m22 = 1.0 - 2.0f * (quat.x * quat.x + quat.y * quat.y); + m.m33 = 1.0; + } private static double q[] = new double[3]; private static int nxt[] = { 1, 2, 0 }; @@ -917,4 +930,28 @@ public class MathTools { } return t; } + + public static Matrix4d glFrustum(double l, double r, double b, double t, double n, double f) { + Matrix4d mat = new Matrix4d(); + mat.m00 = 2.0 * n / (r - l); + mat.m11 = 2.0 * n / (t - b); + mat.m02 = (r+l) / (r-l); + mat.m12 = (t+b) / (t-b); + mat.m22 = -(f+n) / (f-n); + mat.m23 = -(2.0 *f * n) / (f-n); + mat.m32 = -1.0; + return mat; + } + + public static Matrix4d glOrtho(double l, double r, double b, double t, double n, double f) { + Matrix4d mat = new Matrix4d(); + mat.m00 = 2.0 / (r - l); + mat.m11 = 2.0 / (t - b); + mat.m22 = -2.0 / (f-n); + mat.m33 = 1.0; + mat.m03 = -(r+l)/(r-l); + mat.m13 = -(t+b)/(t-b); + mat.m23 = -(f+n)/(f-n); + return mat; + } } -- 2.45.2