package de.frajul.endlessroll.rendering.shader;

import android.content.Context;
import android.opengl.GLES20;

import de.frajul.endlessroll.entities.Entity;
import de.frajul.endlessroll.entities.textures.Texture;
import de.frajul.endlessroll.main.game.Camera;
import de.frajul.endlessroll.rendering.MatrixCreator;

/**
 * Created by Julian on 10.08.2016.
 */
public class EntityShader extends ShaderProgram {

    private int location_mvpMatrix;
    private int location_transformationMatrix;
    private int location_alpha;
    private int location_texAtlasSize;
    private int location_texAtlasIndex;
    private int location_texCoordScaling;

    public EntityShader(Context context) throws Exception {
        super(context, "shader/entityVertexShader.glsl", "shader/entityFragmentShader.glsl");
    }

    @Override
    protected void loadUniformLocations() {
        location_mvpMatrix = super.getUniformLocation("mvpMatrix");
        location_transformationMatrix = super.getUniformLocation("transformationMatrix");
        location_alpha = super.getUniformLocation("alpha");
        location_texAtlasSize = super.getUniformLocation("texAtlasSize");
        location_texAtlasIndex = super.getUniformLocation("texAtlasIndex");
        location_texCoordScaling = super.getUniformLocation("texCoordScaling");
    }

    public void loadMVPMatrix(MatrixCreator matrixCreator, Camera camera) {
        float[] mvpMatrix = matrixCreator.createModelViewProjectionMatrix(camera);
        GLES20.glUniformMatrix4fv(location_mvpMatrix, 1, false, mvpMatrix, 0);
    }

    public void loadTransformationMatrix(MatrixCreator matrixCreator, Entity entity) {
        float[] transformationMatrix = matrixCreator.createTransformationMatrix(entity);
        GLES20.glUniformMatrix4fv(location_transformationMatrix, 1, false, transformationMatrix, 0);
    }

    public void loadAlpha(float alpha) {
        GLES20.glUniform1f(location_alpha, alpha);
    }

    public void loadTextureAtlasInfos(Texture texture, int atlasIndex) {
        GLES20.glUniform2f(location_texAtlasSize, texture.getAtlasWidth(), texture.getAtlasHeight());
        GLES20.glUniform1f(location_texAtlasIndex, atlasIndex);
    }

    public void loadTexCoordScaling(float scalingX, float scalingY){
        GLES20.glUniform2f(location_texCoordScaling, scalingX, scalingY);
    }

}
