From 81ea8e88743bfd6f2ec6710da649dddf8018508a Mon Sep 17 00:00:00 2001 From: Christopher Root Date: Sat, 15 Aug 2015 14:27:08 -0700 Subject: [PATCH] Updating some comments and removing unnecessary code --- .../src/AmbientOcclusionEffect.cpp | 16 ++++--- .../render-utils/src/ambient_occlusion.slf | 47 +++++++------------ 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/libraries/render-utils/src/AmbientOcclusionEffect.cpp b/libraries/render-utils/src/AmbientOcclusionEffect.cpp index b94bd09538..962b57a22c 100644 --- a/libraries/render-utils/src/AmbientOcclusionEffect.cpp +++ b/libraries/render-utils/src/AmbientOcclusionEffect.cpp @@ -219,22 +219,26 @@ void AmbientOcclusion::run(const render::SceneContextPointer& sceneContext, cons batch._glUniform1f(_gSampleRadiusLoc, g_sample_rad); batch._glUniform1f(_gIntensityLoc, g_intensity); - // setup uniforms for extracting depth from the depth buffer and - // converting that depth to a camera-space position, same as DeferredLightingEffect.cpp + // setup uniforms for unpacking a view-space position from the depth buffer + // This is code taken from DeferredLightEffect.render() method in DeferredLightingEffect.cpp. + // DeferredBuffer.slh shows how the unpacking is done and what variables are needed. + + // initialize the view-space unpacking uniforms using frustum data float left, right, bottom, top, nearVal, farVal; glm::vec4 nearClipPlane, farClipPlane; + args->_viewFrustum->computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); - batch._glUniform1f(_nearLoc, nearVal); - float depthScale = (farVal - nearVal) / farVal; - batch._glUniform1f(_depthScaleLoc, depthScale); - float nearScale = -1.0f / nearVal; float depthTexCoordScaleS = (right - left) * nearScale / sWidth; float depthTexCoordScaleT = (top - bottom) * nearScale / tHeight; float depthTexCoordOffsetS = left * nearScale - sMin * depthTexCoordScaleS; float depthTexCoordOffsetT = bottom * nearScale - tMin * depthTexCoordScaleT; + + // now set the position-unpacking unforms + batch._glUniform1f(_nearLoc, nearVal); + batch._glUniform1f(_depthScaleLoc, depthScale); batch._glUniform2f(_depthTexCoordOffsetLoc, depthTexCoordOffsetS, depthTexCoordOffsetT); batch._glUniform2f(_depthTexCoordScaleLoc, depthTexCoordScaleS, depthTexCoordScaleT); diff --git a/libraries/render-utils/src/ambient_occlusion.slf b/libraries/render-utils/src/ambient_occlusion.slf index 73bed26a9c..44ff79a179 100644 --- a/libraries/render-utils/src/ambient_occlusion.slf +++ b/libraries/render-utils/src/ambient_occlusion.slf @@ -52,19 +52,8 @@ uniform vec2 renderTargetResInv; const float PI = 3.14159265; -// const vec2 FocalLen = vec2(1.0, 1.0); -// const vec2 LinMAD = vec2(0.1-10.0, 0.1+10.0) / (2.0*0.1*10.0); - -// const vec2 AORes = vec2(1024.0, 768.0); -// const vec2 InvAORes = vec2(1.0/1024.0, 1.0/768.0); -// const vec2 NoiseScale = vec2(1024.0, 768.0) / 4.0; - const float AOStrength = 1.9; -// const float R = 0.3; -// const float R2 = 0.3*0.3; -// const float NegInvR2 = - 1.0 / (0.3*0.3); - const float R = 0.01; const float R2 = 0.01*0.01; const float NegInvR2 = - 1.0 / (0.01*0.01); @@ -80,38 +69,38 @@ const int NumSamples = 4; out vec4 outFragColor; -// float ViewSpaceZFromDepth(float d){ -// // [0,1] -> [-1,1] clip space -// d = d * 2.0 - 1.0; - -// // Get view space Z -// return -1.0 / (LinMAD.x * d + LinMAD.y); -// } - -// vec3 UVToViewSpace(vec2 uv, float z){ -// //uv = UVToViewA * uv + UVToViewB; -// return vec3(uv * z, z); -// } - -// vec3 GetViewPos(vec2 uv){ -// float z = ViewSpaceZFromDepth(texture(depthTexture, uv).r); -// return UVToViewSpace(uv, z); -// } - +/** + * Gets the normal in view space from a normal texture. + * uv: the uv texture coordinates to look up in the texture at. + */ vec3 GetViewNormalFromTexture(vec2 uv) { // convert [0,1] -> [-1,1], note: since we're normalizing // we don't need to do v*2 - 1.0, we can just do a v-0.5 return normalize(texture(normalTexture, uv).xyz - 0.5); } +/** + * Gets the linearized depth in view space. + * d: the depth value [0-1], usually from a depth texture to convert. + */ float ViewSpaceZFromDepth(float d){ return near / (d * depthScale - 1.0); } +/** + * Converts a uv coordinate and depth value into a 3D view space coordinate. + * uv: the uv coordinates to convert + * z: the view space depth of the uv coordinate. + */ vec3 UVToViewSpace(vec2 uv, float z){ return vec3((depthTexCoordOffset + varTexcoord * depthTexCoordScale) * z, z); } +/** + * Converts a uv coordinate into a 3D view space coordinate. + * The depth of the uv coord is determined from the depth texture. + * uv: the uv coordinates to convert + */ vec3 GetViewPos(vec2 uv){ float z = ViewSpaceZFromDepth(texture(depthTexture, uv).r); return UVToViewSpace(uv, z);