[Vertex shader]

uniform vec3 scale;

uniform mat4 modelMat;
uniform mat3 normMat;

uniform vec3 lightPos;
uniform vec3 camPos;

varying vec2 texCoord;
varying vec3 lightVec;
varying vec3 viewVec;
varying vec3 normal;
varying vec3 lightCoord;

void main(){
	vec4 pos = gl_Vertex;
	pos.xyz *= 100.0;

	pos = modelMat * pos;

	gl_Position = gl_ModelViewProjectionMatrix * pos;

	texCoord = gl_Vertex.xz * 0.5 + 0.5;
	lightVec = lightPos - pos.xyz;
	viewVec = camPos - pos.xyz;

	normal = normMat * gl_Normal;

	lightCoord = pos.xyz * scale + 0.5;
}


[Fragment shader]

uniform sampler2D Base;
uniform sampler3D LightVolume;

varying vec2 texCoord;
varying vec3 lightVec;
varying vec3 viewVec;
varying vec3 normal;
varying vec3 lightCoord;

void main(){
	vec4 base = texture2D(Base, texCoord);
	float shadow = texture3D(LightVolume, lightCoord).x;

	vec3 lVec = normalize(lightVec);
	vec3 vVec = normalize(viewVec);

	float diffuse = saturate(dot(lVec, normal));
	float specular = pow(saturate(dot(reflect(-vVec, normal), lVec)), 16.0);

	gl_FragColor = 0.1 * base + (diffuse * base + 0.6 * specular) * shadow;
}
