2 // Copyright (c) Microsoft. All rights reserved.
3 // This code is licensed under the MIT License (MIT).
4 // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5 // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6 // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7 // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 // Developed by Minigraph
11 // Author: James Stanard
15 #include "ShadowCamera.h"
19 void GameCore::ShadowCamera::UpdateMatrix(
20 Vector3 LightDirection
, Vector3 ShadowCenter
, Vector3 ShadowBounds
,
21 uint32_t BufferWidth
, uint32_t BufferHeight
, uint32_t BufferPrecision
)
23 SetLookDirection( LightDirection
, Vector3(kZUnitVector
) );
25 // Converts world units to texel units so we can quantize the camera position to whole texel units
26 Vector3 RcpDimensions
= Recip(ShadowBounds
);
27 Vector3 QuantizeScale
= Vector3((float)BufferWidth
, (float)BufferHeight
, (float)((1 << BufferPrecision
) - 1)) * RcpDimensions
;
30 // Recenter the camera at the quantized position
33 // Transform to view space
34 ShadowCenter
= ~GetRotation() * ShadowCenter
;
35 // Scale to texel units, truncate fractional part, and scale back to world units
36 ShadowCenter
= Floor( ShadowCenter
* QuantizeScale
) / QuantizeScale
;
37 // Transform back into world space
38 ShadowCenter
= GetRotation() * ShadowCenter
;
40 SetPosition( ShadowCenter
);
42 SetProjMatrix( Matrix4::MakeScale(Vector3(2.0f
, 2.0f
, 1.0f
) * RcpDimensions
) );
46 // Transform from clip space to texture space
47 m_ShadowMatrix
= Matrix4( AffineTransform( Matrix3::MakeScale( 0.5f
, -0.5f
, 1.0f
), Vector3(0.5f
, 0.5f
, 0.0f
) ) ) * m_ViewProjMatrix
;