package com.danitheskunk.skunkworks;

public final class Mat4f {
	private final double[] data;

	public Mat4f() {
		data = new double[16];
		set(0, 0, 1.0);
		set(1, 1, 1.0);
		set(2, 2, 1.0);
		set(3, 3, 1.0);
	}

	public static Mat4f perspective(
		double fovy, double aspect, double zNear, double zFar
	) {
		var mat = new Mat4f();
		double tanHalfFovy = Math.tan(fovy / 2.0);
		mat.set(0, 0, 1.0 / (aspect * tanHalfFovy));
		mat.set(1, 1, 1.0 / tanHalfFovy);

		mat.set(2, 2, zFar / (zFar - zNear));
		mat.set(2, 3, 1.0);
		mat.set(3, 2, -(zFar * zNear) / (zFar - zNear));

		return mat;
	}

	public double[] asArray() {
		return data;
	}

	public float[] asFloatArray() {
		var arr = new float[16];
		for(int i = 0; i < 16; ++i) {
			arr[i] = (float) data[i];
		}
		return arr;
	}

	public double get(int x, int y) {
		return data[x + y * 4];
	}

	public void set(int x, int y, double val) {
		data[x + y * 4] = val;
	}
}
