package projectzombie.display;

import gl_engine.MathHelpers;
import gl_engine.matrix.Matrix4;
import gl_engine.vec.Vec3d;
import projectzombie.Main;
import projectzombie.world.chunk.Chunk;

public class Camera
{
	public double x, y;
	public double angle = 45;
	
	public Matrix4 matrix;
	public Matrix4 projection;
	public Matrix4 projection_sun;
	
	public static Camera camera;
	
	public Camera(int w, int h)
	{
		matrix = Matrix4.identity();
		Vec3d pos = Main.player.getPos();
		angle = Main.player.angle;
		
		x = pos.x - 0.5;
		y = pos.z - 0.5;
		
		matrix = Matrix4.multiply(matrix, Matrix4.rotate(angle + 180, 0, 1, 0));
		matrix = Matrix4.multiply(matrix, Matrix4.rotate(-45, 1, 0, 0));
		matrix = Matrix4.multiply(matrix, Matrix4.translate(0, 0, -16));
		
		projection = Matrix4.projection((double)w / (double)h, 45, 0.1, Chunk.RENDER_DISTANCE*16+32);
		projection = Matrix4.multiply(matrix, projection);
		
		double time = Main.world.getLayer().layergen.getSunPosition();
		
		projection_sun = Matrix4.identity();
		projection_sun = Matrix4.multiply(projection_sun, Matrix4.rotate(angle + 180, 0, 1, 0));
		projection_sun = Matrix4.multiply(projection_sun, Matrix4.translate(0, 0, 8));
		projection_sun = Matrix4.multiply(projection_sun, Matrix4.rotate(-angle - 180, 0, 1, 0));
		projection_sun = Matrix4.multiply(projection_sun, Matrix4.rotate(20, 0, 0, 1));
		projection_sun = Matrix4.multiply(projection_sun, Matrix4.rotate(-time, 1, 0, 0));
		projection_sun = Matrix4.multiply(projection_sun, Matrix4.translate(0, 0, -(16*Chunk.RENDER_DISTANCE+16)));
		projection_sun = Matrix4.multiply(projection_sun, Matrix4.scale(new Vec3d(1.0/20.0, 1.0/20.0, -1.0/(32*Chunk.RENDER_DISTANCE+32))));
	}
}
