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

View File

@ -3,6 +3,7 @@
uniform sampler2D uColor; uniform sampler2D uColor;
uniform sampler2D uNormal; uniform sampler2D uNormal;
uniform sampler2D uDepth; uniform sampler2D uDepth;
uniform sampler2D uAmbientOcclusion;
#define USE_SAMPLER2D_SHADOW 1 #define USE_SAMPLER2D_SHADOW 1
@ -13,7 +14,6 @@ uniform sampler2D uShadowMap;
#endif #endif
uniform sampler2D uPosition; uniform sampler2D uPosition;
uniform vec3 uViewPosition;
uniform vec3 uLightDirection; uniform vec3 uLightDirection;
uniform mat4 uLightSpaceMatrix; uniform mat4 uLightSpaceMatrix;
@ -74,12 +74,13 @@ void main() {
vec3 normal = texture (uNormal, ioFragTexCoords).xyz; vec3 normal = texture (uNormal, ioFragTexCoords).xyz;
float depth = texture (uDepth, ioFragTexCoords).r; float depth = texture (uDepth, ioFragTexCoords).r;
vec3 position = texture (uPosition, ioFragTexCoords).xyz; vec3 position = texture (uPosition, ioFragTexCoords).xyz;
float ambient_occlusion = texture(uAmbientOcclusion, ioFragTexCoords).r;
// ambient lighting // ambient lighting
float ambient_strength = 0.2; float ambient_strength = 0.2;
vec3 ambient = ambient_strength * color; vec3 ambient = ambient_strength * color;
vec3 light_dir = normalize(uLightDirection); vec3 light_dir = uLightDirection;
// diffuse lighting // diffuse lighting
float diff = max(dot(normal, light_dir), 0.0); 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); float spec = pow(max(dot(normal, halfway_dir), 0.0), 32);
vec3 specular = spec * vec3(0.5); vec3 specular = spec * vec3(0.5);
// shadow // shadow (need to transform position and normal to light space)
// TODO: transform to light space
vec4 position_light_space = uViewToLightSpaceMatrix * vec4(position, 1.0); 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); 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 // SSAO Target
mSSAOTarget.Initialize(width, height, RenderTarget::EnableColor); mSSAOTarget.Initialize(width, height, RenderTarget::EnableColor);
mSSAOBlurTarget.Initialize(width, height, RenderTarget::EnableColor);
// Postprocess Target // Postprocess Target
mPostprocessTarget.Initialize(width, height, RenderTarget::EnableColor); mPostprocessTarget.Initialize(width, height, RenderTarget::EnableColor);
@ -493,6 +494,7 @@ void Renderer::CheckRenderBuffers() {
&& (mSSAOTarget.mWidth != mSceneAreaWidth && (mSSAOTarget.mWidth != mSceneAreaWidth
|| mSSAOTarget.mHeight != mSceneAreaHeight)) { || mSSAOTarget.mHeight != mSceneAreaHeight)) {
mSSAOTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor); mSSAOTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor);
mSSAOBlurTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor);
} }
} }
@ -637,23 +639,18 @@ void Renderer::RenderGl() {
gScreenQuad.Draw(GL_TRIANGLES); gScreenQuad.Draw(GL_TRIANGLES);
// Blur pass // Blur pass
mPostprocessTarget.Bind(); mSSAOBlurTarget.Bind();
glViewport(0, 0, mCamera.mWidth, mCamera.mHeight); glViewport(0, 0, mCamera.mWidth, mCamera.mHeight);
glDrawBuffers(1, draw_attachment_0); glDrawBuffers(1, draw_attachment_0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mSSAOTarget.mColorTexture); glBindTexture(GL_TEXTURE_2D, mSSAOTarget.mColorTexture);
glUseProgram(mBlurSSAOProgram.mProgramId); glUseProgram(mBlurSSAOProgram.mProgramId);
mBlurSSAOProgram.SetInt("uColor", 0); mBlurSSAOProgram.SetInt("uAmbientOcclusion", 0);
mBlurSSAOProgram.SetInt("uAmbientOcclusion", 1);
mBlurSSAOProgram.SetInt("uBlurSize", mSSAOBlurSize); mBlurSSAOProgram.SetInt("uBlurSize", mSSAOBlurSize);
mBlurSSAOProgram.SetInt("uDisableColor", mSSAODisableColor);
gScreenQuad.Draw(GL_TRIANGLES); gScreenQuad.Draw(GL_TRIANGLES);
} }
@ -682,6 +679,7 @@ void Renderer::RenderGl() {
glActiveTexture(GL_TEXTURE3); glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, mLight.mShadowMapTarget.mDepthTexture); glBindTexture(GL_TEXTURE_2D, mLight.mShadowMapTarget.mDepthTexture);
mDeferredLighting.SetInt("uShadowMap", 3); mDeferredLighting.SetInt("uShadowMap", 3);
mDeferredLighting.SetMat44("uLightSpaceMatrix", mLight.mLightSpaceMatrix); mDeferredLighting.SetMat44("uLightSpaceMatrix", mLight.mLightSpaceMatrix);
Matrix44f view_to_light_matrix = mCamera.mViewMatrix.inverse() * mLight.mLightSpaceMatrix; Matrix44f view_to_light_matrix = mCamera.mViewMatrix.inverse() * mLight.mLightSpaceMatrix;
mDeferredLighting.SetMat44("uViewToLightSpaceMatrix", view_to_light_matrix); mDeferredLighting.SetMat44("uViewToLightSpaceMatrix", view_to_light_matrix);
@ -691,13 +689,16 @@ void Renderer::RenderGl() {
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mPositionTexture); glBindTexture(GL_TEXTURE_2D, mRenderTarget.mPositionTexture);
mDeferredLighting.SetInt("uPosition", 4); mDeferredLighting.SetInt("uPosition", 4);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_2D, mSSAOBlurTarget.mColorTexture);
mDeferredLighting.SetInt("uAmbientOcclusion", 5);
mDeferredLighting.SetMat44("uViewMatrix", mCamera.mViewMatrix); mDeferredLighting.SetMat44("uViewMatrix", mCamera.mViewMatrix);
Matrix33f view_mat_rot = mCamera.mViewMatrix.block<3,3>(0,0); Matrix33f view_mat_rot = mCamera.mViewMatrix.block<3,3>(0,0);
view_mat_rot = view_mat_rot.transpose(); view_mat_rot = view_mat_rot.transpose();
Vector3f light_direction = view_mat_rot * mLight.mDirection.normalized(); Vector3f light_direction = view_mat_rot * mLight.mDirection.normalized();
mDeferredLighting.SetVec3("uLightDirection", light_direction); mDeferredLighting.SetVec3("uLightDirection", light_direction.normalized());
mDeferredLighting.SetVec3("uViewPosition", mCamera.mEye);
gVertexArray.Bind(); gVertexArray.Bind();
gScreenQuad.Draw(GL_TRIANGLES); 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)); program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f));
gUnitCubeMesh.Draw(GL_TRIANGLES); gUnitCubeMesh.Draw(GL_TRIANGLES);
program.SetMat44("uModelMatrix", Matrix44f::Identity()); program.SetMat44("uModelMatrix", Matrix44f::Identity());
program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f)); program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f));
gXZPlaneMesh.Draw(GL_TRIANGLES); gXZPlaneMesh.Draw(GL_TRIANGLES);
@ -809,7 +809,7 @@ void Renderer::DrawGui() {
mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mPositionTexture; mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mPositionTexture;
break; break;
case SceneRenderModeAmbientOcclusion: case SceneRenderModeAmbientOcclusion:
mRenderTextureRef.mTextureIdPtr = &mSSAOTarget.mColorTexture; mRenderTextureRef.mTextureIdPtr = &mSSAOBlurTarget.mColorTexture;
break; break;
} }

View File

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