VoxelEngine/fragment.glsl
2017-08-27 12:51:26 +02:00

61 lines
2.4 KiB
GLSL

#version 410
layout(location = 0) in vec3 colorin;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec3 pos;
layout(location = 3) in vec4 lightviewpos[3];
layout(location = 8) in vec2 UV;
uniform int numLights;
uniform sampler2D ShadowMaps[3];
uniform vec3 lightpos[3];
uniform vec3 lightColor[3];
layout(location = 0) out vec4 colorOut;
layout(location = 1) out float depth;
const float ambientFactor = 0.25;
const float diffuseFactor = 0.5;
const float specFactor = 1.0;
const float shininess = 16.0;
const float screenGamma = 2.2;
const float pitl = 3.14159265359 / 16.0;
void main()
{
vec3 colorLinear = ambientFactor * colorin;
for(int i = 0; i < numLights; i++){
vec3 lightDir = -normalize(lightpos[i] - pos.xyz);
float lambertian = max(dot(normalize(normal),lightDir),0.0);
float specular = 0;
vec3 viewDir = normalize(-pos.xyz);
vec3 halfDir = normalize(lightDir + viewDir);
float specAngle = max(dot(halfDir, normalize(normal)), 0.0);
specular = int(lambertian > 0)*pow(specAngle, shininess);
//int visible = int(!(texture(ShadowMaps,lightpos[i].xy/2 + vec2(0.5,0.5)).z < (lightpos[i].z)));
float visible = 0;
for(int j = 0; j < 4; j++){
vec2 offs = vec2(sin(j*pitl),cos(j*pitl)) / 700;
visible += float(!(texture(ShadowMaps[i],lightviewpos[i].xy + offs).x <= (lightviewpos[i].z - 0.0005*tan(acos(dot(normal,-normalize(lightpos[i] - pos.xyz))))))) * 1.0/16.0;
}
if(visible == 0.25)
visible = 1.0;
else
for(int j = 4; j < 16; j++){
vec2 offs = vec2(sin(j*pitl),cos(j*pitl)) / 700;
visible += float(!(texture(ShadowMaps[i],lightviewpos[i].xy + offs).x <= (lightviewpos[i].z - 0.0005*tan(acos(dot(normal,-normalize(lightpos[i] - pos.xyz))))))) * 1.0/16.0;
}
colorLinear += (visible * 0.5 + 0.5) *(lambertian * diffuseFactor * colorin * lightColor[i] + specular * specFactor*colorin);
//colorLinear = vec3(visible * specular);
}
vec3 colorGammaCorrected = colorLinear;//pow(colorLinear, vec3(1.0/screenGamma));
//colorOut = vec4(colorin, 1.0)*max(dot(normal,normalize(vec3(0.0,0.0,10.0)-pos)),0.0);
colorOut = vec4(colorGammaCorrected,1.0) * float(gl_FragCoord.y <= 768 && gl_FragCoord.x <= 1024 && gl_FragCoord.x >= 0 && gl_FragCoord.y >= 0);
depth = gl_FragCoord.z;
}