#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 vec4 colorMask;
uniform float cx;
uniform float cy;
uniform float cz;
uniform float alpha;
uniform float k;

uniform bool background;
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);

        color = texture(texturaVolum, texCoord);
        masc = texture(texturaMask, texCoord);

        if(masc.x > 0) {
              mascara = true;
              colorMax = k*vec4(cx,cy,cz,alpha)+(1.0-k)*color;
         } else {
            if(color.x >= umbralSaturacio){
                if(color.x > colorMax.x){
                    colorMax = color;
                }
            }
        }
        return colorMax;
}

void main(void)
{

      vec4 start = TexCoord; // the start position of the ray is stored in the texturecoordinate
      mascara = false;
      vec3 dir = vec3(0,0,0);
      float len = 0.0;
      if(rcMode == ONE_STEP_MODE){
          dir = dirVec;
          len = length(normalize(dir.xyz));
      } else {
          vec2 texc = ((Pos.xy / Pos.w) + 1) / 2; // find the right place to lookup in the backside buffer
          vec4 back_position  = texture2D(backface, texc);
          dir.x = back_position.x - start.x;
          dir.y = back_position.y - start.y;
          dir.z = back_position.z - start.z;
          len = length(dir.xyz); // the length from front to back is calculated and used to terminate the ray
      }
 // the length from front to back is calculated and used to terminate the ray

      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);


          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 || mascara)) break; // terminate

        }

         if ( mascara){
              fColor = color_sample;
              //fColor.a = alpha_acc;
              //fColor.a = 1.0;
          }else {
              color_sample.x = color_sample.x*crem; if (color_sample.x > 1.0) color_sample.x = 1.0;
              color_sample.y = color_sample.y*crem; if (color_sample.y > 1.0) color_sample.y = 1.0;
              color_sample.z = color_sample.z*crem; if (color_sample.z > 1.0) color_sample.z = 1.0;
              fColor =color_sample;
              if (background)
                fColor = 1.0-color_sample; //cremar
             // fColor.a = 1.0;
          }

      fColor.a = 1.0;

}
