From 447575946176363bf03994fd95d4ce3f130d36a2 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Mon, 15 Sep 2014 11:52:44 -0700 Subject: [PATCH] Revert the normal-based ambient occlusion; I wasn't crazy about the look of it. Will perhaps revisit later. --- .../resources/shaders/ambient_occlusion.frag | 12 +++++++---- interface/src/Application.cpp | 18 +++++++++------- .../src/renderer/AmbientOcclusionEffect.cpp | 21 +++++++++++++------ 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/interface/resources/shaders/ambient_occlusion.frag b/interface/resources/shaders/ambient_occlusion.frag index 512922ca43..621b123966 100644 --- a/interface/resources/shaders/ambient_occlusion.frag +++ b/interface/resources/shaders/ambient_occlusion.frag @@ -14,6 +14,9 @@ // the depth texture uniform sampler2D depthTexture; +// the normal texture +uniform sampler2D normalTexture; + // the random rotation texture uniform sampler2D rotationTexture; @@ -57,10 +60,11 @@ vec3 texCoordToViewSpace(vec2 texCoord) { } void main(void) { - vec3 rotationX = texture2D(rotationTexture, gl_TexCoord[0].st * noiseScale).rgb; - vec3 rotationY = normalize(cross(rotationX, vec3(0.0, 0.0, 1.0))); - mat3 rotation = mat3(rotationX, rotationY, cross(rotationX, rotationY)); - + vec3 rotationZ = texture2D(normalTexture, gl_TexCoord[0].st).xyz * 2.0 - vec3(1.0, 1.0, 1.0); + vec3 rotationY = normalize(cross(rotationZ, texture2D(rotationTexture, + gl_TexCoord[0].st * noiseScale).xyz - vec3(0.5, 0.5, 0.5))); + mat3 rotation = mat3(cross(rotationY, rotationZ), rotationY, rotationZ); + vec3 center = texCoordToViewSpace(gl_TexCoord[0].st); vec2 rdenominator = 1.0 / (rightTop - leftBottom); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index d5fb95ade3..8131b7b882 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2892,14 +2892,6 @@ void Application::displaySide(Camera& whichCamera, bool selfAvatarOnly) { _entities.render(); } - // render the ambient occlusion effect if enabled - if (Menu::getInstance()->isOptionChecked(MenuOption::AmbientOcclusion)) { - PerformanceTimer perfTimer("ambientOcclusion"); - PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), - "Application::displaySide() ... AmbientOcclusion..."); - _ambientOcclusionEffect.render(); - } - // restore default, white specular glMaterialfv(GL_FRONT, GL_SPECULAR, WORLD_SPECULAR_COLOR); @@ -2919,6 +2911,16 @@ void Application::displaySide(Camera& whichCamera, bool selfAvatarOnly) { } } + if (!selfAvatarOnly) { + // render the ambient occlusion effect if enabled + if (Menu::getInstance()->isOptionChecked(MenuOption::AmbientOcclusion)) { + PerformanceTimer perfTimer("ambientOcclusion"); + PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), + "Application::displaySide() ... AmbientOcclusion..."); + _ambientOcclusionEffect.render(); + } + } + { PerformanceTimer perfTimer("lighting"); _deferredLightingEffect.render(); diff --git a/interface/src/renderer/AmbientOcclusionEffect.cpp b/interface/src/renderer/AmbientOcclusionEffect.cpp index f1c1538800..4941f715c0 100644 --- a/interface/src/renderer/AmbientOcclusionEffect.cpp +++ b/interface/src/renderer/AmbientOcclusionEffect.cpp @@ -36,12 +36,13 @@ void AmbientOcclusionEffect::init() { + "shaders/ambient_occlusion.frag"); _occlusionProgram->link(); - // create the sample kernel: an array of spherically distributed offset vectors + // create the sample kernel: an array of hemispherically distributed offset vectors const int SAMPLE_KERNEL_SIZE = 16; QVector3D sampleKernel[SAMPLE_KERNEL_SIZE]; for (int i = 0; i < SAMPLE_KERNEL_SIZE; i++) { // square the length in order to increase density towards the center glm::vec3 vector = glm::sphericalRand(1.0f); + vector.z = glm::abs(vector.z); float scale = randFloat(); const float MIN_VECTOR_LENGTH = 0.01f; const float MAX_VECTOR_LENGTH = 1.0f; @@ -51,7 +52,8 @@ void AmbientOcclusionEffect::init() { _occlusionProgram->bind(); _occlusionProgram->setUniformValue("depthTexture", 0); - _occlusionProgram->setUniformValue("rotationTexture", 1); + _occlusionProgram->setUniformValue("normalTexture", 1); + _occlusionProgram->setUniformValue("rotationTexture", 2); _occlusionProgram->setUniformValueArray("sampleKernel", sampleKernel, SAMPLE_KERNEL_SIZE); _occlusionProgram->setUniformValue("radius", 0.1f); _occlusionProgram->release(); @@ -71,10 +73,10 @@ void AmbientOcclusionEffect::init() { unsigned char rotationData[ROTATION_WIDTH * ROTATION_HEIGHT * ELEMENTS_PER_PIXEL]; unsigned char* rotation = rotationData; for (int i = 0; i < ROTATION_WIDTH * ROTATION_HEIGHT; i++) { - glm::vec3 randvec = glm::sphericalRand(1.0f); - *rotation++ = ((randvec.x + 1.0f) / 2.0f) * 255.0f; - *rotation++ = ((randvec.y + 1.0f) / 2.0f) * 255.0f; - *rotation++ = ((randvec.z + 1.0f) / 2.0f) * 255.0f; + float angle = randFloatInRange(0.0f, TWO_PI); + *rotation++ = ((glm::cos(angle) + 1.0f) / 2.0f) * 255.0f; + *rotation++ = ((glm::sin(angle) + 1.0f) / 2.0f) * 255.0f; + *rotation++ = 0.0f; } glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ROTATION_WIDTH, ROTATION_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, rotationData); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -101,6 +103,9 @@ void AmbientOcclusionEffect::render() { glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getPrimaryDepthTextureID()); glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getPrimaryNormalTextureID()); + + glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, _rotationTextureID); // render with the occlusion shader to the secondary/tertiary buffer @@ -137,6 +142,10 @@ void AmbientOcclusionEffect::render() { freeFBO->release(); glBindTexture(GL_TEXTURE_2D, 0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, 0); + glActiveTexture(GL_TEXTURE0); // now render secondary to primary with 4x4 blur