diff --git a/resources/Shaders/SSAO.frag.glsl b/resources/Shaders/SSAO.frag.glsl index 2709d099..23812664 100644 --- a/resources/Shaders/SSAO.frag.glsl +++ b/resources/Shaders/SSAO.frag.glsl @@ -99,12 +99,14 @@ void main() { radius = uRadius; } + float radius2 = radius * radius; vec3 originNormal = getVSFaceNormal(origin); //screenSpaceSampleRadius is in pixels - float screenSpaceSampleRadius = uProjScale * radius / origin.z; + float screenSpaceSampleRadius = -uProjScale * radius / origin.z; + //screenSpaceSampleRadius = clamp(screenSpaceSampleRadius, 0.0f, -uProjScale * radius); float rotationAngleOffset = 30 * originScreenCoord.x ^ originScreenCoord.y + 10 * originScreenCoord.x * originScreenCoord.y; @@ -116,5 +118,6 @@ void main() { //float A = max(0.0, 1.0 - sum * (2.0f / uNumOfSamples)); float A = 1.0f - sum * (2.0f * uIntensityScale / float(uNumOfSamples)); AO = clamp(pow(A, uContrast), 0.0f, 1.0f); + //AO = screenSpaceSampleRadius; //AO = vec4(originNormal, 1.0f); } diff --git a/resources/Shaders/SSAOMipMappedZ.frag.glsl b/resources/Shaders/SSAOMipMappedZ.frag.glsl index 10023cd3..1700dbfe 100644 --- a/resources/Shaders/SSAOMipMappedZ.frag.glsl +++ b/resources/Shaders/SSAOMipMappedZ.frag.glsl @@ -1,8 +1,8 @@ #version 430 -#define MIP_MAPS_LEVELS (3) +#define MIP_MAPS_LEVELS (5) -#define MAX_PIXEL_BEFOR_LOWER_MIP_LEVEL (3) +#define LOWER_MIP_LEVEL (2) //Number of samples per pixel uniform int uNumOfSamples; //#define uNumOfSamples (11) @@ -32,7 +32,7 @@ uniform float uIntensityScale; out float AO; vec3 getVSPosition(ivec2 ScreenSpaceCoord, int mipmaplevel) { - float z = texelFetch(ViewSpaceZ, ScreenSpaceCoord, 0).r; + float z = texelFetch(ViewSpaceZ, ScreenSpaceCoord, mipmaplevel).r; //Get the xy view space coordinates and add the z value from ViewSpaceZ buffer. return vec3((uProjInfo[0] + (ScreenSpaceCoord.x * uProjInfo[1])) * z, (uProjInfo[2] + (ScreenSpaceCoord.y * uProjInfo[3])) * z, z); } @@ -56,8 +56,7 @@ vec3 getSampleViewSpacePos(ivec2 ScreenSpaceCoord, int SampleIndex, float Rotati vec2 screenSpaceSampleOffsetVecor = vec2(cos(angle), sin(angle)); - int mipmapLevel = clamp(int(floor(log2(-ScreenSpaceSampleRadius))) - MAX_PIXEL_BEFOR_LOWER_MIP_LEVEL, 0, MIP_MAPS_LEVELS); - + int mipmapLevel = clamp(int(floor(log2(-ScreenSpaceSampleRadius))) - LOWER_MIP_LEVEL, 0, MIP_MAPS_LEVELS); // Get texel coordinate on where to sample by going screenSpaceSampleOffsetVecor direction in ScreenSpaceSampleRadius units from ScreenSpaceCoord (the point being shaded); ivec2 screenSpaceSampleTexel = (ivec2(ScreenSpaceSampleRadius * screenSpaceSampleOffsetVecor) + ScreenSpaceCoord) >> mipmapLevel; @@ -80,8 +79,6 @@ float sampleAO(ivec2 ScreenSpaceCoord, vec3 Origin, vec3 OriginNormal, float Scr // vn - bias, offset the angle to reduse self occlusion. // epsilon is here to make divison by 0 impossible. return float(vv < Radius2) * max((vn - uBias) / (epsilon + vv), 0.0); - //float f = max(radius2 - vv, 0.0); - //return f * f * f * max((vn - uBias) / (epsilon + vv), 0.0); } @@ -102,9 +99,15 @@ void main() { float radius2 = radius * radius; vec3 originNormal = getVSFaceNormal(origin); + //AO = originNormal; + //return; //screenSpaceSampleRadius is in pixels - float screenSpaceSampleRadius = uProjScale * radius / origin.z; + float screenSpaceSampleRadius = -uProjScale * radius / origin.z; + + //screenSpaceSampleRadius = clamp(screenSpaceSampleRadius, 0.0f, pow(2, MIP_MAPS_LEVELS * LOWER_MIP_LEVEL + 1)); + //AO = clamp(int(floor(log2(screenSpaceSampleRadius))) - LOWER_MIP_LEVEL, 0, MIP_MAPS_LEVELS); + //return; float rotationAngleOffset = 30 * originScreenCoord.x ^ originScreenCoord.y + 10 * originScreenCoord.x * originScreenCoord.y; diff --git a/src/Engine/Rendering/SSAOPass.cpp b/src/Engine/Rendering/SSAOPass.cpp index 74e81124..d6e92f27 100644 --- a/src/Engine/Rendering/SSAOPass.cpp +++ b/src/Engine/Rendering/SSAOPass.cpp @@ -38,6 +38,8 @@ void SSAOPass::InitializeBuffer() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height, 0, GL_RED, GL_FLOAT, nullptr); + //glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height, 0, GL_RED_INTEGER, GL_INT, nullptr); + //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height, 0, GL_RGB, GL_FLOAT, nullptr); m_SSAOFramBuffer.AddResource(std::shared_ptr(new Texture2D(&m_SSAOTexture, GL_COLOR_ATTACHMENT0))); m_SSAOFramBuffer.Generate(); @@ -129,7 +131,7 @@ void SSAOPass::Draw(GLuint depthBuffer, Camera* camera) glBindTexture(GL_TEXTURE_2D, m_SSAOViewSpaceZTexture); // How many pixel there are in a 1m long object 1m away from the camera - glUniform1f(glGetUniformLocation(SSAOShaderHandle, "uProjScale"), m_Renderer->GetViewportSize().Height / (2.0f * glm::tan(glm::radians(camera->FOV() * 0.5f)))); + glUniform1f(glGetUniformLocation(SSAOShaderHandle, "uProjScale"), m_Renderer->GetViewportSize().Height / (-2.0f * glm::tan(glm::radians(camera->FOV()) * 0.5f))); glUniform1f(glGetUniformLocation(SSAOShaderHandle, "uRadius"), m_Radius); glUniform1f(glGetUniformLocation(SSAOShaderHandle, "uBias"), m_Bias); @@ -145,7 +147,3 @@ void SSAOPass::Draw(GLuint depthBuffer, Camera* camera) m_DrawBloomPass->ClearBuffer(); m_DrawBloomPass->Draw(m_SSAOTexture); } - -void ComputeAO(GLuint depthBuffer, Camera* camera) { - -} \ No newline at end of file