#version 400
//#version 330 core
//uniform float depthmult;
//uniform float depthmax;
float depthmult=20.0f;
float depthmax=19.3872f;

varying vec2 texCoord;
//varying vec4 vColor;
uniform sampler2D colorMap;
varying vec4 vpos;
//uniform int abc;

/*
void main (void)
{
	gl_FragColor = texture2D(colorMap, texCoord);
}
*/
//uniform mat4 depthMVP;

void main (void)
{
//    gl_FragColor = vec4(vec3(gl_FragCoord.w),1.0f);
/*
    float depth=gl_FragCoord.w;
    double x2=depth*(65536*65536);
    double r2=floor(x2/(65536*256)); x2=x2-r2*(65536*256);
    double g2=floor(x2/(256*256)); x2=x2-g2*(256*256);
    double b2=floor(x2/(256)); x2=x2-b2*(256);
    double a2=x2;
    float r1=float(r2/256.0f);
    float g1=float(g2/256.0f);
    float b1=float(b2/256.0f);
    float a1=float(a2/256.0f);

    gl_FragColor = vec4(r1,g1,b1,1.0f);
*/

/*---------------------------------------------
    float depth=gl_FragCoord.w;
    double x2=depth*256*256;
    double r2=floor(x2/double(256.0)); x2=x2-r2*double(256.0);
    double g2=x2;
    float r1=float(r2/256.0f);
    float g1=float(g2/256.0f);

    gl_FragColor = vec4(r1,g1,0.0f,1.0f);
-------------------------------------------------
*/
// z range is:
// glm::mat4 projection = glm::perspective(fov, 1920.0f/1080.0f, 0.1f, 200.0f);
// *1000 is for precision, when unpacking do /1000:

    //0.1-200.f which is camera set in mirrors.cpp
    //float x2=(gl_FragCoord.z/200.0f); //first 1000 is camera zfar, second a magic number scale up for greater precision
    //float r1=x2; float g1=x2; float b1=x2;
/*
    float x2=(gl_FragCoord.w*(1000.0f-0.1f)-0.1f)*1000.f; //first 1000 is camera zfar, second a magic number scale up for greater precision
    float r2=floor(x2/float(65536.0)); x2=x2-r2*float(65536.0);
    float g2=floor(x2/float(256.0)); x2=x2-g2*float(256.0);
    float b2=x2;
    float r1=float(r2/255.0f);
    float g1=float(g2/255.0f);
    float b1=float(b2/255.0f);
*/
    //gl_FragColor = vec4(gl_FragCoord.zzz/200.0f,1.0f);

    //gl_FragColor = vec4(vpos.zzz/200.0f,1.0f);//this normalizing value is from projection matrix setup

    //float x=vpos.z/vpos.w*20.0f-19.7369f;
    float x=vpos.z/vpos.w*depthmult-depthmax;

    float depth=x;
    double x2=depth*(65536*256);
    double r2=floor(x2/(65536)); x2=x2-r2*(65536);
    double g2=floor(x2/(256)); x2=x2-g2*(256);
    double b2=floor(x2/(1)); x2=x2-b2*(1);
    //double a2=x2;
    float r1=float(r2/256.0f);
    float g1=float(g2/256.0f);
    float b1=float(b2/256.0f);
    //float a1=float(a2/256.0f);

    gl_FragColor = vec4(r1,g1,b1,1.0f);



/*
    x=x*256.0f*256.0f;
    float x2=floor(x/256.0f);
    float x1=x-256*x2;
    x1/=256.0f;
    x2/=256.0f;
    gl_FragColor = vec4(x1,x2,1.0f,1.0f);//this normalizing value is from projection matrix setup
*/
}

/*
// Ouput data
//layout (location = 0) out float fragmentdepth;
out float fragmentdepth;

void main(){
    // Not really needed, OpenGL does it anyway
    fragmentdepth = gl_FragCoord.z;

    gl_FragColor = vec4(gl_FragCoord.z,gl_FragCoord.z,gl_FragCoord.z,1.0f);//texture2D(colorMap, texCoord);
}
*/
