Skip to content

Fix Shadow Mapping bug in MiniEngine for Non-legacy renderer #891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions MiniEngine/Core/Math/Frustum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,16 @@ Frustum::Frustum( const Matrix4& ProjMat )
// Identify if the projection is perspective or orthographic by looking at the 4th row.
if (ProjMatF[3] == 0.0f && ProjMatF[7] == 0.0f && ProjMatF[11] == 0.0f && ProjMatF[15] == 1.0f)
{
// Orthographic
float Left = (-1.0f - ProjMatF[12]) * RcpXX;
float Right = ( 1.0f - ProjMatF[12]) * RcpXX;
float Top = ( 1.0f - ProjMatF[13]) * RcpYY;
float Bottom = (-1.0f - ProjMatF[13]) * RcpYY;
float Front = ( 0.0f - ProjMatF[14]) * RcpZZ;
float Back = ( 1.0f - ProjMatF[14]) * RcpZZ;
// Orthographic Frustum Extraction
// In ModelViewer, we set the shadowcenter to be the origin. Thus the frustum planes are symmetrical x, y and z axis.
// Also, for Top and Bottom, it is the Top that is negative due to: ProjMatF[10] = -2/(far - near), in an orthographic projmat.

float Left = -RcpXX;
float Right = RcpXX;
float Top = -RcpYY;
float Bottom = RcpYY;
float Front = 0.5f * RcpZZ;
float Back = 0.5f -RcpZZ;

// Check for reverse Z here. The bounding planes need to point into the frustum.
if (Front < Back)
Expand Down
2 changes: 1 addition & 1 deletion MiniEngine/Model/Shaders/DefaultPS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ float4 main(VSOutput vsOutput) : SV_Target0
// Begin accumulating light starting with emissive
float3 colorAccum = emissive;

#if 0
#if 1
float sunShadow = texSunShadow.SampleCmpLevelZero( shadowSampler, vsOutput.sunShadowCoord.xy, vsOutput.sunShadowCoord.z );
colorAccum += ShadeDirectionalLight(Surface, SunDirection, sunShadow * SunIntensity);

Expand Down
10 changes: 8 additions & 2 deletions MiniEngine/ModelViewer/ModelViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "ShadowCamera.h"
#include "Display.h"

#define LEGACY_RENDERER
//#define LEGACY_RENDERER

using namespace GameCore;
using namespace Math;
Expand Down Expand Up @@ -276,7 +276,13 @@ void ModelViewer::RenderScene( void )
Vector3 SunDirection = Normalize(Vector3( costheta * cosphi, sinphi, sintheta * cosphi ));
Vector3 ShadowBounds = Vector3(m_ModelInst.GetRadius());
//m_SunShadowCamera.UpdateMatrix(-SunDirection, m_ModelInst.GetCenter(), ShadowBounds,
m_SunShadowCamera.UpdateMatrix(-SunDirection, Vector3(0, -500.0f, 0), Vector3(5000, 3000, 3000),

// Make sure that the x and z coordinates are 0 for the shadowcenter
// in order for the orthographic frustum to be correctly calculated.
Vector3 origin = Vector3(0);
Vector3 ShadowCenter = origin;

m_SunShadowCamera.UpdateMatrix(-SunDirection, ShadowCenter, Vector3(5000, 3000, 3000),
(uint32_t)g_ShadowBuffer.GetWidth(), (uint32_t)g_ShadowBuffer.GetHeight(), 16);

GlobalConstants globals;
Expand Down