#version 150 compatibility

in   vec4 Position; // For the rasterizer
in   vec4 TexCoord;
in   vec4 Pos;
in   vec4 Color;

in vec3 fL;
in vec3 fV;
in vec4 ambient2;

// Llum
uniform    vec4 Lpos;
uniform    vec3 Ldir;
uniform    vec4 Lint_ambient;
uniform    vec4 Lint_difusa;
uniform    vec4 Lint_especular;
uniform    float Lang;

// Material
uniform   vec4 Mcomp_ambient;
uniform   vec4 Mcomp_difusa;
uniform   vec4 Mcomp_especular;
uniform   float Mcoef;



uniform float a;
uniform float b;
uniform float c;
uniform vec4 obs;
uniform vec3 vrp;
uniform vec3 dirVec;

uniform sampler1D texturaLut;
uniform sampler2D backface;

uniform sampler3D texturaVolum;
uniform sampler3D texturaMask;

uniform float stepsize;
uniform float umbralOpacitat;
uniform float anchura;
uniform float altura;
uniform float profundidad;
uniform float umbralSaturacio;
uniform int colorMode;
uniform int rcMode;
uniform float crem;

uniform float cx;
uniform float cy;
uniform float cz;
uniform float alpha;

out vec4 fColor;

const int ONE_STEP_MODE = 0;

bool mascara;

vec4 getColorSample(int mode, vec3 texCoord, vec4 colorMax){
    vec4 color = vec4(0.0,0.0,0.0,0.0);
    vec4 masc = vec4(0.0,0.0,0.0,0.0);
    vec4 normal = vec4(0.0,0.0,0.0,0.0);
    vec4 prevColor = vec4(0.0,0.0,0.0,0.0);
    vec4 nextColor = vec4(0.0,0.0,0.0,0.0);

        masc = texture(texturaMask,texCoord);

        if (masc.x >0.0){
            mascara = true;
            return vec4(cx,cy,cz,alpha);
        }

        float actValue = vec4(texture(texturaVolum, vec3(texCoord.x, texCoord.y, texCoord.z))).x;

        prevColor.x = vec4(texture(texturaVolum, vec3(texCoord.x-(1.0/anchura), texCoord.y, texCoord.z))).x;
        nextColor.x = vec4(texture(texturaVolum, vec3(texCoord.x+(1.0/anchura), texCoord.y, texCoord.z))).x;
        prevColor.y = vec4(texture(texturaVolum, vec3(texCoord.x, texCoord.y-(1.0/altura), texCoord.z))).y;
        nextColor.y = vec4(texture(texturaVolum, vec3(texCoord.x, texCoord.y+(1.0/altura), texCoord.z))).y;
        prevColor.z = vec4(texture(texturaVolum, vec3(texCoord.x, texCoord.y, texCoord.z-(1.0/profundidad)))).z;
        nextColor.z = vec4(texture(texturaVolum, vec3(texCoord.x, texCoord.y, texCoord.z+(1.0/profundidad)))).z;

        if(actValue*255 > umbralSaturacio /*&& actValue*255 > 40*/){

            normal = vec4(normalize(nextColor.xyz - prevColor.xyz),1.0);

            vec3 N = vec3(normal.x,normal.y,normal.z);
            vec3 V = normalize(fV);
            vec3 L = normalize(fL);

            vec3 H = normalize( L + V );

            float constDifusa = max( dot(L, N), 0.0 );

            float constEspecular = pow( max(dot(N, H), 0.0), Mcoef);

            color = (Lint_ambient * Mcomp_ambient) + (constDifusa * Lint_difusa * texture(texturaLut, actValue)) + (constEspecular * Lint_especular * Mcomp_especular);


        } else {
            color = vec4(0.0,0.0,0.0,0.0);

        }

    return color;
}

void main(void)
{
      float alfa = 1.0;
      mascara = false;
      vec4 start = TexCoord; // the start position of the ray is stored in the texturecoordinate

      vec3 dir = vec3(0,0,0);
      float len = 0.0;


          dir = dirVec;
          len = length(normalize(dir.xyz));


      vec3 norm_dir = normalize(dir);
      float delta = stepsize;
      vec3 delta_dir = norm_dir * delta;
      float delta_dir_len = length(delta_dir);
      vec3 vec = vec3(start.x, start.y, start.z);
      vec4 col_acc = vec4(0,0,0,0);
      float alpha_acc = 0;
      float length_acc = 0;
      vec4 color_sample = vec4(-1.0,-1.0,-1.0,-1.0);
      vec4 aux = vec4(0.0,0.0,0.0,0.0);
      float alpha_sample = 0.0;
      int i = 0;
      vec4 colorMax = vec4(-1.0, -1.0, -1.0, -1.0);



      for(i; i < 3024; i++)
        {
          color_sample = getColorSample(colorMode, vec, color_sample);
           alfa = color_sample.w;

          if (color_sample.a > 0.9999999999999 && color_sample.x > 0.0001 && color_sample.y < 0.001 && color_sample.z < 0.001 ) {
              col_acc = color_sample;
              break;
          }
          // trencar per opacitat col_acc = color_sample, fer break

          alpha_sample = color_sample.a * umbralOpacitat * 100 * (stepsize)/* * 400.0*sqrt(3.0)*/;
          col_acc   += (1.0 - alpha_acc) * color_sample * alpha_sample /** umbralOpacitat*//**128*/;
          alpha_acc += alpha_sample /*+ (1.0-alpha_acc)*alpha_sample*/;
          vec += delta_dir;
          length_acc += delta_dir_len;

             if((vec.x<0.00001 || vec.x>0.999999 || vec.y<0.00001 || vec.y>0.999999 ||
                   vec.z<0.000001|| vec.z>0.999999)) break; // terminate
      }

      if(!mascara){
          col_acc.x = col_acc.x*crem; if(col_acc.x > 1.0) col_acc.x = 1.0; if (col_acc.x < 0.0 ) col_acc.x = 0.0;
          col_acc.y = col_acc.y*crem; if(col_acc.y > 1.0) col_acc.y = 1.0; if (col_acc.y < 0.0 ) col_acc.y = 0.0;
          col_acc.z = col_acc.z*crem; if(col_acc.z > 1.0) col_acc.z = 1.0; if (col_acc.z < 0.0 ) col_acc.z = 0.0;
          fColor =1.0-col_acc;
          fColor.a =1.0;
      }else {
          fColor =1.0-col_acc;
          fColor.a =1.0;
        }
       // fColor =1.0- col_acc;
     /* fColor = col_acc;
      fColor.a = 1.0;*/


}

