metaforce/Runtime/World/CWorldShadow.cpp

129 lines
5.0 KiB
C++
Raw Normal View History

#include "CWorldShadow.hpp"
#include "CWorld.hpp"
#include "CStateManager.hpp"
#include "Graphics/CBooRenderer.hpp"
namespace urde
{
CWorldShadow::CWorldShadow(u32 w, u32 h, bool rgba8)
: m_shader(w, h) {}
2017-08-31 02:42:37 +00:00
void CWorldShadow::EnableModelProjectedShadow(const zeus::CTransform& pos, s32 lightIdx, float f1)
{
zeus::CTransform texTransform = zeus::lookAt(zeus::CVector3f::skZero, x74_lightPos - x68_objPos);
zeus::CTransform posXf = pos;
posXf.origin = zeus::CVector3f::skZero;
texTransform = posXf.inverse() * texTransform;
texTransform = (texTransform * zeus::CTransform::Scale(float(M_SQRT2) * x64_objHalfExtent * f1)).inverse();
texTransform = zeus::CTransform::Translate(0.5f, 0.f, 0.5f) * texTransform;
CBooModel::EnableShadowMaps(m_shader.GetTexture().get(), texTransform);
2017-08-31 02:42:37 +00:00
}
void CWorldShadow::DisableModelProjectedShadow()
{
CBooModel::DisableShadowMaps();
2017-08-31 02:42:37 +00:00
}
void CWorldShadow::BuildLightShadowTexture(const CStateManager& mgr, TAreaId aid, s32 lightIdx,
const zeus::CAABox& aabb, bool motionBlur, bool lighten)
2017-08-31 02:42:37 +00:00
{
if (x80_aid != aid || x84_lightIdx != lightIdx)
{
x88_blurReset = true;
x80_aid = aid;
x84_lightIdx = lightIdx;
}
if (aid != kInvalidAreaId)
{
const CGameArea* area = mgr.GetWorld()->GetAreaAlways(aid);
if (area->IsPostConstructed())
{
const CWorldLight& light = area->GetPostConstructed()->x60_lightsA[lightIdx];
zeus::CVector3f centerPoint = aabb.center();
if (const CPVSAreaSet* pvs = area->GetAreaVisSet())
{
CPVSVisSet lightSet = pvs->Get1stLightSet(lightIdx);
g_Renderer->EnablePVS(lightSet, aid);
}
else
{
CPVSVisSet visSet;
visSet.Reset(EPVSVisSetState::OutOfBounds);
g_Renderer->EnablePVS(visSet, aid);
}
zeus::CVector3f lightToPoint = centerPoint - light.GetPosition();
x64_objHalfExtent = (aabb.max - centerPoint).magnitude();
float distance = lightToPoint.magnitude();
float fov = zeus::radToDeg(std::atan2(x64_objHalfExtent, distance)) * 2.f;
if (fov >= 0.00001f)
{
lightToPoint.normalize();
x4_view = zeus::lookAt(light.GetPosition(), centerPoint, zeus::CVector3f::skDown);
x68_objPos = centerPoint;
x74_lightPos = light.GetPosition();
CGraphics::SetViewPointMatrix(x4_view);
zeus::CFrustum frustum;
frustum.updatePlanes(x4_view, zeus::SProjPersp(
zeus::degToRad(fov), 1.f, 0.1f, distance + x64_objHalfExtent));
g_Renderer->SetClippingPlanes(frustum);
g_Renderer->SetPerspective(fov, m_shader.GetWidth(), m_shader.GetHeight(), 0.1f, 1000.f);
SViewport backupVp = g_Viewport;
zeus::CVector2f backupDepthRange = CGraphics::g_CachedDepthRange;
m_shader.bindRenderTarget();
g_Renderer->SetViewport(0, 0, m_shader.GetWidth(), m_shader.GetHeight());
2018-01-06 06:50:42 +00:00
CGraphics::SetDepthRange(DEPTH_NEAR, DEPTH_FAR);
x34_model = zeus::lookAt(centerPoint - zeus::CVector3f(0.f, 0.f, 0.1f), light.GetPosition());
CGraphics::SetModelMatrix(x34_model);
m_shadowViewProj = CGraphics::GetPerspectiveProjectionMatrix(false) *
CGraphics::g_CameraMatrix.toMatrix4f();
2017-08-31 02:42:37 +00:00
float extent = float(M_SQRT2) * x64_objHalfExtent;
/* Depth test and write */
/* Color white 100% alpha */
m_shader.drawBase(extent);
CGraphics::SetModelMatrix(zeus::CTransform::Identity());
CBooModel::SetDrawingOccluders(true);
g_Renderer->PrepareDynamicLights({});
g_Renderer->DrawUnsortedGeometry(aid, 0, 0);
CBooModel::SetDrawingOccluders(false);
if (lighten)
{
CGraphics::SetModelMatrix(x34_model);
/* No depth test or write */
/* Color white 25% alpha */
m_shader.lightenShadow();
}
if (motionBlur && !x88_blurReset)
{
/* No depth test or write */
/* Color white 85% alpha */
/* Draw in shadow texture */
m_shader.blendPreviousShadow();
}
x88_blurReset = false;
m_shader.resolveTexture();
CBooRenderer::BindMainDrawTarget();
g_Renderer->SetViewport(backupVp.x0_left, backupVp.x4_top, backupVp.x8_width, backupVp.xc_height);
CGraphics::SetDepthRange(backupDepthRange[0], backupDepthRange[1]);
}
}
}
2017-08-31 02:42:37 +00:00
}
void CWorldShadow::ResetBlur()
{
x88_blurReset = true;
2017-08-31 02:42:37 +00:00
}
}