#version 330 core

in VSOutput
{
	vec4 p; //position (world space!)
	vec3 tex; //3d tex coord
} inData;

out vec4 fragColor;

//light direction (from fragment to light, world space)
uniform vec3 vLightVec;
//halfway vector between light and camera
uniform vec3 vHalfVec;
//windowing (x=min, y=max)
uniform vec2 vWindow;
//volume texture
uniform sampler3D texVolume;


void main(void)
{
	//sample 3d tex
	vec4 texval = texture(texVolume,inData.tex);

	//the world-space gradient is stored normalized already
	//but must be reversed for normal (because it points INTO the object)
	vec3 normal = -texval.xyz;
	//original intensity value defines the opacity
	float intensity = texval.a;

	//perform blinn-phong shading
	float diff = max(dot(vLightVec,normal),0.0);
	float spec = pow( max(dot(normal,vHalfVec),0.0), 16);

	//apply windowing
	float windowed = (intensity - vWindow.x) / (vWindow.y - vWindow.x);
	float clamped = clamp(windowed, 0.0, 1.0);

	vec3 color = vec3(intensity,intensity,intensity) * (diff + 0.3) + spec;
	fragColor = vec4(color,clamped);
	//fragColor = vec4(spec,spec,spec,alpha);
}