From ddf92b39c76408836d4a54e1f7202f44f71fe63d Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Sun, 8 Sep 2013 17:52:35 -0700 Subject: [PATCH] Fix for off-axis ambient occlusion. --- .../resources/shaders/ambient_occlusion.frag | 3 +-- interface/src/Application.cpp | 22 ++++++++++++------- interface/src/Application.h | 4 ++++ .../src/renderer/AmbientOcclusionEffect.cpp | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/interface/resources/shaders/ambient_occlusion.frag b/interface/resources/shaders/ambient_occlusion.frag index 4e87ac3221..b3a054cc67 100644 --- a/interface/resources/shaders/ambient_occlusion.frag +++ b/interface/resources/shaders/ambient_occlusion.frag @@ -44,8 +44,7 @@ float texCoordToViewSpaceZ(vec2 texCoord) { // given a texture coordinate, returns the 3D view space coordinate vec3 texCoordToViewSpace(vec2 texCoord) { float z = texCoordToViewSpaceZ(texCoord); - return vec3(((texCoord * 2.0 - vec2(1.0 - gl_ProjectionMatrix[2][0], 1.0)) * - (rightTop - leftBottom) + rightTop + leftBottom) * z / (-2.0 * near), z); + return vec3((leftBottom + texCoord * (rightTop - leftBottom)) * (-z / near), z); } void main(void) { diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index a473469734..ea61179057 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -438,7 +438,7 @@ void Application::updateProjectionMatrix() { float left, right, bottom, top, nearVal, farVal; glm::vec4 nearClipPlane, farClipPlane; - _viewFrustum.computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); + computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); // If we're in Display Frustum mode, then we want to use the slightly adjust near/far clip values of the // _viewFrustumOffsetCamera, so that we can see more of the application content in the application's frustum @@ -446,13 +446,6 @@ void Application::updateProjectionMatrix() { nearVal = _viewFrustumOffsetCamera.getNearClip(); farVal = _viewFrustumOffsetCamera.getFarClip(); } - - // when mirrored, we must flip left and right - if (Menu::getInstance()->isOptionChecked(MenuOption::Mirror)) { - float tmp = left; - left = -right; - right = -tmp; - } glFrustum(left, right, bottom, top, nearVal, farVal); glMatrixMode(GL_MODELVIEW); @@ -2144,6 +2137,19 @@ void Application::setupWorldLight(Camera& whichCamera) { glMateriali(GL_FRONT, GL_SHININESS, 96); } +void Application::computeOffAxisFrustum(float& left, float& right, float& bottom, float& top, float& near, + float& far, glm::vec4& nearClipPlane, glm::vec4& farClipPlane) const { + + _viewFrustum.computeOffAxisFrustum(left, right, bottom, top, near, far, nearClipPlane, farClipPlane); + + // when mirrored, we must flip left and right + if (Menu::getInstance()->isOptionChecked(MenuOption::Mirror)) { + float tmp = left; + left = -right; + right = -tmp; + } +} + void Application::displaySide(Camera& whichCamera) { // transform by eye offset diff --git a/interface/src/Application.h b/interface/src/Application.h index 0630d3cbca..8bb3106375 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -135,6 +135,10 @@ public: void setupWorldLight(Camera& whichCamera); + /// Computes the off-axis frustum parameters for the view frustum, taking mirroring into account. + void computeOffAxisFrustum(float& left, float& right, float& bottom, float& top, float& near, + float& far, glm::vec4& nearClipPlane, glm::vec4& farClipPlane) const; + virtual void nodeAdded(Node* node); virtual void nodeKilled(Node* node); virtual void packetSentNotification(ssize_t length); diff --git a/interface/src/renderer/AmbientOcclusionEffect.cpp b/interface/src/renderer/AmbientOcclusionEffect.cpp index 2169ec00af..907ec7a863 100644 --- a/interface/src/renderer/AmbientOcclusionEffect.cpp +++ b/interface/src/renderer/AmbientOcclusionEffect.cpp @@ -103,7 +103,7 @@ void AmbientOcclusionEffect::render() { float left, right, bottom, top, nearVal, farVal; glm::vec4 nearClipPlane, farClipPlane; - Application::getInstance()->getViewFrustum()->computeOffAxisFrustum( + Application::getInstance()->computeOffAxisFrustum( left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); _occlusionProgram->bind();