#include "client/render/shader/WorldShader.h"

namespace blitz {
namespace shader {

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

precision mediump float;

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

uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

flat out int pass_color;

void main(void){
   	pass_color = color;
	gl_Position = projectionMatrix * viewMatrix * vec4(position, 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 vec3 position;
layout(location = 1) in int color;

uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

flat out int pass_color;

void main(void){
   	pass_color = color;
	gl_Position = projectionMatrix * viewMatrix * vec4(position, 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

WorldShader::WorldShader() : ShaderProgram() {}

void WorldShader::LoadShader() {
	ShaderProgram::LoadProgram(vertexSource, fragmentSource);
}

void WorldShader::GetAllUniformLocation() {
	m_LocationProjection = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
	m_LocationView = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
}

void WorldShader::SetProjectionMatrix(const Mat4f& proj) const {
	LoadMat4(m_LocationProjection, proj);
}

void WorldShader::SetViewMatrix(const Mat4f& view) const {
	LoadMat4(m_LocationView, view);
}

}  // namespace shader
}  // namespace blitz