stk-code_catmod/data/shaders/importance_sampling_specular.frag

34 lines
956 B
GLSL
Raw Normal View History

2014-12-02 11:38:21 -05:00
uniform samplerCube tex;
uniform samplerBuffer samples;
2014-12-02 11:38:21 -05:00
uniform float ViewportSize;
uniform mat4 PermutationMatrix;
out vec4 FragColor;
void main(void)
{
vec2 uv = gl_FragCoord.xy / ViewportSize;
vec3 RayDir = 2. * vec3(uv, 1.) - 1.;
RayDir = normalize((PermutationMatrix * vec4(RayDir, 0.)).xyz);
vec4 FinalColor = vec4(0.);
vec3 up = (RayDir.y < .99) ? vec3(0., 1., 0.) : vec3(0., 0., 1.);
vec3 Tangent = normalize(cross(up, RayDir));
vec3 Bitangent = cross(RayDir, Tangent);
2014-12-07 12:26:51 -05:00
float weight = 0.;
2014-12-02 11:38:21 -05:00
for (int i = 0; i < 1024; i++)
{
2014-12-08 13:16:26 -05:00
float Theta = texelFetch(samples, i).r;
float Phi = texelFetch(samples, i).g;
2014-12-02 11:38:21 -05:00
2014-12-07 12:26:51 -05:00
vec3 L = cos(Theta) * RayDir + sin(Theta) * cos(Phi) * Tangent + sin(Theta) * sin(Phi) * Bitangent;
float NdotL = clamp(dot(RayDir, L), 0., 1.);
FinalColor += textureLod(tex, L, 0.) * NdotL;
weight += NdotL;
2014-12-02 11:38:21 -05:00
}
2014-12-07 12:26:51 -05:00
FragColor = FinalColor / weight;
2014-12-02 11:38:21 -05:00
}