Ported SSAO to deferred lighting

simple_math_single_header
Martin Felis 2018-06-22 17:31:53 +02:00
parent 3d2cdf368a
commit 33a2e1d8cb
4 changed files with 20 additions and 25 deletions

View File

@ -4,10 +4,8 @@ in vec2 ioFragTexCoords;
out vec3 outColor;
uniform sampler2D uColor;
uniform sampler2D uAmbientOcclusion;
uniform int uBlurSize;
uniform int uDisableColor;
float box_blur_occlusion (sampler2D ambient_occlusion, vec2 tex_coord, int size) {
vec2 texel_size = 1.0 / vec2(textureSize(uAmbientOcclusion, 0));
@ -32,7 +30,5 @@ void main() {
occlusion = texture(uAmbientOcclusion, ioFragTexCoords).x;
}
vec4 color = texture(uColor, ioFragTexCoords);
outColor = color.xyz * (1.0 - uDisableColor) * occlusion + uDisableColor * vec3(occlusion);
outColor = vec3(occlusion);
}

View File

@ -3,6 +3,7 @@
uniform sampler2D uColor;
uniform sampler2D uNormal;
uniform sampler2D uDepth;
uniform sampler2D uAmbientOcclusion;
#define USE_SAMPLER2D_SHADOW 1
@ -13,7 +14,6 @@ uniform sampler2D uShadowMap;
#endif
uniform sampler2D uPosition;
uniform vec3 uViewPosition;
uniform vec3 uLightDirection;
uniform mat4 uLightSpaceMatrix;
@ -74,12 +74,13 @@ void main() {
vec3 normal = texture (uNormal, ioFragTexCoords).xyz;
float depth = texture (uDepth, ioFragTexCoords).r;
vec3 position = texture (uPosition, ioFragTexCoords).xyz;
float ambient_occlusion = texture(uAmbientOcclusion, ioFragTexCoords).r;
// ambient lighting
float ambient_strength = 0.2;
vec3 ambient = ambient_strength * color;
vec3 light_dir = normalize(uLightDirection);
vec3 light_dir = uLightDirection;
// diffuse lighting
float diff = max(dot(normal, light_dir), 0.0);
@ -92,14 +93,11 @@ void main() {
float spec = pow(max(dot(normal, halfway_dir), 0.0), 32);
vec3 specular = spec * vec3(0.5);
// shadow
// TODO: transform to light space
// shadow (need to transform position and normal to light space)
vec4 position_light_space = uViewToLightSpaceMatrix * vec4(position, 1.0);
vec3 normal_light_space = normal;
vec3 normal_light_space = (transpose(inverse(uViewToLightSpaceMatrix)) * vec4(normal, 1.0)).xyz;
float shadow = ShadowCalculationPCF(position_light_space, normal);
outColor = ambient + (1.0 - shadow) * (diffuse + specular);
// outColor = ambient + (diffuse + specular);
outColor = (ambient * ambient_occlusion + (1.0 - shadow) * (diffuse + specular)) * ambient_occlusion;
// outColor = (ambient + (diffuse + specular)) * ambient_occlusion;
}

View File

@ -427,6 +427,7 @@ void Renderer::Initialize(int width, int height) {
// SSAO Target
mSSAOTarget.Initialize(width, height, RenderTarget::EnableColor);
mSSAOBlurTarget.Initialize(width, height, RenderTarget::EnableColor);
// Postprocess Target
mPostprocessTarget.Initialize(width, height, RenderTarget::EnableColor);
@ -493,6 +494,7 @@ void Renderer::CheckRenderBuffers() {
&& (mSSAOTarget.mWidth != mSceneAreaWidth
|| mSSAOTarget.mHeight != mSceneAreaHeight)) {
mSSAOTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor);
mSSAOBlurTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor);
}
}
@ -637,23 +639,18 @@ void Renderer::RenderGl() {
gScreenQuad.Draw(GL_TRIANGLES);
// Blur pass
mPostprocessTarget.Bind();
mSSAOBlurTarget.Bind();
glViewport(0, 0, mCamera.mWidth, mCamera.mHeight);
glDrawBuffers(1, draw_attachment_0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mSSAOTarget.mColorTexture);
glUseProgram(mBlurSSAOProgram.mProgramId);
mBlurSSAOProgram.SetInt("uColor", 0);
mBlurSSAOProgram.SetInt("uAmbientOcclusion", 1);
mBlurSSAOProgram.SetInt("uAmbientOcclusion", 0);
mBlurSSAOProgram.SetInt("uBlurSize", mSSAOBlurSize);
mBlurSSAOProgram.SetInt("uDisableColor", mSSAODisableColor);
gScreenQuad.Draw(GL_TRIANGLES);
}
@ -682,6 +679,7 @@ void Renderer::RenderGl() {
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, mLight.mShadowMapTarget.mDepthTexture);
mDeferredLighting.SetInt("uShadowMap", 3);
mDeferredLighting.SetMat44("uLightSpaceMatrix", mLight.mLightSpaceMatrix);
Matrix44f view_to_light_matrix = mCamera.mViewMatrix.inverse() * mLight.mLightSpaceMatrix;
mDeferredLighting.SetMat44("uViewToLightSpaceMatrix", view_to_light_matrix);
@ -691,13 +689,16 @@ void Renderer::RenderGl() {
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mPositionTexture);
mDeferredLighting.SetInt("uPosition", 4);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_2D, mSSAOBlurTarget.mColorTexture);
mDeferredLighting.SetInt("uAmbientOcclusion", 5);
mDeferredLighting.SetMat44("uViewMatrix", mCamera.mViewMatrix);
Matrix33f view_mat_rot = mCamera.mViewMatrix.block<3,3>(0,0);
view_mat_rot = view_mat_rot.transpose();
Vector3f light_direction = view_mat_rot * mLight.mDirection.normalized();
mDeferredLighting.SetVec3("uLightDirection", light_direction);
mDeferredLighting.SetVec3("uViewPosition", mCamera.mEye);
mDeferredLighting.SetVec3("uLightDirection", light_direction.normalized());
gVertexArray.Bind();
gScreenQuad.Draw(GL_TRIANGLES);
@ -770,7 +771,6 @@ void Renderer::RenderScene(RenderProgram &program, const Camera& camera) {
program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f));
gUnitCubeMesh.Draw(GL_TRIANGLES);
program.SetMat44("uModelMatrix", Matrix44f::Identity());
program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f));
gXZPlaneMesh.Draw(GL_TRIANGLES);
@ -809,7 +809,7 @@ void Renderer::DrawGui() {
mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mPositionTexture;
break;
case SceneRenderModeAmbientOcclusion:
mRenderTextureRef.mTextureIdPtr = &mSSAOTarget.mColorTexture;
mRenderTextureRef.mTextureIdPtr = &mSSAOBlurTarget.mColorTexture;
break;
}

View File

@ -80,6 +80,7 @@ struct Renderer {
RenderTarget mRenderTarget;
RenderTarget mDeferredLightingTarget;
RenderTarget mSSAOTarget;
RenderTarget mSSAOBlurTarget;
RenderTarget mPostprocessTarget;
GLTextureRef mRenderTextureRef = { (int)0xbadface };