//pref
ambient|float|0.0|1|2
diffuse|float|0.0|0.3|2
specular|float|0.0|0.25|1
shininess|float|0.01|10.0|30
overlayOpacity|float|0.0|0.8|1.0
overlayDepth|float|0.0|32.0|128.0
overlayVolume|set|2
Classic Blinn-Phong shading.|note
//frag
uniform float stepSize, sliceSize, viewWidth, viewHeight, clipPlaneDepth;
uniform sampler3D intensityVol;
uniform sampler3D gradientVol;
uniform sampler2D backFace;
uniform sampler3D overlayVol;
uniform vec3 clearColor, clipPlane, lightPosition;
uniform int overlays;
uniform float overlayOpacity, overlayDepth, ambient, diffuse, specular, shininess;

#ifdef CUBIC_FILTER
uniform vec3 textureSize;

vec4 texture3Df(sampler3D vol, vec3 coord) {
/* License applicable to this function:
Copyright (c) 2008-2013, Danny Ruijters. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
*  Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
*  Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
*  Neither the name of the copyright holders nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are
those of the authors and should not be interpreted as representing official
policies, either expressed or implied.

When using this code in a scientific project, please cite one or all of the
following papers:
*  Daniel Ruijters and Philippe Thévenaz,
   GPU Prefilter for Accurate Cubic B-Spline Interpolation,
   The Computer Journal, vol. 55, no. 1, pp. 15-20, January 2012.
*  Daniel Ruijters, Bart M. ter Haar Romeny, and Paul Suetens,
   Efficient GPU-Based Texture Interpolation using Uniform B-Splines,
   Journal of Graphics Tools, vol. 13, no. 4, pp. 61-69, 2008.
*/
  // shift the coordinate from [0,1] to [-0.5, textureSize-0.5]
  //vec3 textureSize = uResolution; //textureSize3D(tex, 0));
  //vec3 textureSize = textureSize3D(vol, 0);

  vec3 coord_grid = coord * textureSize - 0.5;
  vec3 index = floor(coord_grid);
  vec3 fraction = coord_grid - index;
  vec3 one_frac = 1.0 - fraction;

  vec3 w0 = 1.0/6.0 * one_frac*one_frac*one_frac;
  vec3 w1 = 2.0/3.0 - 0.5 * fraction*fraction*(2.0-fraction);
  vec3 w2 = 2.0/3.0 - 0.5 * one_frac*one_frac*(2.0-one_frac);
  vec3 w3 = 1.0/6.0 * fraction*fraction*fraction;

  vec3 g0 = w0 + w1;
  vec3 g1 = w2 + w3;
  vec3 mult = 1.0 / textureSize;
  vec3 h0 = mult * ((w1 / g0) - 0.5 + index);  //h0 = w1/g0 - 1, move from [-0.5, textureSize-0.5] to [0,1]
  vec3 h1 = mult * ((w3 / g1) + 1.5 + index);  //h1 = w3/g1 + 1, move from [-0.5, textureSize-0.5] to [0,1]

  // fetch the eight linear interpolations
  // weighting and fetching is interleaved for performance and stability reasons
  vec4 tex000 =  texture3D(vol,h0);
  vec4 tex100 =  texture3D(vol,vec3(h1.x, h0.y, h0.z));
  tex000 = mix(tex100, tex000, g0.x);  //weigh along the x-direction
  vec4 tex010 =  texture3D(vol,vec3(h0.x, h1.y, h0.z));
  vec4 tex110 =  texture3D(vol,vec3(h1.x, h1.y, h0.z));
  tex010 = mix(tex110, tex010, g0.x);  //weigh along the x-direction
  tex000 = mix(tex010, tex000, g0.y);  //weigh along the y-direction
  vec4 tex001 =  texture3D(vol,vec3(h0.x, h0.y, h1.z));
  vec4 tex101 =  texture3D(vol,vec3(h1.x, h0.y, h1.z));
  tex001 = mix(tex101, tex001, g0.x);  //weigh along the x-direction
  vec4 tex011 =  texture3D(vol,vec3(h0.x, h1.y, h1.z));
  vec4 tex111 =  texture3D(vol,h1);
  tex011 = mix(tex111, tex011, g0.x);  //weigh along the x-direction
  tex001 = mix(tex011, tex001, g0.y);  //weigh along the y-direction
  return mix(tex001, tex000, g0.z);  //weigh along the z-direction
}
#else //CUBIC_FILTER
vec4 texture3Df(sampler3D vol, vec3 coord) {
	return texture3D(vol, coord); //trilinear interpolation
}
#endif

