Roi Atalla avatar Roi Atalla committed c1e87d4

Made Vector3 and Vector4 compatible with Vector2.

Comments (0)

Files changed (2)

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

+package com.ra4king.opengl.util.math;
+
+import java.nio.FloatBuffer;
+
+import org.lwjgl.BufferUtils;
+
+public class Vector3 {
+	private float x, y, z;
+	
+	public Vector3() {
+		set(0, 0, 0);
+	}
+	
+	public Vector3(float v) {
+		this(v, v, v);
+	}
+	
+	public Vector3(float x, float y, float z) {
+		set(x, y, z);
+	}
+	
+	public Vector3(Vector2 vec) {
+		set(vec);
+	}
+	
+	public Vector3(Vector2 vec, float z) {
+		set(vec, z);
+	}
+	
+	public Vector3(Vector3 vec) {
+		set(vec);
+	}
+	
+	public Vector3(Vector4 vec) {
+		set(vec);
+	}
+	
+	public float x() {
+		return x;
+	}
+	
+	public Vector3 x(float x) {
+		this.x = x;
+		return this;
+	}
+	
+	public float y() {
+		return y;
+	}
+	
+	public Vector3 y(float y) {
+		this.y = y;
+		return this;
+	}
+	
+	public float z() {
+		return z;
+	}
+	
+	public Vector3 z(float z) {
+		this.z = z;
+		return this;
+	}
+	
+	@Override
+	public boolean equals(Object o) {
+		if(o instanceof Vector3) {
+			Vector3 v = (Vector3)o;
+			return x == v.x && y == v.y && z == v.z;
+		}
+		
+		return false;
+	}
+	
+	public Vector3 set(float x, float y, float z) {
+		this.x = x;
+		this.y = y;
+		this.z = z;
+		return this;
+	}
+	
+	public Vector3 set(Vector2 vec) {
+		return set(vec.x(), vec.y(), 0);
+	}
+	
+	public Vector3 set(Vector2 vec, float z) {
+		return set(vec.x(), vec.y(), z);
+	}
+	
+	public Vector3 set(Vector3 vec) {
+		return set(vec.x, vec.y, vec.z);
+	}
+	
+	public Vector3 set(Vector4 vec) {
+		return set(vec.x(), vec.y(), vec.z());
+	}
+	
+	public Vector3 reset() {
+		x = y = z = 0;
+		return this;
+	}
+	
+	public float length() {
+		return (float)Math.sqrt(x * x + y * y + z * z);
+	}
+	
+	public Vector3 normalize() {
+		float length = length();
+		x /= length;
+		y /= length;
+		z /= length;
+		return this;
+	}
+	
+	public float dot(Vector3 vec) {
+		return x * vec.x + y * vec.y + z * vec.z;
+	}
+	
+	public Vector3 cross(Vector3 vec) {
+		return new Vector3(y * vec.z - vec.y * z, z * vec.x - vec.z * x, x * vec.y - vec.x * y);
+	}
+	
+	public Vector3 add(float x, float y, float z) {
+		this.x += x;
+		this.y += y;
+		this.z += z;
+		return this;
+	}
+	
+	public Vector3 add(Vector3 vec) {
+		return add(vec.x, vec.y, vec.z);
+	}
+	
+	public Vector3 sub(float x, float y, float z) {
+		this.x -= x;
+		this.y -= y;
+		this.z -= z;
+		return this;
+	}
+	
+	public Vector3 sub(Vector3 vec) {
+		return sub(vec.x, vec.y, vec.z);
+	}
+	
+	public Vector3 mult(float f) {
+		return mult(f, f, f);
+	}
+	
+	public Vector3 mult(float x, float y, float z) {
+		this.x *= x;
+		this.y *= y;
+		this.z *= z;
+		return this;
+	}
+	
+	public Vector3 mult(Vector3 vec) {
+		return mult(vec.x, vec.y, vec.z);
+	}
+	
+	private final static FloatBuffer direct = BufferUtils.createFloatBuffer(3);
+	
+	public FloatBuffer toBuffer() {
+		direct.clear();
+		direct.put(x).put(y).put(z);
+		direct.flip();
+		return direct;
+	}
+}

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

