Fix specular lighting in forward mode and fix switching between forward and deferred lighting.

simple_math_single_header
Martin Felis 2018-12-15 22:52:37 +01:00
parent eb67c1deaf
commit 1dc77c2131
4 changed files with 67 additions and 39 deletions

View File

@ -81,8 +81,7 @@ void main() {
// diffuse lighting // diffuse lighting
vec3 normal_dir = normalize(ioFragNormal); vec3 normal_dir = normalize(ioFragNormal);
// vec3 light_dir = normalize(uLightDirection); vec3 light_dir = -(mat3(uViewMatrix)) * uLightDirection;
vec3 light_dir = - transpose(inverse(mat3(uViewMatrix * uModelMatrix))) * uLightDirection;
float diff = max(dot(normal_dir, light_dir), 0.0); float diff = max(dot(normal_dir, light_dir), 0.0);
vec4 diffuse = diff * albedo_color; vec4 diffuse = diff * albedo_color;
@ -90,7 +89,7 @@ void main() {
vec3 view_dir = normalize(-ioFragPosition); vec3 view_dir = normalize(-ioFragPosition);
vec3 halfway_dir = normalize(light_dir + view_dir); vec3 halfway_dir = normalize(light_dir + view_dir);
float spec = pow(max(dot(normal_dir, halfway_dir), 0.0), 64); float spec = pow(max(dot(normal_dir, halfway_dir), 0.0), 32);
vec4 specular = spec * vec4(0.5); vec4 specular = spec * vec4(0.5);
// shadow // shadow

View File

@ -20,9 +20,9 @@ smooth out vec4 ioFragColor;
out vec4 ioFragPosLightSpace; out vec4 ioFragPosLightSpace;
void main() { void main() {
ioFragPosition = (uViewMatrix * uModelMatrix * inCoord).xyz;
mat4 model_view_matrix = uViewMatrix * uModelMatrix; mat4 model_view_matrix = uViewMatrix * uModelMatrix;
ioFragPosition = (model_view_matrix* inCoord).xyz;
ioFragNormal = transpose(inverse(mat3(model_view_matrix))) * inNormal; ioFragNormal = transpose(inverse(mat3(model_view_matrix))) * inNormal;
ioFragTexCoords = inUV; ioFragTexCoords = inUV;
ioFragColor = inColor; ioFragColor = inColor;

View File

@ -496,12 +496,12 @@ void Renderer::Initialize(int width, int height) {
| RenderTarget::EnablePositionTexture | RenderTarget::EnablePositionTexture
| RenderTarget::EnableNormalTexture; | RenderTarget::EnableNormalTexture;
} }
mRenderTarget.Initialize(width, height, render_target_flags); mForwardRenderingTarget.Initialize(width, height, render_target_flags);
mRenderTarget.mVertexArray = &gVertexArray; mForwardRenderingTarget.mVertexArray = &gVertexArray;
mRenderTarget.mQuadMesh = &gScreenQuad; mForwardRenderingTarget.mQuadMesh = &gScreenQuad;
mRenderTarget.mLinearizeDepthProgram = mRenderQuadProgramDepth; mForwardRenderingTarget.mLinearizeDepthProgram = mRenderQuadProgramDepth;
mRenderTarget.mLinearizeDepthProgram.RegisterFileModification(); mForwardRenderingTarget.mLinearizeDepthProgram.RegisterFileModification();
// SSAO Target // SSAO Target
mSSAOTarget.Initialize(width, height, RenderTarget::EnableColor); mSSAOTarget.Initialize(width, height, RenderTarget::EnableColor);
@ -509,6 +509,8 @@ void Renderer::Initialize(int width, int height) {
mDeferredLightingTarget.Initialize(width, height, RenderTarget::EnableColor); mDeferredLightingTarget.Initialize(width, height, RenderTarget::EnableColor);
mRenderOutput.Initialize(width, height, RenderTarget::EnableColor | RenderTarget::EnableDepth);
// Light // Light
mLight.Initialize(); mLight.Initialize();
mLight.mShadowMapTarget.mVertexArray = &gVertexArray; mLight.mShadowMapTarget.mVertexArray = &gVertexArray;
@ -559,24 +561,35 @@ void Renderer::CheckRenderBuffers() {
; ;
} }
if (mSceneAreaWidth != mRenderTarget.mWidth if (mSceneAreaWidth != mRenderOutput.mWidth
|| mSceneAreaHeight != mRenderTarget.mHeight || mSceneAreaHeight != mRenderOutput.mHeight
|| mRenderTarget.mFlags != required_render_flags ) { || mForwardRenderingTarget.mFlags != required_render_flags ) {
mRenderTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, required_render_flags); mRenderOutput.Resize(mSceneAreaWidth, mSceneAreaHeight, required_render_flags);
mForwardRenderingTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, required_render_flags);
mDeferredLightingTarget.Resize(mSceneAreaWidth, mSceneAreaHeight,
RenderTarget::EnableColor
| RenderTarget::EnableDepthTexture
| RenderTarget::EnableNormalTexture
if (mUseDeferred) { // TODO: remove these
mDeferredLightingTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor); | RenderTarget::EnablePositionTexture
} | RenderTarget::EnableLinearizedDepthTexture);
} }
if (mIsSSAOEnabled if (mIsSSAOEnabled
&& (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); mSSAOBlurTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor);
} }
} }
void Renderer::ResizeTargets(int width, int height) {
mSSAOTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor);
mSSAOBlurTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor);
}
void Renderer::RenderGl() { void Renderer::RenderGl() {
CheckRenderBuffers(); CheckRenderBuffers();
@ -640,7 +653,7 @@ void Renderer::RenderGl() {
* mCamera.mProjectionMatrix; * mCamera.mProjectionMatrix;
// enable the render target // enable the render target
mRenderTarget.Bind(); mForwardRenderingTarget.Bind();
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
gLog ("Cannot render: frame buffer invalid!"); gLog ("Cannot render: frame buffer invalid!");
@ -717,7 +730,7 @@ void Renderer::RenderGl() {
RenderScene(*program, mCamera); RenderScene(*program, mCamera);
if (mSettings->RenderMode == SceneRenderModeDepth) { if (mSettings->RenderMode == SceneRenderModeDepth) {
mRenderTarget.RenderToLinearizedDepth(mCamera.mNear, mCamera.mFar, mCamera.mIsOrthographic); mForwardRenderingTarget.RenderToLinearizedDepth(mCamera.mNear, mCamera.mFar, mCamera.mIsOrthographic);
} }
if (mUseDeferred && mIsSSAOEnabled) { if (mUseDeferred && mIsSSAOEnabled) {
@ -730,11 +743,11 @@ void Renderer::RenderGl() {
// Positions // Positions
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mPositionTexture); glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mPositionTexture);
// Normals // Normals
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mNormalTexture); glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mNormalTexture);
// Noise // Noise
glActiveTexture(GL_TEXTURE2); glActiveTexture(GL_TEXTURE2);
@ -788,19 +801,19 @@ void Renderer::RenderGl() {
// TODO: remove and reconstruct position from depth // TODO: remove and reconstruct position from depth
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mPositionTexture); glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mPositionTexture);
mDeferredLighting.SetInt("uPosition", 0); mDeferredLighting.SetInt("uPosition", 0);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture); glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mDepthTexture);
mDeferredLighting.SetInt("uDepth", 1); mDeferredLighting.SetInt("uDepth", 1);
glActiveTexture(GL_TEXTURE2); glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mNormalTexture); glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mNormalTexture);
mDeferredLighting.SetInt("uNormal", 2); mDeferredLighting.SetInt("uNormal", 2);
glActiveTexture(GL_TEXTURE3); glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture); glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mColorTexture);
mDeferredLighting.SetInt("uColor", 3); mDeferredLighting.SetInt("uColor", 3);
glActiveTexture(GL_TEXTURE4); glActiveTexture(GL_TEXTURE4);
@ -860,15 +873,15 @@ void Renderer::RenderGl() {
// glBindFramebuffer // glBindFramebuffer
glBindFramebuffer(GL_READ_FRAMEBUFFER, mDeferredLightingTarget.mFrameBufferId); glBindFramebuffer(GL_READ_FRAMEBUFFER, mDeferredLightingTarget.mFrameBufferId);
glReadBuffer(GL_COLOR_ATTACHMENT0); glReadBuffer(GL_COLOR_ATTACHMENT0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mRenderTarget.mFrameBufferId); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mForwardRenderingTarget.mFrameBufferId);
GLenum buffers[] = { GL_COLOR_ATTACHMENT0}; GLenum buffers[] = { GL_COLOR_ATTACHMENT0};
glDrawBuffers (1, buffers); glDrawBuffers (1, buffers);
glBlitFramebuffer( glBlitFramebuffer(
0, mDeferredLightingTarget.mWidth, 0, mDeferredLightingTarget.mWidth,
0, mDeferredLightingTarget.mHeight, 0, mDeferredLightingTarget.mHeight,
0, mRenderTarget.mWidth, 0, mForwardRenderingTarget.mWidth,
0, mRenderTarget.mHeight, 0, mForwardRenderingTarget.mHeight,
GL_COLOR_BUFFER_BIT, GL_COLOR_BUFFER_BIT,
GL_NEAREST GL_NEAREST
); );
@ -958,7 +971,7 @@ void Renderer::RenderScene(RenderProgram &program, const Camera& camera) {
program.SetMat44("uProjectionMatrix", camera.mProjectionMatrix); program.SetMat44("uProjectionMatrix", camera.mProjectionMatrix);
program.SetMat33("uNormalMatrix", normal_matrix); program.SetMat33("uNormalMatrix", normal_matrix);
program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f)); program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f));
program.SetVec3("uLightDirection", mLight.mDirection); program.SetVec3("uLightDirection", mLight.mDirection);
program.SetVec3("uViewPosition", camera.mEye); program.SetVec3("uViewPosition", camera.mEye);
@ -1031,22 +1044,36 @@ void Renderer::DrawGui() {
ImGui::RadioButton("Positions", &sRendererSettings.RenderMode, 4); ImGui::SameLine(); ImGui::RadioButton("Positions", &sRendererSettings.RenderMode, 4); ImGui::SameLine();
ImGui::RadioButton("AO", &sRendererSettings.RenderMode, 5); ImGui::RadioButton("AO", &sRendererSettings.RenderMode, 5);
if (mUseDeferred) {
glBindFramebuffer(GL_READ_FRAMEBUFFER, mDeferredLightingTarget.mFrameBufferId);
} else {
glBindFramebuffer(GL_READ_FRAMEBUFFER, mForwardRenderingTarget.mFrameBufferId);
}
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mRenderOutput.mFrameBufferId);
glBlitFramebuffer(0, 0, mSceneAreaWidth, mSceneAreaHeight,
0, 0, mSceneAreaWidth, mSceneAreaHeight,
GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
switch (sRendererSettings.RenderMode) { switch (sRendererSettings.RenderMode) {
case SceneRenderModeDefault: case SceneRenderModeDefault:
mRenderTextureRef.mTextureIdPtr = mRenderTextureRef.mTextureIdPtr = &mRenderOutput.mColorTexture;
mUseDeferred ? &mDeferredLightingTarget.mColorTexture : &mRenderTarget.mColorTexture;
break; break;
case SceneRenderModeColor: case SceneRenderModeColor:
mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mColorTexture; mRenderTextureRef.mTextureIdPtr = &mForwardRenderingTarget.mColorTexture;
break; break;
case SceneRenderModeDepth: case SceneRenderModeDepth:
mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mLinearizedDepthTexture; mRenderTextureRef.mTextureIdPtr = &mForwardRenderingTarget.mLinearizedDepthTexture;
break; break;
case SceneRenderModeNormals: case SceneRenderModeNormals:
mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mNormalTexture; mRenderTextureRef.mTextureIdPtr = &mForwardRenderingTarget.mNormalTexture;
break; break;
case SceneRenderModePositions: case SceneRenderModePositions:
mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mPositionTexture; mRenderTextureRef.mTextureIdPtr = &mForwardRenderingTarget.mPositionTexture;
break; break;
case SceneRenderModeAmbientOcclusion: case SceneRenderModeAmbientOcclusion:
mRenderTextureRef.mTextureIdPtr = &mSSAOBlurTarget.mColorTexture; mRenderTextureRef.mTextureIdPtr = &mSSAOBlurTarget.mColorTexture;

View File

@ -96,10 +96,11 @@ struct Renderer {
RenderProgram mSSAOProgram; RenderProgram mSSAOProgram;
RenderProgram mBlurSSAOProgram; RenderProgram mBlurSSAOProgram;
RenderTarget mRenderTarget; RenderTarget mForwardRenderingTarget;
RenderTarget mDeferredLightingTarget; RenderTarget mDeferredLightingTarget;
RenderTarget mSSAOTarget; RenderTarget mSSAOTarget;
RenderTarget mSSAOBlurTarget; RenderTarget mSSAOBlurTarget;
RenderTarget mRenderOutput;
GLTextureRef mRenderTextureRef = { (int)0xbadface }; GLTextureRef mRenderTextureRef = { (int)0xbadface };
GLTextureRef mPositionTextureRef = { (int)0xbadface }; GLTextureRef mPositionTextureRef = { (int)0xbadface };
@ -123,6 +124,7 @@ struct Renderer {
void Initialize(int width, int height); void Initialize(int width, int height);
void Shutdown(); void Shutdown();
void CheckRenderBuffers(); void CheckRenderBuffers();
void ResizeTargets(int width, int height);
void RenderGl(); void RenderGl();
void RenderScene(RenderProgram &program, const Camera& camera); void RenderScene(RenderProgram &program, const Camera& camera);
void DebugDrawShadowCascades(); void DebugDrawShadowCascades();