diff --git a/Build/Shaders/DX11_Rooms.fx b/Build/Shaders/DX11_Rooms.fx index babd71760..5e46c7d52 100644 --- a/Build/Shaders/DX11_Rooms.fx +++ b/Build/Shaders/DX11_Rooms.fx @@ -35,6 +35,11 @@ cbuffer CShadowLightBuffer : register(b4) float3 Padding2; }; +cbuffer RoomBuffer : register(b5) +{ + float4 AmbientColor; +}; + struct VertexShaderInput { float3 Position: POSITION; @@ -61,7 +66,6 @@ Texture2D CausticsTexture : register(t1); Texture2D ShadowMap : register(t2); SamplerState ShadowMapSampler : register(s1); - PixelShaderInput VS(VertexShaderInput input) { PixelShaderInput output; @@ -76,6 +80,19 @@ PixelShaderInput VS(VertexShaderInput input) return output; } +float getShadowFactor(Texture2D shadowMap, SamplerState shadowMapSampler, float2 coords, float realDepth) { + const float texelSize = 1.0f / 1024; + float shadowFactor = 0.0f; + //doing 9 samples + for (float x = -1; x <= 1.0f; x++) { + for (float y = -1; y <= 1.0f; y++) { + float shadowMapDepth = ShadowMap.SampleLevel(ShadowMapSampler, coords + (float2(x, y)*texelSize),0).r; + shadowFactor += shadowMapDepth < realDepth ? 1.0f : 0.0f; + } + } + return shadowFactor / 9.0f; +} + [earlydepthstencil] float4 PS(PixelShaderInput input) : SV_TARGET { @@ -104,8 +121,8 @@ float4 PS(PixelShaderInput input) : SV_TARGET float realDepth = input.LightPosition.z; // If clip space z value greater than shadow map value then pixel is in shadow - if (shadowMapDepth < realDepth) - return float4(output.xyz* colorMul.xyz / 2.0f, 1.0f); + float shadow = getShadowFactor(ShadowMap, ShadowMapSampler, coords, realDepth); + lighting = lerp(lighting, AmbientColor * 2, saturate(shadow)); } } @@ -159,4 +176,5 @@ float4 PS(PixelShaderInput input) : SV_TARGET output.w = 1.0f; return output; -} \ No newline at end of file +} + diff --git a/TR5Main/Game/box.cpp b/TR5Main/Game/box.cpp index f85a335ae..25bfff263 100644 --- a/TR5Main/Game/box.cpp +++ b/TR5Main/Game/box.cpp @@ -1136,7 +1136,7 @@ int CreatureActive(short itemNumber) void InitialiseCreature(short itemNumber) { - InitialiseCreature(itemNumber); + ClearItem(itemNumber); } int StalkBox(ITEM_INFO* item, ITEM_INFO* enemy, short boxNumber) diff --git a/TR5Main/Renderer/Enums.h b/TR5Main/Renderer/Enums.h index 6ee55ecfb..a44053b8a 100644 --- a/TR5Main/Renderer/Enums.h +++ b/TR5Main/Renderer/Enums.h @@ -73,7 +73,7 @@ typedef enum RENDERER_FADE_STATUS { FADE_OUT }; -#define SHADOW_MAP_SIZE 2048 +#define SHADOW_MAP_SIZE 1024 #define TEXTURE_ATLAS_SIZE 4096 #define TEXTURE_PAGE_SIZE 262144 diff --git a/TR5Main/Renderer/Renderer11.cpp b/TR5Main/Renderer/Renderer11.cpp index cc0c05cee..f54644372 100644 --- a/TR5Main/Renderer/Renderer11.cpp +++ b/TR5Main/Renderer/Renderer11.cpp @@ -602,7 +602,7 @@ bool Renderer11::Initialise(int w, int h, int refreshRate, bool windowed, HWND h m_cbLights = createConstantBuffer(sizeof(CLightBuffer)); m_cbMisc = createConstantBuffer(sizeof(CMiscBuffer)); m_cbShadowMap = createConstantBuffer(sizeof(CShadowLightBuffer)); - + m_cbRoom = createConstantBuffer(sizeof(CRoomBuffer)); m_currentCausticsFrame = 0; m_firstWeather = true; @@ -921,7 +921,9 @@ bool Renderer11::drawRooms(bool transparent, bool animated) m_stMisc.AlphaTest = !transparent; updateConstantBuffer(m_cbMisc, &m_stMisc, sizeof(CMiscBuffer)); m_context->PSSetConstantBuffers(3, 1, &m_cbMisc); - + m_stRoom.AmbientColor = room->AmbientLight; + updateConstantBuffer(m_cbRoom, &m_stRoom, sizeof(CRoomBuffer)); + m_context->PSSetConstantBuffers(5, 1, &m_cbRoom); for (int j = firstBucket; j < lastBucket; j++) { RendererBucket* bucket; diff --git a/TR5Main/Renderer/Renderer11.h b/TR5Main/Renderer/Renderer11.h index 702282d7f..d11da65c0 100644 --- a/TR5Main/Renderer/Renderer11.h +++ b/TR5Main/Renderer/Renderer11.h @@ -582,6 +582,10 @@ struct CMiscBuffer { float Padding[14]; }; +struct CRoomBuffer { + Vector4 AmbientColor; +}; + struct RendererAnimatedTexture { int Id; @@ -832,6 +836,8 @@ private: ID3D11Buffer* m_cbLights; CMiscBuffer m_stMisc; ID3D11Buffer* m_cbMisc; + CRoomBuffer m_stRoom; + ID3D11Buffer* m_cbRoom; CShadowLightBuffer m_stShadowMap; ID3D11Buffer* m_cbShadowMap; diff --git a/TR5Main/Shaders/DX11_Rooms.fx b/TR5Main/Shaders/DX11_Rooms.fx index babd71760..5e46c7d52 100644 --- a/TR5Main/Shaders/DX11_Rooms.fx +++ b/TR5Main/Shaders/DX11_Rooms.fx @@ -35,6 +35,11 @@ cbuffer CShadowLightBuffer : register(b4) float3 Padding2; }; +cbuffer RoomBuffer : register(b5) +{ + float4 AmbientColor; +}; + struct VertexShaderInput { float3 Position: POSITION; @@ -61,7 +66,6 @@ Texture2D CausticsTexture : register(t1); Texture2D ShadowMap : register(t2); SamplerState ShadowMapSampler : register(s1); - PixelShaderInput VS(VertexShaderInput input) { PixelShaderInput output; @@ -76,6 +80,19 @@ PixelShaderInput VS(VertexShaderInput input) return output; } +float getShadowFactor(Texture2D shadowMap, SamplerState shadowMapSampler, float2 coords, float realDepth) { + const float texelSize = 1.0f / 1024; + float shadowFactor = 0.0f; + //doing 9 samples + for (float x = -1; x <= 1.0f; x++) { + for (float y = -1; y <= 1.0f; y++) { + float shadowMapDepth = ShadowMap.SampleLevel(ShadowMapSampler, coords + (float2(x, y)*texelSize),0).r; + shadowFactor += shadowMapDepth < realDepth ? 1.0f : 0.0f; + } + } + return shadowFactor / 9.0f; +} + [earlydepthstencil] float4 PS(PixelShaderInput input) : SV_TARGET { @@ -104,8 +121,8 @@ float4 PS(PixelShaderInput input) : SV_TARGET float realDepth = input.LightPosition.z; // If clip space z value greater than shadow map value then pixel is in shadow - if (shadowMapDepth < realDepth) - return float4(output.xyz* colorMul.xyz / 2.0f, 1.0f); + float shadow = getShadowFactor(ShadowMap, ShadowMapSampler, coords, realDepth); + lighting = lerp(lighting, AmbientColor * 2, saturate(shadow)); } } @@ -159,4 +176,5 @@ float4 PS(PixelShaderInput input) : SV_TARGET output.w = 1.0f; return output; -} \ No newline at end of file +} +