2014-05-12 12:39:33 -04:00
|
|
|
// From paper http://graphics.cs.williams.edu/papers/AlchemyHPG11/
|
|
|
|
// and improvements here http://graphics.cs.williams.edu/papers/SAOHPG12/
|
|
|
|
|
|
|
|
uniform sampler2D dtex;
|
|
|
|
uniform vec4 samplePoints[16];
|
|
|
|
|
|
|
|
#ifdef UBO_DISABLED
|
|
|
|
uniform mat4 ViewMatrix;
|
|
|
|
uniform mat4 ProjectionMatrix;
|
|
|
|
uniform mat4 InverseViewMatrix;
|
|
|
|
uniform mat4 InverseProjectionMatrix;
|
2014-05-19 11:57:31 -04:00
|
|
|
uniform vec2 screen;
|
2014-05-12 12:39:33 -04:00
|
|
|
#else
|
|
|
|
layout (std140) uniform MatrixesData
|
|
|
|
{
|
|
|
|
mat4 ViewMatrix;
|
|
|
|
mat4 ProjectionMatrix;
|
|
|
|
mat4 InverseViewMatrix;
|
|
|
|
mat4 InverseProjectionMatrix;
|
|
|
|
mat4 ShadowViewProjMatrixes[4];
|
2014-05-16 20:39:55 -04:00
|
|
|
vec2 screen;
|
2014-05-12 12:39:33 -04:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
out float AO;
|
|
|
|
|
2014-06-22 19:03:55 -04:00
|
|
|
const float sigma = 1.;
|
2014-05-12 17:59:17 -04:00
|
|
|
const float tau = 7.;
|
2014-05-13 20:27:17 -04:00
|
|
|
const float beta = 0.001;
|
2014-05-12 12:39:33 -04:00
|
|
|
const float epsilon = .00001;
|
2014-05-13 21:26:43 -04:00
|
|
|
const float radius = 1.;
|
2014-06-22 19:03:55 -04:00
|
|
|
const float k = 1.5;
|
2014-05-12 12:39:33 -04:00
|
|
|
|
|
|
|
#define SAMPLES 16
|
|
|
|
|
|
|
|
const float invSamples = 1. / SAMPLES;
|
|
|
|
|
2014-05-13 13:40:30 -04:00
|
|
|
vec3 getXcYcZc(int x, int y, float zC)
|
|
|
|
{
|
|
|
|
// We use perspective symetric projection matrix hence P(0,2) = P(1, 2) = 0
|
|
|
|
float xC= (1. - 2 * (float(x) + 0.5) / screen.x) * zC / ProjectionMatrix[0][0];
|
|
|
|
float yC= (1. + 2 * (float(y) + 0.5) / screen.y) * zC / ProjectionMatrix[1][1];
|
|
|
|
return vec3(xC, yC, zC);
|
|
|
|
}
|
2014-05-12 12:39:33 -04:00
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
2014-06-03 13:56:59 -04:00
|
|
|
vec2 uv = gl_FragCoord.xy / screen;
|
2014-05-13 13:40:30 -04:00
|
|
|
float lineardepth = textureLod(dtex, uv, 0.).x;
|
|
|
|
int x = int(gl_FragCoord.x), y = int(gl_FragCoord.y);
|
|
|
|
vec3 FragPos = getXcYcZc(x, y, lineardepth);
|
2014-05-12 12:39:33 -04:00
|
|
|
|
|
|
|
// get the normal of current fragment
|
2014-05-13 13:40:30 -04:00
|
|
|
vec3 ddx = dFdx(FragPos);
|
|
|
|
vec3 ddy = dFdy(FragPos);
|
|
|
|
vec3 norm = -normalize(cross(ddy, ddx));
|
2014-05-12 12:39:33 -04:00
|
|
|
|
|
|
|
float r = radius / FragPos.z;
|
2014-06-22 19:03:55 -04:00
|
|
|
float phi = 3. * (x ^ y) + x * y;
|
2014-05-12 12:39:33 -04:00
|
|
|
float bl = 0.0;
|
2014-06-02 21:41:05 -04:00
|
|
|
float m = log2(r) + 6 + log2(invSamples);
|
2014-05-12 12:39:33 -04:00
|
|
|
|
2014-06-02 21:14:39 -04:00
|
|
|
float theta = 2. * 3.14 * tau * .5 * invSamples + phi;
|
|
|
|
vec2 rotations = vec2(cos(theta), sin(theta)) * screen;
|
|
|
|
vec2 offset = vec2(cos(invSamples), sin(invSamples));
|
|
|
|
|
2014-05-12 12:39:33 -04:00
|
|
|
for(int i = 0; i < SAMPLES; ++i) {
|
|
|
|
float alpha = (i + .5) * invSamples;
|
2014-06-02 21:14:39 -04:00
|
|
|
rotations = vec2(rotations.x * offset.x - rotations.y * offset.y, rotations.x * offset.y + rotations.y * offset.x);
|
2014-05-12 12:39:33 -04:00
|
|
|
float h = r * alpha;
|
2014-06-02 21:14:39 -04:00
|
|
|
vec2 offset = h * rotations;
|
2014-05-12 12:39:33 -04:00
|
|
|
|
2014-06-02 21:41:05 -04:00
|
|
|
m = m + .5;
|
2014-05-13 13:40:30 -04:00
|
|
|
ivec2 ioccluder_uv = ivec2(x, y) + ivec2(offset);
|
2014-05-12 12:39:33 -04:00
|
|
|
|
2014-05-13 13:40:30 -04:00
|
|
|
if (ioccluder_uv.x < 0 || ioccluder_uv.x > screen.x || ioccluder_uv.y < 0 || ioccluder_uv.y > screen.y) continue;
|
2014-05-12 12:39:33 -04:00
|
|
|
|
2014-05-13 13:40:30 -04:00
|
|
|
float LinearoccluderFragmentDepth = textureLod(dtex, vec2(ioccluder_uv) / screen, m).x;
|
|
|
|
vec3 OccluderPos = getXcYcZc(ioccluder_uv.x, ioccluder_uv.y, LinearoccluderFragmentDepth);
|
2014-05-12 12:39:33 -04:00
|
|
|
|
2014-05-13 13:40:30 -04:00
|
|
|
vec3 vi = OccluderPos - FragPos;
|
2014-05-12 12:39:33 -04:00
|
|
|
bl += max(0, dot(vi, norm) - FragPos.z * beta) / (dot(vi, vi) + epsilon);
|
|
|
|
}
|
|
|
|
|
|
|
|
AO = max(pow(1.0 - 2. * sigma * bl * invSamples, k), 0.);
|
|
|
|
}
|