void main() {
	float opacityCorrection = stepSize/sliceSize;
	// get normalized pixel coordinate in view port (e.g. [0,1]x[0,1])
	vec3 backPosition = texture2D(backFace,vec2(gl_FragCoord.x/viewWidth,gl_FragCoord.y/viewHeight)).xyz;
	vec3 start = gl_TexCoord[1].xyz; // starting position of the ray is stored in the texture coordinate
	vec3 dir = normalize(backPosition - start);
	float len = length(backPosition - start);
	if (clipPlaneDepth > -0.5) { //if clip plane intersects ray
		bool frontface = (dot(dir , clipPlane) > 0.0); //does clip plane face the camera?
		//next, distance from ray origin to clip plane
		float dis = dot(dir,clipPlane);
		if (dis != 0.0  )  dis = (-clipPlaneDepth - dot(clipPlane, start.xyz-0.5)) / dis;
		//test: "return" fails on 2006MacBookPro10.4ATI1900, "discard" fails on MacPro10.5NV8800
		if (((frontface) && (dis >= len)) || ((!frontface) && (dis <= 0.0))) {
		 gl_FragColor.rgb = clearColor;
		 return;
		}
		if ((dis > 0.0) && (dis < len)) {
			if (frontface) {
				start = start + dir * dis;
			} else {
				backPosition =  start + dir * (dis);
			}
			len = length(backPosition - start);
		}
	}
	vec3 lightdir = dir;
	dir = dir * stepSize;
	vec4 colorSample,colAcc = vec4(0.0,0.0,0.0,0.0);
	//jitter ray start to avoid wood grain aliasing
	vec3 samplePos = start.xyz + dir* (fract(sin(gl_FragCoord.x * 12.9898 + gl_FragCoord.y * 78.233) * 43758.5453));
	float isSurface = 0.0;
	if (overlays < 1) isSurface = 1.0;
	vec4 overSample = vec4(0.0,0.0,0.0,0.0);

	for(int i = 0; i < int(len / stepSize); i++) {
		colorSample = texture3Df(intensityVol, samplePos);
		if (colorSample.a > 0.0) {
			//start overlays
			if (isSurface < 1.0) {
				float overlayLengthAcc = 0.0;
				vec3 overPos = samplePos;
				float overlaySearchDepth = overlayDepth * sliceSize;
				float maxA = 0.0;
				while (overlayLengthAcc <= overlaySearchDepth) {
					vec4 overSam = texture3Df(overlayVol,overPos);
					overPos += dir;
					overlayLengthAcc += stepSize;
					if (overSam.a > 0.0) {
						maxA = max(maxA, overSam.a);
						overSample = overSample + overSam;
						isSurface += 1.0;
					}
				}
				if (isSurface > 0.0) {
					overSample = overSample/isSurface;
					overSample.a = maxA* overlayOpacity;
				}
				isSurface += 1.0;
				//overSample.a = 0.5;
			}
			colorSample.rgb = mix(colorSample.rgb, overSample.rgb, overSample.a);
			//end overlays

			vec4 gradientSample= texture3Df(gradientVol,samplePos); //interpolate gradient direction and magnitude
			gradientSample.rgb = normalize(gradientSample.rgb*2.0 - 1.0); //direction saved as 0..1, rescale to -1..1

			float lightNormDot = dot(gradientSample.rgb, lightPosition); //with respect to light location
			vec3 a = colorSample.rgb * ambient;
			vec3 d = max(lightNormDot, 0.0) * colorSample.rgb * diffuse;
			float s =   specular * pow(max(dot(reflect(lightPosition, gradientSample.rgb), lightdir), 0.0), shininess);
			colorSample.rgb = a + d + s;

			colorSample.a = 1.0-pow((1.0 - colorSample.a), opacityCorrection);
			colorSample.rgb *= colorSample.a;
			colAcc += (1.0 - colAcc.a) * colorSample; //accumulate color
			if ( colAcc.a > 0.95 ) break; // terminate if opacity > 95%
		}
		samplePos += dir;
	}
	gl_FragColor.rgb = mix(clearColor,colAcc.rgb,colAcc.a);
}