+package com.ra4king.opengl.util.math;
+
+import java.nio.FloatBuffer;
+
+import org.lwjgl.BufferUtils;
+
+public class Vector4 {
+	private float x, y, z, w;
+	
+	public Vector4() {
+		set(0, 0, 0, 0);
+	}
+	
+	public Vector4(float v) {
+		this(v, v, v, v);
+	}
+	
+	public Vector4(float x, float y, float z, float w) {
+		set(x, y, z, w);
+	}
+	
+	public Vector4(Vector2 vec) {
+		set(vec);
+	}
+	
+	public Vector4(Vector2 vec, float z, float w) {
+		set(vec, z, w);
+	}
+	
+	public Vector4(Vector3 vec) {
+		set(vec);
+	}
+	
+	public Vector4(Vector3 vec, float w) {
+		set(vec, w);
+	}
+	
+	public Vector4(Vector4 vec) {
+		set(vec);
+	}
+	
+	public float x() {
+		return x;
+	}
+	
+	public Vector4 x(float x) {
+		this.x = x;
+		return this;
+	}
+	
+	public float y() {
+		return y;
+	}
+	
+	public Vector4 y(float y) {
+		this.y = y;
+		return this;
+	}
+	
+	public float z() {
+		return z;
+	}
+	
+	public Vector4 z(float z) {
+		this.z = z;
+		return this;
+	}
+	
+	public float w() {
+		return w;
+	}
+	
+	public Vector4 w(float w) {
+		this.w = w;
+		return this;
+	}
+	
+	@Override
+	public boolean equals(Object o) {
+		if(o instanceof Vector4) {
+			Vector4 v = (Vector4)o;
+			return x == v.x && y == v.y && z == v.z && w == v.w;
+		}
+		
+		return false;
+	}
+	
+	public Vector4 set(float x, float y, float z, float w) {
+		this.x = x;
+		this.y = y;
+		this.z = z;
+		this.w = w;
+		return this;
+	}
+	
+	public Vector4 set(Vector2 vec) {
+		return set(vec, 0, 0);
+	}
+	
+	public Vector4 set(Vector2 vec, float z, float w) {
+		return set(vec.x(), vec.y(), z, w);
+	}
+	
+	public Vector4 set(Vector3 vec) {
+		return set(vec, 0);
+	}
+	
+	public Vector4 set(Vector3 vec, float w) {
+		return set(vec.x(), vec.y(), vec.z(), w);
+	}
+	
+	public Vector4 set(Vector4 vec) {
+		return set(vec.x, vec.y, vec.z, vec.w);
+	}
+	
+	public Vector4 reset() {
+		x = y = z = w = 0;
+		return this;
+	}
+	
+	public float length() {
+		return (float)Math.sqrt(x * x + y * y + z * z + w * w);
+	}
+	
+	public Vector4 normalize() {
+		float length = length();
+		x /= length;
+		y /= length;
+		z /= length;
+		w /= length;
+		return this;
+	}
+	
+	public float dot(Vector4 vec) {
+		return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
+	}
+	
+	public Vector4 add(float x, float y, float z, float w) {
+		this.x += x;
+		this.y += y;
+		this.z += z;
+		this.w += w;
+		return this;
+	}
+	
+	public Vector4 add(Vector4 vec) {
+		return add(vec.x, vec.y, vec.z, vec.w);
+	}
+	
+	public Vector4 sub(float x, float y, float z, float w) {
+		this.x -= x;
+		this.y -= y;
+		this.z -= z;
+		this.w -= w;
+		return this;
+	}
+	
+	public Vector4 sub(Vector4 vec) {
+		return sub(vec.x, vec.y, vec.z, vec.w);
+	}
+	
+	public Vector4 mult(float f) {
+		return mult(f, f, f, f);
+	}
+	
+	public Vector4 mult(float x, float y, float z, float w) {
+		this.x *= x;
+		this.y *= y;
+		this.z *= z;
+		this.w *= w;
+		return this;
+	}
+	
+	public Vector4 mult(Vector4 vec) {
+		return mult(vec.x, vec.y, vec.z, vec.w);
+	}
+	
+	private final static FloatBuffer direct = BufferUtils.createFloatBuffer(4);
+	
+	public FloatBuffer toBuffer() {
+		direct.clear();
+		direct.put(x).put(y).put(z).put(w);
+		direct.flip();
+		return direct;
+	}
+}
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.