#version 330 core

layout(location=0) in vec3 position;
layout(location=1) in vec4 vertexColor;
layout(location=2) in float scalar;
layout(location=3) in vec3 normal;

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform float scalarA;
uniform float scalarB;

out vec4 vColor;
out vec3 vScalar;
out vec4 fPos;
out vec4 fnormal;

vec3 HSVtoRGB(float h, float s, float v )
{
    int i;
    float f, p, q, t;
    vec3 res;

    if( s == 0.0 )
    {
        // achromatic (grey)
        res.x = v;
        res.y = v;
        res.z = v;
        return res;
    }

    h /= 60.0;         // sector 0 to 5
    i = int(floor( h ));
    f = h - float(i);         // factorial part of h
    p = v * ( 1.0 - s );
    q = v * ( 1.0 - s * f );
    t = v * ( 1.0 - s * ( 1.0 - f ) );

    switch(i)
    {
    case 0:
        res.x = v;
        res.y = t;
        res.z = p;
        break;
    case 1:
        res.x = q;
        res.y = v;
        res.z = p;
        break;
    case 2:
        res.x = p;
        res.y = v;
        res.z = t;
        break;
    case 3:
        res.x = p;
        res.y = q;
        res.z = v;
        break;
    case 4:
        res.x = t;
        res.y = p;
        res.z = v;
        break;
    case 5:      // case 5:
        res.x = v;
        res.y = p;
        res.z = q;
        break;
    default:
        res.x = 0;
        res.y = 0;
        res.z = 0;
        break;
    }
    return res;
}

void main()
{
    fPos = viewMatrix * modelMatrix * vec4(position,1);
    fnormal = viewMatrix * modelMatrix * vec4(normal,0);
    gl_Position = projectionMatrix * fPos;
    vScalar = HSVtoRGB(scalarA * scalar + scalarB, 1.0, 1.0);
    vColor = vertexColor;
}

