/*
 * EntityShader.cpp
 *
 *  Created on: 4 nov. 2020
 *      Author: simon
 */

#include "render/shaders/EntityShader.h"

#ifdef __ANDROID__
static const char vertexSource[] =
R"(#version 300 es

precision mediump float;

layout(location = 0) in vec2 position;
layout(location = 1) in int color;

uniform vec2 camPos;
uniform float zoom;
uniform float aspectRatio;
uniform vec2 translation;
uniform float isometricView;

flat out int pass_color;

void main(void){
    float modelX = position.x + translation.x;
    float modelY = position.y + translation.y;
    float x = (modelX - camPos.x - (modelY - camPos.y) * isometricView) / aspectRatio * zoom;
    float y = ((0.5 * (modelX - camPos.x) + 0.5 * (modelY - camPos.y)) * isometricView + (modelY - camPos.y) * (1.0 - isometricView)) * zoom;
	pass_color = color;
	gl_Position = vec4(x, -y, 0.0, 1.0);
}
)";

static const char fragmentSource[] =
R"(#version 300 es

precision mediump float;

flat in int pass_color;

out vec4 out_color;

void main(void){

    float r = float(pass_color >> 24 & 0xFF) / 255.0;
    float g = float(pass_color >> 16 & 0xFF) / 255.0;
    float b = float(pass_color >> 8 & 0xFF) / 255.0;
    float a = float(pass_color & 0xFF) / 255.0; 
	out_color = vec4(r, g, b, a);

}
)";
#else
static const char vertexSource[] = R"(
#version 330

layout(location = 0) in vec2 position;
layout(location = 1) in int color;

uniform vec2 camPos;
uniform float zoom;
uniform float aspectRatio;
uniform vec2 translation;
uniform float isometricView;

flat out int pass_color;

void main(void){
    float modelX = position.x + translation.x;
    float modelY = position.y + translation.y;
    float x = (modelX - camPos.x - (modelY - camPos.y) * isometricView) / aspectRatio * zoom;
    float y = ((0.5 * (modelX - camPos.x) + 0.5 * (modelY - camPos.y)) * isometricView + (modelY - camPos.y) * (1.0 - isometricView)) * zoom;
	pass_color = color;
	gl_Position = vec4(x, -y, 0.0, 1.0);
}
)";

static const char fragmentSource[] = R"(
#version 330

flat in int pass_color;

out vec4 out_color;

void main(void){

    float r = float(pass_color >> 24 & 0xFF) / 255.0;
    float g = float(pass_color >> 16 & 0xFF) / 255.0;
    float b = float(pass_color >> 8 & 0xFF) / 255.0;
    float a = float(pass_color & 0xFF) / 255.0; 
	out_color = vec4(r, g, b, a);

}
)";
#endif

EntityShader::EntityShader() : ShaderProgram() {}

void EntityShader::loadShader() {
    ShaderProgram::loadProgram(vertexSource, fragmentSource);
}

void EntityShader::getAllUniformLocation() {
    location_aspect_ratio = getUniformLocation("aspectRatio");
    location_zoom = getUniformLocation("zoom");
    location_cam = getUniformLocation("camPos");
    location_translation = getUniformLocation("translation");
    location_viewtype = getUniformLocation("isometricView");
}

void EntityShader::setCamPos(const glm::vec2& camPos) {
    loadVector(location_cam, camPos);
}
void EntityShader::setZoom(float zoom) {
    loadFloat(location_zoom, zoom);
}
void EntityShader::setAspectRatio(float aspectRatio) {
    loadFloat(location_aspect_ratio, aspectRatio);
}
void EntityShader::setModelPos(const glm::vec2& modelPos) {
    loadVector(location_translation, modelPos);
}
void EntityShader::setIsometricView(float isometric) {
    loadFloat(location_viewtype, isometric);
}
