Fix specular lighting in forward mode and fix switching between forward and deferred lighting.
parent
eb67c1deaf
commit
1dc77c2131
|
@ -81,8 +81,7 @@ void main() {
|
|||
|
||||
// diffuse lighting
|
||||
vec3 normal_dir = normalize(ioFragNormal);
|
||||
// vec3 light_dir = normalize(uLightDirection);
|
||||
vec3 light_dir = - transpose(inverse(mat3(uViewMatrix * uModelMatrix))) * uLightDirection;
|
||||
vec3 light_dir = -(mat3(uViewMatrix)) * uLightDirection;
|
||||
float diff = max(dot(normal_dir, light_dir), 0.0);
|
||||
vec4 diffuse = diff * albedo_color;
|
||||
|
||||
|
@ -90,7 +89,7 @@ void main() {
|
|||
vec3 view_dir = normalize(-ioFragPosition);
|
||||
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);
|
||||
|
||||
// shadow
|
||||
|
|
|
@ -20,9 +20,9 @@ smooth out vec4 ioFragColor;
|
|||
out vec4 ioFragPosLightSpace;
|
||||
|
||||
void main() {
|
||||
ioFragPosition = (uViewMatrix * uModelMatrix * inCoord).xyz;
|
||||
|
||||
mat4 model_view_matrix = uViewMatrix * uModelMatrix;
|
||||
|
||||
ioFragPosition = (model_view_matrix* inCoord).xyz;
|
||||
ioFragNormal = transpose(inverse(mat3(model_view_matrix))) * inNormal;
|
||||
ioFragTexCoords = inUV;
|
||||
ioFragColor = inColor;
|
||||
|
|
|
@ -496,12 +496,12 @@ void Renderer::Initialize(int width, int height) {
|
|||
| RenderTarget::EnablePositionTexture
|
||||
| RenderTarget::EnableNormalTexture;
|
||||
}
|
||||
mRenderTarget.Initialize(width, height, render_target_flags);
|
||||
mForwardRenderingTarget.Initialize(width, height, render_target_flags);
|
||||
|
||||
mRenderTarget.mVertexArray = &gVertexArray;
|
||||
mRenderTarget.mQuadMesh = &gScreenQuad;
|
||||
mRenderTarget.mLinearizeDepthProgram = mRenderQuadProgramDepth;
|
||||
mRenderTarget.mLinearizeDepthProgram.RegisterFileModification();
|
||||
mForwardRenderingTarget.mVertexArray = &gVertexArray;
|
||||
mForwardRenderingTarget.mQuadMesh = &gScreenQuad;
|
||||
mForwardRenderingTarget.mLinearizeDepthProgram = mRenderQuadProgramDepth;
|
||||
mForwardRenderingTarget.mLinearizeDepthProgram.RegisterFileModification();
|
||||
|
||||
// SSAO Target
|
||||
mSSAOTarget.Initialize(width, height, RenderTarget::EnableColor);
|
||||
|
@ -509,6 +509,8 @@ void Renderer::Initialize(int width, int height) {
|
|||
|
||||
mDeferredLightingTarget.Initialize(width, height, RenderTarget::EnableColor);
|
||||
|
||||
mRenderOutput.Initialize(width, height, RenderTarget::EnableColor | RenderTarget::EnableDepth);
|
||||
|
||||
// Light
|
||||
mLight.Initialize();
|
||||
mLight.mShadowMapTarget.mVertexArray = &gVertexArray;
|
||||
|
@ -559,14 +561,19 @@ void Renderer::CheckRenderBuffers() {
|
|||
;
|
||||
}
|
||||
|
||||
if (mSceneAreaWidth != mRenderTarget.mWidth
|
||||
|| mSceneAreaHeight != mRenderTarget.mHeight
|
||||
|| mRenderTarget.mFlags != required_render_flags ) {
|
||||
mRenderTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, required_render_flags);
|
||||
if (mSceneAreaWidth != mRenderOutput.mWidth
|
||||
|| mSceneAreaHeight != mRenderOutput.mHeight
|
||||
|| mForwardRenderingTarget.mFlags != 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) {
|
||||
mDeferredLightingTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor);
|
||||
}
|
||||
// TODO: remove these
|
||||
| RenderTarget::EnablePositionTexture
|
||||
| RenderTarget::EnableLinearizedDepthTexture);
|
||||
}
|
||||
|
||||
if (mIsSSAOEnabled
|
||||
|
@ -577,6 +584,12 @@ void Renderer::CheckRenderBuffers() {
|
|||
}
|
||||
}
|
||||
|
||||
void Renderer::ResizeTargets(int width, int height) {
|
||||
mSSAOTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor);
|
||||
mSSAOBlurTarget.Resize(mSceneAreaWidth, mSceneAreaHeight, RenderTarget::EnableColor);
|
||||
|
||||
}
|
||||
|
||||
void Renderer::RenderGl() {
|
||||
CheckRenderBuffers();
|
||||
|
||||
|
@ -640,7 +653,7 @@ void Renderer::RenderGl() {
|
|||
* mCamera.mProjectionMatrix;
|
||||
|
||||
// enable the render target
|
||||
mRenderTarget.Bind();
|
||||
mForwardRenderingTarget.Bind();
|
||||
|
||||
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||
gLog ("Cannot render: frame buffer invalid!");
|
||||
|
@ -717,7 +730,7 @@ void Renderer::RenderGl() {
|
|||
RenderScene(*program, mCamera);
|
||||
|
||||
if (mSettings->RenderMode == SceneRenderModeDepth) {
|
||||
mRenderTarget.RenderToLinearizedDepth(mCamera.mNear, mCamera.mFar, mCamera.mIsOrthographic);
|
||||
mForwardRenderingTarget.RenderToLinearizedDepth(mCamera.mNear, mCamera.mFar, mCamera.mIsOrthographic);
|
||||
}
|
||||
|
||||
if (mUseDeferred && mIsSSAOEnabled) {
|
||||
|
@ -730,11 +743,11 @@ void Renderer::RenderGl() {
|
|||
|
||||
// Positions
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mPositionTexture);
|
||||
glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mPositionTexture);
|
||||
|
||||
// Normals
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mNormalTexture);
|
||||
glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mNormalTexture);
|
||||
|
||||
// Noise
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
|
@ -788,19 +801,19 @@ void Renderer::RenderGl() {
|
|||
|
||||
// TODO: remove and reconstruct position from depth
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mPositionTexture);
|
||||
glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mPositionTexture);
|
||||
mDeferredLighting.SetInt("uPosition", 0);
|
||||
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture);
|
||||
glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mDepthTexture);
|
||||
mDeferredLighting.SetInt("uDepth", 1);
|
||||
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mNormalTexture);
|
||||
glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mNormalTexture);
|
||||
mDeferredLighting.SetInt("uNormal", 2);
|
||||
|
||||
glActiveTexture(GL_TEXTURE3);
|
||||
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture);
|
||||
glBindTexture(GL_TEXTURE_2D, mForwardRenderingTarget.mColorTexture);
|
||||
mDeferredLighting.SetInt("uColor", 3);
|
||||
|
||||
glActiveTexture(GL_TEXTURE4);
|
||||
|
@ -860,15 +873,15 @@ void Renderer::RenderGl() {
|
|||
// glBindFramebuffer
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, mDeferredLightingTarget.mFrameBufferId);
|
||||
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mRenderTarget.mFrameBufferId);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mForwardRenderingTarget.mFrameBufferId);
|
||||
GLenum buffers[] = { GL_COLOR_ATTACHMENT0};
|
||||
glDrawBuffers (1, buffers);
|
||||
|
||||
glBlitFramebuffer(
|
||||
0, mDeferredLightingTarget.mWidth,
|
||||
0, mDeferredLightingTarget.mHeight,
|
||||
0, mRenderTarget.mWidth,
|
||||
0, mRenderTarget.mHeight,
|
||||
0, mForwardRenderingTarget.mWidth,
|
||||
0, mForwardRenderingTarget.mHeight,
|
||||
GL_COLOR_BUFFER_BIT,
|
||||
GL_NEAREST
|
||||
);
|
||||
|
@ -958,7 +971,7 @@ void Renderer::RenderScene(RenderProgram &program, const Camera& camera) {
|
|||
program.SetMat44("uProjectionMatrix", camera.mProjectionMatrix);
|
||||
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("uViewPosition", camera.mEye);
|
||||
|
||||
|
@ -1031,22 +1044,36 @@ void Renderer::DrawGui() {
|
|||
ImGui::RadioButton("Positions", &sRendererSettings.RenderMode, 4); ImGui::SameLine();
|
||||
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) {
|
||||
case SceneRenderModeDefault:
|
||||
mRenderTextureRef.mTextureIdPtr =
|
||||
mUseDeferred ? &mDeferredLightingTarget.mColorTexture : &mRenderTarget.mColorTexture;
|
||||
mRenderTextureRef.mTextureIdPtr = &mRenderOutput.mColorTexture;
|
||||
break;
|
||||
case SceneRenderModeColor:
|
||||
mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mColorTexture;
|
||||
mRenderTextureRef.mTextureIdPtr = &mForwardRenderingTarget.mColorTexture;
|
||||
break;
|
||||
case SceneRenderModeDepth:
|
||||
mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mLinearizedDepthTexture;
|
||||
mRenderTextureRef.mTextureIdPtr = &mForwardRenderingTarget.mLinearizedDepthTexture;
|
||||
break;
|
||||
case SceneRenderModeNormals:
|
||||
mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mNormalTexture;
|
||||
mRenderTextureRef.mTextureIdPtr = &mForwardRenderingTarget.mNormalTexture;
|
||||
break;
|
||||
case SceneRenderModePositions:
|
||||
mRenderTextureRef.mTextureIdPtr = &mRenderTarget.mPositionTexture;
|
||||
mRenderTextureRef.mTextureIdPtr = &mForwardRenderingTarget.mPositionTexture;
|
||||
break;
|
||||
case SceneRenderModeAmbientOcclusion:
|
||||
mRenderTextureRef.mTextureIdPtr = &mSSAOBlurTarget.mColorTexture;
|
||||
|
|
|
@ -96,10 +96,11 @@ struct Renderer {
|
|||
RenderProgram mSSAOProgram;
|
||||
RenderProgram mBlurSSAOProgram;
|
||||
|
||||
RenderTarget mRenderTarget;
|
||||
RenderTarget mForwardRenderingTarget;
|
||||
RenderTarget mDeferredLightingTarget;
|
||||
RenderTarget mSSAOTarget;
|
||||
RenderTarget mSSAOBlurTarget;
|
||||
RenderTarget mRenderOutput;
|
||||
|
||||
GLTextureRef mRenderTextureRef = { (int)0xbadface };
|
||||
GLTextureRef mPositionTextureRef = { (int)0xbadface };
|
||||
|
@ -123,6 +124,7 @@ struct Renderer {
|
|||
void Initialize(int width, int height);
|
||||
void Shutdown();
|
||||
void CheckRenderBuffers();
|
||||
void ResizeTargets(int width, int height);
|
||||
void RenderGl();
|
||||
void RenderScene(RenderProgram &program, const Camera& camera);
|
||||
void DebugDrawShadowCascades();
|
||||
|
|
Loading…
Reference in New Issue