[Bf-blender-cvs] [0c8469c] viewport_experiments: Correct normal reconstruction with optimized formula.

Antony Riakiotakis noreply at git.blender.org
Wed Oct 22 01:16:29 CEST 2014


Commit: 0c8469c44014a47bbc61cf80aad6e726bf1e7daa
Author: Antony Riakiotakis
Date:   Wed Oct 22 00:18:40 2014 +0200
Branches: viewport_experiments
https://developer.blender.org/rB0c8469c44014a47bbc61cf80aad6e726bf1e7daa

Correct normal reconstruction with optimized formula.

This needed some adjustments from the formula I found on the article on
view space reconstruction, since OpenGL makes different assumptions about its
projection matrix.

SSAO does not work yet correctly under new model assumptions but we are going
to substitute with nice horizon based ambient occlusion.

===================================================================

M	source/blender/gpu/intern/gpu_compositing.c
M	source/blender/gpu/shaders/gpu_shader_fx_frag.glsl
M	source/blender/gpu/shaders/gpu_shader_fx_vert.glsl

===================================================================

diff --git a/source/blender/gpu/intern/gpu_compositing.c b/source/blender/gpu/intern/gpu_compositing.c
index 90089da..1cc3c5d 100644
--- a/source/blender/gpu/intern/gpu_compositing.c
+++ b/source/blender/gpu/intern/gpu_compositing.c
@@ -213,6 +213,7 @@ bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct RegionView3D
 			/* normalized trick see http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer */
 			mul_v3_fl(ssao_viewvecs[i], 1.0f / ssao_viewvecs[i][3]);
 			mul_v3_fl(ssao_viewvecs[i], 1.0f / ssao_viewvecs[i][2]);
+			ssao_viewvecs[i][2] = 1.0;
 		}
 
 		/* we need to store the differences */
diff --git a/source/blender/gpu/shaders/gpu_shader_fx_frag.glsl b/source/blender/gpu/shaders/gpu_shader_fx_frag.glsl
index 7b5cabf..c8d59fb 100644
--- a/source/blender/gpu/shaders/gpu_shader_fx_frag.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_fx_frag.glsl
@@ -8,7 +8,7 @@ uniform sampler2D blurredcolorbuffer;
 // depth buffer
 uniform sampler2D depthbuffer;
 // coordinates on framebuffer in normalized (0.0-1.0) uv space
-varying vec4 framecoords;
+varying vec4 uvcoordsvar;
 
 // this includes focal distance in x and aperture size in y
 uniform vec2 dof_params;
@@ -32,27 +32,15 @@ vec3 calculate_view_space_normal(in vec3 viewposition)
     return normal;
 }
 
-vec3 calculate_view_space_position(in vec2 uvcoords, float depth)
-{	
-    //First we need to calculate the view space distance from the shader inputs
-    //This will unfortunately depend on the precision of the depth buffer which is not linear
-    vec4 viewposition = vec4(uvcoords.x, uvcoords.y, depth, 1.0);
-    viewposition = viewposition * 2.0 - 1.0;
-
-    // convert to view space now
-    viewposition = gl_ProjectionMatrixInverse * viewposition;
-    viewposition = viewposition / viewposition.w;
-    
-    return viewposition.xyz;
-}
-
+/* projective matrix version */
 vec3 get_view_space_from_depth(in vec2 uvcoords, float depth)
 {
     /* convert depth to non-normalized range */
     float d = 2.0 * depth - 1.0;
 
-    /* simple depth reconstruction, see http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer */
-    float zview = gl_ProjectionMatrix[2][3] / (d - gl_ProjectionMatrix[2][2]);
+    /* simple depth reconstruction, see http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer
+     * we change the factors from the article to fit the OpennGL model. */
+    float zview = -gl_ProjectionMatrix[2][3] / (d + gl_ProjectionMatrix[2][2]);
 
     vec3 pos = vec3(zview * (ssao_viewvecs[0].xy + uvcoords * ssao_viewvecs[1].xy), zview);
 
@@ -76,11 +64,11 @@ float calculate_ssao_factor(float depth)
     if (depth == 1.0)
         return 0.0;
 
-    vec3 position = get_view_space_from_depth(framecoords.xy, depth);
+    vec3 position = get_view_space_from_depth(uvcoordsvar.xy, depth);
     vec3 normal = calculate_view_space_normal(position);
 
     // find the offset in screen space by multiplying a point in camera space at the depth of the point by the projection matrix.
-    vec4 offset = (gl_ProjectionMatrix * vec4(ssao_params.x, ssao_params.x, position.z, 1.0));
+    vec4 offset = gl_ProjectionMatrix * vec4(ssao_params.x, ssao_params.x, position.z, 1.0);
     float factor = 0.0;
     int x, y;
     
@@ -89,7 +77,7 @@ float calculate_ssao_factor(float depth)
 
     for (x = 0; x < NUM_SAMPLES; x++) {
         for (y = 0; y < NUM_SAMPLES; y++) {
-            vec2 uvcoords = framecoords.xy + (vec2(x,y) - vec2(4.0)) * offset.xy;
+            vec2 uvcoords = uvcoordsvar.xy + (vec2(x,y) - vec2(4.0)) * offset.xy;
 	
             float depth = texture2D(depthbuffer, uvcoords).r;
             if (depth != 1.0) {
@@ -108,7 +96,7 @@ float calculate_ssao_factor(float depth)
 
 void main()
 {
-    float depth = texture2D(depthbuffer, framecoords.xy).r;
+    float depth = texture2D(depthbuffer, uvcoordsvar.xy).r;
     
     //vec3 color = normal * 0.5 + vec3(0.5);
     
@@ -116,11 +104,10 @@ void main()
     //vec4 color = coc * texture2D(blurredcolorbuffer, framecoords.xy) +
     //       (1.0 - coc) * texture2D(colorbuffer, framecoords.xy);
 
-    vec3 position = get_view_space_from_depth(framecoords.xy, depth);
-    vec3 normal = calculate_view_space_normal(position);
+//   vec3 position = get_view_space_from_depth(uvcoordsvar.xy, depth);
+//   vec3 normal = calculate_view_space_normal(position);
 
-//    vec4 color = mix(texture2D(colorbuffer, framecoords.xy), ssao_color, calculate_ssao_factor(depth));
-//    gl_FragColor = vec4(color.xyz, 1.0);
-    gl_FragColor = vec4(normal * 0.5 + vec3(0.5), 1.0);
-    gl_FragColor = vec4(position, 1.0);
+    vec4 color = mix(texture2D(colorbuffer, uvcoordsvar.xy), ssao_color, calculate_ssao_factor(depth));
+    gl_FragColor = vec4(color.xyz, 1.0);
+//    gl_FragColor = vec4(normal * 0.5 + vec3(0.5), 1.0);
 }
diff --git a/source/blender/gpu/shaders/gpu_shader_fx_vert.glsl b/source/blender/gpu/shaders/gpu_shader_fx_vert.glsl
index 141eff1..c91eb45 100644
--- a/source/blender/gpu/shaders/gpu_shader_fx_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_fx_vert.glsl
@@ -1,9 +1,9 @@
 //texture coordinates for framebuffer read
-varying vec4 framecoords;
+varying vec4 uvcoordsvar;
 
 //very simple shader for gull screen FX, just pass values on
 void main()
 {
-	framecoords = gl_MultiTexCoord0;
+	uvcoordsvar = gl_MultiTexCoord0;
 	gl_Position = gl_Vertex;
 }




More information about the Bf-blender-cvs mailing list