diff --git a/Compositor/HDR/hdr.xml b/Compositor/HDR/hdr.xml index d36592276..fc26a7e30 100644 --- a/Compositor/HDR/hdr.xml +++ b/Compositor/HDR/hdr.xml @@ -589,6 +589,7 @@ scene hdr-geometry depth + 0.0 0xfffff7ff color0 diff --git a/Effects/Fallback/hdr-geometry.eff b/Effects/Fallback/hdr-geometry.eff index 1110d2931..1518cad22 100644 --- a/Effects/Fallback/hdr-geometry.eff +++ b/Effects/Fallback/hdr-geometry.eff @@ -4,6 +4,12 @@ hdr-geometry + + + gequal + 1.0 + 0.0 + 0 texture[0]/type diff --git a/Effects/model-combined.eff b/Effects/model-combined.eff index 5791244c1..8b70c8fb1 100644 --- a/Effects/model-combined.eff +++ b/Effects/model-combined.eff @@ -16,12 +16,7 @@ please see Docs/README.model-combined.eff for documentation 0 1.0 - Aircraft/Generic/Effects/null_bumpspec.png - 2d - linear-mipmap-linear - clamp - clamp - normalized + null-normalmap 0 @@ -1297,6 +1292,12 @@ please see Docs/README.model-combined.eff for documentation hdr-geometry + + + gequal + 1.0 + 0.0 + 0 texture[0]/type diff --git a/Effects/model-default.eff b/Effects/model-default.eff index 2e5e8b808..88284f914 100644 --- a/Effects/model-default.eff +++ b/Effects/model-default.eff @@ -651,33 +651,4 @@ - - hdr-geometry - - cull-face - opaque - - 0 - texture[0]/type - texture[0]/image - texture[0]/filter - texture[0]/wrap-s - texture[0]/wrap-t - texture[0]/internal-format - - - vertex-program-two-side - - - Shaders/HDR/geometry.vert - Shaders/HDR/geometry.frag - Shaders/HDR/gbuffer-include.frag - - - color_tex - sampler-2d - 0 - - - diff --git a/Effects/model-pbr.eff b/Effects/model-pbr.eff index 842bb1970..5ea62b6e7 100644 --- a/Effects/model-pbr.eff +++ b/Effects/model-pbr.eff @@ -47,6 +47,12 @@ hdr-geometry + + + gequal + 1.0 + 0.0 + 0 texture[0]/type diff --git a/Effects/skydome.eff b/Effects/skydome.eff index f8340e1a4..f853cf769 100644 --- a/Effects/skydome.eff +++ b/Effects/skydome.eff @@ -303,13 +303,13 @@ hdr-forward - back + - lequal - 0.0 - 1.0 - true + greater + 1.0 + 0.0 + back Shaders/HDR/skydome.vert Shaders/HDR/skydome.frag @@ -335,12 +335,6 @@ hdr-envmap back - - lequal - 0.0 - 1.0 - true - Shaders/HDR/skydome.vert Shaders/HDR/skydome.frag diff --git a/Effects/ws30.eff b/Effects/ws30.eff index 71f7bde28..0359c0cf5 100644 --- a/Effects/ws30.eff +++ b/Effects/ws30.eff @@ -674,6 +674,13 @@ hdr-geometry + + + gequal + 1.0 + 0.0 + + transparent transparent smooth diff --git a/Effects/ws30water.eff b/Effects/ws30water.eff index c2354e2fe..a7c671686 100644 --- a/Effects/ws30water.eff +++ b/Effects/ws30water.eff @@ -118,6 +118,13 @@ hdr-geometry + + + gequal + 1.0 + 0.0 + + transparent transparent smooth diff --git a/Shaders/HDR/ao.frag b/Shaders/HDR/ao.frag index 755e91591..d96d6a9c7 100644 --- a/Shaders/HDR/ao.frag +++ b/Shaders/HDR/ao.frag @@ -31,8 +31,8 @@ float rand(vec2 co) { float sampleAO(vec3 fragPos, vec3 normal, vec2 coords) { - float sampleDepth = texture(depth_tex, coords).r * 2.0 - 1.0; - vec3 samplePoint = positionFromDepth(coords * 2.0 - 1.0, sampleDepth); + float sampleDepth = texture(depth_tex, coords).r; + vec3 samplePoint = positionFromDepth(coords, sampleDepth); vec3 diff = samplePoint - fragPos; float l = length(diff); @@ -47,7 +47,7 @@ float sampleAO(vec3 fragPos, vec3 normal, vec2 coords) void main() { float fragDepth = texture(depth_tex, texCoord).r; - vec3 fragPos = positionFromDepth(texCoord * 2.0 - 1.0, fragDepth * 2.0 - 1.0); + vec3 fragPos = positionFromDepth(texCoord, fragDepth); vec3 normal = normalize(decodeNormal(texture(normal_tex, texCoord).rg)); diff --git a/Shaders/HDR/atmos-aerial-perspective.frag b/Shaders/HDR/atmos-aerial-perspective.frag index fc18d71c9..f95b37968 100644 --- a/Shaders/HDR/atmos-aerial-perspective.frag +++ b/Shaders/HDR/atmos-aerial-perspective.frag @@ -56,7 +56,7 @@ void main() w *= w; float depth = w * DEPTH_RANGE; - vec3 fragPos = positionFromDepth(coord * 2.0 - 1.0, 0.0); + vec3 fragPos = positionFromDepth(coord, 1.0); vec3 rayDir = vec4(fg_ViewMatrixInverse * vec4(normalize(fragPos), 0.0)).xyz; float cameraHeight = length(fg_CameraPositionCart); diff --git a/Shaders/HDR/gbuffer-include.frag b/Shaders/HDR/gbuffer-include.frag index 77f1085ae..de3266427 100644 --- a/Shaders/HDR/gbuffer-include.frag +++ b/Shaders/HDR/gbuffer-include.frag @@ -23,20 +23,26 @@ vec3 decodeNormal(vec2 enc) return n; } -// Given a position in clip space (values in the range [-1,1]), return -// the view space position. +// Given a 2D coordinate in the range [0,1] and a depth value from a depth +// buffer, also in the [0,1] range, return the view space position. vec3 positionFromDepth(vec2 pos, float depth) { - vec4 p = fg_ProjectionMatrixInverse * vec4(pos, depth, 1.0); - p.xyz /= p.w; - return p.xyz; + // We are using a reversed depth buffer. 1.0 corresponds to the near plane + // and 0.0 to the far plane. We convert this back to clip space by doing + // 1.0 - depth to undo the depth reversal + // 2.0 * depth - 1.0 to transform it to clip space [-1,1] + vec4 clipSpacePos = vec4(pos * 2.0 - 1.0, 1.0 - depth * 2.0, 1.0); + vec4 viewSpacePos = fg_ProjectionMatrixInverse * clipSpacePos; + viewSpacePos.xyz /= viewSpacePos.w; + return viewSpacePos.xyz; } // http://www.geeks3d.com/20091216/geexlab-how-to-visualize-the-depth-buffer-in-glsl/ float linearizeDepth(float depth) { - return (2.0 * fg_NearFar.x) / ( - fg_NearFar.y + fg_NearFar.x - depth * (fg_NearFar.y - fg_NearFar.x)); + float z = 1.0 - depth; // Undo the depth reversal + return 2.0 * fg_NearFar.x + / (fg_NearFar.y + fg_NearFar.x - z * (fg_NearFar.y - fg_NearFar.x)); } vec3 decodeSRGB(vec3 screenRGB) diff --git a/Shaders/HDR/lighting.frag b/Shaders/HDR/lighting.frag index 752848088..5de8a01ae 100644 --- a/Shaders/HDR/lighting.frag +++ b/Shaders/HDR/lighting.frag @@ -383,17 +383,15 @@ vec3 getSunIlluminance() void main() { float depth = texture(depth_tex, texCoord).r; - if (depth == 1.0) { - fragHdrColor = vec3(0.0); - return; + if (depth == 0.0) { + discard; } - vec4 gbuffer0 = texture(gbuffer0_tex, texCoord); vec2 gbuffer1 = texture(gbuffer1_tex, texCoord).rg; vec4 gbuffer2 = texture(gbuffer2_tex, texCoord); float ao = texture(ao_tex, texCoord).r; - vec3 pos = positionFromDepth(texCoord * 2.0 - 1.0, depth * 2.0 - 1.0); + vec3 pos = positionFromDepth(texCoord, depth); vec3 v = normalize(-pos); vec3 n = decodeNormal(gbuffer1); diff --git a/Shaders/HDR/ws30.frag b/Shaders/HDR/ws30.frag index 21a5a0c8d..86554caff 100644 --- a/Shaders/HDR/ws30.frag +++ b/Shaders/HDR/ws30.frag @@ -46,11 +46,9 @@ void main() vec3 texel = texture(atlas, vec3(st, lc)).rgb; - gbuffer0.rgb = decodeSRGB(texel) * color.rgb; + gbuffer0.rgb = decodeSRGB(texel); gbuffer0.a = 1.0; gbuffer1 = encodeNormal(normalVS); float specularity = clamp(dot(specular.rgb, vec3(0.333)), 0.0, 1.0); gbuffer2 = vec4(0.0, 1.0-specularity, 0.0, 0.0); } - -