#version 330 core

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

uniform mat4 fullTransformMatrix;
uniform float pointSize;
uniform float scalarA;
uniform float scalarB;

out vec3 fColor;
out vec3 fScalar;

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()
{
    gl_Position = fullTransformMatrix * ( vec4( offset + pointSize * position.xyz , 1 ) );
    fColor = vertexColor.rgb;
    float h = scalarA * scalar + scalarB;
    fScalar = HSVtoRGB(h,1,1);
}
