Roi Atalla avatar Roi Atalla committed 9d5d7e5

Added "inverse" and "determinant" methods in Matrix4 and added "mult(float)" in both Matrix3/4. Also setup files for Example9.2

Comments (0)

Files changed (3)

src/main/java/com/ra4king/opengl/arcsynthesis/gl33/chapter9/example2/Example9_2.java

 package com.ra4king.opengl.arcsynthesis.gl33.chapter9.example2;
 
-public class Example9_2 {
+import com.ra4king.opengl.GLProgram;
+
+public class Example9_2 extends GLProgram {
+	public static void main(String[] args) {
+		new Example9_2().run(true);
+	}
 	
+	public Example9_2() {
+		super("Example 9.2", 500, 500, false);
+	}
+	
+	@Override
+	public void init() {
+		
+	}
+	
+	@Override
+	public void render() {
+		
+	}
 }

src/main/java/com/ra4king/opengl/util/math/Matrix3.java

 		return this;
 	}
 	
+	public Matrix3 mult(float f) {
+		for(int a = 0; a < matrix.length; a++)
+			put(a, get(a) * f);
+		
+		return this;
+	}
+	
 	public Matrix3 mult(float[] m) {
 		float[] newm = new float[matrix.length];
 		

src/main/java/com/ra4king/opengl/util/math/Matrix4.java

 		return put(m.matrix);
 	}
 	
+	public Matrix4 mult(float f) {
+		for(int a = 0; a < matrix.length; a++)
+			put(a, get(a) * f);
+		
+		return this;
+	}
+	
 	public Matrix4 mult(float[] m) {
 		float[] newm = new float[matrix.length];
 		
 		return rotate((float)Math.toRadians(angle),vec);
 	}
 	
+	public float determinant() {
+		float a = get(5)*get(10)*get(15) + get(9)*get(14)*get(7) + get(13)*get(6)*get(11) - get(7)*get(10)*get(13) - get(11)*get(14)*get(5) - get(15)*get(6)*get(9);
+		float b = get(1)*get(10)*get(15) + get(9)*get(14)*get(3) + get(13)*get(2)*get(11) - get(3)*get(10)*get(13) - get(11)*get(14)*get(1) - get(15)*get(2)*get(9);
+		float c = get(1)*get(6)*get(15) + get(5)*get(14)*get(3) + get(13)*get(2)*get(7) - get(3)*get(6)*get(13) - get(7)*get(14)*get(1) - get(15)*get(2)*get(5);
+		float d = get(1)*get(6)*get(11) + get(5)*get(10)*get(3) + get(9)*get(2)*get(7) - get(3)*get(6)*get(9) - get(7)*get(10)*get(1) - get(11)*get(2)*get(5);
+		
+		return get(0)*a - get(4)*b + get(8)*c - get(12)*d;
+	}
+	
+	public Matrix4 inverse() {
+		Matrix4 inv = new Matrix4();
+		
+		inv.put(0, + (get(5)*get(10)*get(15) + get(9)*get(14)*get(7) + get(13)*get(6)*get(11) - get(7)*get(10)*get(13) - get(11)*get(14)*get(5) - get(15)*get(6)*get(9)));
+		inv.put(1, - (get(4)*get(10)*get(15) + get(8)*get(14)*get(7) + get(12)*get(6)*get(11) - get(7)*get(10)*get(12) - get(11)*get(14)*get(4) - get(15)*get(6)*get(8)));
+		inv.put(2, + (get(4)*get(9)*get(15) + get(8)*get(13)*get(7) + get(12)*get(5)*get(11) - get(7)*get(9)*get(12) - get(11)*get(13)*get(4) - get(15)*get(5)*get(8)));
+		inv.put(3, - (get(4)*get(9)*get(14) + get(8)*get(13)*get(6) + get(12)*get(5)*get(10) - get(6)*get(9)*get(12) - get(10)*get(13)*get(4) - get(14)*get(5)*get(8)));
+		
+		inv.put(4, - (get(1)*get(10)*get(15) + get(9)*get(14)*get(3) + get(13)*get(2)*get(11) - get(3)*get(10)*get(13) - get(11)*get(14)*get(1) - get(15)*get(2)*get(9)));
+		inv.put(5, + (get(0)*get(10)*get(15) + get(8)*get(14)*get(3) + get(12)*get(2)*get(11) - get(3)*get(10)*get(12) - get(11)*get(14)*get(0) - get(15)*get(2)*get(8)));
+		inv.put(6, - (get(0)*get(9)*get(15) + get(8)*get(13)*get(3) + get(12)*get(1)*get(11) - get(3)*get(9)*get(12) - get(11)*get(13)*get(0) - get(15)*get(1)*get(8)));
+		inv.put(7, + (get(0)*get(9)*get(14) + get(8)*get(13)*get(2) + get(12)*get(1)*get(10) - get(2)*get(9)*get(12) - get(10)*get(13)*get(0) - get(14)*get(1)*get(8)));
+		
+		inv.put(8, + (get(1)*get(6)*get(15) + get(5)*get(14)*get(3) + get(13)*get(2)*get(7) - get(3)*get(6)*get(13) - get(7)*get(14)*get(1) - get(15)*get(2)*get(5)));
+		inv.put(9, - (get(0)*get(6)*get(15) + get(4)*get(14)*get(3) + get(12)*get(2)*get(7) - get(3)*get(6)*get(12) - get(7)*get(14)*get(0) - get(15)*get(2)*get(4)));
+		inv.put(10, + (get(0)*get(5)*get(15) + get(4)*get(13)*get(3) + get(12)*get(1)*get(7) - get(3)*get(5)*get(12) - get(7)*get(13)*get(0) - get(15)*get(1)*get(4)));
+		inv.put(11, - (get(0)*get(5)*get(14) + get(4)*get(13)*get(2) + get(12)*get(1)*get(6) - get(2)*get(5)*get(12) - get(6)*get(13)*get(0) - get(14)*get(1)*get(4)));
+		
+		inv.put(12, - (get(1)*get(6)*get(11) + get(5)*get(10)*get(3) + get(9)*get(2)*get(7) - get(3)*get(6)*get(9) - get(7)*get(10)*get(1) - get(11)*get(2)*get(5)));
+		inv.put(13, + (get(0)*get(6)*get(11) + get(4)*get(10)*get(3) + get(8)*get(2)*get(7) - get(3)*get(6)*get(8) - get(7)*get(10)*get(0) - get(11)*get(2)*get(4)));
+		inv.put(14, - (get(0)*get(5)*get(11) + get(4)*get(9)*get(3) + get(8)*get(1)*get(7) - get(3)*get(5)*get(8) - get(7)*get(9)*get(0) - get(11)*get(1)*get(4)));
+		inv.put(15, + (get(0)*get(5)*get(10) + get(4)*get(9)*get(2) + get(8)*get(1)*get(6) - get(2)*get(5)*get(8) - get(6)*get(9)*get(0) - get(10)*get(1)*get(4)));
+		
+		return put(inv.transpose().mult(1f/determinant()));
+	}
+	
 	public Quaternion toQuaternion() {
 		float x = get(0)  - get(5) - get(10);
 		float y = get(5)  - get(0) - get(10);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.