Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #line 2
- /**
- * @file ssao.frag
- *
- * @author FH Wedel, Henrik Patjens
- * @date November 2017
- * @brief Fragment-Shader zum Rendern von Objekten mit Forward-Rendering
- *
- * Diese Datei ist Bestandteil des Praktikums "Special Effects und Shaderprogrammierung".
- *
- * Copyright (C) 2017 by FH Wedel
- */
- #define DIST_MIN 0.0001
- #define DIST_MAX 0.005
- // Version wird durch die utils.glsl definiert.
- out vec4 FragColor; /**< Farbe des Fragments */
- // ----------------------------------------------------------------------------
- //
- // Uniforms
- //
- // ----------------------------------------------------------------------------
- layout (binding = 0) uniform sampler2D normalTex;
- layout (binding = 1) uniform sampler2D depthTex;
- //layout (RGBA8, binding = 2) uniform image2D discardTex;
- uniform int sampleSize;
- uniform mat4 ProjectionMatrix;
- uniform mat4 InverseProjectionMatrix;
- uniform int resX;
- uniform int resY;
- // ----------------------------------------------------------------------------
- //
- // Variablen
- //
- // ----------------------------------------------------------------------------
- // ----------------------------------------------------------------------------
- //
- // Funktionen
- //
- // ----------------------------------------------------------------------------
- /**
- * Berechnet die View Space position des aktuellen Fragments
- */
- vec3 getViewPos(){
- float x = (gl_FragCoord.x / resX) * 2.0 - 1.0;
- float y = (gl_FragCoord.y / resY) * 2.0 - 1.0;
- float z = texture(depthTex, gl_FragCoord.xy).r * 2.0 - 1.0;
- vec4 projPos = vec4(x,y,z, 1.0);
- vec4 viewPos = InverseProjectionMatrix * projPos;
- return wdiv(viewPos);
- }
- /**
- * Einsprungpunkt fΓΌr den Fragment-Shader
- */
- void main() {
- float occlusion = 0.0;
- vec3 origin = getViewPos();
- vec3 normal = normalize(texture(normalTex,gl_FragCoord.xy).rgb * 2.0 - 1.0);
- //vec3 normal = normalize(texelFetch(normalTex, ivec2(gl_FragCoord.xy),0).xyz * 2.0 - 1.0);
- vec3 u = normalize(vec3(rand(gl_FragCoord.xy),rand(gl_FragCoord.yz),rand(gl_FragCoord.xz)));
- //TODO: n == u beachten!
- vec3 tangent = normalize(cross(normal,u));
- vec3 bitangent = normalize(cross(normal, tangent));
- mat3 tbn = mat3(tangent, bitangent, normal);
- for(int i = 0; i < sampleSize; i++){
- //sample position berechnen
- vec3 kernel = tbn * sampleHemisphereCos(hammersley(i, sampleSize));
- kernel = kernel + origin;
- //projizieren
- vec4 offset = vec4(kernel,1.0);
- offset = ProjectionMatrix * offset;
- vec3 fOffset = wdiv(offset);
- fOffset.xy = fOffset.xy * 0.5 + 0.5; //scale & bias
- //sample tiefe berechnen
- float depth = texture(depthTex, fOffset.xy).r * 2.0 - 1.0;
- //float dist = fOffset.z - depth;
- if(fOffset.z > depth){
- occlusion += 1.0;
- } /**else {
- imageStore(discardTex, gl_FragCoord.xy, vec4(1,0,0,1));
- }*/
- }
- occlusion = 1.0 - (occlusion/sampleSize);
- FragColor = vec4(occlusion, 0.0, 0.0, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement