
#include "Tsh_ripple.h"


static const char* pVS = " \n\
#version 330 core   \n\
 \n\
layout (location = 0) in vec3 in_vertex; \n\
layout (location = 1) in vec3 in_normal; \n\
layout (location = 2) in vec2 in_uv; \n\
 \n\
uniform mat4 uVP; \n\
uniform mat4 uModel; \n\
 \n\
out vec2 var_uv; \n\
 \n\
void main() \n\
{ \n\
    gl_Position = uVP * uModel * vec4(in_vertex, 1.0); \n\
    var_uv = in_uv; \n\
}";

static const char* pFS = " \n\
#version 330 core \n\
 \n\
in vec2 var_uv; \n\
 \n\
uniform sampler2D uColorMap; \n\
uniform float uTime; \n\
uniform float uCircle_border = 0.01; \n\
uniform vec4 uCircle_color = vec4(0.8, 0.8, 0.8, 1.0); \n\
uniform vec2 uCircle_center = vec2(0.5, 0.5); \n\
uniform float uAmbient = 0.1; \n\
 \n\
out vec4 out_color; \n\
 \n\
void main() { \n\
    vec2 uv = var_uv - uCircle_center; \n\
    vec4 bkg_color = texture2D(uColorMap, uv * vec2(1.0, -1.0)); \n\
    float dist =  sqrt(dot(uv, uv)); \n\
    float timer = mod(uTime*0.05, 5.0) *0.025;\n\
    float mixe, radius; \n\
 \n\
    radius = timer;\n\
    mixe = 1.0 + smoothstep(radius, radius + uCircle_border, dist) - smoothstep(radius - uCircle_border, radius, dist); \n\
    out_color = mix(uCircle_color, vec4(0), mixe); \n\
 \n\
    radius = timer*0.1;\n\
    mixe = 1.0 + smoothstep(radius, radius + uCircle_border, dist) - smoothstep(radius - uCircle_border, radius, dist); \n\
    out_color += mix(uCircle_color, vec4(0), mixe); \n\
 \n\
    radius = timer*0.5;\n\
    mixe = 1.0 + smoothstep(radius, radius + uCircle_border, dist) - smoothstep(radius - uCircle_border, radius, dist); \n\
    out_color += mix(uCircle_color, vec4(0), mixe); \n\
 \n\
    out_color = mix(out_color, bkg_color, 0.5) * uAmbient; \n\
}";


Tsh_ripple::Tsh_ripple()
{
    //
}

bool Tsh_ripple::create()
{
    if (!OShader::init())
    {
        ECO_error_set("err Tsh_ripple init");
        return false;
    }
    if (!addShader(GL_VERTEX_SHADER, pVS))
    {
        ECO_error_set("err Tsh_ripple pvs");
        return false;
    }
    if (!addShader(GL_FRAGMENT_SHADER, pFS))
    {
        ECO_error_set("err Tsh_ripple pfs");
        return false;
    }
    if (!finalize())
    {
        ECO_error_set("err Tsh_ripple finalize");
        return false;
    }

    m_vp = getUniformLocation("uVP");
    m_model = getUniformLocation("uModel");
    m_colorMap = getUniformLocation("uColorMap");
    m_time = getUniformLocation("uTime");
    m_ambient = getUniformLocation("uAmbient");
    m_circle_center = getUniformLocation("uCircle_center");
    m_circle_border = getUniformLocation("uCircle_border");
    m_circle_color = getUniformLocation("uCircle_color");
/*
    if (m_vp == INVALID_UNIFORM_LOCATION
        || m_model == INVALID_UNIFORM_LOCATION
        || m_colorMap == INVALID_UNIFORM_LOCATION
        || m_time == INVALID_UNIFORM_LOCATION
        || m_ambient == INVALID_UNIFORM_LOCATION
        || m_circle_center == INVALID_UNIFORM_LOCATION
        || m_circle_border == INVALID_UNIFORM_LOCATION
        || m_circle_color == INVALID_UNIFORM_LOCATION
        )
        return false;
*/
    return true;
}

void Tsh_ripple::setVP(const OMat4& vp)
{
    glUniformMatrix4fv(m_vp, 1, GL_TRUE, vp.getFv());
}

void Tsh_ripple::setModel(const OMat4& model)
{
    glUniformMatrix4fv(m_model, 1, GL_TRUE, model.getFv());
}

void Tsh_ripple::setTexture(uint32_t texture)
{
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glUniform1i(m_colorMap, 0);
}

void Tsh_ripple::setTime(float value)
{
    glUniform1f(m_time, value);
}


void Tsh_ripple::setAmbient(float value)
{
    glUniform1f(m_ambient, value);
}

void Tsh_ripple::setCircle_center(float x, float y)
{
    glUniform2f(m_circle_center, x,y);
}

void Tsh_ripple::setCircle_border(float value)
{
    glUniform1f(m_circle_border, value);
}

void Tsh_ripple::setCircle_color(OVec4_f& color)
{
    glUniform4f(m_circle_color, color.r, color.g, color.b, color.a);
}

void Tsh_ripple::setCircle_color(float r, float g, float b, float a)
{
    glUniform4f(m_circle_color, r,g,b,a);
}




