package de.frajul.endlessroll.rendering;

import android.opengl.Matrix;

import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.Entity;
import de.frajul.endlessroll.main.game.Camera;

/**
 * Created by Julian on 23.11.2015.
 */
public class MatrixCreator {

    private float width, height;

    public void setMVPMSize(float width, float height) {
        this.width = width;
        this.height = height;
    }

    public float[] createModelViewProjectionMatrix(Camera camera) {
        float[] mvpMatrix = new float[16];
        float[] projectionMatrix = new float[16];
        float[] viewMatrix = new float[16];

        float ratio = width / height;
        Matrix.frustumM(projectionMatrix, 0, -ratio + camera.getX(), ratio + camera.getX(), -1 + camera.getY(), 1 + camera.getY(), 1, 2);
        Matrix.setLookAtM(viewMatrix, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0);
        Matrix.multiplyMM(mvpMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
        return mvpMatrix;
    }

    public float[] createTransformationMatrix(Entity entity) {
        float width = entity.getWidth();
        float height = entity.getHeight();
        float rotation = entity.getRotation();
        Vector position = entity.getPosition();
        return createTransformationMatrix(width, height, rotation, position);
    }

    public float[] createTransformationMatrix(float width, float height, float rotation, Vector position) {
        float[] transformationMatrix = new float[16];
        Matrix.setIdentityM(transformationMatrix, 0);
        Matrix.translateM(transformationMatrix, 0, position.x, position.y, 0);
        Matrix.rotateM(transformationMatrix, 0, rotation, 0, 0, 1);
        Matrix.scaleM(transformationMatrix, 0, width * .5f, height * .5f, 0);
        return transformationMatrix;
    }

}
