#include "system.h"
varying vec2 varPos; // range [-1;1], clipspace
varying vec2 varCoord; // range [0;1]
varying vec2 varICoord; // range [0;1280], [0;720]
uniform mat4 u_Inverse_MVP; // inverse model-view-projection of camera
uniform sampler2DMS texWSNormal; // AA texture for worldspace normal
uniform sampler2DMS texWSDepth; // AA texture for depth
#if IS_VERTEX
in vec4 inVertex;
void main(){ // this is a vertex of a huge triangle or quad, usually full-screen
gl_Position = inVertex;
vec2 ssize=textureSize(texWSNormal);
varPos = inVertex.xy;
varCoord=(inVertex.xy+1.0)*0.5;
varICoord = varCoord * ssize.xy;
}
#endif
#if IS_FRAGMENT
out vec4 glFragColor;
vec4 DepthToWorldspacePosition(in vec2 my_varPos,in float my_zOverW){
vec4 H = vec4(my_varPos.x,my_varPos.y,my_zOverW*2-1,1);
vec4 tmp1 = u_Inverse_MVP * H;
vec4 WorldPos = tmp1/tmp1.w;
return WorldPos;
}
uniform vec4 lights[8]={
vec4(21.996891,3.728675,-7.664107,1),
vec4(5.377327,3.0,-31.299137,1),
vec4(15.553305,9.069536,-24.629501,1),
vec4(1.0,3.0,1.0,1),
vec4(18.981777,6.258294,-27.141813,1),
vec4(18.981777,6.258294,-27.141813,1),
vec4(18.945974,5.920466,-47.028144,1),
vec4(18.945974,5.920466,-47.028144,1)
};
uniform mat4 biasShadow = mat4(
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
uniform mat4 u_ShadowMatrix;
uniform vec4 u_ShadowDir; // xyz is direction, w=bias
uniform sampler2DShadow texShadowMap;
float ComputeShadow(in vec4 worldPos,in vec3 norm1){ // a simplistic PCF, one tap.
float lightIntensity = 1.0;
vec3 lightDir = vec3(0.3,0.5,-1.0); lightDir = normalize(lightDir)*lightIntensity;
float dot1 = dot(u_ShadowDir.xyz,norm1);
if(dot1<=0.0)return 0;
vec4 coord = u_ShadowMatrix * worldPos;
coord.z -= u_ShadowDir.w; // bias . Front faces were culled, there're light-leaks, not shadow-leaks
float shadow = shadow2DProj(texShadowMap,coord).x;
return shadow*dot1;
}
void main(){
glFragColor=vec4(0);
ivec2 icoord = ivec2(varICoord);
/*
for(int i=0;i< NUM_MSAA_SAMPLES ;i++){
glFragColor += texelFetch(tex,icoord,i);
}
glFragColor/= NUM_MSAA_SAMPLES;
*/
vec4 diffuse = vec4(0);
float ambient = 0.3;
for(int i=0;i< NUM_MSAA_SAMPLES ;i++){
vec4 norm1 = texelFetch(texWSNormal,icoord,i);
float zOverW = texelFetch(texWSDepth,icoord,i).x;
vec4 WorldPos = DepthToWorldspacePosition(varPos,zOverW);
diffuse+= (ambient+ComputeShadow(WorldPos,norm1.xyz)); // assuming sky-light color is white
for(int j=0;j<8;j++){
vec3 Lpos = (lights[j]).xyz - WorldPos.xyz;
float d1 = dot(Lpos,Lpos);
float dist = 10.0/(d1);
dist *= dot(Lpos,norm1.xyz)/sqrt(d1); // actually "dist" is "lightAttenuation"
diffuse+=max(dist,0)*vec4(2,1,1,1);
}
}
diffuse/= NUM_MSAA_SAMPLES;
glFragColor = diffuse; // we're writing to a 1280x720 single-sampled FBO that has no depth-buffer, and has texture-format RGBA16F or anything that's HDR, in additive-blending mode.
}
#endif